anxiao_web/pages/cmddispatch/api/moveDeviceWebsocket.js

321 lines
9.7 KiB
JavaScript
Raw Normal View History

2024-07-24 09:22:32 +08:00
import { movedevicewebsocketurl } from "../../../public/js/url.js";
import {connectWebsocket,stationInfoTableList} from "../js/index.js";
import { drawMoveDeviceMarker } from "../js/marker/moveDeviceMarker.js";
import {playDeviceSosAudio,stopDeviceSosAudio} from "../audio/audio.js";
import { showStationIpcRightVideo } from "../js/schoolBaseInfo.js";
import { autoOpenRightSlide } from "../js/pageEventTool.js";
import {updateMarkerLatlng,setMoveDeviceAlarmNormalIcon,deleteOneMoveDeviceMarkerHash,closemdevAlarmIndex} from "../js/marker/moveDeviceMarker.js"
import { createSchoolTree } from "../js/tree.js";
import { getAlarmListbyScopeReq } from "../js/showAlarmListTable.js";
import { showAlarmAllList } from "../js/showAlarmListTable.js";
import { addMoveDeviceAlarmMarker, delMoveDeviceAlarmMarker } from "../js/marker/moveDeviceEventMarker.js";
let mdWsFlag = 60;
let mdWsTimer = null;
let mdWebsocketConnect = null;
let deviceHash={};
function moveDeviceWebsocket(){
return new Promise((resolve, reject) => {
mdWebsocketConnect = new WebSocket(movedevicewebsocketurl);
mdWebsocketConnect.onopen = function (evt) {
mdWsFlag = 3;
if (mdWsTimer != null) {
clearInterval(mdWsTimer);
mdWsTimer = null;
mdWsFlag=60;
}
resolve({
"msg":"移动设备websocket连接成功",
"code":0,
"mdWebsocketConnect":mdWebsocketConnect
})
setInterval(()=>{
let messageObj = {
"msgcode": 99,
"msgname": "hb",
"message":"hb"
}
if(mdWebsocketConnect!=null){
mdWebsocketConnect.send(JSON.stringify(messageObj));
}
},5000)
};
mdWebsocketConnect.onclose = function (evt) {
console.log("移动设备的websocket已经断开onclose");
mdWebsocketConnect.close();
mdevWebsocketReconnection();
};
mdWebsocketConnect.onerror = function (evt) {
console.log("移动设备的websocket已经断开onerror")
mdWebsocketConnect.close();
mdevWebsocketReconnection();
};
mdWebsocketConnect.onmessage = function (evt) {
var data=JSON.parse(evt.data);
var msgcode=data["msgcode"];
var message=data["message"];
if(msgcode=="4000"){//设备心跳
websoceketDeviceBear(message);
}else if(msgcode=="4001"){//这是普通的事件
//查询设备的事件跟报警
mdevEventSos(message);
}else if(msgcode=="4002"){//设备在线,离线状态
deviceStatusUpdate(message);
}else if(msgcode=="403"){//获取移动设备的设备列表
//这里需要判断一下,如果是重连,就不做任何事情,只需要连接一下
var list=message["list"];
var pagepro=message["pagepro"];
if(list!=null){
for(var dev=0;dev<list.length;dev++){
var deviceid=list[dev]["deviceid"];
list[dev]["sosState"]="normal";
deviceHash[deviceid]=list[dev];
}
}
if(pagepro=="end"||pagepro=="start-end"){
$(".school_baoan_number").html(Object.keys(deviceHash).length)
//更新marker
drawMoveDeviceMarker();
//需要把设备合并到学校的表
mdevMergeStationData();
//查询告警记录
showAlarmAllList();
}
}
}
})
}
function mdevMergeStationData(){
for(var i=0;i<stationInfoTableList.length;i++){
for(var d in deviceHash){
var station=deviceHash[d]["station"];
if(station==stationInfoTableList[i]["name"]){
var obj={
"id":4,
"text":deviceHash[d]["showname"],
"iconCls":"icon_detachment",
...deviceHash[d]
}
if(!("mdev" in stationInfoTableList[i])){
stationInfoTableList[i]["mdev"]=[];
}
stationInfoTableList[i]["mdev"].push(obj);
}
}
}
//建立数
createSchoolTree();
}
//设备状态改变4000
function websoceketDeviceBear(message){
for(var i=0;i<message.length;i++){
var deviceid=message[i]["deviceid"];
var mapLongi=message[i]["longitude"];
var mapLati=message[i]["latitude"];
var timestamp=timeFormat(message[i]["timestamp"]);//定位时间
var address=message[i]["address"];
var arg0=message[i]["arg0"];//(如果是执法仪,表示剩余内存,如果是车的,表示另外一个意思)
var arg1=message[i]["arg1"];//信号类型
var arg2=message[i]["arg2"];
var val0=message[i]["val0"];//信号强度
var power=message[i]["power"];
var speed=message[i]["speed"];
var locate=message[i]["locate"];//定位方式
var info=message[i]["info"];//表示设备信息("str,str,str"),第一个字段表示产品ID,第二个表示串号,第三个表示卡号,第4个表示版本号
if(deviceid in deviceHash){
deviceHash[deviceid]["arg0"]=arg0;
deviceHash[deviceid]["arg1"]=arg1;
deviceHash[deviceid]["arg2"]=arg2;
deviceHash[deviceid]["power"]=power;
deviceHash[deviceid]["longitude"]=mapLongi;
deviceHash[deviceid]["latitude"]=mapLati;
deviceHash[deviceid]["speed"]=speed;
deviceHash[deviceid]["address"]=address;
deviceHash[deviceid]["timestamp"]=timestamp;
if(deviceHash[deviceid]["status"]=="offline"){
deviceHash[deviceid]["status"]="online";
}
if(locate=="amap"){//wifi
var locate="网络定位";
}else if(locate=="GPS"){//移动网络
var locate="GPS定位";
}else{
var locate="未定位";
}
deviceHash[deviceid]["locate"]=locate;
deviceHash[deviceid]["timestamp"]=timestamp;
if(arg1==1){//wifi
var img=`./pages/cmddispatch/image/wifi${val0}.png`;
}else{//移动网络
var img=`./pages/cmddispatch/image/4g${val0}.png`;
}
deviceHash[deviceid]["arg1"]=img;
if(info!=null){
if(info.length!=0){
var infoArr=info.split(",");
deviceHash[deviceid]["cardNumber"]=infoArr[2];
if(infoArr.length>3){
deviceHash[deviceid]["appVersion"]=infoArr[3];
}else{
deviceHash[deviceid]["appVersion"]="未知";
}
}else{
var infoArr=[];
deviceHash[deviceid]["cardNumber"]="";
deviceHash[deviceid]["appVersion"]="未知";
}
}else{
var infoArr=[];
deviceHash[deviceid]["cardNumber"]="";
deviceHash[deviceid]["appVersion"]="未知";
}
//判断是否越界
if(message[i]["status"].length!=0){
if(message[i]["status"].indexOf("[")!=-1){
var data=JSON.parse(message[i]["status"]);
if(message[i]["status"].indexOf("statusext")!=-1){//找到了这个字段,新的协议
for(var s=0;s<data.length;s++){
if(data[s].indexOf("statusext")!=-1){//数据结构是["求助","{"statusext":"etc"}"]
var alarmObj=JSON.parse(data[s]);
if("alarm" in alarmObj){
var alarm=alarmObj["alarm"];
switch(alarm){
case ""://正常
// clearMoveDeviceSos(deviceid)
break;
case "sos"://事件
// createMoveDeviceSos(deviceid);
break;
default://测试报需要先按照正式报一起处理
break;
}
}
}
}
}
}
}
//如果marker在地图上面显示就去更新位置
updateMarkerLatlng(deviceHash[deviceid]);
}
}
}
function createMoveDeviceSos(obj){
var deviceid=obj["deviceid"];
//显示marker为事件状态
if(deviceid in deviceHash){
if(deviceHash[deviceid]["sosState"]=="normal"){
deviceHash[deviceid]["sosState"]="alarm";
deviceHash[deviceid]["alarmid"]=obj["alarmid"]
//播放声音
playDeviceSosAudio(deviceid);
//自动打开右侧菜单
autoOpenRightSlide();
//自动去打开学校附近IPC的视频
showStationIpcRightVideo(deviceHash[deviceid]);
//添加告警marker
addMoveDeviceAlarmMarker(deviceHash[deviceid]);
}
}
}
function clearMoveDeviceSos(deviceid){
//显示marker为事件状态
if(deviceid in deviceHash){
if(deviceHash[deviceid]["sosState"]=="alarm"){
deviceHash[deviceid]["sosState"]="normal";
//播放声音
stopDeviceSosAudio(deviceid);
//改变marker的状态
setMoveDeviceAlarmNormalIcon(deviceHash[deviceid]);
closemdevAlarmIndex();
//删除marker
delMoveDeviceAlarmMarker(deviceid)
}
}
}
function mdevEventSos(message){
for(var i=0;i<message.length;i++){
var major=message[i]["major"];
if(major=="terminal.sos"){//设备告警
//重新调用接口获取sos
getAlarmListbyScopeReq();
}
}
}
//设备状态改变4002
function deviceStatusUpdate(message){
for(var i=0;i<message.length;i++){
var deviceid=message[i]["deviceid"];
var status=message[i]["status"];
if(deviceid in deviceHash){
if(deviceHash[deviceid]["status"]!=status){
//如果设备在线,去更新一下marker,如果设备离线,直接删除marker
deviceHash[deviceid]["status"]=status;
//更新marker
if(status=="offline"){
deleteOneMoveDeviceMarkerHash(deviceid)
}
}
}
}
}
//时间格式化11/23 12:12:12
function timeFormat(time){
var currentTime = new Date(time);
var year = currentTime.getFullYear();
var getMonth = (currentTime.getMonth()) + 1;
var getDate = currentTime.getDate();
var getHours = currentTime.getHours();
var getMinutes = currentTime.getMinutes();
var getSeconds = currentTime.getSeconds();
if(getMonth < 10) {
getMonth = "0" + getMonth;
}
if(getDate < 10) {
getDate = "0" + getDate;
}
if(getHours < 10) {
getHours = "0" + getHours;
}
if(getMinutes < 10) {
getMinutes = "0" + getMinutes;
}
if(getSeconds < 10) {
getSeconds = "0" + getSeconds;
}
return getMonth + "/" + getDate + " " + getHours + ":" + getMinutes + ":" + getSeconds;
}
function mdevWebsocketReconnection(){
mdWebsocketConnect = null;
mdWsTimer = null;
if (mdWsTimer == null) {
mdWsTimer = setInterval(function () {
connectWebsocket();
mdWsFlag--;
if(mdWsFlag==0){
layer.msg("网络中断,请重新连接...");
clearInterval(mdWsTimer);
mdWsTimer = null;
}
}, 5000)
}
}
export {
moveDeviceWebsocket,
deviceHash,
clearMoveDeviceSos,
createMoveDeviceSos
}