forked from Apollon77/node-mbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (191 loc) · 5.91 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
/* jshint -W097 */
// jshint strict:true
/*jslint node: true */
/*jslint esversion: 6 */
'use strict';
var mbusBinding = require('bindings')('mbus');
var xmlParser = require('xml2js');
function MbusMaster(options) {
this.options = options;
this.mbusMaster = new mbusBinding.MbusMaster();
}
MbusMaster.prototype.connect = function 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) {
var 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;
};
MbusMaster.prototype.close = function close(callback, wait) {
if (wait === undefined) {
if (callback) wait = true;
else wait = false;
}
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;
}
var self = this;
setTimeout(function() {
self.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 unsuccessfull'));
}
return false;
}
};
MbusMaster.prototype.getData = function getData(address, callback) {
if (!this.mbusMaster.connected && !this.options.autoConnect) {
if (callback) callback(new Error('Not connected and autoConnect is false'));
return;
}
var self = this;
this.connect(function(err) {
if (err) {
if (callback) callback(err);
return;
}
self.mbusMaster.get(address, function(err, data) {
if (!err && data) {
//data = JSON.parse(data).MBusData;
var parserOpt = {
explicitArray: false,
mergeAttrs: true,
valueProcessors: [xmlParser.processors.parseNumbers],
attrValueProcessors: [xmlParser.processors.parseNumbers]
};
xmlParser.parseString(data, parserOpt, function (err, result) {
if (!err) {
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);
});
});
};
MbusMaster.prototype.scanSecondary = function scanSecondary(callback) {
if (!this.mbusMaster.connected && !this.options.autoConnect) {
if (callback) callback(new Error('Not connected and autoConnect is false'));
return;
}
var self = this;
this.connect(function(err) {
if (err) {
if (callback) callback(err);
return;
}
self.mbusMaster.scan(function(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);
});
});
};
MbusMaster.prototype.setPrimaryId = function setPrimaryId(oldAddress, newAddress, callback) {
if (!this.mbusMaster.connected && !this.options.autoConnect) {
if (callback) callback(new Error('Not connected and autoConnect is false'));
return;
}
var self = this;
this.connect(function(err) {
if (err) {
if (callback) callback(err);
return;
}
self.mbusMaster.setPrimaryId(oldAddress, newAddress, function(err) {
if (err) {
err = new Error(err);
}
else {
err = null;
}
if (callback) callback(err);
});
});
};
module.exports = MbusMaster;