-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathumbhandler.js
More file actions
292 lines (245 loc) · 10.3 KB
/
umbhandler.js
File metadata and controls
292 lines (245 loc) · 10.3 KB
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
/**
* Copyright (c) 2020 OTT Hydromet Fellbach GmbH
*
* UMB handler class
*
* @summary UMB handler class
* @author Martin Kiepfer <martin.kiepfer@otthydromet.com>
*/
const net = require('net');
let mod_umbparser = require('./umbparser');
const { throws } = require('assert');
const { EventEmitter } = require('events');
const umb_consts = require('./umb_consts').umb_consts;
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
let l_socket_id = 0;
/**
* UMB Handler class that is responsible to coordinate communication to UMB devices
*/
class UMBHandler
{
#node;
#address;
#com_intf;
#umbparser
/**
*
* @param {node} node Reference to Node-Red instrance
* @param {int} address Device-Adresse of the device to communicate with
* @param {int} com_intf Communication-com_intf to be used (0: Serial, 1: IP)
* @param {Object} paramset Context of paramters: ip_port, ip_address, sp_tty, sp_baud, sp_parity
*/
constructor(node, address, com_intf, paramset)
{
var self = this;
const default_paramset = {
ip_port: "9750",
ip_address: "192.168.0.1",
sp_tty: "/dev/ttyS0",
sp_baud: "19200",
sp_parity: "8N1"
}
this.paramset = Object.assign({}, default_paramset, paramset);
this.#node = node;
this.#address = address;
this.#com_intf = com_intf;
this.#umbparser = new mod_umbparser.UMBParser(this.#node);
this.#node.status({fill:"red", shape:"ring", text:"disconnected"});
}
/**
* This function will execute a UMB transfer depending on the configured
*
* @param {Array} umbreq Raw UMB frame data for the request to be queried
*/
async syncTransfer(umbreq) {
if(this.#com_intf == 1) {
return this.#syncTransfer_NET(umbreq);
} else {
return this.#syncTransfer_SERIAL(umbreq);
}
}
/**
* This internal function will execute a UMB transfer over serial comminucation channel
*
* @param {Array} umbreq Raw UMB frame data for the request to be queried
*/
async #syncTransfer_SERIAL(umbreq) {
let fnct_retval = undefined;
let data_sent = false;
let datatimer = undefined;
let num_retries = 0;
let serialport = new SerialPort(this.paramset.sp_tty, {
baudRate: parseInt(this.paramset.sp_baud),
dataBits: 8,
parity: (this.paramset.sp_parity == "8N1") ? 'none' : 'even',
stopBits: 1,
flowControl: false
});
while(fnct_retval == undefined) {
this.#node.log("TX start (length:" + umbreq.length + ")");
this.#node.status({fill:"green",shape:"ring",text:"connected"});
await new Promise( (resolve, reject) => {
serialport.on('data', (chunk) => {
let parsedFrame;
this.#node.log("tty RX: " + chunk.length + "bytes");
this.#node.trace(chunk.toString('hex'));
// @Note: Some USB to serial adapters seem to receive 1 byte frames, although no device is connected
parsedFrame = this.#umbparser.ParseReadBuf(chunk);
this.#node.log("Parser status: " + parsedFrame.parserState);
switch(parsedFrame.parserState) {
case mod_umbparser.PAR_STATE.PARSER_FINISHED:
this.#node.log("Frametype: " + parsedFrame.umbframe.type);
this.#node.log("Framestatus: " + parsedFrame.umbframe.status);
this.#node.log("Framecmd: " + parsedFrame.umbframe.cmd);
resolve(parsedFrame);
break;
case mod_umbparser.PAR_STATE.PARSER_IDLE:
case mod_umbparser.PAR_STATE.PARSER_PROCESSING:
// still processing wait for complete frame to be parsed
this.#node.log("processing...");
break;
case mod_umbparser.PAR_STATE.PARSER_ERROR:
resolve("Parsing error");
break;
case mod_umbparser.PAR_STATE.PARSER_CRCERROR:
resolve("CRC error");
break;
}
});
serialport.on('error', (error) => {
resolve("Data error on serial port (" + error + ")");
});
if(!data_sent) {
this.#umbparser.resetParser();
// send request
serialport.write(umbreq);
data_sent = true;
datatimer = setTimeout(() => {
this.#node.log("Data timeout");
resolve("Data timeout");
}, umb_consts.UMB_TIMEOUT.TIMEOUT_LONG*2);
}
}).then( (result)=> {
clearTimeout(datatimer);
// data timeout. execute 3 retries
if (result == "Data timeout")
{
this.#node.log("Data timeout #" + num_retries)
num_retries++;
data_sent = false;
if (num_retries > 3) {
fnct_retval = "Data Timeout";
}
}
else {
fnct_retval = result;
}
});
}
this.#node.log("TX end (" + fnct_retval + ")");
if(serialport.isOpen) {
serialport.close();
}
this.#node.status({fill:"red",shape:"ring",text:"disconnected"});
return fnct_retval;
}
/**
* This internal function will execute a UMB transfer over network communication
*
* @param {Array} umbreq Raw UMB frame data for the request to be queried
*/
async #syncTransfer_NET(umbreq)
{
let fnct_retval = undefined;
let num_retries = 0;
let socket = new net.Socket();
let socket_id = l_socket_id++;
let conTimeout;
this.#node.log("TX start (length:" + umbreq.length + ")");
while(fnct_retval == undefined) {
// Setup connection
await new Promise( (resolve, reject) => {
this.#node.log("[" + socket_id + "] Socket created");
// Setup commuication handlers
socket.setNoDelay(true);
socket.on('error', (ex) => {
this.#node.log("[" + socket_id + "] Socket error (" + ex + ")");
this.#node.log(ex);
reject('Socket error (' + ex + ')');
});
socket.on('close', (hadError) => {
this.#node.log("[" + socket_id + "] Socket closed (Error: " + hadError + ")");
reject('Socket closed');
this.#node.status({fill:"red",shape:"ring",text:"disconnected"});
});
socket.on('connect', () => {
clearTimeout(conTimeout);
this.#node.log("[" + socket_id + "] Socket connected");
this.#node.status({fill:"green",shape:"ring",text:"connected"});
socket.setTimeout(umb_consts.UMB_TIMEOUT.TIMEOUT_LONG*2, () => {
this.#node.log("Data timeout");
reject("Data timeout");
});
socket.write(umbreq);
})
socket.on('data', (data) => {
this.#node.log("[" + socket_id + "] Socket RX: " + data.length + "bytes");
this.#node.log("Valid input buffer detected");
let parsedFrame = this.#umbparser.ParseReadBuf(data);
this.#node.log("Parsing status:")
this.#node.log("parser status: " + parsedFrame.parserState);
if(parsedFrame.parserState == "finished")
{
this.#node.log("Frametype: " + parsedFrame.umbframe.type);
this.#node.log("Framestatus: " + parsedFrame.umbframe.status);
this.#node.log("Framecmd: " + parsedFrame.umbframe.cmd);
resolve(parsedFrame);
}
else if(parsedFrame.parserState == "processing")
{
this.#node.log("processing...");
}
});
// Prepare timeouts
conTimeout = setTimeout(() => {
this.#node.log("Connection timeout");
reject("Connection timeout");
}, 5000);
// Connect & send request
socket.connect(this.paramset.ip_port, this.paramset.ip_address);
})
// Wait for result
.then( (result) => {
// disable all timers
clearTimeout(conTimeout);
socket.setTimeout(0);
if(result != undefined) {
fnct_retval = result;
}
this.#node.status({fill:"red", shape:"ring", text:"disconnected"});
})
.catch( (error) => {
// disable all timers
clearTimeout(conTimeout);
socket.setTimeout(0);
// data timeout. execute 3 retries
if (error == "Data timeout")
{
this.#node.log("Data timeout #" + num_retries)
num_retries++;
if (num_retries > 3) {
fnct_retval = "Data timeout";
}
}
else {
fnct_retval = error;
}
});
}
socket.end();
this.#node.log("TX end (" + fnct_retval + ")");
return fnct_retval;
}
}
module.exports.UMBHandler = UMBHandler;