UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

107 lines (106 loc) 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/es.parse-int.js"); require("core-js/modules/es.regexp.exec.js"); require("core-js/modules/es.regexp.to-string.js"); require("core-js/modules/es.string.replace.js"); class DlmsDateTime { constructor() { let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; this.yearHighbyte = 0; this.yearLowbyte = 0; this.month = 0; this.dayOfMonth = 0; this.dayOfWeek = 0; this.hour = 0; this.minute = 0; this.second = 0; this.hundredthsOfSecond = 0; this.deviationHighbyte = 0; this.deviationLowbyte = 0; this.clockStatus = 0; this.size = 12; this.value = ''; this.valueTime = null; if (data instanceof Date) { this.initializeFromDateTime(data); } else if (data instanceof Array) { this.initializeFromBytes(data); } } initializeFromDateTime(dateTime) { const yearHex = dateTime.getFullYear().toString(16).padStart(4, '0'); this.yearHighbyte = parseInt(yearHex.substring(0, 2), 16); this.yearLowbyte = parseInt(yearHex.substring(2), 16); this.month = dateTime.getMonth() + 1; // JavaScript months are zero-based this.dayOfMonth = dateTime.getDate(); this.dayOfWeek = this.getDayOfWeekValue(dateTime.getDay()); this.hour = dateTime.getHours(); this.minute = dateTime.getMinutes(); this.second = dateTime.getSeconds(); this.hundredthsOfSecond = 0xff; this.deviationLowbyte = 0x00; this.deviationHighbyte = 0x80; this.clockStatus = 0x00; this.valueTime = dateTime; this.value = dateTime.toISOString().replace('T', ' ').substring(0, 19); } initializeFromBytes(bytes) { if (bytes.length < 12) { throw new Error('Invalid byte array length'); } this.yearHighbyte = bytes[0]; this.yearLowbyte = bytes[1]; this.month = bytes[2]; this.dayOfMonth = bytes[3]; this.dayOfWeek = bytes[4]; this.hour = bytes[5]; this.minute = bytes[6]; this.second = bytes[7]; this.hundredthsOfSecond = bytes[8]; this.deviationLowbyte = bytes[9]; this.deviationHighbyte = bytes[10]; this.clockStatus = bytes[11]; const year = this.yearHighbyte << 8 | this.yearLowbyte; this.valueTime = new Date(Number(year), Number(this.month) - 1, Number(this.dayOfMonth), Number(this.hour), Number(this.minute), Number(this.second)); this.value = "".concat(this.dayOfMonth.toString().padStart(2, '0'), "/") + "".concat(this.month.toString().padStart(2, '0'), "/") + "".concat(year, " ") + "".concat(this.hour.toString().padStart(2, '0'), ":") + "".concat(this.minute.toString().padStart(2, '0')); } getDayOfWeekValue(jsDayOfWeek) { // Map JavaScript's day of the week to custom mapping switch (jsDayOfWeek) { case 0: return 7; // Sunday case 1: return 1; // Monday case 2: return 2; // Tuesday case 3: return 3; // Wednesday case 4: return 4; // Thursday case 5: return 5; // Friday case 6: return 6; // Saturday default: throw new Error('Invalid day of the week'); } } getValue() { return this.valueTime; } getBytes() { return [this.yearHighbyte, this.yearLowbyte, this.month, this.dayOfMonth, this.dayOfWeek, this.hour, this.minute, this.second, this.hundredthsOfSecond, this.deviationHighbyte, this.deviationLowbyte, this.clockStatus]; } } exports.default = DlmsDateTime;