forked from OttHydromet/node-red-umbparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathumbcontroller_node.js
More file actions
227 lines (203 loc) · 7.64 KB
/
umbcontroller_node.js
File metadata and controls
227 lines (203 loc) · 7.64 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
/**
* Copyright (c) 2020 OTT Hydromet Fellbach GmbH
*
* Node-Red UMB parser
*
* @summary Node-Red UMB parser
* @author Martin Kiepfer <martin.kiepfer@otthydromet.com>
*/
let mod_umbhandler = require('./umbhandler');
let mod_umbparser = require('./umbparser');
const { parse, resolve } = require('path');
const { promises } = require('fs');
const { memory } = require('console');
const umb_consts = require('./umb_consts').umb_consts;
const serialport = require('serialport')
var l_node = undefined;
var l_umbhandler;
var l_cur_config;
var umb_channels = {
name: {value: "WS10"},
channels: {value: [
{enabled:true, ch:"100", chname:"Temperature", chunit:"°C"},
]},
status: ""
};
const UNIT_SYSTEMS = new Map(
[
["°C", "metric"],
["°F", "imperial"],
["m/s", "metric"],
["km/h", "metric"],
["mph", "imperial"],
["kts", "imperial"],
["l/m²", "metric"],
["mm", "metric"],
["in", "imperial"],
["mil", "imperial"],
["l/m²/h", "metric"],
["mm/h", "metric"],
["in/h", "imperial"],
["in/m", "imperial"],
["mil/h", "imperial"],
]
);
/**
* Checks if a given unit @cur_unit is withing a given unit system (@cfg_unitsystem).
*
* @param {string} cur_unit Current unit to be checked (e.g. '°C')
* @param {string} cfg_unitsystem Current selected unit system (possible values: 'all', 'imperial', 'metric')
*/
function checkUnit(cur_unit, cfg_unitsystem) {
let cur_unitsystem = UNIT_SYSTEMS.get(cur_unit);
retval = true;
if(cfg_unitsystem == "all") {
retval = true;
}
else {
if( (cfg_unitsystem == cur_unitsystem)
|| (cur_unitsystem == undefined) ) {
retval = true;
}
else {
retval = false;
}
}
return retval;
}
module.exports = function(RED) {
function UMBControllerNode(config) {
RED.nodes.createNode(this, config);
// BUG: NRU-15 - Communication only working with to-address 0
l_node = this;
l_cur_config = config;
this.cfg_channels = RED.nodes.getNode(config.channels);
if(this.cfg_channels)
{
this.query_channels = [];
this.cfg_channels.channels.forEach(element => {
if(element.enabled)
this.query_channels.push(element.ch);
});
}
let umbgen = new mod_umbparser.UMBGenerator(this);
l_umbhandler = new mod_umbhandler.UMBHandler(l_node, parseInt(config.dev_address, 16), config.com_intf, {
ip_address: config.ip_address,
ip_port: config.ip_port,
sp_tty: config.sp_tty,
sp_baud: config.sp_baud,
sp_parity: config.sp_parity,
});
l_node.on('input', function(msg) {
let umbreq = umbgen.createMultiChReq(parseInt(config.dev_address, 16), this.query_channels);
l_umbhandler.syncTransfer(umbreq).then((response) => {
let retmsg = new Object;
retmsg.payload = response;
l_node.send(retmsg);
});
});
}
RED.nodes.registerType("umbcontroller", UMBControllerNode);
// Register internal URL to list serial port configurations
RED.httpAdmin.get("/ttys", RED.auth.needsPermission('serialport.list'), function(req,res) {
l_node.log("tty list: ");
serialport.list().then( (ports) => {
var tty_list = [];
ports.forEach(cur_tty => {
console.log(cur_tty);
tty_list.push(cur_tty.path);
});
res.json(tty_list);
}, (err) => {
console.error(err);
res.json("");
});
});
// Register internal URL to query channel list (used by channel_list config node)
RED.httpAdmin.get("/umbchannels", RED.auth.needsPermission('umbchannels.read'), function(req,res) {
let umbgen = new mod_umbparser.UMBGenerator(l_node);
let cfg_unitsystem = new URLSearchParams(req.url).get("unitsystem");
/* 1. query number of blocks and channels */
new Promise((resolve, reject) => {
let umbreq = umbgen.createChNumReq(parseInt(l_cur_config.dev_address, 16));
l_umbhandler.syncTransfer(umbreq).then((response) => {
if(response.umbframe == undefined)
{
l_node.log("Error: " + response);
reject("Error: " + response);
}
else
{
numChannels = response.umbframe.framedata.parsed.numChannels;
l_node.log("channels detected: " + numChannels);
resolve(numChannels);
}
});
})
/* 2. Query channel list */
.then((parm) => {
return new Promise((resolve, reject) => {
let umbreq = umbgen.createChListReq(parseInt(l_cur_config.dev_address, 16), 0);
l_umbhandler.syncTransfer(umbreq).then((response) => {
if(response.umbframe == undefined)
{
l_node.log("Error: " + response);
reject("Error: " + response);
}
else
{
l_node.log("Channellist read");
channelList = response.umbframe.framedata.parsed.channels;
resolve(channelList);
}
});
})
})
/* 3. Query channel details */
.then((channelList) => {
return new Promise((resolve, reject) => {
let channelCfg = new Array();
(async () => {
await channelList.reduce(async (memo, curChannel) => {
await memo;
let umbreq = umbgen.createChDetailsReq(parseInt(l_cur_config.dev_address, 16), curChannel);
await l_umbhandler.syncTransfer(umbreq).then((response) => {
if(response.umbframe == undefined)
{
l_node.log("Error: " + response);
reject("Error: " + response);
return response;
}
else
{
l_node.log("CurChannel: " + curChannel);
curChDetails = new Object();
curChDetails.enabled = false;
curChDetails.ch = curChannel;
curChDetails.chname = response.umbframe.framedata.parsed.name;
curChDetails.chunit = response.umbframe.framedata.parsed.unit;
if(checkUnit(curChDetails.chunit, cfg_unitsystem)) {
channelCfg.push(curChDetails);
}
else {
l_node.log("skipped");
}
}
});
}, undefined);
channelCfg;
resolve(channelCfg);
})();
});
})
.then((channelCfg) => {
umb_channels.channels.value = channelCfg;
res.json(umb_channels);
})
.catch((error) => {
l_node.log("Error: = " + error);
umb_channels.error = error;
res.json(umb_channels);
});
});
}