|
|
|
@ -1,10 +1,16 @@
@@ -1,10 +1,16 @@
|
|
|
|
|
import { ref, computed } from 'vue' |
|
|
|
|
import { ref, computed, reactive } from 'vue' |
|
|
|
|
import { defineStore } from 'pinia' |
|
|
|
|
import type { IOnlineDevice, IUpFirmwareStatus } from '@/views/stationData/type' |
|
|
|
|
import ZMQWorker from '@/composables/useZMQJsonWorker' |
|
|
|
|
import { getSubTopic, type SubMsgData } from '@/utils/zmq' |
|
|
|
|
import { getDeviceTopic } from '@/views/stationData/utils' |
|
|
|
|
import dayjs from "dayjs"; |
|
|
|
|
|
|
|
|
|
export interface SiteInfo { |
|
|
|
|
id: string |
|
|
|
|
onlineCount: number |
|
|
|
|
offlineCount: number |
|
|
|
|
devices: Map<string, IOnlineDevice> |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const useTransferDataStore = defineStore('transfer', () => { |
|
|
|
|
const subDevices = getSubTopic('client', 'status', 'transfer') |
|
|
|
@ -13,20 +19,52 @@ export const useTransferDataStore = defineStore('transfer', () => {
@@ -13,20 +19,52 @@ export const useTransferDataStore = defineStore('transfer', () => {
|
|
|
|
|
|
|
|
|
|
const connectSite = ref<any>(null) |
|
|
|
|
|
|
|
|
|
async function initConnectSite() { |
|
|
|
|
if (connectSite.value) return |
|
|
|
|
connectSite.value = { |
|
|
|
|
title: '未命名站点', |
|
|
|
|
edit: true, |
|
|
|
|
editTitle: '未命名站点', |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
const checkDeviceStatusInterval = ref<NodeJS.Timeout>() |
|
|
|
|
|
|
|
|
|
const siteMap = reactive(new Map<string, SiteInfo>()) |
|
|
|
|
|
|
|
|
|
const devicesMap = reactive(new Map<string, IOnlineDevice>()) |
|
|
|
|
const checkDeviceStatusInterval = ref<NodeJS.Timeout>() |
|
|
|
|
|
|
|
|
|
function checkDeviceStatus() { |
|
|
|
|
checkDeviceStatusInterval.value = setInterval(checkDeviceStatusFn, 100); |
|
|
|
|
// =========== mock =================
|
|
|
|
|
// let i = 0
|
|
|
|
|
//
|
|
|
|
|
// function mockSubDeviceMsg() {
|
|
|
|
|
// // 随机生成 SN
|
|
|
|
|
// const sn = `SN-${Math.floor(Math.random() * 1000)}`
|
|
|
|
|
// // const siteId = `${i % 2 === 0 ? 'site1' : 'site2'}`
|
|
|
|
|
// const siteId = `siteId-${Math.floor(Math.random() * 1000)}`
|
|
|
|
|
// const clientIp = `192.168.0.${Math.floor(Math.random() * 255)}`
|
|
|
|
|
// const version = `v${(Math.random() * 2 + 1).toFixed(2)}`
|
|
|
|
|
// const footprint = (Math.random() * 50000).toFixed(2) // KB
|
|
|
|
|
//
|
|
|
|
|
// // const sn = 123
|
|
|
|
|
// // const siteId = 123123
|
|
|
|
|
// // const clientIp = `192.168.0.${Math.floor(Math.random() * 255)}`
|
|
|
|
|
// // const version = `v${(Math.random() * 2 + 1).toFixed(2)}`
|
|
|
|
|
// // const footprint = (Math.random() * 50000).toFixed(2) // KB
|
|
|
|
|
//
|
|
|
|
|
// const msg: any = {
|
|
|
|
|
// feedback: [clientIp, sn, siteId, version, footprint],
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// getSubDevicesCb(msg)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// let a = setInterval(() => {
|
|
|
|
|
// i++
|
|
|
|
|
// mockSubDeviceMsg()
|
|
|
|
|
// }, 1000)
|
|
|
|
|
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
|
// clearInterval(a)
|
|
|
|
|
// }, 8000)
|
|
|
|
|
|
|
|
|
|
// =========== mock end =================
|
|
|
|
|
|
|
|
|
|
function startCheckStatus() { |
|
|
|
|
clearInterval(checkDeviceStatusInterval.value) |
|
|
|
|
checkDeviceStatusInterval.value = setInterval(checkDeviceStatusFn, 1000); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const checkDeviceStatusFn = () => { |
|
|
|
@ -37,6 +75,7 @@ export const useTransferDataStore = defineStore('transfer', () => {
@@ -37,6 +75,7 @@ export const useTransferDataStore = defineStore('transfer', () => {
|
|
|
|
|
device.status = '离线'; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
reassignDevicesToSites() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function formatSizeFromKB(num: number): string { |
|
|
|
@ -77,8 +116,34 @@ export const useTransferDataStore = defineStore('transfer', () => {
@@ -77,8 +116,34 @@ export const useTransferDataStore = defineStore('transfer', () => {
|
|
|
|
|
} |
|
|
|
|
devicesMap.set(sn, device) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function reassignDevicesToSites() { |
|
|
|
|
devicesMap.forEach((device) => { |
|
|
|
|
const siteId = device.site_id |
|
|
|
|
if (!siteId) return |
|
|
|
|
|
|
|
|
|
// 若该站点还未创建,则初始化
|
|
|
|
|
if (!siteMap.has(siteId)) { |
|
|
|
|
siteMap.set(siteId, { |
|
|
|
|
id: siteId, |
|
|
|
|
onlineCount: 0, |
|
|
|
|
offlineCount: 0, |
|
|
|
|
devices: reactive(new Map<string, IOnlineDevice>()), |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 获取站点信息并更新
|
|
|
|
|
const siteInfo = siteMap.get(siteId)! |
|
|
|
|
siteInfo.devices.set(device.sn, device) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
isConnected.value = devicesMap.size > 0 |
|
|
|
|
// 更新site 在线数量和离线数量
|
|
|
|
|
siteMap.forEach((siteInfo) => { |
|
|
|
|
const devices = Array.from(siteInfo.devices.values()) |
|
|
|
|
siteInfo.onlineCount = devices.filter(item => item.status === '在线').length |
|
|
|
|
siteInfo.offlineCount = devices.filter(item => item.status === '离线').length |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const onlineCount = computed(() => { |
|
|
|
@ -93,8 +158,8 @@ export const useTransferDataStore = defineStore('transfer', () => {
@@ -93,8 +158,8 @@ export const useTransferDataStore = defineStore('transfer', () => {
|
|
|
|
|
|
|
|
|
|
onMounted(() => { |
|
|
|
|
worker.subscribe(getDeviceTopic, getSubDevicesCb) |
|
|
|
|
checkDeviceStatus() |
|
|
|
|
document.addEventListener('visibilitychange', checkDeviceStatusFn) |
|
|
|
|
startCheckStatus() |
|
|
|
|
document.addEventListener('visibilitychange', handleVisibilityChange) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const route = useRoute() |
|
|
|
@ -102,11 +167,18 @@ export const useTransferDataStore = defineStore('transfer', () => {
@@ -102,11 +167,18 @@ export const useTransferDataStore = defineStore('transfer', () => {
|
|
|
|
|
watch(() => route.path, (val) => { |
|
|
|
|
if (!['/station/data-transfer', '/station'].includes(val)) { |
|
|
|
|
clearInterval(checkDeviceStatusInterval.value) |
|
|
|
|
document.removeEventListener('visibilitychange', checkDeviceStatusFn) |
|
|
|
|
document.removeEventListener('visibilitychange', handleVisibilityChange) |
|
|
|
|
worker.unsubscribe(subDevices) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
function handleVisibilityChange() { |
|
|
|
|
if (document.hidden) { |
|
|
|
|
clearInterval(checkDeviceStatusInterval.value) |
|
|
|
|
} else { |
|
|
|
|
startCheckStatus() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function upFirmwareStatus(sn: string, feedback: any[]) { |
|
|
|
|
const device = devicesMap.get(sn) |
|
|
|
@ -181,14 +253,13 @@ export const useTransferDataStore = defineStore('transfer', () => {
@@ -181,14 +253,13 @@ export const useTransferDataStore = defineStore('transfer', () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
siteMap, |
|
|
|
|
isConnected, |
|
|
|
|
devicesMap, |
|
|
|
|
connectSite, |
|
|
|
|
checkDeviceStatusInterval, |
|
|
|
|
onlineCount, |
|
|
|
|
offlineCount, |
|
|
|
|
checkDeviceStatus, |
|
|
|
|
initConnectSite, |
|
|
|
|
upFirmwarePending, |
|
|
|
|
upFirmwareReset, |
|
|
|
|
upFirmwareStatus, |
|
|
|
|