@iotile/iotile-device
Version:
A typescript library for interfacing with IOTile BLE devices
126 lines • 4.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const iotile_reports_1 = require("./iotile-reports");
const msgpack_lite_1 = require("msgpack-lite");
const iotile_common_1 = require("@iotile/iotile-common");
;
class IOTileEvent {
constructor(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;
}
toObject() {
let 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
};
}
get readingID() {
return this._readingID;
}
}
exports.IOTileEvent = IOTileEvent;
;
class FlexibleDictionaryReport extends iotile_reports_1.IOTileReport {
constructor(uuid, readings, events, options) {
super();
if (options == null) {
options = FlexibleDictionaryReport.DEFAULT_OPTIONS;
}
this._lowestID = 0;
this._highestID = 0;
let 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;
this.sortAndCalculateIDRange();
}
get deviceID() {
return this._uuid;
}
get readingIDRange() {
return [this._lowestID, this._highestID];
}
get streamer() {
return this._streamerIndex;
}
get rawData() {
if (this._cachedMsgpack == null)
this._cachedMsgpack = this.toMsgpack();
return this._cachedMsgpack;
}
get numEvents() {
return this._events.length;
}
sortAndCalculateIDRange() {
this._events.sort((a, b) => a.readingID - b.readingID);
this._lowestID = 0;
this._highestID = 0;
for (let event of this._events) {
if (this._lowestID == 0 || event.readingID < this._lowestID) {
this._lowestID = event.readingID;
}
if (this._highestID == 0 || event.readingID > this._highestID) {
this._highestID = event.readingID;
}
}
}
toObject() {
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(event => event.toObject()),
data: [],
lowest_id: this._lowestID,
highest_id: this._highestID
};
}
toMsgpack() {
let obj = this.toObject();
let encoded = msgpack_lite_1.encode(obj);
return encoded.buffer.slice(0, encoded.byteLength);
}
get receivedTime() {
return this._receivedTime;
}
}
FlexibleDictionaryReport.DEFAULT_OPTIONS = {
streamer: 0x100,
selector: 0xFFFF,
reportID: 0,
sentTimestamp: 0xFFFFFFFF
};
exports.FlexibleDictionaryReport = FlexibleDictionaryReport;
//# sourceMappingURL=flexible-dict-report.js.map