@teamcymbeline/node-red-contrib-mcprotocol 0.0.0-stage → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/nodes/write.js ADDED
@@ -0,0 +1,284 @@
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2019, 2020 Steve-Mcl
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ module.exports = function(RED) {
26
+ var connection_pool = require("../connection_pool.js");
27
+
28
+ function isNumeric(n) {
29
+ return !isNaN(parseFloat(n)) && isFinite(n);
30
+ }
31
+ function mcWrite(config) {
32
+ RED.nodes.createNode(this, config);
33
+ this.name = config.name;
34
+ this.topic = config.topic;
35
+ this.connection = config.connection;
36
+ this.address = config.address;
37
+ this.data = config.data;
38
+ this.address = config.address || ""; //address
39
+ this.addressType = config.addressType || "str";
40
+ this.data = config.data || ""; //data
41
+ this.dataType = config.dataType || "num";
42
+ this.errorHandling = config.errorHandling;
43
+ this.outputs = config.errorHandling === "output2" ? 2 : 1;//1 output pins if throw or msg.error, 2 outputs if errors to go to seperate output pin
44
+ this.logLevel = RED.settings.logging.console.level;
45
+ this.connectionConfig = RED.nodes.getNode(this.connection);
46
+ var context = this.context();
47
+ var node = this;
48
+ node.busy = false;
49
+ node.busyTimeMax = 1000; //Initial value, will be updated bt selected connection
50
+ //var mcprotocol = require('../mcprotocol.js');
51
+ if (this.connectionConfig) {
52
+ node.status({ fill: "yellow", shape: "ring", text: "initialising" });
53
+ var options = Object.assign({}, node.connectionConfig.options);
54
+ options.logLevel = this.logLevel;
55
+ this.connection = connection_pool.get(
56
+ this.connectionConfig.port,
57
+ this.connectionConfig.host,
58
+ options
59
+ );
60
+ if(options.timeout && isNumeric(options.timeout)) node.busyTimeMax = parseInt(options.timeout);
61
+
62
+ this.connection.on("error", function(error) {
63
+ console.error(error);
64
+ node.status({ fill: "red", shape: "ring", text: "error" });
65
+ });
66
+ this.connection.on("open", function(error) {
67
+ node.status({ fill: "green", shape: "dot", text: "connected" });
68
+ });
69
+ this.connection.on("close", function(error) {
70
+ node.status({ fill: "red", shape: "dot", text: "not connected" });
71
+ });
72
+ function handleError(err, msg, node, config, dont_send_msg){
73
+ if(typeof err === "string"){
74
+ err = new Error(err);
75
+ }
76
+ if(!config) config = {};
77
+ if(typeof config === "string"){
78
+ config = {
79
+ errorHandling: config
80
+ }
81
+ }
82
+ switch (config.errorHandling) {
83
+ case "throw":
84
+ node.error(err,msg);
85
+ break;
86
+ case "msg":
87
+ msg.error = err;
88
+ if(!dont_send_msg) node.send(msg);//send error on 1st pin
89
+ break;
90
+ case "output2":
91
+ node.send([null,{payload: err}]);//send error on 2nd pin
92
+ break;
93
+
94
+ default:
95
+ node.error(err,msg);
96
+ break;
97
+ }
98
+ }
99
+ function myReply(problem, msg) {
100
+ node.busy = false; //reset busy - allow node to be triggered
101
+ clearTimeout(node.busyMonitor);
102
+
103
+ if (msg.timeout) {
104
+ node.status({ fill: "red", shape: "ring", text: "timeout" });
105
+ handleError("timeout", msg, node, node.errorHandling);
106
+ //node.error("timeout", msg);
107
+ var dbgmsg = {
108
+ f: "myReply(msg)",
109
+ msg: msg,
110
+ error: "timeout"
111
+ };
112
+ // console.error(dbgmsg);
113
+ return; //halt flow
114
+ }
115
+
116
+ if (problem) {
117
+ msg.problem = true;
118
+ node.status({ fill: "grey", shape: "ring", text: "Quality Issue" });
119
+ } else {
120
+ node.status({ fill: "green", shape: "dot", text: "Good" });
121
+ }
122
+ node.msgMem.payload = !problem;
123
+ node.msgMem.mcWriteDetails = msg;
124
+ if(problem) {
125
+ handleError(msg.error || "", node.msgMem, node, node.errorHandling);
126
+ } else {
127
+ node.send(node.msgMem);
128
+ }
129
+ }
130
+ this.on("input", function(msg) {
131
+ if (msg.disconnect === true || msg.topic === "disconnect") {
132
+ this.connection.closeConnection();
133
+ return;
134
+ } else if (msg.connect === true || msg.topic === "connect") {
135
+ this.connection.connect();
136
+ return;
137
+ } else if (msg.reinitialize === true || msg.topic === "reinitialize") {
138
+ this.connection.reinitialize();
139
+ return;
140
+ }
141
+
142
+ if (node.busy) return; //TODO: Consider queueing inputs?
143
+ node.request = undefined;
144
+ node.msgMem = msg;
145
+
146
+ var isObject = function(val) {
147
+ if (val === null) {
148
+ return false;
149
+ }
150
+ return typeof val === "function" || typeof val === "object";
151
+ };
152
+ var addr; // = /* node.address || */ config.address || msg.payload.address;
153
+ var data;
154
+ //address - address
155
+ RED.util.evaluateNodeProperty(
156
+ node.address,
157
+ node.addressType,
158
+ node,
159
+ msg,
160
+ (err, value) => {
161
+ if (err) {
162
+ handleError("Unable to evaluate address", msg, node, node.errorHandling);
163
+ //node.error("Unable to evaluate address", msg);
164
+ node.status({
165
+ fill: "red",
166
+ shape: "ring",
167
+ text: "Unable to evaluate address"
168
+ });
169
+ return;
170
+ } else {
171
+ addr = value;
172
+ if (addr == "") {
173
+ handleError("address is empty", msg, node, node.errorHandling);
174
+ //node.error("address is empty", msg);
175
+ node.status({
176
+ fill: "red",
177
+ shape: "ring",
178
+ text: "address is empty"
179
+ });
180
+ return;
181
+ }
182
+ }
183
+ }
184
+ );
185
+
186
+ //data - data
187
+ node.trace("node.data...");
188
+ node.trace(node.data);
189
+
190
+ // var csv2arr = function (str) {
191
+ // return node.data.split(',').map(Number);
192
+ // }
193
+ if (node.dataType == "str") {
194
+ node.trace("mcwrite input:node.dataType == 'str'");
195
+ data = node.data;
196
+ } else if (node.dataType == "num") {
197
+ node.trace("mcwrite input:node.dataType == 'num'");
198
+ //data = [node.data];
199
+ data = node.data;
200
+ } else if (node.dataType == "csv") {
201
+ node.trace("mcwrite input:node.dataType == 'csv'");
202
+ //data = csv2arr(node.data);
203
+ data = node.data;
204
+ } else {
205
+ node.trace("mcwrite input:evaluateNodeProperty");
206
+ RED.util.evaluateNodeProperty(
207
+ node.data,
208
+ node.dataType,
209
+ node,
210
+ msg,
211
+ (err, value) => {
212
+ if (err) {
213
+ msg.dataerr = err;
214
+ handleError("Unable to evaluate data", msg, node, node.errorHandling);
215
+ //node.error("Unable to evaluate data", msg);
216
+ node.status({
217
+ fill: "red",
218
+ shape: "ring",
219
+ text: "Unable to evaluate data"
220
+ });
221
+ return;
222
+ } else {
223
+ data = value;
224
+ }
225
+ }
226
+ );
227
+ }
228
+
229
+ if (data == null) {
230
+ handleError("data is empty", msg, node, node.errorHandling);
231
+ //node.error("Data is empty", msg);
232
+ return;
233
+ }
234
+
235
+ try {
236
+ node.status({ fill: "yellow", shape: "ring", text: "write" });
237
+ node.busy = true;
238
+ if (node.busyTimeMax) {
239
+ this.busyMonitor = setTimeout(function() {
240
+ if (node.busy) {
241
+ node.status({ fill: "red", shape: "ring", text: "timeout" });
242
+ handleError("timeout", msg, node, node.errorHandling);
243
+ //node.error("timeout", msg || node.msgMem || {});
244
+ node.busy = false;
245
+ return;
246
+ }
247
+ }, node.busyTimeMax);
248
+ }
249
+
250
+ node.trace("mcwrite input: addr, data...");
251
+ node.trace(addr);
252
+ node.trace(data);
253
+
254
+ this.connection.write(addr, data, myReply);
255
+ } catch (error) {
256
+ node.busy = false;
257
+ //node.error(error, msg || node.msgMem || {});
258
+ handleError(error, msg, node, node.errorHandling);
259
+ node.status({ fill: "red", shape: "ring", text: "error" });
260
+ var dbgmsg = {
261
+ info:
262
+ "write.js-->on 'input' - try this.connection.write(addr, data, myReply)",
263
+ connection: `host: ${node.connectionConfig.host}, port: ${node.connectionConfig.port}`,
264
+ address: addr,
265
+ data: data,
266
+ error: error
267
+ };
268
+ node.warn(dbgmsg);
269
+ return;
270
+ }
271
+ });
272
+ node.status({ fill: "green", shape: "ring", text: "ready" });
273
+ } else {
274
+ node.error("configuration not setup");
275
+ node.status({ fill: "red", shape: "ring", text: "error" });
276
+ }
277
+ }
278
+ RED.nodes.registerType("MC Write", mcWrite);
279
+ mcWrite.prototype.close = function() {
280
+ if (this.connection) {
281
+ this.connection.disconnect();
282
+ }
283
+ };
284
+ };
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "@teamcymbeline/node-red-contrib-mcprotocol",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.4.1",
4
+ "description": "Node-RED nodes to read from and write to MITSUBISHI PLCs over Ethernet using MC Protocol",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/TeamCymbeline/node-red-contrib-mcprotocol.git"
8
+ },
9
+ "keywords": [
10
+ "node-red",
11
+ "MITSUBISHI",
12
+ "MC PROTOCOL",
13
+ "MELSEC",
14
+ "PLC"
15
+ ],
16
+ "node-red": {
17
+ "nodes": {
18
+ "mcRead": "nodes/read.js",
19
+ "mcWrite": "nodes/write.js",
20
+ "mcConnection": "nodes/connection.js"
21
+ }
22
+ },
23
+ "author": {
24
+ "name": "TeamCymbeline"
25
+ },
26
+ "license": "MIT",
27
+ "bugs": {
28
+ "url": "https://github.com/TeamCymbeline/node-red-contrib-mcprotocol/issues"
29
+ },
30
+ "homepage": "https://github.com/TeamCymbeline/node-red-contrib-mcprotocol#readme"
31
+ }