@ngreatorex/homie-device
Version:
Homie Device for NodeJS
94 lines • 3.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultConfiguration = void 0;
const lodash_1 = __importDefault(require("lodash"));
const framework_1 = require("./framework");
const HomieProperty_1 = __importDefault(require("./HomieProperty"));
exports.DefaultConfiguration = {
isRange: false,
type: "switch",
};
class HomieNode extends framework_1.HomieTopologyElement {
constructor(parent, config) {
super(lodash_1.default.merge({}, exports.DefaultConfiguration, config), parent);
this.addProperty = (config) => {
super.assertConfigurable();
return this.properties$[config.name] = new HomieProperty_1.default(this, config);
};
this.getProperty = (propName) => this.properties$[propName];
this.onConnect = () => {
super.onConnect();
const properties = [];
lodash_1.default.each(this.properties$, (property) => {
properties.push(property.name);
});
this.publishAttributes({
name: this.friendlyName,
properties: properties.join(","),
type: this.config.type,
});
if (this.config.isRange) {
this.publishAttribute("array", `${this.config.startRange}-${this.config.endRange}`);
}
lodash_1.default.each(this.properties$, (prop) => {
prop.onConnect();
});
};
this.onOffline = () => {
super.onOffline();
lodash_1.default.each(this.properties$, (prop) => {
prop.onOffline();
});
};
this.onDisconnect = () => {
super.onDisconnect();
lodash_1.default.each(this.properties$, (prop) => {
prop.onDisconnect();
});
};
this.onError = (err) => {
super.onError(err);
lodash_1.default.each(this.properties$, (prop) => {
prop.onError(err);
});
};
this.onStatsInterval = () => {
super.onStatsInterval();
lodash_1.default.each(this.properties$, (prop) => {
prop.onStatsInterval();
});
};
this.publishPropertyValue = (property, value, rangeIndex) => {
if (this.isRange && rangeIndex === undefined)
throw new Error("This node represents a range, but no range index was specified");
else if (!this.isRange && rangeIndex !== undefined)
throw new Error("This node does not represent a range, but a range index was specified");
const topic = this.isRange
? `${this.name}_${rangeIndex}/${property.name}`
: `${this.name}/${property.name}`;
this.rawPublish(topic, value.toString(), { retain: property.retained });
};
if (config.name.indexOf("_") >= 0) {
throw new Error(`A HomieNode name cannot include an underscore. The name provided was "${config.name}"`);
}
this.properties$ = {};
if (config.isRange) {
if (config.startRange === undefined) {
throw new Error("IsRange=true but StartRange=undefined");
}
if (config.endRange === undefined) {
throw new Error("IsRange=true but EndRange=undefined");
}
}
}
get type() { return this.config.type; }
get isRange() { return this.config.isRange; }
get startRange() { return this.config.startRange; }
get endRange() { return this.config.endRange; }
get properties() { return Object.values(this.properties$); }
}
exports.default = HomieNode;
//# sourceMappingURL=HomieNode.js.map