EMS_front/src/stores/useElecTotalMeterStore.ts

153 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ref } from "vue";
import { defineStore } from "pinia";
import dayjs from "dayjs";
import type {
NiagaraElecData,
ElecCostSummary,
ElecStandCostSummary,
} from "../utils/types";
import { CalcuEleCost, CalcuEleStandCost } from "../utils/CalcuEleCost";
const useElecStore = defineStore("elecData", () => {
const elecData = ref<NiagaraElecData[]>([]);
// @ts-ignore
let timerId = null;
const elecFlowCostSummary = ref<ElecCostSummary | null>(null);
const elecStandCostSummary = ref<ElecStandCostSummary | null>(null);
// get data from baja
const getElecDataFromBaja = () => {
// @ts-ignore
window.require &&
// @ts-ignore
window.requirejs(["baja!"], (baja: any) => {
console.log("進入 bajaSubscriber 準備執行用電度數 BQL 訂閱");
// 定義BQL 查詢
const Total_kwhBql = `local:|foxs:4918|station:|neql:EMS:Total_kwh|bql:select slotPath,parent.displayName,displayName,NumericInterval.historyConfig.id,out`;
// 執行各電表的 BQL 查詢
fetchElecData(baja, Total_kwhBql);
});
};
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"),
out: record.get("out")?.get("value") ?? null,
});
},
after: () => {
const validElecList = eleclist.filter(
(item) => item.id !== undefined
);
elecData.value = [];
elecData.value.push(...validElecList);
validElecList.forEach((item) => {
gettimeToHistory(item);
});
},
},
});
};
const gettimeToHistory = (item: NiagaraElecData) => {
const id = item.id;
const startTime = dayjs(
`${dayjs().year()}-01-01T00:00:00.000+08:00`
).format("YYYY-MM-DDTHH:mm:ss.000+08:00");
const endTime = dayjs().format("YYYY-MM-DDTHH:mm:ss.000+08:00");
const ordString = `local:|foxs:4918|history:${id}?period=timerange;;start=${startTime};end=${endTime}|bql:history:HistoryRollup.rollup(baja:RelTime '3600000')`; //每小时一个rollup
console.log(ordString);
// @ts-ignore
window.require &&
// @ts-ignore
window.requirejs(["baja!"], (baja: any) => {
console.log("進入 bajaSubscriber 準備執行 BQL 訂閱");
const dataMap = new Map<string, number>(); // key: timestamp string, value: energy diff
let lastTimestamp: string | null = null;
let lastValue: number | null = null;
baja.Ord.make(ordString).get({
cursor: {
before: () => {
console.log(`開始訂閱 ${id} 的歷史資料`);
},
each: (record: any) => {
const currentValue = record.get("min");
const timestamp = record.get("timestamp").$cEncStr;
if (
currentValue !== null &&
!isNaN(currentValue) &&
currentValue !== 0 &&
timestamp !== null
) {
if (
lastTimestamp !== null &&
lastValue !== null &&
lastValue !== 0
) {
const diff = currentValue - lastValue;
dataMap.set(lastTimestamp, diff);
}
lastValue = currentValue;
lastTimestamp = timestamp;
}
},
after: () => {
console.log("⏱️ 每小時差值 map", dataMap);
elecFlowCostSummary.value = CalcuEleCost(dataMap);
elecStandCostSummary.value = CalcuEleStandCost(dataMap);
},
limit: -1,
offset: 0,
},
});
});
};
// 定時更新資料的函數
const updateHistoryData = () => {
console.log("定時器觸發,重新獲取歷史資料");
if (elecData.value && elecData.value.length > 0) {
elecData.value.forEach((item) => {
gettimeToHistory(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,
elecFlowCostSummary,
elecStandCostSummary,
};
});
export default useElecStore;