@iotile/iotile-cloud
Version:
A typescript library for interfacing with the IOTile Cloud API
375 lines • 14.1 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var unit_1 = require("./unit");
var mdo_1 = require("./mdo");
var stats_1 = require("./stats");
var model_delta_1 = require("../base/model-delta");
var iotile_common_1 = require("@iotile/iotile-common");
var Stream = /** @class */ (function () {
function Stream(data) {
if (data === void 0) { data = {}; }
this.enabled = true;
this.inputUnit = null;
this.outputUnit = null;
this.slug = data.slug;
this.id = data.id;
this.variable = data.variable;
this.device = data.device;
this.project = data.project;
this.mdoType = data.mdo_type || null;
this.mdo = new mdo_1.Mdo(data);
this.stats = new stats_1.Stats();
this.rawValueFormat = data.raw_value_format || '<L';
this._rawData = data;
this.projectId = data.project_id;
this.createdOn = data.created_on;
this.type = data.data_type;
this.enabled = data.enabled || true;
//We always clear isModified since it will be set by the ProjectOverlay on an already built object
this.isModified = false;
this.data = [];
this.dataLabel = data.data_label;
if (data.block) {
this.block = data.block;
}
if (data.var_name) {
this.variableName = data.var_name;
}
if (data.var_type) {
this.variableType = data.var_type;
}
this.variableLocalId = data.var_lid;
if (data.input_unit) {
this.inputUnit = new unit_1.Unit(data.input_unit);
}
if (data.output_unit) {
this.outputUnit = new unit_1.Unit(data.output_unit);
}
}
Stream.prototype.loadData = function (data) {
this.slug = data.slug;
this.id = data.id;
this.variable = data.variable;
this.device = data.device;
this.project = data.project;
this.mdoType = data.mdo_type || null;
this.mdo = new mdo_1.Mdo(data);
this.stats = new stats_1.Stats();
this.rawValueFormat = data.raw_value_format || '<L';
this._rawData = data;
this.projectId = data.project_id;
this.createdOn = data.created_on;
this.type = data.data_type;
this.enabled = data.enabled || true;
//We always clear isModified since it will be set by the ProjectOverlay on an already built object
this.isModified = false;
this.data = [];
this.dataLabel = data.data_label;
if (data.block) {
this.block = data.block;
}
if (data.var_name) {
this.variableName = data.var_name;
}
if (data.var_type) {
this.variableType = data.var_type;
}
this.variableLocalId = data.var_lid;
if (data.input_unit) {
this.inputUnit = new unit_1.Unit(data.input_unit);
}
if (data.output_unit) {
this.outputUnit = new unit_1.Unit(data.output_unit);
}
};
Stream.prototype.toJson = function () {
var obj = {
id: this.id,
project_id: this.projectId,
project: this.project,
device: this.device,
variable: this.variable,
data_label: this.dataLabel,
raw_value_format: this.rawValueFormat,
mdo_type: this.mdoType,
slug: this.slug,
type: this.type,
created_on: this.createdOn,
enabled: this.enabled,
input_unit: null,
output_unit: null
};
if (this.inputUnit) {
obj['input_unit'] = this.inputUnit.toJson();
}
if (this.outputUnit) {
obj['output_unit'] = this.outputUnit.toJson();
}
this.mdo.addToObject(obj, true);
return obj;
};
Stream.prototype.addStats = function (stats) {
this.stats = stats;
};
Stream.prototype.addData = function (data) {
this.data = data;
};
Stream.prototype.getLocalVarId = function () {
if (this.variable) {
var elements = this.variable.split("--");
if (elements.length === 3) {
return elements[2];
}
}
return "";
};
Stream.prototype.resetData = function () {
this.data = [];
};
Stream.prototype.getPatchPayload = function () {
var basic = {
mdo_type: this.mdoType,
enabled: this.enabled
};
var payload = Object.assign(basic, this.mdo.getPatchPayload());
if (this.inputUnit) {
payload['input_unit'] = this.inputUnit.slug;
}
if (this.outputUnit) {
payload['output_unit'] = this.outputUnit.slug;
}
if (this.dataLabel) {
payload['data_label'] = this.dataLabel;
}
return payload;
};
return Stream;
}());
exports.Stream = Stream;
var StreamDelta = /** @class */ (function (_super) {
__extends(StreamDelta, _super);
function StreamDelta() {
return _super !== null && _super.apply(this, arguments) || this;
}
return StreamDelta;
}(model_delta_1.ModelDelta));
exports.StreamDelta = StreamDelta;
var StreamLabelDelta = /** @class */ (function (_super) {
__extends(StreamLabelDelta, _super);
function StreamLabelDelta(oldLabel, newLabel, slug, guid) {
var _this = _super.call(this, StreamLabelDelta.ClassName, slug, guid) || this;
_this.oldLabel = oldLabel;
_this.newLabel = newLabel;
return _this;
}
StreamLabelDelta.prototype.check = function (stream) {
if (stream.dataLabel == this.newLabel) {
return model_delta_1.DeltaStatus.Outdated;
}
else if (stream.dataLabel !== this.oldLabel) {
return model_delta_1.DeltaStatus.Conflicted;
}
return model_delta_1.DeltaStatus.Applies;
};
StreamLabelDelta.prototype.apply = function (stream) {
stream.dataLabel = this.newLabel;
};
StreamLabelDelta.prototype.getPatch = function () {
return {
data_label: this.newLabel
};
};
StreamLabelDelta.prototype.serializeArguments = function () {
return {
oldLabel: this.oldLabel,
newLabel: this.newLabel
};
};
StreamLabelDelta.Deserialize = function (guid, slug, serializedArgs) {
return new StreamLabelDelta(serializedArgs.oldLabel, serializedArgs.newLabel, slug, guid);
};
StreamLabelDelta.ClassName = "StreamLabelDelta";
return StreamLabelDelta;
}(StreamDelta));
exports.StreamLabelDelta = StreamLabelDelta;
var StreamMDODelta = /** @class */ (function (_super) {
__extends(StreamMDODelta, _super);
function StreamMDODelta(oldMDO, oldType, newMDO, newType, slug, guid) {
var _this = _super.call(this, StreamMDODelta.ClassName, slug, guid) || this;
_this.oldMDO = oldMDO;
_this.oldType = oldType;
_this.newMDO = newMDO;
_this.newType = newType;
return _this;
}
StreamMDODelta.prototype.check = function (stream) {
if (stream.mdo.equal(this.newMDO) && stream.mdoType == this.newType) {
return model_delta_1.DeltaStatus.Outdated;
}
else if (!stream.mdo.equal(this.oldMDO) || stream.mdoType !== this.oldType) {
return model_delta_1.DeltaStatus.Conflicted;
}
return model_delta_1.DeltaStatus.Applies;
};
StreamMDODelta.prototype.apply = function (stream) {
stream.mdo.setFromMdo(this.newMDO);
stream.mdoType = this.newType;
};
StreamMDODelta.prototype.getPatch = function () {
return {
mdo_type: this.newType,
mdo_label: this.newMDO.label,
multiplication_factor: this.newMDO.m,
division_factor: this.newMDO.d,
offset: this.newMDO.o
};
};
StreamMDODelta.prototype.serializeArguments = function () {
return {
oldMDO: this.oldMDO.toJson(false),
oldType: this.oldType,
newMDO: this.newMDO.toJson(false),
newType: this.newType
};
};
StreamMDODelta.Deserialize = function (guid, slug, serializedArgs) {
var oldMDO = new mdo_1.Mdo(serializedArgs.oldMDO);
var oldType = serializedArgs.oldType;
var newMDO = new mdo_1.Mdo(serializedArgs.newMDO);
var newType = serializedArgs.newType;
return new StreamMDODelta(oldMDO, oldType, newMDO, newType, slug, guid);
};
StreamMDODelta.ClassName = "StreamMDODelta";
return StreamMDODelta;
}(StreamDelta));
exports.StreamMDODelta = StreamMDODelta;
var UnitType;
(function (UnitType) {
UnitType[UnitType["Input"] = 0] = "Input";
UnitType[UnitType["Output"] = 1] = "Output";
})(UnitType = exports.UnitType || (exports.UnitType = {}));
/*
* A delta representing a change in units for a stream.
* This class has a slight complication in that the old units for a stream
* may be null if they were never initialized.
*/
var StreamUnitsDelta = /** @class */ (function (_super) {
__extends(StreamUnitsDelta, _super);
function StreamUnitsDelta(oldUnits, newUnits, type, slug, classname, guid) {
var _this = _super.call(this, classname, slug, guid) || this;
_this.oldUnit = oldUnits;
_this.newUnit = newUnits;
_this.type = type;
if (_this.newUnit == null) {
throw new iotile_common_1.ArgumentError("You must always set a valid new unit in StreamUnitsDelta");
}
return _this;
}
StreamUnitsDelta.prototype.getSlug = function (unit) {
if (unit == null) {
return null;
}
return unit.slug;
};
StreamUnitsDelta.prototype.check = function (stream) {
var unitSlug = this.getSlug(stream.inputUnit);
if (this.type == UnitType.Output) {
unitSlug = this.getSlug(stream.outputUnit);
}
var oldSlug = this.getSlug(this.oldUnit);
var newSlug = this.getSlug(this.newUnit);
if (unitSlug === newSlug) {
return model_delta_1.DeltaStatus.Outdated;
}
else if (unitSlug === oldSlug) {
return model_delta_1.DeltaStatus.Applies;
}
else {
return model_delta_1.DeltaStatus.Conflicted;
}
};
/*
* If the old units are null we need to create them from scratch
* otherwise we can just update them with our new data.
*/
StreamUnitsDelta.prototype.apply = function (stream) {
if (this.type == UnitType.Input && stream.inputUnit == null) {
stream.inputUnit = new unit_1.Unit(this.newUnit.toJson());
}
else if (this.type == UnitType.Output && stream.outputUnit == null) {
stream.outputUnit = new unit_1.Unit(this.newUnit.toJson());
}
else {
var unit = stream.outputUnit;
if (this.type == UnitType.Input) {
unit = stream.inputUnit;
}
if (unit) {
unit.setFromUnit(this.newUnit);
}
}
};
StreamUnitsDelta.prototype.getPatch = function () {
var paramName = 'output_unit';
if (this.type == UnitType.Input) {
paramName = 'input_unit';
}
var patch = {};
patch[paramName] = this.newUnit.slug;
return patch;
};
StreamUnitsDelta.prototype.serializeArguments = function () {
return {
oldUnit: this.oldUnit ? this.oldUnit.toJson() : null,
newUnit: this.newUnit.toJson(),
type: this.type
};
};
return StreamUnitsDelta;
}(StreamDelta));
exports.StreamUnitsDelta = StreamUnitsDelta;
var StreamInputUnitsDelta = /** @class */ (function (_super) {
__extends(StreamInputUnitsDelta, _super);
function StreamInputUnitsDelta(oldUnits, newUnits, slug, guid) {
return _super.call(this, oldUnits, newUnits, UnitType.Input, slug, StreamInputUnitsDelta.ClassName, guid) || this;
}
StreamInputUnitsDelta.Deserialize = function (guid, slug, serializedArgs) {
var oldUnit = null;
if (serializedArgs.oldUnit) {
oldUnit = new unit_1.Unit(serializedArgs.oldUnit);
}
var newUnit = new unit_1.Unit(serializedArgs.newUnit);
return new StreamInputUnitsDelta(oldUnit, newUnit, slug, guid);
};
StreamInputUnitsDelta.ClassName = "StreamInputUnitsDelta";
return StreamInputUnitsDelta;
}(StreamUnitsDelta));
exports.StreamInputUnitsDelta = StreamInputUnitsDelta;
var StreamOutputUnitsDelta = /** @class */ (function (_super) {
__extends(StreamOutputUnitsDelta, _super);
function StreamOutputUnitsDelta(oldUnits, newUnits, slug, guid) {
return _super.call(this, oldUnits, newUnits, UnitType.Output, slug, StreamOutputUnitsDelta.ClassName, guid) || this;
}
StreamOutputUnitsDelta.Deserialize = function (guid, slug, serializedArgs) {
var oldUnit = null;
if (serializedArgs.oldUnit) {
oldUnit = new unit_1.Unit(serializedArgs.oldUnit);
}
var newUnit = new unit_1.Unit(serializedArgs.newUnit);
return new StreamOutputUnitsDelta(oldUnit, newUnit, slug, guid);
};
StreamOutputUnitsDelta.ClassName = "StreamOutputUnitsDelta";
return StreamOutputUnitsDelta;
}(StreamUnitsDelta));
exports.StreamOutputUnitsDelta = StreamOutputUnitsDelta;
//# sourceMappingURL=stream.js.map