@iotile/iotile-cloud
Version:
A typescript library for interfacing with the IOTile Cloud API
231 lines • 8.23 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 model_delta_1 = require("../base/model-delta");
var Device = /** @class */ (function () {
function Device(data) {
if (data === void 0) { data = {}; }
this.busy = false;
this.id = data.id;
this.slug = data.slug;
this.gid = data.gid;
this.label = data.label || data.slug;
this.template = data.template || '';
this.rawData = data;
this.project = data.project;
this.drifterMode = data.drifter_mode || false;
this.isModified = false;
this.active = data.active || true;
this.state = data.state || undefined;
this.externalId = data.external_id || undefined;
this.sensorGraphSlug = data.sg;
this.properties = [];
this.propertyMap = {};
if ('busy' in data) {
this.busy = data.busy;
}
if ('org' in data) {
this.org = data.org;
}
if ('lat' in data && data.lat !== null) {
this.lat = parseFloat(data.lat);
}
if ('lon' in data && data.lon !== null) {
this.lon = parseFloat(data.lon);
}
}
Device.prototype.toJson = function () {
var result = this.rawData;
result.label = this.label;
result.drifter_mode = this.drifterMode;
result.lat = this.lat;
result.lon = this.lon;
return result;
};
Device.prototype.getPatchPayload = function () {
var payload = {
label: this.label
};
if (this.lat) {
payload.lat = this.lat;
}
if (this.lon) {
payload.lon = this.lon;
}
payload.active = this.active;
if (this.state) {
payload.state = this.state;
}
return payload;
};
Device.prototype.addProperties = function (properties) {
var _this = this;
this.properties = properties;
this.properties.forEach(function (property) {
_this.propertyMap[property.name] = property;
});
};
Device.prototype.getProperty = function (name) {
return this.propertyMap[name];
};
Device.prototype.isDataBlock = function () {
return this.dataBlock != null;
};
Device.prototype.getStateDisplay = function () {
if (this.state) {
var factory = {
N0: 'Available',
N1: 'Active',
B0: 'Resetting',
B1: 'Archiving'
};
return factory[this.state];
}
else {
return '';
}
};
return Device;
}());
exports.Device = Device;
var DeviceDelta = /** @class */ (function (_super) {
__extends(DeviceDelta, _super);
function DeviceDelta() {
return _super !== null && _super.apply(this, arguments) || this;
}
return DeviceDelta;
}(model_delta_1.ModelDelta));
exports.DeviceDelta = DeviceDelta;
var DeviceLocationDelta = /** @class */ (function (_super) {
__extends(DeviceLocationDelta, _super);
function DeviceLocationDelta(oldLat, oldLng, newLat, newLng, slug, guid) {
var _this = _super.call(this, DeviceLocationDelta.ClassName, slug, guid) || this;
_this.oldLat = oldLat;
_this.oldLng = oldLng;
_this.newLat = newLat;
_this.newLng = newLng;
return _this;
}
DeviceLocationDelta.prototype.check = function (device) {
if (device.lat === this.newLat && device.lon === this.newLng) {
return model_delta_1.DeltaStatus.Outdated;
}
else if ((device.lat === this.oldLat && device.lon === this.oldLng) ||
(isNaN(device.lat) &&
isNaN(this.oldLat) &&
isNaN(device.lon) &&
isNaN(this.oldLng))) {
return model_delta_1.DeltaStatus.Applies;
}
else {
return model_delta_1.DeltaStatus.Conflicted;
}
};
DeviceLocationDelta.prototype.apply = function (device) {
device.lat = this.newLat;
device.lon = this.newLng;
};
DeviceLocationDelta.prototype.getPatch = function () {
return {
lat: this.newLat,
lon: this.newLng
};
};
DeviceLocationDelta.prototype.serializeArguments = function () {
return {
oldLat: this.oldLat,
oldLng: this.oldLng,
newLat: this.newLat,
newLng: this.newLng
};
};
DeviceLocationDelta.Deserialize = function (guid, slug, serializedArgs) {
return new DeviceLocationDelta(serializedArgs.oldLat, serializedArgs.oldLng, serializedArgs.newLat, serializedArgs.newLng, slug, guid);
};
DeviceLocationDelta.ClassName = 'DeviceLocationDelta';
return DeviceLocationDelta;
}(DeviceDelta));
exports.DeviceLocationDelta = DeviceLocationDelta;
var DeviceDrifterDelta = /** @class */ (function (_super) {
__extends(DeviceDrifterDelta, _super);
function DeviceDrifterDelta(oldDrifter, newDrifter, slug, guid) {
var _this = _super.call(this, DeviceDrifterDelta.ClassName, slug, guid) || this;
_this.oldDrifter = oldDrifter;
_this.newDrifter = newDrifter;
return _this;
}
DeviceDrifterDelta.prototype.check = function (device) {
if (device.drifterMode === this.newDrifter) {
return model_delta_1.DeltaStatus.Outdated;
}
else {
return model_delta_1.DeltaStatus.Applies;
}
};
DeviceDrifterDelta.prototype.apply = function (device) {
device.drifterMode = this.newDrifter;
};
DeviceDrifterDelta.prototype.getPatch = function () {
return {};
};
DeviceDrifterDelta.prototype.serializeArguments = function () {
return {
oldDrifter: this.oldDrifter,
newDrifter: this.newDrifter
};
};
DeviceDrifterDelta.Deserialize = function (guid, slug, serializedArgs) {
return new DeviceDrifterDelta(serializedArgs.oldDrifter, serializedArgs.newDrifter, slug, guid);
};
DeviceDrifterDelta.ClassName = 'DeviceDrifterDelta';
return DeviceDrifterDelta;
}(DeviceDelta));
exports.DeviceDrifterDelta = DeviceDrifterDelta;
var DeviceLabelDelta = /** @class */ (function (_super) {
__extends(DeviceLabelDelta, _super);
function DeviceLabelDelta(oldLabel, newLabel, slug, guid) {
var _this = _super.call(this, DeviceLabelDelta.ClassName, slug, guid) || this;
_this.oldLabel = oldLabel;
_this.newLabel = newLabel;
return _this;
}
DeviceLabelDelta.prototype.check = function (device) {
if (device.label == this.newLabel) {
return model_delta_1.DeltaStatus.Outdated;
}
else if (device.label !== this.oldLabel) {
return model_delta_1.DeltaStatus.Conflicted;
}
return model_delta_1.DeltaStatus.Applies;
};
DeviceLabelDelta.prototype.apply = function (device) {
device.label = this.newLabel;
};
DeviceLabelDelta.prototype.getPatch = function () {
return {
label: this.newLabel
};
};
DeviceLabelDelta.prototype.serializeArguments = function () {
return {
oldLabel: this.oldLabel,
newLabel: this.newLabel
};
};
DeviceLabelDelta.Deserialize = function (guid, slug, serializedArgs) {
return new DeviceLabelDelta(serializedArgs.oldLabel, serializedArgs.newLabel, slug, guid);
};
DeviceLabelDelta.ClassName = 'DeviceLabelDelta';
return DeviceLabelDelta;
}(DeviceDelta));
exports.DeviceLabelDelta = DeviceLabelDelta;
//# sourceMappingURL=device.js.map