EMS_front/src/stores/useElecDistStore.ts
2025-04-23 16:52:10 +08:00

129 lines
4.0 KiB
TypeScript

import { ref } from "vue";
import { defineStore } from "pinia";
import dayjs from "dayjs";
import type { NiagaraElecData } from "../utils/types";
const useElecStore = defineStore("elecDist", () => {
const elecData = ref<NiagaraElecData[]>([]);
// @ts-ignore
let timerId = null;
// get data from baja
const getElecDataFromBaja = () => {
// @ts-ignore
window.require &&
// @ts-ignore
window.requirejs(["baja!"], (baja: any) => {
console.log("進入 bajaSubscriber 準備執行 BQL 訂閱");
// 定義BQL 查詢
const subSysKwhBql = `local:|foxs:4918|station:|neql:EMS:SubSys_kwh|bql:select slotPath,parent.displayName,displayName,NumericInterval.historyConfig.id`;
// 執行各電表的 BQL 查詢
fetchElecData(baja, subSysKwhBql);
});
};
const fetchElecData = (baja: any, bql: string) => {
let eleclist: NiagaraElecData[] = [];
baja.Ord.make(bql).get({
cursor: {
before: () => {},
each: (record: any) => {
eleclist.push({
slotPath: record.get("slotPath"),
displayName: record.get("parent$2edisplayName"),
id: record.get("NumericInterval$2ehistoryConfig$2eid").$cEncStr,
out: 0,
});
},
after: () => {
const validElecList = eleclist.filter(
(item) => item.id !== undefined
);
elecData.value = [];
elecData.value.push(...validElecList);
validElecList.forEach((item) => {
subscribeToHistory(item);
});
},
},
});
};
const subscribeToHistory = (item: NiagaraElecData) => {
const startTime = dayjs()
.subtract(2, "hour")
.format("YYYY-MM-DDTHH:mm:ss.000+08:00"); // 現在時間前2個小時
const endTime = dayjs().format("YYYY-MM-DDTHH:mm:ss.000+08:00"); // 現在的時間
const id = item.id;
console.log(
`local:|foxs:4918|history:${id}?period=timerange;start=${startTime};end=${endTime}|bql:history:HistoryRollup.rollup(baja:RelTime '3600000')`
);
const ordString = `local:|foxs:4918|history:${id}?period=timerange;start=${startTime};end=${endTime}|bql:history:HistoryRollup.rollup(baja:RelTime '3600000')`;
// @ts-ignore
window.require &&
// @ts-ignore
window.requirejs(["baja!"], (baja: any) => {
console.log("進入 bajaSubscriber 準備執行 BQL 訂閱");
let historyData: number[] = [];
baja.Ord.make(ordString).get({
cursor: {
before: () => {
console.log(`開始訂閱 ${id} 的歷史資料`);
},
each: (record: any) => {
let currentValue = record.get("min");
historyData.push(currentValue);
},
after: () => {
const diff = historyData[historyData.length - 1] - historyData[0];
console.log(
`收到 ${id} 的歷史資料: 最後一筆減第一筆的值為:`,
diff
);
elecData.value = elecData.value.map((elec) => {
if (elec.id === id) {
console.log(`更新 ${id} 的 out 值為:`, diff);
return { ...elec, out: diff };
}
return elec;
});
},
},
});
});
};
// 定時更新資料的函數
const updateHistoryData = () => {
console.log("定時器觸發,重新獲取歷史資料");
if (elecData.value && elecData.value.length > 0) {
elecData.value.forEach((item) => {
subscribeToHistory(item);
});
}
};
// 啟動定時器
const startTimer = () => {
timerId = setInterval(() => {
updateHistoryData();
}, 60 * 60 * 1000); // 每小時執行一次
};
// 停止定時器
const stopTimer = () => {
// @ts-ignore
if (timerId) {
clearInterval(timerId);
timerId = null;
console.log("計時器已停止");
}
};
return { getElecDataFromBaja, startTimer, stopTimer, elecData };
});
export default useElecStore;