import { n4Sup } from "@CON"; import { getCurElectricity, getElectricMeter, getHourElectricMeter, getWeekElectricMeter, } from "@STORE"; import { roundDecimal } from "@UTIL"; // 首頁指定區域總電錶歷史信息 /* @param {Date} start yyyy-mm-dd @param {Date} end yyyy-mm-dd @param {string} range hourly|daily */ export async function getTotalElectricByBaja(ordPath, date, range, dispatch) { const year = date.getFullYear(); const month = date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : `0${date.getMonth() + 1}`; const curDate = date.getDate(); let startDate; const endDate = `${year}-${month}-${curDate}`; const rangePara = range === "weekly" ? "daily" : range; const weekday = date.getDay(); if (range === "weekly") { startDate = `${year}-${month}-${curDate - weekday - 7}`; } else { startDate = `${year}-${month}-${curDate - 1}`; } let result = []; window.require && window.require(["baja!"], function (baja) { baja.Ord.make( `local:|foxs:|history:/${n4Sup}/${ordPath}?peroid=timerange;start=${startDate}T00:00:00.000+08:00;end=${endDate}T24:00:00.000+08:00;delta=true|bql:history:HistoryRollup.rollup(history:RollupInterval '${rangePara}')`, ).get({ cursor: { before: function () {}, each: function () { result.push({ date: this.get("timestamp").$date, // time: this.get("timestamp").$time, timestamp: this.get("timestamp").$cEncStr, min: this.get("min"), max: this.get("max"), sum: this.get("sum"), avg: this.get("avg"), }); }, after: function () { const yesterday = result.filter((r) => r.date.$day === date.getDate() - 1); const today = result.filter((r) => r.date.$day === date.getDate()); if (range === "daily") { // 日 dispatch( getElectricMeter([ { text: "今日用電量", data: roundDecimal(today[0].sum) }, { text: "昨日用電量", data: roundDecimal(yesterday[0].sum) }, ]), ); } else if (range === "hourly") { dispatch( getHourElectricMeter({ current: today.map((t) => t.sum), last: yesterday.map((y) => y.sum), }), ); } else if (range === "weekly") { // 周 const weekParam = new Date( `${year}-${month}-${curDate - weekday}T00:00:00`, ).getTime(); const currentData = result.filter( (r) => new Date(`${r.timestamp}`).getTime() >= weekParam, ); const lastData = result.filter( (r) => new Date(`${r.timestamp}`).getTime() < weekParam, ); getWeekElectricMeter({ current: currentData.map((c) => c.sum), last: lastData.map((l) => l.sum), }); } }, limit: -1, }, }); }); } export async function getCurPower(ordPath, dispatch) { window.require && window.require(["baja!"], function (baja) { const sub = new baja.Subscriber(); function passValueToFront(value) { const newValue = roundDecimal(value); dispatch( getCurElectricity([ { data: newValue, text: "即時功率 " }, { data: roundDecimal(newValue / 4), text: "即時契約容量占比 " }, ]), ); } sub.attach("changed", function (prop) { if (prop && prop.$displayName === "Out") { passValueToFront(prop.$getValue().getValueDisplay()) } }) sub.attach("subscribed", function (prop) { if (prop && prop.$displayName === "Out") { passValueToFront(prop.$getValue().getValueDisplay()) } }); // ord 為要訂閱的點位 baja.Ord.make(`local:|foxs:|station:|slot:/${ordPath}`) .get() .then(folder => { folder.getSlots().is("control:NumericWritable").eachValue((point) => { if (point.getDisplayName() === "P") { baja.Ord.make(`local:|foxs:|station:|slot:/${ordPath}/${point.getDisplayName() }`).get({ subscriber: sub }) } }) }) }); }