mm_os 4.2.4 → 4.2.7

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.
Files changed (77) hide show
  1. package/README.md +1 -1
  2. package/README_EN.md +2 -2
  3. package/adapter/adapter.js +105 -52
  4. package/adapter/custom_persistence.js +32 -31
  5. package/adapter/mqtt.js +59 -79
  6. package/adapter/socket.js +188 -58
  7. package/adapter/web.js +92 -68
  8. package/adapter/websocket.js +43 -146
  9. package/com/api/drive.js +732 -708
  10. package/com/api/index.js +196 -198
  11. package/com/api/script.tpl.js +24 -24
  12. package/com/cmd/config.tpl.json +48 -122
  13. package/com/cmd/drive.js +176 -1548
  14. package/com/cmd/index.js +287 -1066
  15. package/com/cmd/job/config.json +66 -0
  16. package/com/cmd/job/index.js +241 -0
  17. package/com/cmd/job/test.js +188 -0
  18. package/com/cmd/script.tpl.js +12 -33
  19. package/com/db/drive.js +2206 -2019
  20. package/com/db/index.js +241 -242
  21. package/com/event/config.tpl.json +3 -1
  22. package/com/event/drive.js +3 -1
  23. package/com/event/index.js +73 -46
  24. package/com/event/script.tpl.js +22 -22
  25. package/com/mqtt/config.tpl.json +16 -8
  26. package/com/mqtt/drive.js +696 -676
  27. package/com/mqtt/index.js +1012 -822
  28. package/com/mqtt/index.test.js +503 -0
  29. package/com/mqtt/mm_mqtt.js +446 -420
  30. package/com/mqtt/script.tpl.js +3 -7
  31. package/com/nav/drive.js +7 -7
  32. package/com/nav/index.js +228 -231
  33. package/com/param/index.js +19 -17
  34. package/com/pendant/index.js +467 -441
  35. package/com/socket/drive.js +679 -651
  36. package/com/socket/index.js +94 -48
  37. package/com/sql/drive.js +1283 -1259
  38. package/com/sql/index.js +152 -150
  39. package/com/static/drive.js +1 -1
  40. package/com/static/index.js +232 -226
  41. package/com/static/script.tpl.js +25 -25
  42. package/com/task/drive.js +428 -405
  43. package/com/task/index.js +154 -148
  44. package/com/task/script.tpl.js +37 -34
  45. package/com/template/drive.js +1 -1
  46. package/com/template/index.js +141 -138
  47. package/com.js +87 -6
  48. package/common/middleware/web_after/index.js +5 -11
  49. package/common/middleware/web_base/index.js +1 -1
  50. package/common/middleware/web_cors/index.js +1 -1
  51. package/common/middleware/web_error/index.js +131 -119
  52. package/common/middleware/web_error/middleware.json +2 -1
  53. package/common/middleware/web_logger/index.js +1 -1
  54. package/common/middleware/web_main/index.js +2 -0
  55. package/common/middleware/web_static/index.js +2 -2
  56. package/common/middleware/web_waf/index.js +1 -1
  57. package/core/app/config.tpl.json +4 -4
  58. package/core/app/index.js +710 -534
  59. package/core/middleware/index.js +1 -9
  60. package/core/mod/config.tpl.json +1 -1
  61. package/core/model/config.tpl.json +1 -1
  62. package/core/plugin/config.tpl.json +15 -3
  63. package/core/plugin/index.js +31 -0
  64. package/core/pusher/index.js +158 -161
  65. package/core/scene/loop.js +1306 -1255
  66. package/core/store/index.js +1794 -1755
  67. package/core/store/sql.js +1488 -1464
  68. package/core/zone/bat/index.js +744 -728
  69. package/core/zone/config.tpl.json +3 -3
  70. package/core/zone/index.js +655 -616
  71. package/os.js +0 -4
  72. package/package.json +16 -16
  73. package/server.js +583 -427
  74. package/com/cmd/nlp.js +0 -525
  75. package/core/app/index copy.js +0 -611
  76. package/software.js +0 -194
  77. package/tcp.js +0 -279
