Appearance
4.1:夜间超压微信告警
场景:在夜间值班薄弱时段,压力异常时立即通知值班人员。
运算:在时间22:00-06:00内,如果压力值 > 超高报警限,则通过HTTP请求调用企业微信机器人Webhook,发送格式化的告警消息。
目的:实现关键告警的即时推送,缩短故障响应时间,保障夜间生产安全。
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 watchKeys = ["压力值"]
/**
* 监听变量的值变化事件
* @param {Array} names 变量名数组
* @param {Function} callback 回调函数
*/
watch(watchKeys, (name, value) => {
if(`压力值`.value > `超高报警限`.value && isNightTime()){
/**
* 发送文本到企业微信群
* key 为群机器人的Webhook地址中的key部分
*/
sendWxTextMessage("key", "报警监控:压力值过高请尽快处理!", (res) => {
console.log(res)
})
}
});
/**
* 判断是否在夜间
*/
function isNightTime() {
return new Date().getHours() >= 22 || new Date().getHours() < 6;
}4.2:批次完成微信通知
场景:生产批次完成时,自动向生产管理群发送通知。
运算:当批次结束信号 == 1时,触发微信推送,消息内容为:“【SCADA】批次 #{当前批次号} 已完成,产量 {实际产量} kg,合格率 {合格率}%”。
目的:实现生产进度信息的自动同步,提升管理透明度和各部门协同效率。
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 watchKeys = ["批次结束信号"]
/**
* 监听变量的值变化事件
* @param {Array} names 变量名数组
* @param {Function} callback 回调函数
*/
watch(watchKeys, (name, value) => {
if (`批次结束信号`.value == 1 ){
/**
* 发送文本到企业微信群
* key为群机器人的Webhook地址中的key部分
*/
sendWxTextMessage("key", "【SCADA】批次 #{"+`当前批次号`.value+"} 已完成,产量 {"+`实际产量`.value+"} kg,合格率 {"+ `合格率`.value +"}%", (res) => {
console.log(res)
})
}
});4.3:设备保养钉钉提醒
场景:设备运行时间达到保养阈值时,自动提醒维护人员。
运算:当设备累计运行小时数 >= 保养周期(如2000小时),则通过钉钉机器人发送消息:“保养提醒:{设备名称} 已运行 {运行小时数} 小时,请按计划进行保养!”。
目的:实现预防性维护的自动化提醒,避免设备欠保养运行,降低意外故障率。
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 watchKeys = ["设备累计运行小时数"]
/**
* 监听变量的值变化事件
* @param {Array} names 变量名数组
* @param {Function} callback 回调函数
*/
watch(watchKeys, (name, value) => {
if (`设备累计运行小时数`.value >= 2000 ){
/**
* 发送钉钉文本到钉钉群中
* access_token 群机器人webhook地址中
*/
sendDingTextMessage("access_token", "保养提醒:{"+`设备名称`.value+"} 已运行 {"+`运行小时数`.value+"} 小时,请按计划进行保养!", (res) => {
console.log(res)
})
}
});4.4:能源超标钉钉预警
场景:实时监测能耗,单小时用电量超过预算时立即预警。
运算:每小时整点计算上一小时的用电量Energy_Used,若Energy_Used > 预算阈值,则触发钉钉推送:“能源预警:{时间区间} 用电量 {Energy_Used} kWh,超过预算值 {预算阈值} kWh!”。
目的:实现能耗的精细化、实时化监控,及时预警异常,为节能管理提供抓手。
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');
// 每一个整点执行
const task = cron.schedule('0 * * * *', () => {
// 计算上一小时的用电量
calculateLastHour();
}, {
scheduled: true // 不立即执行,而是等待到规定的时间再执行
});
// 启动定时任务
task.start();
/**
* 计算上一小时的用电量
*/
function calculateLastHour(){
// 计算上一小时的用电量
const energyUsed = `Energy_Used_Total`.value - `Energy_Used_Total_Old`.value
// 更新旧的用电总量
`Energy_Used_Total_Old`.value = `Energy_Used_Total`.value
// 判断是否超过预算阈值
if(energyUsed > `预算阈值`.value){
/**
* 发送钉钉文本到钉钉群中
* access_token 群机器人webhook地址中
*/
sendDingTextMessage("access_token", "能源预警:{" + getCurrentDateTime() + "} 用电量 {"+ energyUsed +"} kWh,超过预算值 "+ `预算阈值`.value +"kWh!", (res) => {
console.log(res)
})
}
}
/**
* 获取当前时间
*/
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}`;
}