@iotile/iotile-cloud
Version:
A typescript library for interfacing with the IOTile Cloud API
385 lines • 14.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var variable_1 = require("./variable");
var stream_1 = require("./stream");
var device_1 = require("./device");
var sensorgraph_1 = require("./sensorgraph");
var project_overlay_1 = require("../cloud/project-overlay");
var iotile_common_1 = require("@iotile/iotile-common");
var Project = /** @class */ (function () {
function Project(data) {
if (data === void 0) { data = {}; }
this.id = data.id;
this.gid = data.gid;
this.name = data.name;
this.orgSlug = data.org;
this.template = data.project_template;
this.slug = data.slug;
this.rawData = data;
this.devices = [];
this.variables = [];
this.streams = [];
this.sensorGraphs = [];
this.deviceMap = {};
this.variableMap = {};
this.projTemplateMap = {};
this.streamMap = {};
this.sensorGraphMap = {};
this.overlay = new project_overlay_1.ProjectOverlay();
this.properties = [];
this.createdBy = data.created_by;
this.createdOn = new Date(data.created_on);
if ('project_template' in data) {
this.projectTemplate = data.project_template;
}
if (data.counts) {
this.counts = data.counts;
}
else {
this.counts = {};
}
this.deviceMap = {};
this.streamMap = {};
this.variableMap = {};
this.propertyMap = {};
}
Project.prototype.toJson = function () {
return this.rawData;
};
Project.prototype.serialize = function () {
var proj = this.toJson();
//Now add in any added linked objects
proj.devices = this.devices.map(function (dev) { return dev.toJson(); });
proj.variables = this.variables.map(function (variable) { return variable.toJson(); });
proj.streams = this.streams.map(function (stream) { return stream.toJson(); });
proj.sensorGraphs = this.sensorGraphs.map(function (sg) { return sg.toJson(); });
return proj;
};
Project.Unserialize = function (obj) {
//Extract the org first since the slug is used above
var org = obj['org'];
obj['orgSlug'] = org.slug;
var proj = new Project(obj);
if (obj['devices'] && obj['devices'].length > 0) {
proj.addDevices(obj['devices'].map(function (data) { return new device_1.Device(data); }));
}
if (obj['variables'] && obj['variables'].length > 0) {
proj.addVariables(obj['variables'].map(function (variable) { return new variable_1.Variable(variable); }));
}
if (obj['streams'] && obj['streams'].length > 0) {
proj.addStreams(obj['streams'].map(function (stream) { return new stream_1.Stream(stream); }));
}
if (obj['sensorGraphs'] && obj['sensorGraphs'].length > 0) {
proj.addSensorGraphs(obj['sensorGraphs'].map(function (sg) { return new sensorgraph_1.SensorGraph(sg); }));
}
return proj;
};
Project.prototype.addDevices = function (devices) {
var _this = this;
this.devices = devices;
this.deviceMap = {};
this.devices.forEach(function (d) {
_this.deviceMap[d.slug] = d;
});
};
Project.prototype.addStreams = function (streams) {
var _this = this;
this.streams = streams;
this.streamMap = {};
this.streams.forEach(function (s) {
_this.streamMap[s.slug] = s;
});
};
Project.prototype.removeDevice = function (slug) {
var device = this.deviceMap[slug];
delete this.deviceMap[slug];
this.devices = this.devices.filter(function (d) { return d.slug !== slug; });
return device;
};
Project.prototype.addStream = function (stream) {
this.streams.push(stream);
this.streamMap[stream.slug] = stream;
};
Project.prototype.addSensorGraphs = function (sg) {
var _this = this;
this.sensorGraphs = sg;
this.sensorGraphs.forEach(function (sg) {
_this.sensorGraphMap[sg.slug] = sg;
});
};
Project.prototype.addSensorGraph = function (sg) {
this.sensorGraphs.push(sg);
this.sensorGraphMap[sg.slug] = sg;
};
Project.prototype.getSensorGraph = function (slug) {
return this.sensorGraphMap[slug];
};
Project.prototype.addProjectTemplates = function (templates) {
var _this = this;
this.projTemplateMap = {};
templates.forEach(function (pt) {
_this.projTemplateMap[pt.slug] = pt;
});
};
Project.prototype.getProjectTemplate = function () {
if (this.projTemplateMap[this.template]) {
return this.projTemplateMap[this.template];
}
else {
throw new iotile_common_1.ArgumentError("Can't find Project Template for slug " + this.template);
}
};
Project.prototype.addVariables = function (variables) {
var _this = this;
this.variables = variables;
this.variableMap = {};
this.variables.forEach(function (v) {
_this.variableMap[v.slug] = v;
});
};
Project.prototype.addVariable = function (variable) {
this.variables.push(variable);
this.variableMap[variable.slug] = variable;
};
Project.prototype.getVariable = function (slug) {
return this.variableMap[slug];
};
Project.prototype.getVariableForStream = function (slug) {
if (slug) {
var elements = slug.split("--");
if (elements.length === 4) {
var varSlug = "v--" + elements[1] + "--" + elements[3];
return this.getVariable(varSlug);
}
}
return;
};
Project.prototype.getDevice = function (slug, unmodified) {
var device = this.deviceMap[slug];
if (!device) {
return null;
}
device = new device_1.Device(device.toJson());
if (unmodified || !this.overlay) {
return device;
}
this.overlay.applyDevice(device, true);
return device;
};
Project.prototype.deviceModified = function (slug) {
return this.overlay.deviceModified(slug);
};
Project.prototype.hasDevice = function (slug) {
return (slug in this.deviceMap);
};
Project.prototype.getStream = function (slug, unmodified) {
var stream = this.streamMap[slug];
if (!stream) {
return null;
}
stream = new stream_1.Stream(stream.toJson());
if (unmodified || !this.overlay) {
return stream;
}
this.overlay.applyStream(stream, true);
return stream;
};
Project.prototype.streamsForDevice = function (deviceSlug, filterByIOInfo) {
if (filterByIOInfo === void 0) { filterByIOInfo = false; }
var streams = {};
if (filterByIOInfo) {
var device = this.getDevice(deviceSlug);
if (device) {
var sg = this.getSensorGraph(device.sensorGraphSlug);
var lids = sg.getStreamLids();
var streamIDBase = ['s', this.gid, device.gid].join('--');
for (var _i = 0, lids_1 = lids; _i < lids_1.length; _i++) {
var lid = lids_1[_i];
var streamSlug = streamIDBase + '--' + lid;
var stream = this.getStream(streamSlug);
if (stream == null) {
throw new iotile_common_1.ArgumentError("Could not find stream specified in sensorgraph ioInfo: " + streamSlug);
}
streams[streamSlug] = stream;
}
}
}
else {
for (var _a = 0, _b = this.streams; _a < _b.length; _a++) {
var stream = _b[_a];
if (stream.device == deviceSlug) {
var stream_obj = this.getStream(stream.slug);
if (stream_obj) {
streams[stream_obj.slug];
}
}
}
}
return streams;
};
Project.prototype.getSensorGraphSlugs = function () {
return this.devices.map(function (device) { return device.sensorGraphSlug; });
};
/*
* Apply an overlay permanently to the devices and streams in this project.
* This is distinct from updating our separate overlay object that is just
* used on demand when streams and devices are requested rather than flattened
* into the objects themselves. This function is useful as an optimization for
* simulating the effects of pulling cloud data after the contents of this overlay
* were successfully patched to iotile.cloud.
*
* We do not mark the device and stream modified here since this function is used
* to reproduce synced data from the cloud, so the device is not modified with
* respect to what is stored in the cloud.
*/
Project.prototype.applyOverlay = function (overlay) {
for (var _i = 0, _a = this.devices; _i < _a.length; _i++) {
var device = _a[_i];
overlay.applyDevice(device, false);
}
for (var _b = 0, _c = this.streams; _b < _c.length; _b++) {
var stream = _c[_b];
overlay.applyStream(stream, false);
}
//If these changes cause any of our internal overlay deltas to not longer apply, remove them
this.overlay.prune(this);
};
Project.prototype.addProperties = function (properties) {
var _this = this;
this.properties = properties;
this.propertyMap = {};
this.properties.forEach(function (property) {
_this.propertyMap[property.name] = property;
});
};
Project.prototype.getProperty = function (name) {
return this.propertyMap[name];
};
Project.prototype.computeInputValue = function (stream, rawValue, mdo) {
var result;
var varSlug = stream.variable;
var varObj = this.variableMap[varSlug];
// 1.- Compute Factor
if (!mdo) {
mdo = stream.mdo || varObj.mdo;
}
result = mdo.computeValue(rawValue);
// 2.- Modify to internal storage units
var inputUnit = stream.inputUnit;
if (!inputUnit && varObj) {
inputUnit = varObj.inputUnit;
}
if (inputUnit && inputUnit.mdo) {
result = inputUnit.mdo.computeValue(result);
}
return result;
};
Project.prototype.getInputUnits = function (stream) {
if (stream.inputUnit) {
return stream.inputUnit;
}
var variable = this.variableMap[stream.variable];
if (!variable) {
return null;
}
return variable.inputUnit;
};
Project.prototype.getOutputUnits = function (stream) {
if (stream.outputUnit) {
return stream.outputUnit;
}
var variable = this.variableMap[stream.variable];
if (!variable) {
return null;
}
return variable.outputUnit;
};
Project.prototype.computeValue = function (stream, data) {
var result = undefined;
var mdo;
if (data.value) {
if (!stream) {
return result;
}
if (stream.outputUnit) {
mdo = stream.outputUnit.mdo;
if (mdo) {
result = mdo.computeValue(data.value);
}
}
else {
result = data.outValue ? data.outValue : data.value;
}
return result;
}
};
Project.prototype.computeOutputValue = function (stream, value) {
var result;
var varSlug = stream.variable;
var varObj = this.variableMap[varSlug];
// Modify from internal storage units
var outputUnit = stream.outputUnit;
if (!outputUnit && varObj) {
outputUnit = varObj.outputUnit;
}
if (outputUnit && outputUnit.mdo) {
result = outputUnit.mdo.computeValue(value);
}
else {
result = value;
}
return result;
};
/**
*
* @description
* Given a DataPoint with a .rawValue, compute
* value using the stream.mdo and stream.inputUnit
* Then compute outValue using the stream.outputUnit
* Finally, DisplayValue represents the string version of outValue
*
* @param {DataPoint} dataPoint should have rawValue defined
* @param {Stream} stream is the stream to use to get input/output units
*
* @returns {DataPoint} Modified dataPoint
*/
Project.prototype.processDataPoint = function (stream, dataPoint) {
if (dataPoint.rawValue) {
dataPoint.value = this.computeInputValue(stream, dataPoint.rawValue, undefined);
}
if (dataPoint.value) {
dataPoint.outValue = this.computeOutputValue(stream, dataPoint.value);
}
// Figure out units for displayValue field
var outUnit = stream.outputUnit;
var variable;
if (!outUnit) {
// If stream has no OutputUnit, default to the Variable one.
variable = this.getVariable(stream.variable);
if (variable) {
outUnit = variable.outputUnit;
}
}
if (dataPoint.outValue && dataPoint.value) {
if (outUnit) {
dataPoint.displayValue = dataPoint.outValue.toFixed(outUnit.decimalPlaces);
}
else {
if (variable) {
dataPoint.displayValue = dataPoint.outValue.toFixed(variable.decimalPlaces);
}
else {
dataPoint.displayValue = dataPoint.value.toString();
}
}
return dataPoint;
}
else {
throw new iotile_common_1.InvalidDataError('Error processing data point', 'Data point is missing value');
}
};
;
return Project;
}());
exports.Project = Project;
//# sourceMappingURL=project.js.map