UNPKG

homebridge-vwconnectid

Version:
143 lines 9.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SmartChargingAccessory = void 0; const axios_1 = __importDefault(require("axios")); class SmartChargingAccessory { constructor(platform, accessory, intervalId = null) { this.platform = platform; this.accessory = accessory; this.intervalId = intervalId; this.accessory.getService(this.platform.Service.AccessoryInformation) .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Volkswagen') .setCharacteristic(this.platform.Characteristic.Model, this.platform.vwConn.vehicles.data.find(({ vin }) => vin === this.platform.config.weconnect.vin).model) .setCharacteristic(this.platform.Characteristic.SerialNumber, this.platform.config.weconnect.vin); this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch); this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName); this.service.getCharacteristic(this.platform.Characteristic.On) .onSet(this.setOn.bind(this)) .onGet(this.getOn.bind(this)); } async fetchJSONData(url) { try { const response = await axios_1.default.get(url); return response.data; } catch (error) { this.platform.log.info('Error fetching JSON data:', error); throw error; } } async smartChargingLoop() { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11; try { const range = (_d = (_c = (_b = (_a = this.platform.vwConn.idData) === null || _a === void 0 ? void 0 : _a.charging) === null || _b === void 0 ? void 0 : _b.batteryStatus) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.cruisingRangeElectric_km; const targetSOC = (_h = (_g = (_f = (_e = this.platform.vwConn.idData) === null || _e === void 0 ? void 0 : _e.charging) === null || _f === void 0 ? void 0 : _f.chargingSettings) === null || _g === void 0 ? void 0 : _g.value) === null || _h === void 0 ? void 0 : _h.targetSOC_pct; const currentSOC = (_m = (_l = (_k = (_j = this.platform.vwConn.idData) === null || _j === void 0 ? void 0 : _j.charging) === null || _k === void 0 ? void 0 : _k.batteryStatus) === null || _l === void 0 ? void 0 : _l.value) === null || _m === void 0 ? void 0 : _m.currentSOC_pct; const isCharging = ((_r = (_q = (_p = (_o = this.platform.vwConn.idData) === null || _o === void 0 ? void 0 : _o.charging) === null || _p === void 0 ? void 0 : _p.chargingStatus) === null || _q === void 0 ? void 0 : _q.value) === null || _r === void 0 ? void 0 : _r.chargingState) === 'charging'; const isReadyForCharging = ((_v = (_u = (_t = (_s = this.platform.vwConn.idData) === null || _s === void 0 ? void 0 : _s.charging) === null || _t === void 0 ? void 0 : _t.chargingStatus) === null || _u === void 0 ? void 0 : _u.value) === null || _v === void 0 ? void 0 : _v.chargingState) === 'readyForCharging'; const isReduced = ((_z = (_y = (_x = (_w = this.platform.vwConn.idData) === null || _w === void 0 ? void 0 : _w.charging) === null || _x === void 0 ? void 0 : _x.chargingSettings) === null || _y === void 0 ? void 0 : _y.value) === null || _z === void 0 ? void 0 : _z.maxChargeCurrentAC) === 'reduced'; const highTariffKmTreshold = (_2 = (_1 = (_0 = this.accessory) === null || _0 === void 0 ? void 0 : _0.context) === null || _1 === void 0 ? void 0 : _1.device) === null || _2 === void 0 ? void 0 : _2.highTariffKmTreshold; const lowTariffKmTreshold = (_5 = (_4 = (_3 = this.accessory) === null || _3 === void 0 ? void 0 : _3.context) === null || _4 === void 0 ? void 0 : _4.device) === null || _5 === void 0 ? void 0 : _5.lowTariffKmTreshold; const minRedeliveryTreshold = -1 * ((_8 = (_7 = (_6 = this.accessory) === null || _6 === void 0 ? void 0 : _6.context) === null || _7 === void 0 ? void 0 : _7.device) === null || _8 === void 0 ? void 0 : _8.minRedeliveryTreshold); const maxDeliveryTreshold = (_11 = (_10 = (_9 = this.accessory) === null || _9 === void 0 ? void 0 : _9.context) === null || _10 === void 0 ? void 0 : _10.device) === null || _11 === void 0 ? void 0 : _11.maxDeliveryTreshold; const currentMonth = new Date().getMonth() + 1; const lowTariffKmTresholdSOC = currentSOC / range * lowTariffKmTreshold; const targetSolarSOC = lowTariffKmTresholdSOC + (80 - lowTariffKmTresholdSOC) * this.getMonthlyFraction(currentMonth); if (range < highTariffKmTreshold) { if (isReduced) { this.platform.log.info('range below highTariffKmTreshold, setting max charge current'); this.platform.vwConn.setChargingSetting('chargeCurrent', 'maximum'); } if (isReadyForCharging) { this.platform.log.info('range below highTariffKmTreshold, start charging'); this.platform.vwConn.startCharging(); } } else { const url = this.accessory.context.device.energyDataSource; this.fetchJSONData(url) .then((data) => { if (currentSOC < targetSolarSOC && data.lowTariff) { if (isReduced) { this.platform.log.info('SOC below targetSolarSOC, setting maximum charge current'); this.platform.vwConn.setChargingSetting('chargeCurrent', 'maximum'); } if (isReadyForCharging) { this.platform.log.info('SOC below targetSolarSOC and low tariff, start charging'); this.platform.vwConn.startCharging(); } else if (isCharging) { this.platform.log.info(`Smart charging: ${currentSOC}% to ${targetSolarSOC}%`); } } else if (data.minAvg < minRedeliveryTreshold) { if (isCharging && isReduced) { this.platform.log.info('JSON Data minAvg: ', data.minAvg); this.platform.log.info('minute average power < minRedeliveryTreshold, currently charging, so setting maximum charge current'); this.platform.vwConn.setChargingSetting('chargeCurrent', 'maximum'); return; } if (!isReduced) { this.platform.log.info('JSON Data minAvg: ', data.minAvg); this.platform.log.info('minute average power < minRedeliveryTreshold, currently maximum charge current, setting to reduced'); this.platform.vwConn.setChargingSetting('chargeCurrent', 'reduced'); } if (isReadyForCharging) { this.platform.log.info('JSON Data minAvg: ', data.minAvg); this.platform.log.info('minute average power < minRedeliveryTreshold, start charging'); this.platform.vwConn.startCharging(); } } else if (data.minAvg > maxDeliveryTreshold && !isReduced && isCharging) { this.platform.log.info('JSON Data minAvg: ', data.minAvg); this.platform.log.info('minute average power > maxDeliveryTreshold, currently charging at max, so setting to reduced'); this.platform.vwConn.setChargingSetting('chargeCurrent', 'reduced'); return; } else if (data.minAvg > maxDeliveryTreshold && isCharging) { this.platform.log.info('JSON Data minAvg: ', data.minAvg); this.platform.log.info('minute average power > maxDeliveryTreshold, currently charging, so stop charging'); this.platform.vwConn.stopCharging(); } }) .catch((error) => { this.platform.log.error('Error in fetching energy data: ', error); }); } } catch (error) { this.platform.log.error('Error in smartChargingLoop: ', error); // Handle the error (e.g., logging, fallback values, etc.) } } async setOn(value) { if (value) { this.intervalId = setInterval(() => { this.smartChargingLoop(); }, 60000); } else { if (this.intervalId) { clearInterval(this.intervalId); this.intervalId = null; } } this.platform.log.info('Set smart charging Characteristic On ->', value); } async getOn() { return (this.intervalId != null); } getMonthlyFraction(currentMonth) { if (currentMonth < 1 || currentMonth > 12) { throw new Error("Invalid month. Month should be between 1 and 12."); } // Calculate the cosine value const radians = (2 * Math.PI / 12) * (currentMonth - 12); return (1 + Math.cos(radians)) / 2; } } exports.SmartChargingAccessory = SmartChargingAccessory; //# sourceMappingURL=smartChargingAccessory.js.map