-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
executable file
·285 lines (254 loc) · 8.58 KB
/
main.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
*
* hpcontrol adapter
*
*
* file io-package.json comments:
*
* {
* "common": {
* "name": "hpcontrol", // name has to be set and has to be equal to adapters folder name and main file name excluding extension
* "version": "0.0.0", // use "Semantic Versioning"! see http://semver.org/
* "title": "Node.js hpcontrol Adapter", // Adapter title shown in User Interfaces
* "authors": [ // Array of authord
* "name <[email protected]>"
* ]
* "desc": "hpcontrol adapter", // Adapter description shown in User Interfaces. Can be a language object {de:"...",ru:"..."} or a string
* "platform": "Javascript/Node.js", // possible values "javascript", "javascript/Node.js" - more coming
* "mode": "daemon", // possible values "daemon", "schedule", "subscribe"
* "schedule": "0 0 * * *" // cron-style schedule. Only needed if mode=schedule
* "loglevel": "info" // Adapters Log Level
* },
* "native": { // the native object is available via adapter.config in your adapters code - use it for configuration
* "test1": true,
* "test2": 42
* }
* }
*
*/
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
"use strict";
var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
var adapter = utils.adapter('hpcontrol');
var net = require('net');
var interval;
var objectList = require(__dirname+'/objectlist.json');
// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', function (callback) {
try {
clearInterval(interval);
adapter.log.info('cleaned everything up...');
} catch (e) {
}
callback();
});
// is called if a subscribed object changes
adapter.on('objectChange', function (id, obj) {
// Warning, obj can be null if it was deleted
//adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
});
// is called if a subscribed state changes
adapter.on('stateChange', function (id, state) {
// Warning, state can be null if it was deleted
//adapter.log.info('YYYYY stateChange ' + id + ' ' + JSON.stringify(state));
// you can use the ack flag to detect if it is status (true) or command (false)
if (state && !state.ack) {
adapter.log.info('ack is not set!');
}
});
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
adapter.on('message', function (obj) {
if (typeof obj == 'object' && obj.message) {
if (obj.command == 'send') {
// e.g. send email or pushover or whatever
console.log('send command');
// Send response in callback if required
if (obj.callback) adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
});
// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', function () {
main();
});
// Setup missing objects after start.
function setupObjects() {
adapter.setObjectNotExists('values', {
type: 'channel',
common: {
name: 'values'
},
native: {}
});
var count = Object.keys(objectList).length;
for (var i=0; i<count; i++) {
if (isset(objectList[i]) &&
'type' in objectList[i] &&
objectList[i].type!="undefined") {
var name = objectList[i].name;
var unit = '';
var role = '';
var type = '';
switch(objectList[i].type) {
case "TEMPERATURE":
unit = '°C';
type = 'number';
role = 'value.temperature';
break;
case "SECONDS":
unit = 's';
type = 'number';
role = 'value';
break;
case "IO":
type = 'boolean';
role = 'state';
break;
case "IP":
type = 'string';
role = 'value';
break;
case "ANALOG":
case "ERROR":
case "COUNTER":
case "IMPULSE":
type = 'number';
role = 'value';
break;
case "FLOWRATE":
unit = 'l/h';
type = 'number';
role = 'value';
break;
case "HEATQUANTITY":
unit = 'kWh';
type = 'number';
role = 'value';
break;
case "TIMESTAMP":
type = 'string';
role = 'value.datetime';
break;
case "ENUM":
type = 'string';
role = 'value';
break;
default:
adapter.log.info("Undefined type in setup: "+i+":"+objectList[i].type);
break;
}
var obj = {
type: 'state',
common: {
name: name,
type: type,
role: role,
unit: unit
},
native: {}
}
var id = addPrefix(i);
adapter.setObjectNotExists('values.'+id, obj);
}
}
}
function addPrefix(index) {
var result = index.toString();
while(result.length<3) {
result = "0"+result;
}
return result;
}
// convert integer to IP
function int2ip (v)
{
var part1 = v & 255;
var part2 = ((v >> 8) & 255);
var part3 = ((v >> 16) & 255);
var part4 = ((v >> 24) & 255);
return part4 + "." + part3 + "." + part2 + "." + part1;
}
function isset(o) {
return (typeof o)!='undefined';
}
// Set object state
function setValue(index, value) {
if (index>=0 && isset(objectList[index]) &&
'type' in objectList[index]) {
var state;
switch(objectList[index].type) {
case "TEMPERATURE":
case "ANALOG":
state = value/10;
break;
case "SECONDS":
case "ERROR":
case "COUNTER":
case "FLOWRATE":
case "HEATQUANTITY":
case "IMPULSE":
state = value;
break;
case "IP":
state = int2ip(value);
break;
case "IO":
state = value > 0;
break;
case "TIMESTAMP":
var date = new Date(value * 1000);
state = date.toLocaleDateString()+' '+date.toLocaleTimeString();
break;
case "ENUM":
if (isset(objectList[index].enum)) {
state = objectList[index].enum[value];
}
else {
adapter.log("ENUM not defined");
}
break;
default:
adapter.log.info("Undefined type in setvalue: "+index+":"+objectList[index].type);
break;
}
var id = addPrefix(index);
adapter.setState('values.'+id, {val: state, ack: true});
}
}
// connect to heat pump and read out values
function loadValues() {
var client = new net.Socket();
client.connect(adapter.config.hpport, adapter.config.hpip, function () {
var buf = Buffer.from([0,0, 11, 188]);
client.write(buf);
buf = Buffer.from([0, 0, 0, 0]);
client.write(buf);
});
client.on('data', function (data) {
var laenge = data.length;
for (var i = 0; i < laenge ; i+=4) {
var result = 0;
result = ((data[i+3]) |
(data[i + 2] << 8) |
(data[i + 1] << 16) |
(data[i] << 24));
setValue(i/4-3, result);
}
client.end();
adapter.log.info('Values received');
});
client.on('error', function(data) {
adapter.log.error("Clientsocket error.")
});
}
function main() {
setupObjects();
//adapter.subscribeStates('*');
loadValues();
interval = setInterval(function () {
loadValues();
}, 120000);
adapter.log.info('ioBroker.hpcontrol started');
}