-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.js
230 lines (201 loc) · 6.43 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict';
const humanizeduration = require('humanize-duration');
const huminizeoptions = {
language: 'de',
conjunction: ' und ',
serialComma: false,
};
const types = require('./types');
function createFirmwareString(buf) {
let firmware = '';
for (const key in buf) {
if ({}.hasOwnProperty.call(buf, key)) {
firmware += buf[key] === 0 ? '' : String.fromCharCode(buf[key]);
}
}
return firmware;
}
function int2ipAddress(value) {
const part1 = value & 255;
const part2 = (value >> 8) & 255;
const part3 = (value >> 16) & 255;
const part4 = (value >> 24) & 255;
return part4 + '.' + part3 + '.' + part2 + '.' + part1;
}
function createStateString(values) {
let stateStr = '';
const state1 = values[117];
const state2 = values[118];
const duration = values[120];
// Text aus Define
if (Object.prototype.hasOwnProperty.call(types.stateMessages, state1)) {
stateStr = types.stateMessages[state1];
if (state2 === 0 || state2 === 2) {
stateStr += ' seit ';
} else if (state2 === 1) {
stateStr += ' in ';
}
// Sonderbehandlung bei WP-Fehlern - Zeitstempel des zuletzt aufgetretenen Fehlers nehmen
if (state2 === 2) {
stateStr += new Date(values[95] * 1000).toString();
} else {
stateStr += humanizeduration(duration * 1000, huminizeoptions);
}
} else {
stateStr = 'Unknown [' + state1 + ']';
}
return stateStr;
}
function createExtendedStateString(values) {
let stateStr = '';
const defrostValve = values[37];
const heatSourceMotor = values[43];
const compressor1 = values[44];
const state3 = values[119];
const ahpStufe = values[121];
const ahpTemp = values[122] / 10;
if (Object.prototype.hasOwnProperty.call(types.extendetStateMessages, state3)) {
stateStr = types.extendetStateMessages[state3];
if (state3 === 6) {
// Estrich Programm
stateStr += ' Stufe ' + ahpStufe + ' - ' + ahpTemp + ' °C';
} else if (state3 === 7) {
// Abtauen
if (defrostValve === 1) {
stateStr += 'Abtauen (Kreisumkehr)';
} else if (compressor1 === 0 && heatSourceMotor === 1) {
stateStr += 'Luftabtauen';
} else {
stateStr += 'Abtauen';
}
}
} else {
stateStr = 'Unknown [' + state3 + ']';
}
return stateStr;
}
function createOperationStateString(state) {
let stateStr = '';
if (Object.prototype.hasOwnProperty.call(types.hpMode, state)) {
stateStr = types.hpMode[state];
} else {
stateStr = 'Unknown [' + state + ']';
}
return stateStr;
}
function createHotWaterStateString(values) {
let stateStr = '';
const hotWaterBoilerValve = values[38];
const opStateHotWater = values[124];
if (opStateHotWater === 0) {
stateStr = 'Sperrzeit';
} else if (opStateHotWater === 1 && hotWaterBoilerValve === 1) {
stateStr = 'Aufheizen';
} else if (opStateHotWater === 1 && hotWaterBoilerValve === 0) {
stateStr = 'Temp. OK';
} else if (opStateHotWater === 3) {
stateStr = 'Aus';
} else {
stateStr = 'Unknown [' + opStateHotWater + '/' + hotWaterBoilerValve + ']';
}
return stateStr;
}
function createCode(time, code, codeTypes) {
return {
code,
date: new Date(time * 1000),
message: Object.prototype.hasOwnProperty.call(codeTypes, code) ? codeTypes[code] : codeTypes[-1],
};
}
function createCodeList(timeArray, codeArray, codeTypes) {
const logArray = [];
for (let i = 0; i < timeArray.length; i++) {
logArray.push(createCode(timeArray[i], codeArray[i], codeTypes));
}
return logArray;
}
function createOutageCodeList(timeArray, codeArray) {
return createCodeList(timeArray, codeArray, types.outageCodes);
}
function createErrorCodeList(timeArray, codeArray) {
return createCodeList(timeArray, codeArray, types.errorCodes);
}
function toInt32ArrayReadBE(buffer) {
const i32a = new Int32Array(buffer.length / 4);
for (let i = 0; i < i32a.length; i++) {
i32a[i] = buffer.readInt32BE(i * 4);
}
return i32a;
}
function createHeatPumptTypeString(value) {
return Object.prototype.hasOwnProperty.call(types.hpTypes, value) ? types.hpTypes[value] : types.hpTypes[-1];
}
function value2LuxtronikSetTemperatureValue(realValue) {
// Allow only integer temperature. Add factor x10.
return parseInt(realValue * 10, 10);
}
function value2LuxtronikSetHundrethValue(realValue) {
// Allow only integer temperature. Add factor x100.
return parseInt(realValue * 100, 10);
}
function isValidOperationMode(value) {
return Object.prototype.hasOwnProperty.call(types.hpMode, value.toString());
}
function limitRange(value, min, max) {
if (value < min) {
value = min;
}
if (value > max) {
value = max;
}
return value;
}
function createTimerTableTypeString(value) {
let tableStr = '';
if (Object.prototype.hasOwnProperty.call(types.timerTableTypes, value)) {
tableStr = types.timerTableTypes[value];
} else {
tableStr = 'unbekannt';
}
return tableStr;
}
function secondsToTimeString(value) {
const timeMilliseconds = new Date(value * 1000);
const timeStr = timeMilliseconds.toISOString().substr(11, 5);
return timeStr;
}
function createTimerTable(parameters, startindex, rows, swapOnOff = false) {
const timerTable = [];
for (let rowindex = 0; rowindex < rows; rowindex++) {
let onTime = secondsToTimeString(parameters[startindex + rowindex * 2]);
let offTime = secondsToTimeString(parameters[startindex + rowindex * 2 + 1]);
if (swapOnOff === true) {
const tmp = onTime;
onTime = offTime;
offTime = tmp;
}
timerTable.push({
on: onTime,
off: offTime,
});
}
return timerTable;
}
module.exports = {
createFirmwareString,
int2ipAddress,
createStateString,
createExtendedStateString,
createOperationStateString,
createHotWaterStateString,
createOutageCodeList,
createErrorCodeList,
toInt32ArrayReadBE,
createHeatPumptTypeString,
value2LuxtronikSetTemperatureValue,
value2LuxtronikSetHundrethValue,
isValidOperationMode,
limitRange,
createTimerTableTypeString,
createTimerTable,
};