添加报警
parent
e5978a0f48
commit
7e980242ca
@ -1 +1,119 @@
|
|||||||
const websocketUrl = 'ws://119.45.202.115:7181/ws';
|
const websocketUrl = 'ws://119.45.202.115:7181/ws';
|
||||||
|
const deviceDataTime = {};
|
||||||
|
const overtime = 1000 * 60 * 10;
|
||||||
|
// const overtime = 1000;
|
||||||
|
let alarmReminderSocket = null;
|
||||||
|
let overtimeTimer = null;
|
||||||
|
let overtimeCallback = null;
|
||||||
|
|
||||||
|
const resolveMessagePayload = (data) => {
|
||||||
|
if (typeof data !== 'string') {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(data, (key, value) => {
|
||||||
|
if (key === 'objid' && value !== null && value !== undefined) {
|
||||||
|
return String(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateDeviceDataTime = (payload) => {
|
||||||
|
const monitorId = payload?.deviceParam?.monitorId;
|
||||||
|
const objid = payload?.deviceParam?.objid;
|
||||||
|
|
||||||
|
if (!monitorId || objid === undefined || objid === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceDataTime[monitorId] = {
|
||||||
|
objid: String(objid),
|
||||||
|
time: Date.now()
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const startOvertimeTimer = () => {
|
||||||
|
if (overtimeTimer) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
overtimeTimer = setInterval(() => {
|
||||||
|
if (typeof overtimeCallback !== 'function') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
Object.keys(deviceDataTime).forEach((monitorId) => {
|
||||||
|
const item = deviceDataTime[monitorId];
|
||||||
|
|
||||||
|
if (!item || now - item.time <= overtime) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
overtimeCallback({
|
||||||
|
monitorId,
|
||||||
|
...item
|
||||||
|
});
|
||||||
|
delete deviceDataTime[monitorId];
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const connectAlarmReminder = (onMessage, url = websocketUrl) => {
|
||||||
|
if (typeof onMessage !== 'function') {
|
||||||
|
throw new Error('connectAlarmReminder 需要传入消息回调函数');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (alarmReminderSocket) {
|
||||||
|
alarmReminderSocket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
alarmReminderSocket = new WebSocket(url);
|
||||||
|
|
||||||
|
alarmReminderSocket.onmessage = (event) => {
|
||||||
|
const payload = resolveMessagePayload(event.data);
|
||||||
|
updateDeviceDataTime(payload);
|
||||||
|
onMessage(payload, event);
|
||||||
|
};
|
||||||
|
|
||||||
|
return alarmReminderSocket;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const onAlarmReminderOvertime = (callback) => {
|
||||||
|
if (typeof callback !== 'function') {
|
||||||
|
throw new Error('onAlarmReminderOvertime 需要传入超时回调函数');
|
||||||
|
}
|
||||||
|
|
||||||
|
overtimeCallback = callback;
|
||||||
|
startOvertimeTimer();
|
||||||
|
};
|
||||||
|
|
||||||
|
export const closeAlarmReminder = () => {
|
||||||
|
if (!alarmReminderSocket) {
|
||||||
|
if (overtimeTimer) {
|
||||||
|
clearInterval(overtimeTimer);
|
||||||
|
overtimeTimer = null;
|
||||||
|
}
|
||||||
|
overtimeCallback = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
alarmReminderSocket.close();
|
||||||
|
alarmReminderSocket = null;
|
||||||
|
|
||||||
|
if (overtimeTimer) {
|
||||||
|
clearInterval(overtimeTimer);
|
||||||
|
overtimeTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
overtimeCallback = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { websocketUrl, deviceDataTime, overtime };
|
||||||
|
|||||||
Loading…
Reference in New Issue