@ali-pay/node-red-contrib-s7 3.2.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/.github/ISSUE_TEMPLATE/bug_report.md +40 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/CHANGELOG.md +146 -0
- package/LICENSE +674 -0
- package/README.md +136 -0
- package/package.json +36 -0
- package/red/locales/de/s7.json +127 -0
- package/red/locales/en-US/s7.json +127 -0
- package/red/s7.html +1444 -0
- package/red/s7.js +712 -0
- package/src/tools.js +85 -0
- package/test/flow_writeTest.json +1507 -0
package/src/tools.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
/*
|
|
3
|
+
Copyright: (c) 2016-2020, ST-One Ltda., Guilherme Francescon Cittolin <guilherme@st-one.io>
|
|
4
|
+
GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const util = require('util');
|
|
9
|
+
const os = require('os');
|
|
10
|
+
const exec = util.promisify(require('child_process').exec);
|
|
11
|
+
|
|
12
|
+
const pnToolsPath = 'pn-tools';
|
|
13
|
+
const pnDevRoleMask = 0x02;
|
|
14
|
+
|
|
15
|
+
class Tools {
|
|
16
|
+
|
|
17
|
+
constructor() {
|
|
18
|
+
this.pnToolsAvailable = null;
|
|
19
|
+
this.ifaceCache = new Map();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async isPnToolsAvailable() {
|
|
23
|
+
if (this.pnToolsAvailable === null) {
|
|
24
|
+
try {
|
|
25
|
+
await exec(`${pnToolsPath} version`);
|
|
26
|
+
this.pnToolsAvailable = true;
|
|
27
|
+
} catch (e) {
|
|
28
|
+
this.pnToolsAvailable = false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return this.pnToolsAvailable;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getIfaces() {
|
|
36
|
+
let ifaces = os.networkInterfaces();
|
|
37
|
+
return Object.keys(ifaces).filter(v => ifaces[v][0] && !ifaces[v][0].internal)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async listDevicesPN() {
|
|
41
|
+
if (!await this.isPnToolsAvailable()) return [];
|
|
42
|
+
|
|
43
|
+
let proms = [];
|
|
44
|
+
for (const iface of this.getIfaces()) {
|
|
45
|
+
proms.push(exec(`${pnToolsPath} discovery -o -i "${iface}"`).then(res => {return {iface, res};}));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let devs = [];
|
|
49
|
+
let results = await Promise.all(proms);
|
|
50
|
+
|
|
51
|
+
for (const elm of results) {
|
|
52
|
+
let iface = elm.iface;
|
|
53
|
+
let out = elm.res.stdout.trim().split('\n');
|
|
54
|
+
if (out.length < 2) continue;
|
|
55
|
+
|
|
56
|
+
let keys = out.shift().split('\t');
|
|
57
|
+
for (const row of out) {
|
|
58
|
+
let dev = {};
|
|
59
|
+
let elms = row.split('\t');
|
|
60
|
+
for (const i in elms) {
|
|
61
|
+
dev[keys[i]] = elms[i];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.ifaceCache.set(dev['MAC Address'], iface);
|
|
65
|
+
devs.push(dev);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// filters out devices that don't have the "IO-Controller" (0x02) bit set
|
|
70
|
+
return devs.filter(val => (parseInt(val['Device Role']) & pnDevRoleMask));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async flashLedPN(mac_addr, iface) {
|
|
74
|
+
if (!await this.isPnToolsAvailable()) return;
|
|
75
|
+
|
|
76
|
+
iface = iface || this.ifaceCache.get(mac_addr);
|
|
77
|
+
if (!iface) {
|
|
78
|
+
throw new Error(`Could not determine interface for [${mac_addr}]`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
await exec(`${pnToolsPath} flashled -i "${iface}" -t "${mac_addr}"`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
module.exports = new Tools();
|