tlms_esd 0.1.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/cli.js ADDED
@@ -0,0 +1,280 @@
1
+ import { exec } from 'node:child_process';
2
+ import { basename, join } from 'node:path';
3
+
4
+ function toArr(any) {
5
+ return any == null ? [] : Array.isArray(any) ? any : [any];
6
+ }
7
+
8
+ function toVal(out, key, val, opts) {
9
+ var x, old=out[key], nxt=(
10
+ !!~opts.string.indexOf(key) ? (val == null || val === true ? '' : String(val))
11
+ : typeof val === 'boolean' ? val
12
+ : !!~opts.boolean.indexOf(key) ? (val === 'false' ? false : val === 'true' || (out._.push((x = +val,x * 0 === 0) ? x : val),!!val))
13
+ : (x = +val,x * 0 === 0) ? x : val
14
+ );
15
+ out[key] = old == null ? nxt : (Array.isArray(old) ? old.concat(nxt) : [old, nxt]);
16
+ }
17
+
18
+ function mri (args, opts) {
19
+ args = args || [];
20
+ opts = opts || {};
21
+
22
+ var k, arr, arg, name, val, out={ _:[] };
23
+ var i=0, j=0, idx=0, len=args.length;
24
+
25
+ const alibi = opts.alias !== void 0;
26
+ const strict = opts.unknown !== void 0;
27
+ const defaults = opts.default !== void 0;
28
+
29
+ opts.alias = opts.alias || {};
30
+ opts.string = toArr(opts.string);
31
+ opts.boolean = toArr(opts.boolean);
32
+
33
+ if (alibi) {
34
+ for (k in opts.alias) {
35
+ arr = opts.alias[k] = toArr(opts.alias[k]);
36
+ for (i=0; i < arr.length; i++) {
37
+ (opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
38
+ }
39
+ }
40
+ }
41
+
42
+ for (i=opts.boolean.length; i-- > 0;) {
43
+ arr = opts.alias[opts.boolean[i]] || [];
44
+ for (j=arr.length; j-- > 0;) opts.boolean.push(arr[j]);
45
+ }
46
+
47
+ for (i=opts.string.length; i-- > 0;) {
48
+ arr = opts.alias[opts.string[i]] || [];
49
+ for (j=arr.length; j-- > 0;) opts.string.push(arr[j]);
50
+ }
51
+
52
+ if (defaults) {
53
+ for (k in opts.default) {
54
+ name = typeof opts.default[k];
55
+ arr = opts.alias[k] = opts.alias[k] || [];
56
+ if (opts[name] !== void 0) {
57
+ opts[name].push(k);
58
+ for (i=0; i < arr.length; i++) {
59
+ opts[name].push(arr[i]);
60
+ }
61
+ }
62
+ }
63
+ }
64
+
65
+ const keys = strict ? Object.keys(opts.alias) : [];
66
+
67
+ for (i=0; i < len; i++) {
68
+ arg = args[i];
69
+
70
+ if (arg === '--') {
71
+ out._ = out._.concat(args.slice(++i));
72
+ break;
73
+ }
74
+
75
+ for (j=0; j < arg.length; j++) {
76
+ if (arg.charCodeAt(j) !== 45) break; // "-"
77
+ }
78
+
79
+ if (j === 0) {
80
+ out._.push(arg);
81
+ } else if (arg.substring(j, j + 3) === 'no-') {
82
+ name = arg.substring(j + 3);
83
+ if (strict && !~keys.indexOf(name)) {
84
+ return opts.unknown(arg);
85
+ }
86
+ out[name] = false;
87
+ } else {
88
+ for (idx=j+1; idx < arg.length; idx++) {
89
+ if (arg.charCodeAt(idx) === 61) break; // "="
90
+ }
91
+
92
+ name = arg.substring(j, idx);
93
+ val = arg.substring(++idx) || (i+1 === len || (''+args[i+1]).charCodeAt(0) === 45 || args[++i]);
94
+ arr = (j === 2 ? [name] : name);
95
+
96
+ for (idx=0; idx < arr.length; idx++) {
97
+ name = arr[idx];
98
+ if (strict && !~keys.indexOf(name)) return opts.unknown('-'.repeat(j) + name);
99
+ toVal(out, name, (idx + 1 < arr.length) || val, opts);
100
+ }
101
+ }
102
+ }
103
+
104
+ if (defaults) {
105
+ for (k in opts.default) {
106
+ if (out[k] === void 0) {
107
+ out[k] = opts.default[k];
108
+ }
109
+ }
110
+ }
111
+
112
+ if (alibi) {
113
+ for (k in out) {
114
+ arr = opts.alias[k] || [];
115
+ while (arr.length > 0) {
116
+ out[arr.shift()] = out[k];
117
+ }
118
+ }
119
+ }
120
+
121
+ return out;
122
+ }
123
+
124
+ var name = "tlms_esd";
125
+ var version$1 = "0.1.0";
126
+ var description = "管道泄漏紧急关断系统";
127
+ var type = "module";
128
+ var main = "index.js";
129
+ var exports = {
130
+ cli: "./cli.js",
131
+ "default": "./main.js",
132
+ emu: "./emulator.js"
133
+ };
134
+ var dependencies = {
135
+ "modbus-serial": "^8.0.17"
136
+ };
137
+ var peerDependencies = {
138
+ pm2: "^5.4.1"
139
+ };
140
+ var devDependencies = {
141
+ "@rollup/plugin-commonjs": "^26.0.1",
142
+ "@rollup/plugin-json": "^6.1.0",
143
+ "@rollup/plugin-node-resolve": "^15.2.3",
144
+ eslint: "^8.57.0",
145
+ globby: "^13.2.2",
146
+ "iconv-lite": "^0.6.3",
147
+ mri: "^1.2.0",
148
+ nodemon: "^3.1.4",
149
+ nodes7: "^0.3.18",
150
+ rimraf: "^3.0.2",
151
+ rollup: "^4.18.0",
152
+ yaml: "^2.4.5"
153
+ };
154
+ var directories = {
155
+ conf: "example",
156
+ lib: "lib"
157
+ };
158
+ var scripts = {
159
+ build: "rollup -c",
160
+ dev: "node ./dev.js JSC",
161
+ watch: "nodemon ./dev.js JSC",
162
+ prepare: "pnpm build"
163
+ };
164
+ var bin = {
165
+ tlms: "./cli.js"
166
+ };
167
+ var keywords = [
168
+ "modbusTCP",
169
+ "PLC",
170
+ "S7"
171
+ ];
172
+ var author = "";
173
+ var license = "ISC";
174
+ var packageManager = "pnpm@9.4.0+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a";
175
+ var pkg = {
176
+ name: name,
177
+ version: version$1,
178
+ description: description,
179
+ type: type,
180
+ main: main,
181
+ exports: exports,
182
+ dependencies: dependencies,
183
+ peerDependencies: peerDependencies,
184
+ devDependencies: devDependencies,
185
+ directories: directories,
186
+ scripts: scripts,
187
+ bin: bin,
188
+ keywords: keywords,
189
+ author: author,
190
+ license: license,
191
+ packageManager: packageManager
192
+ };
193
+
194
+ const argv = mri(process.argv.slice(2), {
195
+ boolean: ['help', 'version'],
196
+ alias: {
197
+ H: 'help',
198
+ V: 'version',
199
+ P: 'path',
200
+ }
201
+ });
202
+ const [cmd = 'start', controller_name = 0] = argv._;
203
+ const acturator_names = argv._.slice(1);
204
+
205
+ const _path = argv.path ?? '.';
206
+ process.chdir(_path);
207
+ const work_path = process.cwd();
208
+ const module_path = import.meta.dirname;
209
+ const version = pkg.version;
210
+
211
+ function show_help() {
212
+ console.log(`usage:
213
+
214
+ tlms [subcommand] [controller_name or acturator_name_list] [options]
215
+
216
+ subcommand 子命令:
217
+ help 打印本帮助
218
+ start 以当前目录下的配置文件运行TLMS,这是默认子命令
219
+ stop 结束当前目录配置文件所在的TLMS
220
+ list 显示有多少个pm2托管的进程,包括TLMS实例
221
+ debug 以当前目录下的配置文件运行TLMS,但不压入后台,用于调试
222
+ emu 启动一个临时的管道节点仿真器用来测试
223
+ log 显示日志
224
+ flush 清空log
225
+
226
+ controller_name or acturator_name_list:
227
+ 控制器名称或序号,或者是执行器名称列表。
228
+ 默认为 "0",即在配置文件的第一个控制器。
229
+ 控制器只能有一个名称,用于 start stop 子命令后。
230
+ 执行器可以是多个名称,用空格分开,用于 emu 子命令后。
231
+
232
+ options:
233
+ --version | -V | -v 显示版本号,会忽略任何 subcommand 子命令
234
+ --help | -H 打印本帮助,会忽略任何 subcommand 子命令
235
+ --path | -P 指示配置文件所在的目录,默认为 "."
236
+
237
+ 例子:
238
+ tlms 以当前目录下的配置文件运行
239
+ tlms start ./conf 以 ./conf 目录下的配置文件运行
240
+ tlms stop 停止
241
+ tlms emu GD8 YA1 YA1 DY8 启动指定4个节点的仿真器
242
+ `);
243
+ }
244
+
245
+ if (argv.version) {
246
+ console.log(`V${version}`);
247
+ } else if (argv.help) {
248
+ show_help();
249
+ } else if (cmd === 'start') {
250
+ exec(
251
+ `pm2 start --name="tlms-${basename(work_path)}" "${join(module_path, 'main.js')}" -- ${controller_name}`,
252
+ { cwd: work_path }
253
+ );
254
+ } else if (cmd === 'stop') {
255
+ exec(
256
+ `pm2 delete "tlms-${basename(work_path)}"`,
257
+ { cwd: work_path }
258
+ );
259
+ } else if (cmd === 'list') {
260
+ exec(`pm2 list`, (error, stdout, _) => {
261
+ if (error) {
262
+ console.error(`exec error: ${error}`);
263
+ return;
264
+ }
265
+ console.log(stdout);
266
+ });
267
+ } else if (cmd === 'debug') {
268
+ process.argv[2] = controller_name;
269
+ import('./main.js');
270
+ } else if (cmd === 'emu') {
271
+ console.log(acturator_names); // @debug
272
+ process.argv = [process.argv[0], process.argv[1], ...acturator_names];
273
+ import('./emulator.js');
274
+ } else if (cmd === 'log') {
275
+ exec(`pm2 log`);
276
+ } else if (cmd === 'flush') {
277
+ exec(`pm2 flush`);
278
+ } else {
279
+ show_help();
280
+ }
package/dev.js ADDED
@@ -0,0 +1,5 @@
1
+ import { run } from './src/index.js';
2
+
3
+ process.chdir('./example');
4
+ const controller_name = process.argv[2];
5
+ run(controller_name);