UNPKG

@iotile/iotile-device

Version:

A typescript library for interfacing with IOTile BLE devices

185 lines 7.01 kB
"use strict"; /** * A dictionary based report that can either be saved as JSON or msgpack. * * This is suitable for uploading complex event data on behalf of an IOTile device. */ 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 iotile_reports_1 = require("./iotile-reports"); var msgpack_lite_1 = require("msgpack-lite"); var iotile_common_1 = require("@iotile/iotile-common"); ; var IOTileEvent = /** @class */ (function () { function IOTileEvent(stream, deviceTimestamp, summaryData, rawData, readingID, utcTimestamp) { this._stream = stream; this._deviceTimestamp = deviceTimestamp; this._summaryData = summaryData; this._rawData = rawData; if (readingID == null) { readingID = 0; } if (utcTimestamp == null) utcTimestamp = null; this._readingID = readingID; this._utcTimestamp = utcTimestamp; } IOTileEvent.prototype.toObject = function () { var serializedTimestamp = null; if (this._utcTimestamp != null) serializedTimestamp = this._utcTimestamp.toISOString(); return { stream: this._stream, device_timestamp: this._deviceTimestamp, streamer_local_id: this._readingID, timestamp: serializedTimestamp, data: this._rawData, extra_data: this._summaryData }; }; Object.defineProperty(IOTileEvent.prototype, "readingID", { get: function () { return this._readingID; }, enumerable: true, configurable: true }); return IOTileEvent; }()); exports.IOTileEvent = IOTileEvent; ; var FlexibleDictionaryReport = /** @class */ (function (_super) { __extends(FlexibleDictionaryReport, _super); function FlexibleDictionaryReport(uuid, readings, events, options) { var _this = _super.call(this) || this; if (options == null) { options = FlexibleDictionaryReport.DEFAULT_OPTIONS; } _this._lowestID = 0; _this._highestID = 0; var receivedTime = options.receivedTime; if (receivedTime == null) { receivedTime = new Date(); } _this._receivedTime = receivedTime; _this._uuid = uuid; _this._reportID = options.reportID; _this._streamerIndex = options.streamer; _this._streamerSelector = options.selector; _this._sentTimestamp = options.sentTimestamp; _this._cachedMsgpack = null; if (readings.length > 0) { throw new iotile_common_1.ArgumentError("Passing readings to a FlexibleDictionaryReport is not yet supported"); } _this._readings = readings; _this._events = events; /** * The cloud expects that all of our events will be sorted by readingID so make sure * they are properly sorted and keep track of lowest and highest. */ _this.sortAndCalculateIDRange(); return _this; } Object.defineProperty(FlexibleDictionaryReport.prototype, "deviceID", { get: function () { return this._uuid; }, enumerable: true, configurable: true }); Object.defineProperty(FlexibleDictionaryReport.prototype, "readingIDRange", { get: function () { return [this._lowestID, this._highestID]; }, enumerable: true, configurable: true }); Object.defineProperty(FlexibleDictionaryReport.prototype, "streamer", { get: function () { return this._streamerIndex; }, enumerable: true, configurable: true }); Object.defineProperty(FlexibleDictionaryReport.prototype, "rawData", { get: function () { if (this._cachedMsgpack == null) this._cachedMsgpack = this.toMsgpack(); return this._cachedMsgpack; }, enumerable: true, configurable: true }); Object.defineProperty(FlexibleDictionaryReport.prototype, "numEvents", { get: function () { return this._events.length; }, enumerable: true, configurable: true }); FlexibleDictionaryReport.prototype.sortAndCalculateIDRange = function () { this._events.sort(function (a, b) { return a.readingID - b.readingID; }); this._lowestID = 0; this._highestID = 0; for (var _i = 0, _a = this._events; _i < _a.length; _i++) { var event_1 = _a[_i]; if (this._lowestID == 0 || event_1.readingID < this._lowestID) { this._lowestID = event_1.readingID; } if (this._highestID == 0 || event_1.readingID > this._highestID) { this._highestID = event_1.readingID; } } }; FlexibleDictionaryReport.prototype.toObject = function () { return { format: "v100", device: this._uuid, streamer_index: this._streamerIndex, streamer_selector: this._streamerSelector, incremental_id: this._reportID, device_sent_timestamp: this._sentTimestamp, events: this._events.map(function (event) { return event.toObject(); }), data: [], lowest_id: this._lowestID, highest_id: this._highestID }; }; FlexibleDictionaryReport.prototype.toMsgpack = function () { var obj = this.toObject(); var encoded = msgpack_lite_1.encode(obj); /* * The raw data returned by encode() is a Uint8Array. This has * a length that can be smaller than the underlying ArrayBuffer * that is overallocated to minimize reallocations. When we * return the underlying ArrayBuffer, we need to make sure to * truncate it to the same size as the Uint8Array. */ return encoded.buffer.slice(0, encoded.byteLength); }; Object.defineProperty(FlexibleDictionaryReport.prototype, "receivedTime", { get: function () { return this._receivedTime; }, enumerable: true, configurable: true }); FlexibleDictionaryReport.DEFAULT_OPTIONS = { streamer: 0x100, selector: 0xFFFF, reportID: 0, sentTimestamp: 0xFFFFFFFF }; return FlexibleDictionaryReport; }(iotile_reports_1.IOTileReport)); exports.FlexibleDictionaryReport = FlexibleDictionaryReport; //# sourceMappingURL=flexible-dict-report.js.map