// 可以月份,星期筛选功能的闹钟测试,用到的类是Date,events
var events = require('events'); // 引入事件模块
var timerBatEmiter = new events.EventEmitter(); // 全局事件触发器
// 批处理定时器类定义
class timerbatClass {
constructor(jsnObjTimer) {
if (jsnObjTimer.id) this.id = new Number(jsnObjTimer.id);
if (jsnObjTimer.projeck_id != undefined) this.projeck_id = new Number(jsnObjTimer.projeck_id); // 所属哪个项目
if (jsnObjTimer.bat_id != undefined) this.bat_id = new Number(jsnObjTimer.bat_id); // 定时到了要执行的批处理
if (jsnObjTimer.hour !== undefined) this.hour = new Number(jsnObjTimer.hour); // 时定时值
if (jsnObjTimer.minute !== undefined) this.minute = new Number(jsnObjTimer.minute); // 分定时值
if (jsnObjTimer.second !== undefined) this.second = new Number(jsnObjTimer.second); // 秒定时值
if (jsnObjTimer.name != undefined) this.name = new String(jsnObjTimer.name); // 用于描述的名字
if (jsnObjTimer.run != undefined) this.run = new String(jsnObjTimer.run); // 是否启动'UNKNOW','YES','NO'
if (jsnObjTimer.once != undefined) this.once = new String(jsnObjTimer.once); // 只运行一次'UNKNOW','YES','NO'
if (jsnObjTimer.monflt != undefined) this.monflt = new String(jsnObjTimer.monflt); // 月过滤JSON 格式数组 1-12
if (jsnObjTimer.wekflt != undefined) this.wekflt = new String(jsnObjTimer.wekflt); // 周过滤JSON 格式数组 1-7
}
}
var gAppTimerBatArray = [];// 全局批处理定时器列表,用户每次更新PUT,POST,DELETE请求更新数据库后,最好一定时间定时重新更新本数组
var appStartUp5s = 0;// 计数整个应用程序启动后前10秒
// 1秒时时更新事件,驱动整个系统定时器计数
function onTimerRuning1S() {
var dateTime = new Date();
var hour = dateTime.getHours();
var minute = dateTime.getMinutes();
var second = dateTime.getSeconds();
var week = dateTime.getDay(); // 一个星期中的第几天,0是星期天
var date = dateTime.getDate(); // 一个月的几号
var month = dateTime.getMonth();// 一年中的每几月分,0是第一个月
console.log("\r\n---timerbat.js onTimerRuning1S()--%d:%d:%d--星期%d--%d号----%d月份---", hour, minute, second, week, date, month + 1);
if (Number(appStartUp5s) < 5) {
appStartUp5s = Number(appStartUp5s) + 1;
return;
}
// 第5秒初始化 gAppTimerBatArray
if (Number(appStartUp5s) == 5) {
appStartUp5s = 6;
timerBatEmiter.emit('STARTUP_5S');// 发送初始化定时器数组事件
return;
}
gAppTimerBatArray.length
// 第11秒后每次都要查找有哪些定时器发生闹钟事件
gAppTimerBatArray.forEach(jsnObjTimer => {
if (jsnObjTimer.id) {
if (jsnObjTimer.run == "YES") {
console.log("timer " + jsnObjTimer.name + " run ... ...");
if (hour == new Number(jsnObjTimer.hour) && (minute == new Number(jsnObjTimer.minute)) && second == new Number(jsnObjTimer.second)) {
// 检查有无周week筛选
var weekrepeat = false;
let jsonWekflt = JSON.parse(jsnObjTimer.wekflt);
if ((!jsonWekflt) || (jsonWekflt.length == 0)) {
weekrepeat = true;// 无week筛选认为是对的
}
else {// 有筛选,其中一项相等就行了
if (jsonWekflt.indexOf(week) > -1) {
weekrepeat = true;
}
}
// 检查有无月month筛选
var monrepeat = false;
let jsonMonflt = JSON.parse(jsnObjTimer.monflt);
if ((!jsonMonflt) || (jsonMonflt.length == 0)) {
monrepeat = true;// 无week筛选认为是对的
}
else {// 有筛选,其中一项相等就行了
if (jsonMonflt.indexOf(month) > -1) {
monrepeat = true;
}
}
if ((weekrepeat == true) && (monrepeat == true)) {
console.log('批量定时器 ' + jsnObjTimer.name + ' 发生定时事件');
let isonce = jsnObjTimer.once;
if (isonce == 'YES') {//单次运行
//单次运行,要自动关闭,修改数据库,并发出更新事件
}
}
}
}
};
});
}
setInterval(onTimerRuning1S, 1000);
// 5秒计时更新事件处理函数
timerBatEmiter.on('STARTUP_5S', () => {
let timerlist = [
{
"id": 1,
"hour": 7,
"minute": 18,
"second": 10,
"name": "批定时1",
"run": "YES",
"once": "NO",
"monflt": "[]", //说明[0,1,... ...,11] 对应第一月份到12月分
"wekflt": "[]", //说明[0,1,... ...,6] 对应星期天到星期6
},
{
"id": 2,
"hour": 7,
"minute": 18,
"second": 10,
"name": "批定时2",
"run": "YES",
"once": "NO",
"monflt": "[3]", //说明[0,1,... ...,11] 对应第一月份到12月分
"wekflt": "[6]", //说明[0,1,... ...,6] 对应星期天到星期6
},
{
"id": 3,
"hour": 6,
"minute": 5,
"second": 0,
"name": "批定时3",
"run": "NO",
"once": "NO",
"monflt": "[]", //说明[0,1,... ...,11] 对应第一月份到12月分
"wekflt": "[]", //说明[0,1,... ...,6] 对应星期天到星期6
}
];
timerlist.forEach(element => {
let newAlamer = new timerbatClass(element);
gAppTimerBatArray.push(newAlamer);
});
});