UNPKG

@corvina/device-client

Version:
170 lines 5.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AggregatedMessagePublisher = exports.MessagePublisher = void 0; const types_1 = require("../common/types"); const messagepublisherpolicies_1 = require("./messagepublisherpolicies"); class MessagePublisher { _topic; _topicType; _tagName; _modelPath; _nextTime; _policy; _armed; _stateToPublish; _lastStateVersion = 0; _lastPublishedStateVersion = 0; _messageSender; constructor({ sourceTag, modelPath, topic, topicType, }) { this._tagName = sourceTag; this._modelPath = modelPath; this._topic = topic; this._topicType = topicType; this._tagName = sourceTag; this._nextTime = -1; this._policy = null; this._armed = false; this._stateToPublish = new messagepublisherpolicies_1.State(); } get policy() { return this._policy; } get modelPath() { return this._modelPath; } setPolicy(policy) { this._policy = policy.clone(); } update({ tagName, newState, currentTime }) { this._lastStateVersion++; if (tagName == this._tagName) { this._stateToPublish.timestamp = newState.timestamp; this._stateToPublish.value = (0, types_1.castCorvinaType)(newState.value, this._topicType); } if (this._policy) { this._nextTime = this._policy.updateState({ tagName, newState, currentTime, }); } else { this._nextTime = -1; } return this._nextTime; } rearm(currentTime) { if (this._policy) { this._nextTime = this._policy.rearm(currentTime); } else { this._nextTime = -1; } this._armed = true; return this._nextTime; } nextTime(currentTime) { if (!this._armed) { return this.rearm(currentTime); } return this._nextTime; } publish(currentTime, messageSender, options) { const ts = this._lastPublishedStateVersion == this._lastStateVersion ? Date.now() : this._stateToPublish.timestamp; if (!ts) { if (options?.cb) { options.cb(new Error("Nothing to send"), undefined); } return; } if (!this._topic) { if (options?.cb) { options.cb(new Error("No topic to send"), undefined); } this._lastPublishedStateVersion = this._lastStateVersion; this.rearm(currentTime); return; } messageSender.sendMessage(this._topic, { t: ts, v: this._stateToPublish.value, }, options); this._lastPublishedStateVersion = this._lastStateVersion; this.rearm(currentTime); } get topic() { return this._topic; } get topicType() { return this._topicType; } get tagName() { return this._tagName; } toString() { return `MessagePublisher@${this._tagName} => ${this._topic} ( ${this._policy ? this._policy.toString() : "NULL"})`; } } exports.MessagePublisher = MessagePublisher; class AggregatedMessagePublisher extends MessagePublisher { _recomputePolicy = false; constructor({ sourceTag, modelPath, topic }) { super({ sourceTag, modelPath, topic, topicType: "struct" }); this._fields = []; } get modelPath() { return this._modelPath; } addField({ tagName, fieldName, type }) { this._fields.push({ tagName, fieldName, type, lastValueToPublish: undefined, }); this._recomputePolicy = true; } update({ tagName, newState, currentTime }) { if (this._policy && this._recomputePolicy) { this._fields.forEach((f) => { if (f.tagName == tagName) { f.lastValueToPublish = (0, types_1.castCorvinaType)(newState.value, f.type); this._stateToPublish.timestamp = newState.timestamp; } this._policy.setFieldTagName({ fieldName: f.fieldName, tagName: f.tagName, }); }); this._policy = this._policy.multiTagVersion(this._fields.map((f) => f.tagName)); } return super.update({ tagName, newState, currentTime }); } publish(currentTime, messageSender, options) { const ts = this._lastPublishedStateVersion == this._lastStateVersion ? Date.now() : this._stateToPublish.timestamp; const x = this._stateToPublish.value || {}; this._fields.forEach((f) => { x[f.fieldName] = f.lastValueToPublish; }); if (!this._topic) { if (options?.cb) { options.cb(new Error("No topic to send"), undefined); } this._lastPublishedStateVersion = this._lastStateVersion; this.rearm(currentTime); return; } messageSender.sendMessage(this._topic, { t: ts, v: x }, options); this._lastPublishedStateVersion = this._lastStateVersion; this.rearm(currentTime); } toString() { return `AggregatedMessagePublisher@${this._tagName} => ${this._topic} ( ${this._policy ? this._policy.toString() : "NULL"})`; } get fields() { return this._fields; } _fields; } exports.AggregatedMessagePublisher = AggregatedMessagePublisher; //# sourceMappingURL=messagepublisher.js.map