@iotile/iotile-device
Version:
A typescript library for interfacing with IOTile BLE devices
211 lines • 8.38 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const virtual_device_1 = require("../virtual-device");
const iotile_common_1 = require("@iotile/iotile-common");
const utilities_1 = require("../utilities");
class BasicControllerTile extends virtual_device_1.VirtualTile {
constructor(iotileID, versionInfo, firmwareVersion = "2.11.4", hwTag = "btc1_v3") {
super(8, 'NRF52 ', firmwareVersion);
this.hwTag = hwTag;
this.appInfo = this.combineVersion(versionInfo.appTag, versionInfo.appVersion);
this.osInfo = this.combineVersion(versionInfo.osTag, versionInfo.osVersion);
this.iotileID = iotileID;
this.streamerAcks = {};
this.streams = {};
this.highestUniqueID = 0;
this.realtimeStreamers = [];
this.queuedReports = [];
this.prestreamingHook = null;
this.downloadStreamState = null;
this.notificationCallback = null;
}
combineVersion(tag, version) {
let parts = version.split('.');
if (parts.length != 2) {
throw new iotile_common_1.ArgumentError(`Unable to parse 2 component version string: ${version}`);
}
let major = parseInt(parts[0]);
let minor = parseInt(parts[1]);
return (major << 26) | (minor << 20) | tag;
}
isBufferedStream(stream) {
let streamType = stream >> 12;
if (streamType === 5 || streamType === 0) {
return true;
}
return false;
}
addRealtimeStreamer(stream, intervalMS) {
let state = {
intervalMS: intervalMS,
timeoutHandle: null,
stream: stream
};
this.realtimeStreamers.push(state);
if (this.isStreamingEnabled()) {
state.timeoutHandle = window.setInterval(() => this.handleRealtimeStreamer(state), intervalMS);
}
}
handleRealtimeStreamer(state) {
if (this.notificationCallback == null)
return;
if (!(state.stream in this.streams))
return;
let streamData = this.streams[state.stream];
if (streamData.length == 0)
return;
let lastValue = streamData[streamData.length - 1];
let report = utilities_1.buildIndividualReport(this.iotileID, state.stream, lastValue.value);
this.notificationCallback(report);
}
isStreamingEnabled() {
return this.notificationCallback !== null;
}
enableStreaming(callback) {
if (this.isStreamingEnabled()) {
return;
}
this.notificationCallback = callback;
for (let report of this.queuedReports) {
callback(report);
}
this.queuedReports = [];
for (let streamer of this.realtimeStreamers) {
streamer.timeoutHandle = window.setInterval(() => this.handleRealtimeStreamer(streamer), streamer.intervalMS);
}
}
disableStreaming() {
if (!this.isStreamingEnabled()) {
return;
}
this.notificationCallback = null;
for (let streamer of this.realtimeStreamers) {
if (streamer.timeoutHandle == null)
continue;
window.clearInterval(streamer.timeoutHandle);
streamer.timeoutHandle = null;
}
}
getHardwareTag() {
return [this.hwTag];
}
getDeviceInfo() {
return [this.iotileID, 0, 0, 0, 0, 0, this.osInfo, this.appInfo];
}
acknowledgeStreamer(streamer, force, value) {
let oldHighest = this.streamerAcks[streamer];
if (oldHighest == null) {
oldHighest = 0;
}
if (streamer < 0x100 && value > this.highestUniqueID) {
this.highestUniqueID = value;
}
if (value > oldHighest || force) {
this.streamerAcks[streamer] = value;
return [0];
}
return [2147713054];
}
inspectVirtualStream(stream) {
if (!(stream in this.streams)) {
return [virtual_device_1.packError(0x8002, 0x8001), 0];
}
if (this.isBufferedStream(stream)) {
return [virtual_device_1.packError(0x8002, 0x8001), 0];
}
let streamData = this.streams[stream];
if (streamData.length === 0) {
return [virtual_device_1.packError(0x8002, 0x8000), 0];
}
return [0, streamData[streamData.length - 1].value];
}
pushReading(value, stream) {
if (!(stream in this.streams))
this.streams[stream] = [];
let streamValue = {
timestamp: Math.floor(Date.now() / 1000.0),
uniqueID: 0,
value: value
};
if (this.isBufferedStream(stream)) {
streamValue.uniqueID = ++this.highestUniqueID;
}
this.streams[stream].push(streamValue);
return [0];
}
beginDownloadStream(stream) {
this.downloadStreamState = {
stream: stream,
currIndex: 0
};
let count = 0;
if (stream in this.streams) {
count = this.streams[stream].length;
}
return [0, count, Math.floor(Date.now() / 1000.0)];
}
downloadReading(stream) {
if (this.downloadStreamState == null) {
return [virtual_device_1.packError(0x8002, 0x8000), 0, 0];
}
let streamData = this.streams[this.downloadStreamState.stream];
if (this.downloadStreamState.currIndex >= streamData.length) {
return [virtual_device_1.packError(0x8002, 0x8000), 0, 0];
}
let streamValue = streamData[this.downloadStreamState.currIndex++];
return [0, streamValue.timestamp, streamValue.value];
}
}
__decorate([
virtual_device_1.tileRPC(0x0002, "", "10s"),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Object)
], BasicControllerTile.prototype, "getHardwareTag", null);
__decorate([
virtual_device_1.tileRPC(0x1008, "", "LLBBBBLL"),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Object)
], BasicControllerTile.prototype, "getDeviceInfo", null);
__decorate([
virtual_device_1.tileRPC(0x200F, "HHL", "L"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, Boolean, Number]),
__metadata("design:returntype", Object)
], BasicControllerTile.prototype, "acknowledgeStreamer", null);
__decorate([
virtual_device_1.tileRPC(0x200B, "H", "LL"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number]),
__metadata("design:returntype", void 0)
], BasicControllerTile.prototype, "inspectVirtualStream", null);
__decorate([
virtual_device_1.tileRPC(0x2000, "LH", "L"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, Number]),
__metadata("design:returntype", void 0)
], BasicControllerTile.prototype, "pushReading", null);
__decorate([
virtual_device_1.tileRPC(0x2008, "H", "LLL"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number]),
__metadata("design:returntype", void 0)
], BasicControllerTile.prototype, "beginDownloadStream", null);
__decorate([
virtual_device_1.tileRPC(0x2009, "", "LLL"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number]),
__metadata("design:returntype", void 0)
], BasicControllerTile.prototype, "downloadReading", null);
exports.BasicControllerTile = BasicControllerTile;
//# sourceMappingURL=basic-controller.js.map