UNPKG

@ngreatorex/homie-device

Version:
110 lines 3.95 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const events_1 = require("events"); const lodash_1 = __importDefault(require("lodash")); const winston = __importStar(require("winston")); class HomieTopologyBase extends events_1.EventEmitter { constructor(name, friendlyName) { super(); this.publish = (path, value, options) => { if (path.startsWith("/")) { path = path.substring(1); } let safeOptions; if (lodash_1.default.isBoolean(options)) { safeOptions = { retain: options }; } else if (!options) { safeOptions = undefined; } else { safeOptions = options; } this.rawPublish(`${this.name}/${path}`, value, safeOptions); }; this.subscribe = (path) => { if (path.startsWith("/")) { path = path.substring(1); } this.rawSubscribe(`${this.name}/${path}`); }; this.publishAttribute = (name, value) => { this.publish(`$${name}`, value, true); }; this.publishAttributes = (attributes) => { Object.entries(attributes).forEach(([key, value]) => { if (value != null && value !== undefined) { this.publishAttribute(key, value.toString()); } }); }; this.publishProperty = (name, value) => { this.publish(name, value, false); }; this.publishStats = (stats) => { Object.entries(stats).forEach(([key, value]) => { if (value != null && value !== undefined) { this.publish(`$stats/${key}`, value.toString(), false); } }); }; this.name$ = name; this.friendlyName$ = friendlyName; this.isConnected$ = false; this.logger = winston.child({ name: this.name, type: this.constructor.name, }); } get name() { return this.name$; } get friendlyName() { return this.friendlyName$; } set friendlyName(value) { this.friendlyName$ = value; } get isConnected() { return this.isConnected$; } onConnect() { this.isConnected$ = true; this.logger.debug("connected"); this.emit("connect"); } onDisconnect() { this.isConnected$ = false; this.logger.verbose("Disconnected"); this.emit("disconnect"); } onOffline() { this.isConnected$ = false; this.logger.debug("offline"); this.emit("offline"); } onError(err) { this.logger.debug("error", err); this.emit("error", err); } onStatsInterval() { this.logger.debug("reporting stats"); this.emit("stats-interval"); } } exports.default = HomieTopologyBase; //# sourceMappingURL=HomieTopologyBase.js.map