forked from samkrew/node-mbus
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
277 lines (257 loc) · 8.29 KB
/
index.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
/* jshint -W097 */
// jshint strict:true
/*jslint node: true */
/*jslint esversion: 6 */
'use strict';
const mbusBinding = require('bindings')('mbus');
const xmlParser = require('xml2js');
class MbusMaster {
constructor(options) {
this.options = options || {};
this.mbusMaster = new mbusBinding.MbusMaster();
}
connect(callback) {
if (this.mbusMaster.connected && this.mbusMaster.communicationInProgress) {
if (callback) {
callback(new Error('Communication already in progress'));
}
return false;
}
if (this.mbusMaster.connected) {
if (callback) {
callback(null);
}
return true;
}
if (this.options.host && this.options.port) {
if (!this.options.timeout) this.options.timeout = 0;
if (this.mbusMaster.openTCP(this.options.host, this.options.port, this.options.timeout/1000)) {
if (callback) {
callback(null);
}
return true;
}
else {
if (callback) {
callback(new Error('No connection possible to MBus Host ' + this.options.host + ':' + this.options.port));
}
return false;
}
}
else if (this.options.serialPort) {
const baudRate = this.options.serialBaudRate || 0;
if (this.mbusMaster.openSerial(this.options.serialPort, baudRate)) {
if (callback) {
callback(null);
}
return true;
}
else {
if (callback) {
callback(new Error('No connection possible to MBus Serial port ' + this.options.serialPort));
}
return false;
}
}
if (callback) {
callback(new Error('No valid connection parameter provided'));
}
return false;
}
connectAsync() {
return new Promise((resolve, reject) => {
this.connect((err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
}
close(callback, wait) {
if (wait === undefined) {
wait = !!callback;
}
if (wait && !callback) wait = false;
if (this.mbusMaster.connected && this.mbusMaster.communicationInProgress) {
if (!wait) {
if (callback) {
callback(new Error('Communication still in progress.'));
}
return false;
}
setTimeout(() => {
this.close(callback, wait);
}, 500);
return undefined;
}
if (!this.mbusMaster.connected) {
if (callback) {
callback(null);
}
return true;
}
if (this.mbusMaster.close()) {
if (callback) {
callback(null);
}
return true;
}
else {
if (callback) {
callback(new Error('Close unsuccessful'));
}
return false;
}
}
closeAsync() {
return new Promise((resolve, reject) => {
this.close((err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
}
getData(address, options, callback) {
// default options
// pingFirst: Work-around buggy behaviour with some M-Bus devices,
// notably Sontex Supercal531
// https://github.com/rscada/libmbus/pull/95
if (typeof(options) === "function") {
callback = options;
options = null;
}
const pingFirst = (options && options.pingFirst !== undefined) ? options.pingFirst : true;
if (!this.mbusMaster.connected && !this.options.autoConnect) {
if (callback) callback(new Error('Not connected and autoConnect is false'));
return;
}
this.connect((err) => {
if (err) {
if (callback) callback(err);
return;
}
this.mbusMaster.get(address, pingFirst, (err, data) => {
if (!err && data) {
//data = JSON.parse(data).MBusData;
const parserOpt = {
explicitArray: false,
mergeAttrs: true,
valueProcessors: [xmlParser.processors.parseNumbers],
attrValueProcessors: [xmlParser.processors.parseNumbers]
};
xmlParser.parseString(data, parserOpt, (err, result) => {
if (!err && result && result.MBusData) {
result = result.MBusData;
if (result.DataRecord && !Array.isArray(result.DataRecord)) {
result.DataRecord = [result.DataRecord];
}
}
if (callback) callback(err, result);
});
return;
}
else {
err = new Error(err);
}
if (callback) callback(err, data);
});
});
}
getDataAsync(address, options = null) {
return new Promise((resolve, reject) => {
this.getData(address, options, (err, data) => {
if (err) {
reject(err);
}
else {
resolve(data);
}
});
});
}
scanSecondary(callback) {
if (!this.mbusMaster.connected && !this.options.autoConnect) {
if (callback) callback(new Error('Not connected and autoConnect is false'));
return;
}
this.connect((err) => {
if (err) {
if (callback) callback(err);
return;
}
this.mbusMaster.scan((err, data) => {
if (!err && data !== null && data !== undefined && typeof data === 'string' ) {
if (data === '') {
data = [];
}
else {
try {
data = JSON.parse(data);
}
catch (e) {
err = new Error(e + ': ' + data);
data = null;
}
}
}
else {
err = new Error(err);
}
if (callback) callback(err, data);
});
});
}
scanSecondaryAsync() {
return new Promise((resolve, reject) => {
this.scanSecondary((err, data) => {
if (err) {
reject(err);
}
else {
resolve(data);
}
});
});
}
setPrimaryId(oldAddress, newAddress, callback) {
if (!this.mbusMaster.connected && !this.options.autoConnect) {
if (callback) callback(new Error('Not connected and autoConnect is false'));
return;
}
this.connect((err) => {
if (err) {
if (callback) callback(err);
return;
}
this.mbusMaster.setPrimaryId(oldAddress, newAddress, (err) => {
if (err) {
err = new Error(err);
}
else {
err = null;
}
if (callback) callback(err);
});
});
}
setPrimaryIdAsync(oldAddress, newAddress) {
return new Promise((resolve, reject) => {
this.setPrimaryId(oldAddress, newAddress, (err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
}
}
module.exports = MbusMaster;