anxiao_web/pages/cmddispatch/api/stationDeviceWebsocket.js

127 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {stationwebsocketurl} from "../../../public/js/url.js";
import { loginInfo } from "../js/index.js";
import {connectWebsocket} from "../js/index.js";
import { updatePushButtonEvent } from "../js/marker/pushButtonEventMarker.js";
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 1035://报警按钮
strangerAlarm(message);
break
case 1041://天网
console.log(message);
break;
case 1042://行为
console.log(message);
break;
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"];
if(list!=null){
for (var iot = 0; iot < list.length; iot++) {
var deviceid=list[iot]["deviceid"];
iotDeviceHash[deviceid]=list[iot];
}
}
if (pagepro == "end" || pagepro == "start-end") {
//列表推送结束
//查询物联网的报警
//显示物联网的撒点
}
}
//报警按钮
function strangerAlarm(message){
//移动设备告警也会转到微站的websocket,这里接受只取按钮的
for(var i=0;i<message.length;i++){
var alarmType=message[i]["alarmType"];
if(alarmType!="sos.mdev"){
updatePushButtonEvent(message[i]);
}
}
}
export {
stationWebsocket,
iotDeviceHash
}