mm_os 4.2.6 → 4.2.8
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/README.md +1 -1
- package/README_EN.md +2 -2
- package/adapter/adapter.js +105 -52
- package/adapter/custom_persistence.js +32 -31
- package/adapter/mqtt.js +59 -79
- package/adapter/socket.js +188 -58
- package/adapter/web.js +92 -68
- package/adapter/websocket.js +43 -146
- package/com/api/drive.js +732 -708
- package/com/api/index.js +196 -198
- package/com/cmd/config.tpl.json +48 -122
- package/com/cmd/drive.js +176 -1548
- package/com/cmd/index.js +287 -1066
- package/com/cmd/job/config.json +66 -0
- package/com/cmd/job/index.js +241 -0
- package/com/cmd/job/test.js +188 -0
- package/com/cmd/script.tpl.js +12 -33
- package/com/db/drive.js +2206 -2019
- package/com/db/index.js +241 -242
- package/com/event/config.tpl.json +3 -1
- package/com/event/drive.js +3 -1
- package/com/event/index.js +73 -49
- package/com/event/script.tpl.js +1 -1
- package/com/mqtt/config.tpl.json +16 -8
- package/com/mqtt/drive.js +696 -676
- package/com/mqtt/index.js +1012 -822
- package/com/mqtt/index.test.js +503 -0
- package/com/mqtt/mm_mqtt.js +446 -420
- package/com/mqtt/script.tpl.js +3 -7
- package/com/nav/drive.js +7 -7
- package/com/nav/index.js +228 -231
- package/com/param/index.js +19 -17
- package/com/pendant/index.js +467 -441
- package/com/socket/drive.js +679 -616
- package/com/socket/index.js +94 -48
- package/com/sql/drive.js +1283 -1259
- package/com/sql/index.js +152 -150
- package/com/static/drive.js +1 -1
- package/com/static/index.js +232 -226
- package/com/task/drive.js +428 -405
- package/com/task/index.js +154 -148
- package/com/template/drive.js +1 -1
- package/com/template/index.js +141 -138
- package/com.js +87 -6
- package/common/middleware/web_after/index.js +4 -10
- package/common/middleware/web_base/index.js +1 -1
- package/common/middleware/web_cors/index.js +1 -1
- package/common/middleware/web_error/index.js +131 -119
- package/common/middleware/web_error/middleware.json +2 -1
- package/common/middleware/web_logger/index.js +1 -1
- package/common/middleware/web_main/index.js +2 -0
- package/common/middleware/web_static/index.js +2 -2
- package/common/middleware/web_waf/index.js +1 -1
- package/core/app/config.tpl.json +4 -4
- package/core/app/index.js +710 -534
- package/core/middleware/index.js +1 -9
- package/core/mod/config.tpl.json +1 -1
- package/core/model/config.tpl.json +1 -1
- package/core/plugin/config.tpl.json +15 -3
- package/core/plugin/index.js +31 -0
- package/core/pusher/index.js +158 -161
- package/core/scene/loop.js +1306 -1255
- package/core/store/index.js +1794 -1755
- package/core/store/sql.js +1488 -1464
- package/core/zone/bat/index.js +744 -728
- package/core/zone/config.tpl.json +3 -3
- package/core/zone/index.js +655 -616
- package/os.js +0 -4
- package/package.json +16 -16
- package/server.js +583 -427
- package/com/cmd/nlp.js +0 -525
- package/core/app/index copy.js +0 -611
- package/software.js +0 -194
- package/tcp.js +0 -279
package/com/task/drive.js
CHANGED
|
@@ -1,405 +1,428 @@
|
|
|
1
|
-
const Item = require(
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
*
|
|
108
|
-
* @
|
|
109
|
-
*/
|
|
110
|
-
Drive.prototype.
|
|
111
|
-
|
|
112
|
-
if (
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
*
|
|
120
|
-
* @param {string} time 时间字符串
|
|
121
|
-
* @param {Function} func 回调函数
|
|
122
|
-
* @returns {Function} 时间匹配函数
|
|
123
|
-
*/
|
|
124
|
-
Drive.prototype.
|
|
125
|
-
if (time
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
var arr = time.split(
|
|
147
|
-
var fmt =
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
*
|
|
156
|
-
* @param {
|
|
157
|
-
* @param {
|
|
158
|
-
* @returns {Function}
|
|
159
|
-
*/
|
|
160
|
-
Drive.prototype.
|
|
161
|
-
var
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
*
|
|
196
|
-
* @
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
*
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
this.
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
//
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
//
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
this.
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
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;
|