UNPKG

@iotile/iotile-device

Version:

A typescript library for interfacing with IOTile BLE devices

90 lines 3.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Errors = require("../common/error-space"); const IOTileTypes = require("../common/iotile-types"); const ring_buffer_1 = require("../common/ring-buffer"); class IOTileTracingInterface { constructor() { this.removeTracingHandler = null; this.receivedData = new ring_buffer_1.RingBuffer(128, true); this.resolveWaiter = null; this.rejectWaiter = null; this.waiterTimer = null; this.waitLength = 0; this.waiterTimeout = 1000; this.channel = undefined; } async open(channel) { this.channel = channel; this.clearWaiter(); this.clearData(); this.removeTracingHandler = await this.channel.subscribe(IOTileTypes.IOTileCharacteristic.Tracing, (value) => { this.receiveTracingData(value); }); } async close() { if (this.removeTracingHandler !== null) { await this.removeTracingHandler(); this.removeTracingHandler = null; } if (this.rejectWaiter != null) { this.rejectWaiter(new Errors.ConnectionError("Tracing interface was closed.")); } this.clearWaiter(); this.clearData(); } receiveTracingData(value) { this.receivedData.push(value); if (this.resolveWaiter != null && this.waitLength && this.receivedData.count >= this.waitLength) { this.resolveWaiter(this.receivedData.pop(this.waitLength)); this.clearWaiter(); } else if (this.resolveWaiter != null) { this.startWatchdogTimer(); } } startWatchdogTimer() { if (this.rejectWaiter == null) { return; } if (this.waiterTimer != null) { clearTimeout(this.waiterTimer); } this.waiterTimer = window.setTimeout(() => { if (this.rejectWaiter != null) { this.rejectWaiter(new Errors.StreamingTimeoutError("Timeout waiting for tracing data.")); this.clearWaiter(); } }, this.waiterTimeout); } clearWaiter() { this.rejectWaiter = null; this.resolveWaiter = null; if (this.waiterTimer != null) { clearTimeout(this.waiterTimer); } this.waiterTimer = null; this.waitLength = null; } waitForData(numBytes, timeout = 1000) { if (this.resolveWaiter != null) { throw new Errors.OperationAtInvalidTimeError("You can only have one waiter waiting for tracing data at a time.", IOTileTypes.AdapterState.Connected, "Internal Tracing Error."); } return new Promise((resolve, reject) => { if (numBytes <= this.receivedData.count) { resolve(this.receivedData.pop(numBytes)); return; } this.waitLength = numBytes; this.resolveWaiter = resolve; this.rejectWaiter = reject; this.waiterTimeout = timeout; this.startWatchdogTimer(); }); } clearData() { this.receivedData.reset(); } } exports.IOTileTracingInterface = IOTileTracingInterface; //# sourceMappingURL=iotile-iface-tracing.js.map