guandu_front/src/stores/useElecTotalMeterStore.js
2025-05-19 12:06:36 +08:00

196 lines
6.3 KiB
JavaScript
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 { calcuEleCost } from "@/utils";
const useElecStore = defineStore("elecData", () => {
const elecData = ref([]);
const elecPriceData = ref([]);
let timerId = null;
const todayelecdata = ref(new Map());
const yesterdayelecdata = ref(new Map());
const elecFlowCostSummary = ref(null);
const getElecDataFromBaja = () => {
window.require &&
window.requirejs(["baja!"], (baja) => {
console.log("進入 bajaSubscriber 準備執行用電價 BQL 訂閱");
// 定義BQL 查詢
const price_kwhBql = `local:|foxs:4918|station:|neql:EMS:parameter|bql:select slotPath,parent.displayName,displayName,NumericInterval.historyConfig.id,out`;
// 執行查詢
let pricelist = [];
baja.Ord.make(price_kwhBql).get({
cursor: {
before: () => {},
each: (record) => {
pricelist.push({
slotPath: record.get("slotPath"),
displayName: record.get("displayName"),
id: record.get("NumericInterval$2ehistoryConfig$2eid").$cEncStr,
out: record.get("out")?.get("value") ?? 0,
});
},
after: () => {
elecPriceData.value = [];
elecPriceData.value.push(...pricelist);
},
limit: -1,
offset: 0,
},
});
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 查詢
let eleclist = [];
baja.Ord.make(Total_kwhBql).get({
cursor: {
before: () => {},
each: (record) => {
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) => {
const id = item.id;
const startTime = dayjs("2025-05-08T16:30:00.000+08:00")
.subtract(13, "day")
.startOf("day")
.format("YYYY-MM-DDTHH:mm:ss.000+08:00");
const endTime = dayjs("2025-05-08T16:30:00.000+08:00")
.endOf("day")
.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) => {
console.log("進入 bajaSubscriber 準備執行 BQL 訂閱");
const dataMap = new Map();
let lastTimestamp = null;
let lastValue = null;
baja.Ord.make(ordString).get({
cursor: {
before: () => {
console.log(`開始訂閱 ${id} 的歷史資料`);
},
each: (record) => {
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);
// 清空之前的數據,避免累積
todayelecdata.value = new Map();
yesterdayelecdata.value = new Map();
// 提取今天和昨天的数据
for (const [timestamp, value] of dataMap) {
const date = dayjs(timestamp).format("YYYY-MM-DD");
const today = dayjs("2025-05-08T16:30:00.000+08:00").format("YYYY-MM-DD");
const yesterday = dayjs("2025-05-08T16:30:00.000+08:00").subtract(1, "day").format("YYYY-MM-DD");
if (date === today) {
todayelecdata.value.set(timestamp, value);
} else if (date === yesterday) {
yesterdayelecdata.value.set(timestamp, value);
}
}
console.log("今天每小時差值 map", todayelecdata.value);
console.log("昨天每小時差值 map", yesterdayelecdata.value);
elecFlowCostSummary.value = calcuEleCost(
dataMap,
elecPriceData.value
);
},
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,
elecPriceData,
elecFlowCostSummary,
todayelecdata,
yesterdayelecdata,
};
});
export default useElecStore;