anxiao_web/pages/video/api/stationDeviceWebsocket.js

127 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-07-24 09:22:32 +08:00
import {stationwebsocketurl} from "../../../public/js/url.js";
import { loginInfo } from "../js/index.js";
import { updateIotDeviceMarker } from "../js/marker/iotDeviceMarker.js";
import { alarmEventListTable,alarmEventList } from "../js/orgApi/getAlarmEventList.js";
let wsFlag = 60;
let wsTimer = null;
let stationWebsocketConnect = null;
let iotDeviceHash = {};//保存所有的物联网设备
function stationWebsocket() {
return new Promise((resolve, reject) => {
if (stationWebsocketConnect == null) {
stationWebsocketConnect = new WebSocket(stationwebsocketurl);
stationWebsocketConnect.onopen = function (evt) {
if (wsTimer != null) {
clearInterval(wsTimer);
wsTimer = null;
wsFlag=60;
}
resolve({
"msg": "微型站websocket连接成功",
"code": 0,
"stationWebsocketConnect": stationWebsocketConnect
})
var messageObj = {
"msgcode": 100,
"msgname": "session",
"message": loginInfo["session"]
}
var msg = JSON.stringify(messageObj);
stationWebsocketConnect.send(msg);
setInterval(()=>{
let messageObj = {
"msgcode": 99,
"msgname": "hb",
"message":"hb"
}
if(stationWebsocketConnect!=null){
stationWebsocketConnect.send(JSON.stringify(messageObj));
}
},5000)
};
stationWebsocketConnect.onclose = function (evt) {
console.log("微站的websocket已经断开onclose")
stationWebsocketConnect.close();
websocketReconnection();
};
stationWebsocketConnect.onerror = function (evt) {
console.log("微站的websocket已经断开onerror")
stationWebsocketConnect.close();
websocketReconnection();
}
stationWebsocketConnect.onmessage = function (evt) {
var data = JSON.parse(evt.data);
var msgcode = data["msgcode"];
var message = data["message"];
switch (msgcode) {
case 1020://18.接受别处登录的消息
//kickout(message);
break;
case 103://获取传感器的数据
wsStationSensorList(message);
break;
case 1028://事件按钮(平安屋)
//pushbuttonWebsocket(message);
break;
case 1035://陌生人报警
strangerAlarm(message);
default:
break;
}
}
}
})
}
function websocketReconnection(){
stationWebsocketConnect = null;
wsTimer = null;
if (wsTimer == null) {
wsTimer = setInterval(function () {
connectWebsocket();
wsFlag--;
if(wsFlag==0){
layer.msg("网络中断,请重新连接...");
clearInterval(wsTimer);
wsTimer = null;
}
}, 5000)
}
}
//获取传感器的列表
function wsStationSensorList(message) {
var list = message["list"];
var pagepro = message["pagepro"];
for (var iot = 0; iot < list.length; iot++) {
var deviceid=list[iot]["deviceid"];
iotDeviceHash[deviceid]=list[iot];
}
if (pagepro == "end" || pagepro == "start-end") {
//列表推送结束
//查询物联网的报警
//显示物联网的撒点
updateIotDeviceMarker();
}
}
function strangerAlarm(message){
for(var i=0;i<message.length;i++){
alarmEventList.unshift(message[i]);
}
alarmEventListTable();
}
export {
stationWebsocket,
iotDeviceHash
}