Skip to content

5.1:每小时通信自检

场景:定期检查SCADA与下层PLC的通信连接健康状态。
运算:每整点0分0秒执行,检查指定PLC的通信状态字。若为断开,则在系统日志中记录“PLC通信中断”,并触发一个本地声光报警信号。
目的:主动监控通信链路,在发生中断时能第一时间发现并记录,辅助快速定位网络问题。

javascript
const { getVariant, executeSql, exePgSqlR, exePgSqlCUD, exeSqliteSqlR, exeSqliteSqlCUD, getCurrentOrLastValue, setVariantValue, watch, watchAlarm, table, executeDataSourceSql, sendWxTextMessage, sendWxMessage, sendDingTextMessage, sendDingMessage, alarm } = require("GlobalConfig");//头文件请勿删除
const { getFormulaGroupList, getFormulaList, delFormula, getLastDownFormula, addFormula, editFormula, downloadFormula } = require("FormulaApi");//头文件请勿删除

const cron = require('node-cron');

// 获取当前时间字符串
function getCurrentDateTime() {
    const now = new Date();

    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');

    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 每个整点触发
const task = cron.schedule('0 * * * *', () => {
    if(!`plc通讯状态`.value){
        `声光报警信号`.value = 1
        // user_log表中添加记录
        table('user_log')
            .insert([{
                id: new Date().getTime(), // 获取当前时间戳当作id
                insert_time: getCurrentDateTime(),
                username: '' ,
                nickname: '' ,
                role_name: '' ,
                department_name: '' ,
                operational_source: '' ,
                log_type: '' ,
                address: '' ,
                content: 'PLC通信中断'
            }])
            .execute((result) => {
                if (result.success) {
                    console.log('插入成功')
                }
            })
    }
}, {
    scheduled: true // 不立即执行,而是等待到规定的时间再执行
});

// 启动定时任务
task.start();

5.2:每日定时设备启停

场景:实现生产车间设备的自动定时开关机。
运算:
每天 08:50,顺序启动:照明系统-> 通风系统-> 主生产线设备。
每天 21:10,顺序停止:主生产线设备-> (延时10分钟)-> 通风系统-> (延时5分钟)-> 照明系统。
目的:实现生产过程的自动化定时管理,节约人力,并保证设备启停的安全顺序。

javascript
const { getVariant, executeSql, exePgSqlR, exePgSqlCUD, exeSqliteSqlR, exeSqliteSqlCUD, getCurrentOrLastValue, setVariantValue, watch, watchAlarm, table, executeDataSourceSql, sendWxTextMessage, sendWxMessage, sendDingTextMessage, sendDingMessage, alarm } = require("GlobalConfig");//头文件请勿删除
const { getFormulaGroupList, getFormulaList, delFormula, getLastDownFormula, addFormula, editFormula, downloadFormula } = require("FormulaApi");//头文件请勿删除

const cron = require('node-cron');

// 每天08:50
const task = cron.schedule('50 8 * * *', () => {
    `照明系统`.value = 1
    `通风系统`.value = 1
    `主生产线系统`.value = 1
}, {
    scheduled: true // 不立即执行,而是等待到规定的时间再执行
});
// 每天21:10
const taskEnd = cron.schedule('10 21 * * *', () => {
    `主生产线系统`.value = 0
    // 延时10分钟,停止通风系统
    setTimeout(()=>{
        `通风系统`.value = 0
    }, 10 * 60 * 1000)
    // 延时15分钟,停止照明系统
    setTimeout(()=>{
        `照明系统`.value = 0
    }, 15 * 60 * 1000)
}, {
    scheduled: true // 不立即执行,而是等待到规定的时间再执行
});

// 启动08:50定时任务
task.start();
// 启动21:10定时任务
taskEnd.start();

SCADA帮助文档