forked from Xumpy/node-red-solaredge-modbus-client
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathse-modbus-json.js
160 lines (136 loc) · 5.4 KB
/
se-modbus-json.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
function ModbusScanner(){
const net = require('net');
const modbus = require('modbus-tcp');
function conv_buffer(buffer, index, length, type, device){
let buf_byte_length = 2;
let start = (index - device.start - 1) * buf_byte_length;
let end = start + (length * buf_byte_length);
switch(type) {
case "String(16)":
case "String(32)":
return Buffer.from(buffer).slice(start, end).toString().replace(/\0.*$/g,'')
break
case "uint16":
return Buffer.from(buffer).slice(start, end).readUInt16BE().toString()
break
case "uint32":
case "acc32":
return Buffer.from(buffer).slice(start, end).readUInt32BE().toString()
break
case "int16":
return Buffer.from(buffer).slice(start, end).readInt16BE().toString()
break
case "int32":
return Buffer.from(buffer).slice(start, end).readInt32BE().toString()
break
}
}
function fetch_register(modbusClient, mapping_json){
let device = {
start: mapping_json[0][0] - 1,
end: mapping_json[mapping_json.length - 1][0] + mapping_json[mapping_json.length - 1][1] - 1
}
return new Promise((resolve, reject) => {
modbusClient.readHoldingRegisters(1, device.start, device.end, (error, buffers) => {
if (error) {
console.log(error);
reject(error);
}
resolve({
mapping_json: mapping_json,
buffer: Buffer.concat(buffers),
device: device
});
});
});
}
function fetch_device(modbusClient, node, config){
let result = {NM_Module: config.device};
let per_item = config.json.config.fetch;
let data = config.json.data;
let promises = [];
for (let i=0; i< Math.ceil(data.length / per_item); i++){
let start_index = i * per_item;
let stop_index = ((i + 1) * per_item >= data.length ? data.length : (i + 1) * per_item);
promises.push( fetch_register(modbusClient, data.slice(start_index, stop_index)) );
}
Promise.all(promises).then(promise => {
node.status({fill:"green",shape:"dot",text:"connected"});
for(let prom of promise){
prom.mapping_json.map(map => {
result[map[2]] = conv_buffer(prom.buffer, map[0], map[1], map[3], prom.device);
})
}
node.send({payload: result});
setTimeout(function () {
fetch_device(modbusClient, node, config);
}, config.poll);
}).catch(error => {
node.send(error);
});
}
function exception_handler(config, node, error){
node.error(error);
node.status({fill:"red",shape:"ring",text:"disconnected"});
}
this.connect_and_fetch = function connect_and_fetch(config, node){
let socket;
node.on('close', function(){ node.stopped = true; socket.destroy(); });
config = device_to_config(config);
try{
socket = new net.Socket();
modbusClient= new modbus.Client();
modbusClient.writer().pipe(socket);
socket.pipe(modbusClient.reader());
socket.connect({ host: config.host, port: config.port });
socket.on('close', function(){
var wait = ms => new Promise((r, j)=>setTimeout(r, ms));
if (!node.stopped) (async () => {
await wait(1000); await connect_and_fetch(config, node);
})(); });
node.on('error', function(error){ socket.destroy(); exception_handler(config, node, error); });
socket.on('error', function(error){ socket.destroy(); exception_handler(config, node, error); });
modbusClient.on( 'error', function(error){ socket.destroy(); exception_handler(config, node, error); });
fetch_device(modbusClient, node, config);
} catch (error){
exception_handler(config, node, error);
}
}
}
function device_to_config(config){
switch (config.device) {
case "se_inverter": config.json = require('./config_json/se_inverter.json'); break; // Credits to Brad Slattman
case "em_300": config.json = require('./config_json/em_300.json'); break;
}
return config;
}
module.exports = function(RED) {
function start_node(config){
RED.nodes.createNode(this,config);
let node = this;
node.stopped = false;
node.status({fill:"red",shape:"ring",text:"disconnected"});
if (config.device === undefined) {config.device = "se_inverter"; }
new ModbusScanner().connect_and_fetch(config, node);
}
RED.nodes.registerType("se-modbus",start_node);
}
module.exports.console = function(host, port, poll, device){
let config = {
host: host,
port: port,
poll: poll,
device: device
}
node = {
status: function(status){},
on: function(event, fun){},
send: function(data){
console.log(data);
},
error: function(data){
console.error(data);
}
}
new ModbusScanner().connect_and_fetch(config, node);
}