package/com/task/drive.js CHANGED
@@ -1,405 +1,428 @@
1
- const Item = require('mm_machine').Drive;
2
-
3
- /**
4
- * 延迟
5
- * @param {number} milli_seconds 毫秒
6
- */
7
- function sleep(milli_seconds) {
8
- var end_time = new Date().getTime() + milli_seconds;
9
- while (new Date().getTime() < end_time) { }
10
- }
11
-
12
- /**
13
- * 计算时间差
14
- * @param {string} start_time 开始时间
15
- * @param {string} end_time 结束时间
16
- * @returns {number} 返回间隔时长
17
- */
18
- function span(start_time, end_time) {
19
- var stime = Date.parse(start_time);
20
- var etime = Date.parse(end_time);
21
- return etime - stime;
22
- };
23
-
24
- /**
25
- * 转为时间
26
- * @param {string} time_str 时间字符串
27
- * @returns {Date} 时间类型
28
- */
29
- function toTime(time_str) {
30
- var str = time_str.replace('T', ' ').replace('Z', '').replaceAll('.', '/').replaceAll('-', '/');
31
- return new Date(str);
32
- };
33
-
34
- /**
35
- * 定时器类函数
36
- */
37
- class Drive extends Item {
38
- static config = {
39
- // 应用名称 例如:demo_app
40
- 'app': 'server',
41
- // 插件名称 例如:demo_plugin
42
- 'plugin': 'sys',
43
- // 执行次数
44
- 'num': 10,
45
- // 时间间隔(毫秒ms)
46
- 'interval': 1000,
47
- // 等待时长(毫秒ms)
48
- 'wait': 0,
49
- // 执行时间
50
- 'time': '',
51
- // 执行开始日期
52
- 'date_start': '',
53
- // 执行结束日期
54
- 'date_end': ''
55
- };
56
-
57
- /**
58
- * 构造函数
59
- * @param {object} config 配置参数
60
- * @param {object} parent 父对象
61
- * @class
62
- */
63
- constructor(config, parent) {
64
- super({ ...Drive.config, ...config }, parent);
65
-
66
- // 更新配置并重载脚本
67
- this.mode = 3;
68
- }
69
- }
70
-
71
- /**
72
- * 预设
73
- */
74
- Drive.prototype._preset = function () {
75
- /// 状态
76
- this.state = 'start';
77
- /// 等候器
78
- this.timeout;
79
- /// 定时执行器
80
- this.interval;
81
- /// 当前执行次数
82
- this.num = 1;
83
- };
84
-
85
- /**
86
- * 设置等待执行
87
- * @param {Function} func 回调函数
88
- */
89
- Drive.prototype.setTimeout = function (func) {
90
- if (func) {
91
- this.timeout = setTimeout(func, this.config.wait);
92
- }
93
- };
94
-
95
- /**
96
- * 设置间隔执行
97
- * @param {Function} func 回调函数
98
- */
99
- Drive.prototype.setInterval = async function (func) {
100
- sleep(this.config.wait);
101
- this.interval = setInterval(func, this.config.interval);
102
- };
103
-
104
- /**
105
- * 创建时间匹配函数
106
- * @param {string} time 时间字符串
107
- * @param {Function} func 回调函数
108
- * @returns {Function} 时间匹配函数
109
- */
110
- Drive.prototype._createTimeFn = function (time, func) {
111
- if (!time) return func;
112
- if (time.indexOf(' ') !== -1) {
113
- return this._createDateTimeFn(time, func);
114
- }
115
- return this._createTimeOnlyFn(time, func);
116
- };
117
-
118
- /**
119
- * 创建日期时间匹配函数
120
- * @param {string} time 时间字符串
121
- * @param {Function} func 回调函数
122
- * @returns {Function} 时间匹配函数
123
- */
124
- Drive.prototype._createDateTimeFn = function (time, func) {
125
- if (time.indexOf('-') !== -1) {
126
- var arr = time.split('-');
127
- var fmt = arr.length > 2 ? 'yyyy-MM-dd hh:mm:ss' : 'MM-dd hh:mm:ss';
128
- return function () {
129
- if (new Date().toStr(fmt) === time) func();
130
- };
131
- }
132
- var arr = time.split(':');
133
- var fmt = arr.length == 3 ? 'dd hh:mm:ss' : (arr.length == 2 ? 'dd hh:mm' : 'dd hh');
134
- return function () {
135
- if (new Date().toStr(fmt) === time) func();
136
- };
137
- };
138
-
139
- /**
140
- * 创建仅时间匹配函数
141
- * @param {string} time 时间字符串
142
- * @param {Function} func 回调函数
143
- * @returns {Function} 时间匹配函数
144
- */
145
- Drive.prototype._createTimeOnlyFn = function (time, func) {
146
- var arr = time.split(':');
147
- var fmt = arr.length == 3 ? 'hh:mm:ss' : (arr.length == 2 ? 'hh:mm' : 'mm');
148
- return function () {
149
- if (new Date().toStr(fmt) === time) func();
150
- };
151
- };
152
-
153
- /**
154
- * 创建带开始时间的执行函数
155
- * @param {Function} fn 原始函数
156
- * @param {number} start 开始时间
157
- * @param {number} end 结束时间
158
- * @returns {Function} 执行函数
159
- */
160
- Drive.prototype._createStartFn = function (fn, start, end) {
161
- var _this = this;
162
- if (end) {
163
- return function () {
164
- var now = new Date();
165
- if (span(now, end) >= 0 && span(now, start) <= 0) fn();
166
- else if (span(now, end) < 0) {
167
- _this.clearSub();
168
- _this.notify(_this.config.name, 'time_end');
169
- }
170
- };
171
- }
172
- return function () {
173
- if (span(new Date(), start) <= 0) fn();
174
- };
175
- };
176
-
177
- /**
178
- * 创建带结束时间的执行函数
179
- * @param {Function} fn 原始函数
180
- * @param {number} end 结束时间
181
- * @returns {Function} 执行函数
182
- */
183
- Drive.prototype._createEndFn = function (fn, end) {
184
- var _this = this;
185
- return function () {
186
- if (span(new Date(), end) >= 0) fn();
187
- else {
188
- _this.clearSub();
189
- _this.notify(_this.config.name, 'time_end');
190
- }
191
- };
192
- };
193
-
194
- /**
195
- * 设置时期执行
196
- * @param {Function} func 回调函数
197
- * @returns {object} 执行结果
198
- */
199
- Drive.prototype.setPeriod = function (func) {
200
- var cg = this.config;
201
- var fn = this._createTimeFn(cg.time, func);
202
- if (cg.date_start) {
203
- return this._createStartFn(fn, toTime(cg.date_start), cg.date_end ? toTime(cg.date_end) : null);
204
- }
205
- if (cg.date_end) {
206
- return this._createEndFn(fn, toTime(cg.date_end));
207
- }
208
- return fn;
209
- };
210
-
211
- /**
212
- * 设置按次数执行
213
- * @param {Function} func 回调函数
214
- * @returns {object} 执行结果
215
- */
216
- Drive.prototype.setNum = function (func) {
217
- var _this = this;
218
- return function () {
219
- if (_this.state === 'start') {
220
- try {
221
- if (_this.config.num < 1) {
222
- func();
223
- } else if (_this.num < _this.config.num) {
224
- func();
225
- _this.num += 1;
226
- } else {
227
- func();
228
- _this.clear();
229
- _this.notify(_this.config.name, 'completed');
230
- }
231
- } catch (err) {
232
- _this.log('error', '定时任务执行失败!', _this.config.name, err);
233
- }
234
- }
235
- };
236
- };
237
-
238
- /**
239
- * 结束定时器
240
- */
241
- Drive.prototype.end = function () {
242
- // 当前状态为结束
243
- this.state = 'end';
244
- this.notify(this.config.name, 'suspend');
245
- this.clear();
246
- };
247
-
248
- /**
249
- * 清除定时器(子函数)
250
- */
251
- Drive.prototype.clearSub = function () {
252
- if (this.interval) {
253
- clearInterval(this.interval);
254
- }
255
- if (this.timeout) {
256
- clearTimeout(this.timeout);
257
- }
258
- };
259
-
260
- /**
261
- * 清除定时器
262
- * @param {number} millisecond 时间间隔, 单位: 毫秒
263
- */
264
- Drive.prototype.clear = function (millisecond) {
265
- if (millisecond) {
266
- var _this = this;
267
- setTimeout(() => {
268
- _this.clearSub();
269
- }, millisecond);
270
- } else {
271
- this.clearSub();
272
- }
273
- };
274
-
275
- /**
276
- * 执行线程
277
- * @returns {object} 当前类
278
- */
279
- Drive.prototype.main = async function () {
280
- this.log('debug', '定时任务(默认), 执行中...');
281
- };
282
-
283
- /**
284
- * 执行线程
285
- * @param {Function} func 回调函数
286
- * @returns {object} 当前类
287
- */
288
- Drive.prototype.run = async function (func) {
289
- this.init();
290
- if (func) {
291
- await this.setInterval(this.setPeriod(this.setNum(func)));
292
- } else {
293
- await this.setInterval(this.setPeriod(this.setNum(() => {
294
- this.main();
295
- })));
296
- }
297
- return this;
298
- };
299
-
300
- /**
301
- * 初始化定时器
302
- */
303
- Drive.prototype.init = function () {
304
- // 当前执行次数
305
- this.num = 1;
306
- // 当前状态为开启
307
- this.state = 'start';
308
- this.notify(this.config.name, 'init');
309
- };
310
-
311
- /**
312
- * 启动定时器
313
- */
314
- Drive.prototype.start = function () {
315
- // 当前状态为开启
316
- this.state = 'start';
317
- this.notify(this.config.name, 'start');
318
- };
319
-
320
- /**
321
- * 暂停定时器
322
- */
323
- Drive.prototype.stop = function () {
324
- // 当前状态为开启
325
- this.state = 'stop';
326
- this.notify(this.config.name, 'stop');
327
- };
328
-
329
- /**
330
- * 通知函数, 当定时器执行完最后一次时, 会调用通知函数
331
- * @param {string} name 名称
332
- * @param {string} state 状态
333
- */
334
- Drive.prototype.notify = function (name, state) {
335
- // switch (state) {
336
- // case "init":
337
- // this.log('debug', '初始化');
338
- // break;
339
- // case "start":
340
- // this.log('debug', '开始执行');
341
- // break;
342
- // case "stop":
343
- // this.log('debug', '已暂停');
344
- // break;
345
- // case "suspend":
346
- // // 主动中断
347
- // this.log('debug', '已中断');
348
- // break;
349
- // case "time_end":
350
- // this.log('debug', '已到期');
351
- // break;
352
- // case "completed":
353
- // this.log('debug', '已完成');
354
- // break;
355
- // default:
356
- // break;
357
- // }
358
- };
359
-
360
- /**
361
- * 销毁资源
362
- */
363
- Drive.prototype.dispose = function () {
364
- this.clearSub();
365
- this.interval = undefined;
366
- this.timeout = undefined;
367
- };
368
-
369
- /**
370
- * 获取插件
371
- * @param {string} app 应用名称
372
- * @param {string} name 插件名称
373
- * @returns {object} 返回获取到的插件
374
- */
375
- Drive.prototype.plugin = function (app, name) {
376
- var l = $.slash;
377
- var app_name = app || this.config_file.between('app' + l, l);
378
- var plugin_name = name || this.config_file.between('plugin' + l, l);
379
- var plus;
380
- var plugins = $.pool.plugin[app_name];
381
- if (plugins) {
382
- plus = plugins.get(plugin_name);
383
- }
384
- return plus;
385
- };
386
-
387
- /**
388
- * 获取模型
389
- * @param {string} type 模型类型
390
- * @returns {object} 返回获取到的模型
391
- */
392
- Drive.prototype.getModel = function (type) {
393
- let model = { ...this.config };
394
- let dir = this.getDir();
395
- let l = $.slash;
396
- let app_name = dir.between('app' + l, l);
397
- let plugin_name = dir.between('plugin' + l, l);
398
- let name = dir.basename();
399
- model.app = app_name;
400
- model.plugin = plugin_name;
401
- model.name = model.name || app_name + '_' + name;
402
- return model;
403
- };
404
-
405
- module.exports = Drive;
1
+ const Item = require("mm_machine").Drive;
2
+
3
+ /**
4
+ * 延迟
5
+ * @param {number} milli_seconds 毫秒
6
+ */
7
+ function sleep(milli_seconds) {
8
+ var end_time = new Date().getTime() + milli_seconds;
9
+ while (new Date().getTime() < end_time) {}
10
+ }
11
+
12
+ /**
13
+ * 计算时间差
14
+ * @param {string} start_time 开始时间
15
+ * @param {string} end_time 结束时间
16
+ * @returns {number} 返回间隔时长
17
+ */
18
+ function span(start_time, end_time) {
19
+ var stime = Date.parse(start_time);
20
+ var etime = Date.parse(end_time);
21
+ return etime - stime;
22
+ }
23
+
24
+ /**
25
+ * 转为时间
26
+ * @param {string} time_str 时间字符串
27
+ * @returns {Date} 时间类型
28
+ */
29
+ function toTime(time_str) {
30
+ var str = time_str
31
+ .replace("T", " ")
32
+ .replace("Z", "")
33
+ .replaceAll(".", "/")
34
+ .replaceAll("-", "/");
35
+ return new Date(str);
36
+ }
37
+
38
+ /**
39
+ * 定时器类函数
40
+ */
41
+ class Drive extends Item {
42
+ static config = {
43
+ // 任务名称,用于动态增删改查
44
+ name: "",
45
+ // 任务标题
46
+ title: "",
47
+ // 任务描述
48
+ description: "",
49
+ // 状态 0未启用,1启用
50
+ state: 1,
51
+ // 执行次数
52
+ num: 10,
53
+ // 时间间隔(毫秒ms)
54
+ interval: 1000,
55
+ // 等待时长(毫秒ms)
56
+ wait: 0,
57
+ // 执行时间
58
+ time: "",
59
+ // 执行开始日期
60
+ date_start: "",
61
+ // 执行结束日期
62
+ date_end: "",
63
+ };
64
+
65
+ /**
66
+ * 构造函数
67
+ * @param {object} config 配置参数
68
+ * @param {object} parent 父对象
69
+ * @class
70
+ */
71
+ constructor(config, parent) {
72
+ super({ ...Drive.config, ...config }, parent);
73
+
74
+ // 更新配置并重载脚本
75
+ this.mode = 3;
76
+ }
77
+ }
78
+
79
+ /**
80
+ * 预设
81
+ */
82
+ Drive.prototype._preset = function () {
83
+ /// 状态
84
+ this.state = "start";
85
+ /// 等候器
86
+ this.timeout;
87
+ /// 定时执行器
88
+ this.interval;
89
+ /// 当前执行次数
90
+ this.num = 1;
91
+ };
92
+
93
+ /**
94
+ * 设置等待执行
95
+ * @param {Function} func 回调函数
96
+ */
97
+ Drive.prototype.setTimeout = function (func) {
98
+ if (func) {
99
+ if (this.timeout) {
100
+ clearTimeout(this.timeout);
101
+ }
102
+ this.timeout = setTimeout(func, this.config.wait);
103
+ }
104
+ };
105
+
106
+ /**
107
+ * 设置间隔执行
108
+ * @param {Function} func 回调函数
109
+ */
110
+ Drive.prototype.setInterval = async function (func) {
111
+ await sleep(this.config.wait);
112
+ if (this.interval) {
113
+ clearInterval(this.interval);
114
+ }
115
+ this.interval = setInterval(func, this.config.interval);
116
+ };
117
+
118
+ /**
119
+ * 创建时间匹配函数
120
+ * @param {string} time 时间字符串
121
+ * @param {Function} func 回调函数
122
+ * @returns {Function} 时间匹配函数
123
+ */
124
+ Drive.prototype._createTimeFn = function (time, func) {
125
+ if (!time) return func;
126
+ if (time.indexOf(" ") !== -1) {
127
+ return this._createDateTimeFn(time, func);
128
+ }
129
+ return this._createTimeOnlyFn(time, func);
130
+ };
131
+
132
+ /**
133
+ * 创建日期时间匹配函数
134
+ * @param {string} time 时间字符串
135
+ * @param {Function} func 回调函数
136
+ * @returns {Function} 时间匹配函数
137
+ */
138
+ Drive.prototype._createDateTimeFn = function (time, func) {
139
+ if (time.indexOf("-") !== -1) {
140
+ var arr = time.split("-");
141
+ var fmt = arr.length > 2 ? "yyyy-MM-dd hh:mm:ss" : "MM-dd hh:mm:ss";
142
+ return function () {
143
+ if (new Date().toStr(fmt) === time) func();
144
+ };
145
+ }
146
+ var arr = time.split(":");
147
+ var fmt =
148
+ arr.length == 3 ? "dd hh:mm:ss" : arr.length == 2 ? "dd hh:mm" : "dd hh";
149
+ return function () {
150
+ if (new Date().toStr(fmt) === time) func();
151
+ };
152
+ };
153
+
154
+ /**
155
+ * 创建仅时间匹配函数
156
+ * @param {string} time 时间字符串
157
+ * @param {Function} func 回调函数
158
+ * @returns {Function} 时间匹配函数
159
+ */
160
+ Drive.prototype._createTimeOnlyFn = function (time, func) {
161
+ var arr = time.split(":");
162
+ var fmt = arr.length == 3 ? "hh:mm:ss" : arr.length == 2 ? "hh:mm" : "mm";
163
+ return function () {
164
+ if (new Date().toStr(fmt) === time) func();
165
+ };
166
+ };
167
+
168
+ /**
169
+ * 创建带开始时间的执行函数
170
+ * @param {Function} fn 原始函数
171
+ * @param {number} start 开始时间
172
+ * @param {number} end 结束时间
173
+ * @returns {Function} 执行函数
174
+ */
175
+ Drive.prototype._createStartFn = function (fn, start, end) {
176
+ var _this = this;
177
+ if (end) {
178
+ return function () {
179
+ var now = new Date();
180
+ if (span(now, end) >= 0 && span(now, start) <= 0) fn();
181
+ else if (span(now, end) < 0) {
182
+ _this.clearSub();
183
+ _this.notify(_this.config.name, "time_end");
184
+ }
185
+ };
186
+ }
187
+ return function () {
188
+ if (span(new Date(), start) <= 0) fn();
189
+ };
190
+ };
191
+
192
+ /**
193
+ * 创建带结束时间的执行函数
194
+ * @param {Function} fn 原始函数
195
+ * @param {number} end 结束时间
196
+ * @returns {Function} 执行函数
197
+ */
198
+ Drive.prototype._createEndFn = function (fn, end) {
199
+ var _this = this;
200
+ return function () {
201
+ if (span(new Date(), end) >= 0) fn();
202
+ else {
203
+ _this.clearSub();
204
+ _this.notify(_this.config.name, "time_end");
205
+ }
206
+ };
207
+ };
208
+
209
+ /**
210
+ * 设置时期执行
211
+ * @param {Function} func 回调函数
212
+ * @returns {object} 执行结果
213
+ */
214
+ Drive.prototype.setPeriod = function (func) {
215
+ var cg = this.config;
216
+ var fn = this._createTimeFn(cg.time, func);
217
+ if (cg.date_start) {
218
+ return this._createStartFn(
219
+ fn,
220
+ toTime(cg.date_start),
221
+ cg.date_end ? toTime(cg.date_end) : null,
222
+ );
223
+ }
224
+ if (cg.date_end) {
225
+ return this._createEndFn(fn, toTime(cg.date_end));
226
+ }
227
+ return fn;
228
+ };
229
+
230
+ /**
231
+ * 设置按次数执行
232
+ * @param {Function} func 回调函数
233
+ * @returns {object} 执行结果
234
+ */
235
+ Drive.prototype.setNum = function (func) {
236
+ var _this = this;
237
+ return function () {
238
+ if (_this.state === "start") {
239
+ try {
240
+ if (_this.config.num < 1) {
241
+ func();
242
+ } else if (_this.num < _this.config.num) {
243
+ func();
244
+ _this.num += 1;
245
+ } else {
246
+ func();
247
+ _this.clear();
248
+ _this.notify(_this.config.name, "completed");
249
+ }
250
+ } catch (err) {
251
+ _this.log("error", "定时任务执行失败!", _this.config.name, err);
252
+ }
253
+ }
254
+ };
255
+ };
256
+
257
+ /**
258
+ * 结束定时器
259
+ */
260
+ Drive.prototype.end = function () {
261
+ // 当前状态为结束
262
+ this.state = "end";
263
+ this.notify(this.config.name, "suspend");
264
+ this.clear();
265
+ };
266
+
267
+ /**
268
+ * 清除定时器(子函数)
269
+ */
270
+ Drive.prototype.clearSub = function () {
271
+ if (this.interval) {
272
+ clearInterval(this.interval);
273
+ }
274
+ if (this.timeout) {
275
+ clearTimeout(this.timeout);
276
+ }
277
+ };
278
+
279
+ /**
280
+ * 清除定时器
281
+ * @param {number} millisecond 时间间隔, 单位: 毫秒
282
+ */
283
+ Drive.prototype.clear = function (millisecond) {
284
+ if (millisecond) {
285
+ var _this = this;
286
+ setTimeout(() => {
287
+ _this.clearSub();
288
+ }, millisecond);
289
+ } else {
290
+ this.clearSub();
291
+ }
292
+ };
293
+
294
+ /**
295
+ * 执行线程
296
+ * @returns {object} 当前类
297
+ */
298
+ Drive.prototype.main = async function () {
299
+ this.log("debug", "定时任务(默认), 执行中...");
300
+ };
301
+
302
+ /**
303
+ * 执行线程
304
+ * @param {Function} func 回调函数
305
+ * @returns {object} 当前类
306
+ */
307
+ Drive.prototype.run = async function (func) {
308
+ this.init();
309
+ if (func) {
310
+ await this.setInterval(this.setPeriod(this.setNum(func)));
311
+ } else {
312
+ await this.setInterval(
313
+ this.setPeriod(
314
+ this.setNum(() => {
315
+ this.main();
316
+ }),
317
+ ),
318
+ );
319
+ }
320
+ return this;
321
+ };
322
+
323
+ /**
324
+ * 初始化定时器
325
+ */
326
+ Drive.prototype.init = function () {
327
+ // 当前执行次数
328
+ this.num = 1;
329
+ // 当前状态为开启
330
+ this.state = "start";
331
+ this.notify(this.config.name, "init");
332
+ };
333
+
334
+ /**
335
+ * 启动定时器
336
+ */
337
+ Drive.prototype.start = function () {
338
+ // 当前状态为开启
339
+ this.state = "start";
340
+ this.notify(this.config.name, "start");
341
+ };
342
+
343
+ /**
344
+ * 暂停定时器
345
+ */
346
+ Drive.prototype.stop = function () {
347
+ // 当前状态为开启
348
+ this.state = "stop";
349
+ this.notify(this.config.name, "stop");
350
+ };
351
+
352
+ /**
353
+ * 通知函数, 当定时器执行完最后一次时, 会调用通知函数
354
+ * @param {string} name 名称
355
+ * @param {string} state 状态
356
+ */
357
+ Drive.prototype.notify = function (name, state) {
358
+ // switch (state) {
359
+ // case "init":
360
+ // this.log('debug', '初始化');
361
+ // break;
362
+ // case "start":
363
+ // this.log('debug', '开始执行');
364
+ // break;
365
+ // case "stop":
366
+ // this.log('debug', '已暂停');
367
+ // break;
368
+ // case "suspend":
369
+ // // 主动中断
370
+ // this.log('debug', '已中断');
371
+ // break;
372
+ // case "time_end":
373
+ // this.log('debug', '已到期');
374
+ // break;
375
+ // case "completed":
376
+ // this.log('debug', '已完成');
377
+ // break;
378
+ // default:
379
+ // break;
380
+ // }
381
+ };
382
+
383
+ /**
384
+ * 销毁资源
385
+ */
386
+ Drive.prototype.dispose = function () {
387
+ this.clearSub();
388
+ this.interval = undefined;
389
+ this.timeout = undefined;
390
+ };
391
+
392
+ /**
393
+ * 获取插件
394
+ * @param {string} app 应用名称
395
+ * @param {string} name 插件名称
396
+ * @returns {object} 返回获取到的插件
397
+ */
398
+ Drive.prototype.plugin = function (app, name) {
399
+ var l = $.slash;
400
+ var app_name = app || this.config_file.between("app" + l, l);
401
+ var plugin_name = name || this.config_file.between("plugin" + l, l);
402
+ var plus;
403
+ var plugins = $.pool.plugin[app_name];
404
+ if (plugins) {
405
+ plus = plugins.get(plugin_name);
406
+ }
407
+ return plus;
408
+ };
409
+
410
+ /**
411
+ * 获取模型
412
+ * @param {string} type 模型类型
413
+ * @returns {object} 返回获取到的模型
414
+ */
415
+ Drive.prototype.getModel = function (type) {
416
+ let model = { ...this.config };
417
+ let dir = this.getDir();
418
+ let l = $.slash;
419
+ let app_name = dir.between("app" + l, l);
420
+ let plugin_name = dir.between("plugin" + l, l);
421
+ let name = dir.basename();
422
+ model.app = app_name;
423
+ model.plugin = plugin_name;
424
+ model.name = model.name || app_name + "_" + name;
425
+ return model;
426
+ };
427
+
428
+ module.exports = Drive;