smithtek-mako-rf 1.4.0 → 1.6.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smithtek-mako-rf",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "description": "Smithtek dedicated node for communicating with the Mako PLC over RS485 or RF",
5
5
  "keywords": [
6
6
  "node-red",
@@ -496,17 +496,23 @@ module.exports = function (RED) {
496
496
  ensureBusState(this);
497
497
 
498
498
  this.on("close", (removed, done) => {
499
- if (removed) {
500
- const s = BUS.get(this.id);
501
- if (s) {
502
- try {
503
- s.client.close(() => {});
504
- } catch (_e) {}
499
+ const s = BUS.get(this.id);
500
+ if (s) {
501
+ // Clear queue and reset flags
502
+ s.queue = [];
503
+ s.busy = false;
504
+
505
+ try {
506
+ s.client.close(() => {});
507
+ } catch (_e) {}
508
+
509
+ if (removed) {
505
510
  BUS.delete(this.id);
506
511
  }
507
512
  }
508
513
  done();
509
514
  });
515
+
510
516
  }
511
517
  RED.nodes.registerType("smithtek-mako-rf-bus", SmithtekMakoRfBusConfig);
512
518
 
@@ -1,190 +0,0 @@
1
-
2
- /**
3
- * number2bcd -> takes a bcd number and returns the corresponding decimal value
4
- * @param {Number} number BCD number to convert
5
- * @param {*} digits no of digits (default 4)
6
- */
7
- const bcd2number = function (number, digits = 4) {
8
- let loByte = (number & 0x00ff);
9
- let hiByte = (number >> 8) & 0x00ff;
10
- let n = 0;
11
- n += (loByte & 0x0F) * 1;
12
- if (digits < 2) return n;
13
- n += ((loByte >> 4) & 0x0F) * 10;
14
- if (digits < 3) return n;
15
- n += (hiByte & 0x0F) * 100;
16
- if (digits < 4) return n;
17
- n += ((hiByte >> 4) & 0x0F) * 1000;
18
- return n;
19
- }
20
-
21
-
22
- /**
23
- * number2bcd -> takes a number and returns the corresponding BCD value.
24
- * @param {Number} number number to convert to bcd
25
- * @param {Number} [digits] no of digits (default 4)
26
- * @returns {Buffer} nodejs buffer
27
- */
28
- const number2bcd = function (number, digits) {
29
- var s = digits || 4; //default value: 4
30
- var n = 0;
31
-
32
- n = (number % 10);
33
- number = (number / 10) | 0;
34
- if (s < 2) return n;
35
- n += (number % 10) << 4;
36
- number = (number / 10) | 0;
37
- if (s < 3) return n;
38
- n += (number % 10) << 8;
39
- number = (number / 10) | 0;
40
- if (s < 4) return n;
41
- n += (number % 10) << 12;
42
- number = (number / 10) | 0;
43
- return n;
44
- }
45
-
46
- function byteToBits(val) {
47
- var bits = [];
48
- for (let index = 0; index < 8; index++) {
49
- const bit = getBit(val, index);
50
- bits.push(bit);
51
- }
52
-
53
- return {
54
- bits: bits,
55
- bit0: bits[0],
56
- bit1: bits[1],
57
- bit2: bits[2],
58
- bit3: bits[3],
59
- bit4: bits[4],
60
- bit5: bits[5],
61
- bit6: bits[6],
62
- bit7: bits[7],
63
- }
64
- }
65
- function wordToBits(val) {
66
- var bits = [];
67
- for (let index = 0; index < 16; index++) {
68
- const bit = getBit(val, index);
69
- bits.push(bit);
70
- }
71
- return {
72
- bits: bits,
73
- bit0: bits[0],
74
- bit1: bits[1],
75
- bit2: bits[2],
76
- bit3: bits[3],
77
- bit4: bits[4],
78
- bit5: bits[5],
79
- bit6: bits[6],
80
- bit7: bits[7],
81
- bit8: bits[8],
82
- bit9: bits[9],
83
- bit10: bits[10],
84
- bit11: bits[11],
85
- bit12: bits[12],
86
- bit13: bits[13],
87
- bit14: bits[14],
88
- bit15: bits[15],
89
- }
90
- }
91
-
92
- //Get Bit
93
- function getBit(number, bitPosition) {
94
- return (number & (1 << bitPosition)) === 0 ? 0 : 1;
95
- }
96
- //Set Bit
97
- function setBit(number, bitPosition) {
98
- return number | (1 << bitPosition);
99
- }
100
- //Clear Bit
101
- function clearBit(number, bitPosition) {
102
- const mask = ~(1 << bitPosition);
103
- return number & mask;
104
- }
105
- //Update Bit
106
- function updateBit(number, bitPosition, bitValue) {
107
- const bitValueNormalized = bitValue ? 1 : 0;
108
- const clearMask = ~(1 << bitPosition);
109
- return (number & clearMask) | (bitValueNormalized << bitPosition);
110
- }
111
- function bitsToByte(bits) {
112
- var byte = 0;
113
- for (let index = 0; index < 8; index++) {
114
- let bit = bits[index];
115
- if (bit) byte = setBit(byte, index);
116
- }
117
- return byte;
118
- }
119
- function bitsToWord(val) {
120
- var wd = 0;
121
- for (let index = 0; index < 16; index++) {
122
- let bit = val[index];
123
- if (bit) wd = setBit(wd, index);
124
- }
125
- return wd;
126
- }
127
-
128
- const SWAPOPTS = ["swap16", "swap32", "swap64"];
129
- const TYPEOPTS = [
130
- "int", "int8", "byte",
131
- "uint", "uint8",
132
- "int16", "int16le", "int16be", "uint16", "uint16le", "uint16be",
133
- "int32", "int32le", "int32be", "uint32", "uint32le", "uint32be",
134
- "bigint64", "bigint64le", "bigint64be", "biguint64", "biguint64le", "biguint64be",
135
- "float", "floatle", "floatbe", "double", "doublele", "doublebe",
136
- "8bit", "16bit", "16bitle", "16bitbe", "bool",
137
- "bcd", "bcdle", "bcdbe",
138
- "string", "hex", "ascii", "utf8", "utf-8", "utf16le", "ucs2", "latin1", "binary", "buffer"
139
- ];
140
-
141
- /**
142
- * helper function to set a nested property by path
143
- * @param {*} obj - the object in which to set a properties value
144
- * @param {string} path - the path to the property e.g. payload.value
145
- * @param {*} val - the value to set in obj.path
146
- */
147
- function setObjectProperty(obj, path, val, sep) {
148
- sep = sep == null ? "." : sep;
149
- const keys = path.split(sep);
150
- const lastKey = keys.pop();
151
- const lastObj = keys.reduce((obj, key) =>
152
- obj[key] = obj[key] || {},
153
- obj);
154
- lastObj[lastKey] = val;
155
- }
156
-
157
- /**
158
- * helper function to get a property by path
159
- * @param {*} obj - the object in which to set a properties value
160
- * @param {string} path - the path to the property e.g. payload.value
161
- * @param {*} [sep] - the path property separator (defaults to `.`)
162
- */
163
- function getObjectProperty(obj, path, sep) {
164
- sep = sep == null ? "." : sep;
165
- for (var i=0, path=path.split(sep), len=path.length; i<len; i++){
166
- obj = obj[path[i]];
167
- };
168
- return obj;
169
- }
170
-
171
- function isNumber(n) {
172
- if (n === "" || n === true || n === false) return false;
173
- return !isNaN(parseFloat(n)) && isFinite(n);
174
- }
175
-
176
- exports.bcd2number = bcd2number;
177
- exports.number2bcd = number2bcd;
178
- exports.byteToBits = byteToBits;
179
- exports.wordToBits = wordToBits;
180
- exports.bitsToByte = bitsToByte;
181
- exports.bitsToWord = bitsToWord;
182
- exports.getBit = getBit;
183
- exports.setBit = setBit;
184
- exports.clearBit = clearBit;
185
- exports.updateBit = updateBit;
186
- exports.isNumber = isNumber;
187
- exports.setObjectProperty = setObjectProperty;
188
- exports.getObjectProperty = getObjectProperty;
189
- exports.SWAPOPTS = SWAPOPTS;
190
- exports.TYPEOPTS = TYPEOPTS;