UNPKG

homebridge-smartsystem

Version:

SmartServer (Proxy TCP sockets to the cloud, Smappee MQTT, Duotecno IP Nodes, Homekit interface)

142 lines 7.87 kB
"use strict"; // P1 implementation // Johan Coppieters, Jul 2022. // var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.P1 = void 0; const logger_1 = require("../duotecno/logger"); const powerbase_1 = require("./powerbase"); const logger_2 = require("../duotecno/logger"); const api_1 = require("../duotecno/api"); class P1 extends powerbase_1.PowerBase { constructor(system) { super("p1", system); // readconfig and init are called by super } // // return data for ejs pages // // async getData(context: Context, message: String): Promise<any> { // // nothing special... for now // return super.getData(context, message); // } // // config mgt // // async updateConfig(config: any) { // // nothing special... for now // return super.updateConfig(config); // } subscribe() { return __awaiter(this, void 0, void 0, function* () { const kInterval = 5; (0, logger_1.log)("p1", "connecting to P1: " + this.config.address); if (this.config.address) { this.timer = setInterval(() => __awaiter(this, void 0, void 0, function* () { this.realtimeCounter += kInterval; // fetch data const message = yield this.fetchData(); if (message) this.consume(message); // call process/bindings/rules this.applyBindings(); this.applyRules(); }), kInterval * 1000); } }); } unsubscribe() { return __awaiter(this, void 0, void 0, function* () { if (this.timer) { clearInterval(this.timer); this.timer = null; } }); } fetchData() { return __awaiter(this, void 0, void 0, function* () { try { (0, logger_2.debug)("p1", "fetching from " + this.config.address + "/api/v1/telegram"); const res = yield (0, api_1.get)(this.config.address, 80, "/api/v1/telegram", undefined); (0, logger_2.debug)("p1", "received: " + res); const result = {}; res.split("\r\n").filter(l => (l[0] === '1')).map(L => { // 0-0:1.0.0(220720172521S) // 1-0:62.7.0(00.135*kW) const S = L.split(":")[1]; // remove preamble 1-0 if (!S) return undefined; // no key-value -> remove const V = S.split("("); // split "key(value" const key = V[0]; if (V.length < 2) return undefined; // no value -> remove const value = V[1].substring(0, V[1].length - 1); // remove last ")" const VU = value.split("*"); // split "value*unit" return { key, value: VU[0], unit: VU[1] }; }).forEach(R => result[R.key] = { value: parseFloat(R.value), unit: R.unit }); return result; } catch (error) { (0, logger_1.err)("p1", "error fetching data: " + JSON.stringify(error)); return null; } }); } consume(data) { 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; if (!data['32.7.0']) return; this.channels = { 0: { name: "Zonnepanelen L1", type: "CT", flow: "PRODUCTION", voltage: (_a = data['32.7.0']) === null || _a === void 0 ? void 0 : _a.value, power: ((_b = data['22.7.0']) === null || _b === void 0 ? void 0 : _b.value) * 1000, exportEnergy: 0, importEnergy: 0, phaseId: 0, current: 0, apparentPower: 0, cosPhi: 0, formula: "" }, 1: { name: "Zonnepanelen L2", type: "CT", flow: "PRODUCTION", voltage: (_c = data['52.7.0']) === null || _c === void 0 ? void 0 : _c.value, power: ((_d = data['42.7.0']) === null || _d === void 0 ? void 0 : _d.value) * 1000, exportEnergy: 0, importEnergy: 0, phaseId: 1, current: 0, apparentPower: 0, cosPhi: 0, formula: "" }, 2: { name: "Zonnepanelen L3", type: "CT", flow: "PRODUCTION", voltage: (_e = data['72.7.0']) === null || _e === void 0 ? void 0 : _e.value, power: ((_f = data['62.7.0']) === null || _f === void 0 ? void 0 : _f.value) * 1000, exportEnergy: 0, importEnergy: 0, phaseId: 2, current: 0, apparentPower: 0, cosPhi: 0, formula: "" }, 3: { name: "Grid L1", type: "CT", flow: "CONSUMPTION", voltage: (_g = data['32.7.0']) === null || _g === void 0 ? void 0 : _g.value, power: ((_h = data['21.7.0']) === null || _h === void 0 ? void 0 : _h.value) * 1000, exportEnergy: 0, importEnergy: 0, phaseId: 0, current: (_j = data['31.7.0']) === null || _j === void 0 ? void 0 : _j.value, apparentPower: 0, cosPhi: 0, formula: "" }, 4: { name: "Grid L2", type: "CT", flow: "CONSUMPTION", voltage: (_k = data['52.7.0']) === null || _k === void 0 ? void 0 : _k.value, power: ((_l = data['41.7.0']) === null || _l === void 0 ? void 0 : _l.value) * 1000, exportEnergy: 0, importEnergy: 0, phaseId: 1, current: (_m = data['51.7.0']) === null || _m === void 0 ? void 0 : _m.value, apparentPower: 0, cosPhi: 0, formula: "" }, 5: { name: "Grid L3", type: "CT", flow: "CONSUMPTION", voltage: (_o = data['72.7.0']) === null || _o === void 0 ? void 0 : _o.value, power: ((_p = data['61.7.0']) === null || _p === void 0 ? void 0 : _p.value) * 1000, exportEnergy: 0, importEnergy: 0, phaseId: 2, current: (_q = data['71.7.0']) === null || _q === void 0 ? void 0 : _q.value, apparentPower: 0, cosPhi: 0, formula: "" } }; // console.log(data); this.voltages = [(_r = data['32.7.0']) === null || _r === void 0 ? void 0 : _r.value, (_s = data['52.7.0']) === null || _s === void 0 ? void 0 : _s.value, (_t = data['72.7.0']) === null || _t === void 0 ? void 0 : _t.value]; this.realtime = { totalPower: ((_u = data['1.7.0']) === null || _u === void 0 ? void 0 : _u.value) * 1000, totalReactivePower: ((_v = data['2.7.0']) === null || _v === void 0 ? void 0 : _v.value) * 1000, totalExportEnergy: ((_w = data['2.8.1']) === null || _w === void 0 ? void 0 : _w.value) + ((_x = data['2.8.2']) === null || _x === void 0 ? void 0 : _x.value), totalImportEnergy: ((_y = data['1.8.1']) === null || _y === void 0 ? void 0 : _y.value) + ((_z = data['1.8.2']) === null || _z === void 0 ? void 0 : _z.value), monitorStatus: 0, utcTimeStamp: new Date().getTime() }; this.production = this.realtime.totalPower; this.consumption = this.realtime.totalReactivePower; } } exports.P1 = P1; //# sourceMappingURL=p1.js.map