-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
312 lines (273 loc) · 11 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
*
* resol adapter
*
*
* file io-package.json comments:
*
* {
* "common": {
* "name": "resol", // 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 resol Adapter", // Adapter title shown in User Interfaces
* "authors": [ // Array of authord
* "name <[email protected]>"
* ]
* "desc": "resol 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": {
* "IP": "10.0.0.150",
* "Port": 7053
},
* }
*
*/
/* jshint -W097 */ // jshint strict:false
/*jslint node: true */
"use strict";
var vbus = require('resol-vbus');
var _ = require('lodash');
var spec = vbus.Specification.getDefaultSpecification();
// you have to require the utils module and call adapter function
var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
var ctx = {
headerSet: null,
hsc: null,
connection: null,
};
var adapter;
function startAdapter(options) {
options = options || {};
// you have to call the adapter function and pass a options object
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.resol.0
adapter = utils.adapter('resol');
// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', function (callback) {
try {
ctx.connection.disconnectd();
callback();
} 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
});
// 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('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 () {
if (adapter.config.outputLevelDebug === undefined || adapter.config.outputLevelDebug === null) {
adapter.config.outputLevelDebug = false;
}
main();
});
return adapter;
}
function main() {
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// adapter.config:
adapter.log.info('config ipAddress: ' + adapter.config.ipAddress);
adapter.log.info('config port: ' + adapter.config.port);
adapter.log.info('config password: ' + adapter.config.password);
adapter.log.info('config interval: ' + adapter.config.interval);
adapter.log.info('config forceReInit: ' + adapter.config.forceReInit);
adapter.log.info('config outputLevelDebug: ' + adapter.config.outputLevelDebug);
// in this resol all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
adapter.checkPassword('admin', 'iobroker', function (res) {
console.log('check user admin pw ioboker: ' + res);
});
adapter.checkGroup('admin', 'admin', function (res) {
console.log('check group user admin group admin: ' + res);
});
function initResol() {
ctx.headerSet = new vbus.HeaderSet();
var forceReInit = adapter.config.forceReInit;
ctx.hsc = new vbus.HeaderSetConsolidator({
interval: adapter.config.interval * 1000,
timeToLive: (adapter.config.interval * 1000) + 1000,
});
var ConnectionClass = vbus['TcpConnection'];
ctx.connection = new ConnectionClass({
host: adapter.config.ipAddress,
password: adapter.config.password
});
ctx.connection.on('packet', function (packet) {
ctx.headerSet.removeAllHeaders();
ctx.headerSet.addHeader(packet);
ctx.hsc.addHeader(packet);
if (forceReInit) {
ctx.hsc.emit('headerSet', ctx.hsc);
}
});
ctx.hsc.on('headerSet', function (headerSet) {
var packetFields = spec.getPacketFieldsForHeaders(ctx.headerSet.getSortedHeaders());
if (adapter.config.outputLevelDebug) {
adapter.log.info('headerSet packetFields received: ' + JSON.stringify(packetFields));
}
var data = _.map(packetFields, function (pf) {
return {
id: pf.id,
name: pf.name,
value: pf.rawValue,
deviceName: pf.packetSpec.sourceDevice.fullName,
deviceId: pf.packetSpec.sourceDevice.deviceId,
addressId: pf.packetSpec.sourceDevice.selfAddress,
unit: pf.packetFieldSpec.type.unit.unitId,
typeId: pf.packetFieldSpec.type.typeId,
rootTypeId: pf.packetFieldSpec.type.rootTypeId
};
});
var first = true;
_.each(data, function (item) {
var deviceId = item.deviceId.replace(/_/g, '');
var channelId = deviceId + '.' + item.addressId;
var objectId = channelId + '.' + item.id.replace(/_/g, '');
if (forceReInit) {
initDevice(deviceId, channelId, objectId, item);
}
adapter.setState(objectId, item.value, true);
if (adapter.config.outputLevelDebug && first) {
first = false;
{
// set lastmessageReceived
var deviceName = "lastMessageReceived";
var lastMessageReceivedId = channelId + "." + deviceName;
var obj = {
type: 'state',
common: {
name: deviceName,
type: 'string',
write: false
},
native: {}
};
adapter.setObjectNotExists(lastMessageReceivedId, obj, function () {
adapter.setState(lastMessageReceivedId, new Date().toLocaleString("de-AT"), true);
});
}
{
// set lastHeaderSetPacketFields
var deviceName = "lastHeaderSetPacketFields";
var lastHeaderSetPacketFieldsId = channelId + "." + deviceName;
var obj = {
type: 'state',
common: {
name: deviceName,
type: 'string',
write: false
},
native: {}
};
adapter.setObjectNotExists(lastHeaderSetPacketFieldsId, obj, function () {
adapter.setState(lastHeaderSetPacketFieldsId, JSON.stringify(packetFields), true);
});
}
}
});
if (forceReInit) {
adapter.extendForeignObject('system.adapter.' + adapter.namespace, {
native: {
forceReInit: false
}
});
forceReInit = false;
}
});
ctx.connection.connect();
ctx.hsc.startTimer();
}
function initDevice(deviceId, channelId, objectId, item) {
adapter.setObjectNotExists(deviceId, {
type: 'device',
common: {
name: item.deviceName
},
native: {}
});
adapter.setObjectNotExists(channelId, {
type: 'channel',
common: {
name: channelId
},
native: {}
});
var common = {
name: item.name,
type: 'number',
write: false
};
switch (item.unit) {
case 'DegreesCelsius':
var name = 'Unknown';
switch (item.name) {
case 'Temperature S1':
name = 'Kollektor';
break;
case 'Temperature S2':
name = 'Boiler';
break;
case 'Temperature S3':
name = 'Puffer';
break;
};
common.name = name;
common.min = -100;
common.max = +300;
common.role = 'value.temperature';
break;
case 'Percent':
common.min = 0;
common.max = 100;
common.role = 'value.volume';
break;
case 'Hours':
break;
case 'WattHours':
break;
case 'None':
break;
default:
break;
}
adapter.setObjectNotExists(objectId, {
type: 'state',
common: common,
native: {}
});
}
initResol();
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}