anxiao_web/pages/intelligenceEarlyWarning/api/stationDeviceWebsocket.js

173 lines
4.5 KiB
JavaScript

import {stationwebsocketurl} from "../../../public/js/url.js";
import {signedSchoolArr,clockloginInfo,initSchoolSignedData} from "../js/index.js"
let wsFlag = 60;
let wsTimer = null;
let stationWebsocketConnect = null;
function clockStationWebsocket() {
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": clockloginInfo["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 1038://打卡记录
updateWebsockeClockData(message);
break;
default:
break;
}
}
}
})
}
// setTimeout(()=>{
// var message=[{
// brigade:"长沙芙蓉区",
// borough:"马王堆所",
// detachment: "火炬警务区",
// personId: "毛毛",
// personName: "毛毛",
// personType: "volunteer",
// signId: "毛毛_49f165ab_20236811",
// signTimestamp: 1686195503129,
// station: "实验小学",
// tagId: "04F96E125F6680",
// targetId: "49f165ab",
// taskId: "non",
// }]
// updateWebsockeClockData(message)
// },6000)
var timer=null;
function updateWebsockeClockData(message){
for(var i=0;i<message.length;i++){
var station=message[i]["station"];
var personType=message[i]["personType"];
for(var j=0;j<signedSchoolArr.length;j++){
if(station==signedSchoolArr[j]["station"]){
switch(personType){
case "policeman"://民警
signedSchoolArr[j]["police_sign"]++;
break;
case "guarder"://保安
signedSchoolArr[j]["guarder_sign"]++;
break;
case "volunteer"://志愿者
signedSchoolArr[j]["volunteer_sign"]++;
break;
default:
break;
}
signedSchoolArr[j]["sign_total"]++;
break;
}
}
}
//解决页面数据量太大,刷新不及时
// if(timer==null){
// timer=setTimeout(()=>{
// timer=null;
// //刷新页面
// initSchoolSignedData();
// },10000)
// }
//刷新页面
initSchoolSignedData();
}
function websocketReconnection(){
stationWebsocketConnect = null;
wsTimer = null;
if (wsTimer == null) {
wsTimer = setInterval(function () {
connecClockWebsocket();
wsFlag--;
if(wsFlag==0){
layer.msg("网络中断,请重新连接...");
clearInterval(wsTimer);
wsTimer = null;
}
}, 5000)
}
}
function connectClockStationWebsocket(){
//连接站点websocket
clockStationWebsocket().then((res)=>{
var code=res["code"];
if(code==0){
var stationWebsocketConnect=res["stationWebsocketConnect"];
createStationWebsocket(stationWebsocketConnect);
}
})
function createStationWebsocket(stationWebsocketConnect){
var stationTempArr=[];
signedSchoolArr.forEach((item)=>{
var name=item.name
stationTempArr.push(name);
if(stationTempArr.length>=500){
sendStationList(stationWebsocketConnect,stationTempArr);
stationTempArr=[];
}
})
sendStationList(stationWebsocketConnect,stationTempArr);
}
//发送微站的数据
function sendStationList(stationWebsocketConnect,stationTempArr){
var getIot={
"msgcode":102,
"msgname":"station-sensor-list",
"message":stationTempArr
}
var msg = JSON.stringify(getIot);
stationWebsocketConnect.send(msg);
}
}
export {
connectClockStationWebsocket
}