UNPKG

@corvina/device-client

Version:
104 lines 3.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const stream_1 = require("stream"); const configparser_1 = __importDefault(require("./configparser")); const messagepublisherpolicies_1 = require("./messagepublisherpolicies"); const logger_service_1 = require("./logger.service"); class CorvinaDataInterface extends stream_1.EventEmitter { _config; CYCLE_TIME; _nextTick; _internalTimer; _sender; get config() { return this._config; } constructor(sender) { super(); this._sender = sender; this.CYCLE_TIME = 1000; } setCycleTime(cycleTime) { this.CYCLE_TIME = cycleTime; } monotonicTimer() { return Number(process.hrtime.bigint() / 1000000n); } start() { this.doPublish(); this._internalTimer = setInterval(() => { this.doPublish(); }, this.CYCLE_TIME); } stop() { clearInterval(this._internalTimer); this._internalTimer = null; } doPublish() { if (!this._config) { return; } this._nextTick = messagepublisherpolicies_1.INVALID_STATE_TS; const now = this.monotonicTimer() + this.CYCLE_TIME / 2; for (const p of this._config.tagPublishers.values()) { for (const mp of p.values()) { if (mp.nextTime(now) <= now) { mp.publish(now, this._sender); } } } } applyConfig(config) { this._config = (0, configparser_1.default)(config); } notifyTag(tagName, newState, options) { let nextTime = messagepublisherpolicies_1.INVALID_STATE_TS; const currentTime = this.monotonicTimer(); const tagPublishers = this._config.tagPublishers.get(tagName); if (!tagPublishers || tagPublishers.size == 0) { const err = "Cannot publish unconfigured tag " + tagName; if (options?.cb) { options.cb(new Error(err), tagName, undefined); } logger_service_1.l.debug("Cannot publish unconfigured tag " + tagName); return; } let nothingToPublish = true; tagPublishers.forEach((publisher) => { nextTime = Math.min(nextTime, publisher.update({ tagName, newState, currentTime })); if (nextTime <= currentTime || options?.forceImmediateSend) { nothingToPublish = false; const internalOptions = { ...options }; if (options?.cb) { internalOptions.cb = (err, pkt) => { if (err) { options.cb(err, tagName, publisher.modelPath); } else { options.cb(undefined, tagName, publisher.modelPath); } }; } else { const internalOptions = { ...options }; internalOptions.cb = (err, pkt) => { if (err) { logger_service_1.l.error(err); } }; } publisher.publish(currentTime, this._sender, internalOptions); } }); if (nothingToPublish) { if (options?.cb) { options.cb(undefined, undefined, undefined); } } } } exports.default = CorvinaDataInterface; //# sourceMappingURL=corvinadatainterface.js.map