anxiao_web/pages/video/api/moveDeviceWebsocket.js

212 lines
6.4 KiB
JavaScript
Raw Permalink Normal View History

2024-07-24 09:22:32 +08:00
import { movedevicewebsocketurl } from "../../../public/js/url.js";
import { updateMoveDeviceMarker,addMoveDeviceMarker,moveDeviceMarkerHash } from "../js/marker/moveDeviceMarker.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"){//这是普通的事件
//查询设备的事件跟报警
//getDevicePhotoAndSos();
}else if(msgcode=="4002"){//设备在线,离线状态
deviceStatusUpdate(message);
}else if(msgcode=="403"){//获取移动设备的设备列表
//这里需要判断一下,如果是重连,就不做任何事情,只需要连接一下
var list=message["list"];
var pagepro=message["pagepro"];
for(var dev=0;dev<list.length;dev++){
var deviceid=list[dev]["deviceid"];
deviceHash[deviceid]=list[dev];
}
if(pagepro=="end"||pagepro=="start-end"){
for(var d in deviceHash){
var status=deviceHash[d]["status"];
if(status=="online"){
addMoveDeviceMarker(deviceHash[d])
}
}
}
}
}
})
}
//设备状态改变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";
//只要设备状态发生改变,就需要重置列表
//更新marker
updateMoveDeviceMarker(deviceHash[deviceid]);
}
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"]="未知";
}
//如果marker在地图上面显示就去更新位置
if(deviceid in moveDeviceMarkerHash){
updateMoveDeviceMarker(deviceHash[deviceid]);
}
}
}
}
//设备状态改变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
updateMoveDeviceMarker(deviceHash[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}