anxiao_web/pages/index/js/index.js

234 lines
6.5 KiB
JavaScript

import { getBrigadeGrade,showBrigadeGrade} from "./orgApi/getBrigadeGrade.js";
import {getBououghGrade,showBoroughGrade} from "./orgApi/getBoroughGrade.js"
import {getStationGrade,showStationGrade} from "./orgApi/getStationGrade.js"
import { getStationsData,showBrigadeStationsData,stationBelongBorough } from "./orgApi/getStationsData.js";
import { initCarschool,updateBackground} from "./carousel.js";
import { setTokenActive } from "./session.js";
import { login } from "./login.js";
import {getstationcount,stationInfoPageReq} from "./orgApi/getStationInfo.js"
import { timestampFormat,getCityAndWeather,getWeekDate } from "../../../public/js/tool.js";
import { stationWebsocket } from "../api/stationDeviceWebsocket.js";
import { moveDeviceWebsocket } from "../api/moveDeviceWebsocket.js";
import { showAlarmEventList } from "./orgApi/getAlarmEventList.js";
var layuiObj={};
var loginInfo=null;//登录信息
var brigadeGradeArr=[];//支队评分
var boroughGradeArr=[];//派出所评分
var stationGradeArr=[];//学校评分
var stationsInfoArr=[];//学校今天信息
var stationInfoTableList=[];//学校基本信息
layui.use(['carousel','table'],function() {
if(loginInfo==null){
layuiObj["layer"] = layui.layer;
layuiObj["carousel"] = layui.carousel;
layuiObj["layuitable"] = layui.table;
layuiObj["rate"] = layui.rate;
var height=parseFloat($("#carousel").css("height"));
//建造实例化
layuiObj["ins"] = layuiObj["carousel"].render({
elem: '#carousel',
interval:5000,
indicator:'none',
//autoplay:false,
height:height,
anim:'fade',
width: '100%', //设置背景容器的宽度
arrow: 'none' //始终显示箭头,不会消失
});
//触发轮播切换事件
layuiObj["carousel"].on('change(myCarousel)', function(obj){ //test1来源于对应HTML容器的 lay-filter="test1" 属性值
var index=obj["index"];
updateBackground(index);
});
//先登录成功
login().then(res=>{
loginInfo=res.data.data;
console.log(loginInfo);
axios.defaults.headers.common['Authorization'] = loginInfo["session"];
setTokenActive();
//这里需要调用session的饱和接口
initApi();
})
}
})
//跳转到指挥调度模块
$(".cmdDispatch").on('click',function(){
window.open(`./cmdDispatch.html?session=${loginInfo["session"]}`);
})
async function initApi(){
//获取系统的评分
var brigadeGradeRes=await getBrigadeGrade();
brigadeGradeArr= brigadeGradeRes.data.data;
//获取所有的派出所评分
var boroughGradeRes=await getBououghGrade();
var boroughGrade = boroughGradeRes.data.data;
for(var i=0;i<boroughGrade.length;i++){
boroughGrade[i]["schoolInfo"]=[];
}
boroughGradeArr=boroughGrade;
//获取所有的学校评分
var stationGradeRes=await getStationGrade();
stationGradeArr=stationGradeRes.data.data;
//获取所有的学校信息
var stationsDataRes=await getStationsData();
stationsInfoArr=stationsDataRes.data.data;
//获取学校的信息
var stationcountRes=await getstationcount();
var stationcount=stationcountRes.data.data.count;
var stationlistRes= await stationInfoPageReq(stationcount)
stationlistRes.forEach(item=>{
var data=item.data.data;
for(let i=0;i<data.length;i++){
stationInfoTableList.push(data[i]);
}
})
//显示brigade的评分
showBrigadeGrade();
//显示borough的评分
showBoroughGrade();
//显示学校的评分
showStationGrade();
//显示学校的的信息
showBrigadeStationsData();
//合并学校的信息
stationBelongBorough();
//初始化轮播
initCarschool();
//初始化工具
initTool();
//获取报警数据
showAlarmEventList()
connectWebsocket()
}
function connectWebsocket(){
//连接站点websocket
stationWebsocket().then((res)=>{
var code=res["code"];
if(code==0){
var stationWebsocketConnect=res["stationWebsocketConnect"];
createStationWebsocket(stationWebsocketConnect);
}
})
moveDeviceWebsocket().then((res)=>{
var code=res["code"];
if(code==0){
//发送数据
var mdWebsocketConnect=res["mdWebsocketConnect"];
createMdWebsocket(mdWebsocketConnect);
}
})
}
function createStationWebsocket(stationWebsocketConnect){
var stationTempArr=[];
stationInfoTableList.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);
}
function createMdWebsocket(mdWebsocketConnect){
var mobileDeviceArr=[];
var messageObj = {
"msgcode":400,
"msgname":"stationlist"
}
stationInfoTableList.forEach((item)=>{
var name=item.name;
mobileDeviceArr.push(name);
if(mobileDeviceArr.length>=500){
messageObj["message"]=mobileDeviceArr;
moveDeviceSendData(mdWebsocketConnect,mobileDeviceArr)
mobileDeviceArr=[];
}
})
//告诉我需要推送那些移动设备给我
messageObj["message"]=mobileDeviceArr;
moveDeviceSendData(mdWebsocketConnect,messageObj);
sendstationMdevList402(mdWebsocketConnect,mobileDeviceArr)
}
//移动互联网的websocket
function moveDeviceSendData(mdWebsocketConnect,messageObj){
var msg = JSON.stringify(messageObj);
mdWebsocketConnect.send(msg);
}
//移动设备发送数据
function sendstationMdevList402(mdWebsocketConnect,mobileDeviceArr){
var getMD={
"msgcode":402,
"msgname":"station-mdev-list",
"message":mobileDeviceArr
}
moveDeviceSendData(mdWebsocketConnect,getMD);
}
function initTool(){
getCityAndWeather();
$(".currentTime>span:first-child").html(getWeekDate());
$(".currentTime>span:last-child").html(timestampFormat());
setInterval(() => {
$(".currentTime>span:last-child").html(timestampFormat());
}, 1000);
}
export {
loginInfo,
layuiObj,
brigadeGradeArr,
boroughGradeArr,
stationGradeArr,
stationsInfoArr,
stationInfoTableList
}