UNPKG

@iotile/iotile-device

Version:

A typescript library for interfacing with IOTile BLE devices

97 lines 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = require("../config"); const IOTileTypes = require("../common/iotile-types"); const iotile_report_parser_1 = require("./iotile-report-parser"); const iotile_reports_1 = require("../common/iotile-reports"); class IOTileStreamingInterface { constructor(bufferSize, expand = false) { this.removeStreamingHandler = null; this.reportParser = new iotile_report_parser_1.ReportParser(bufferSize, expand); } async open(channel) { this.channel = channel; this.reportParser.reset(); this.removeStreamingHandler = await this.channel.subscribe(IOTileTypes.IOTileCharacteristic.Streaming, (value) => { this.receiveStreamingData(value); }); } async close() { if (this.removeStreamingHandler !== null) { await this.removeStreamingHandler(); this.removeStreamingHandler = null; this.reportParser.reset(); } } stop() { this.reportParser.stop(); } reset() { this.reportParser.reset(); } receiveStreamingData(value) { if (this.channel == null) { return; } try { let reports = this.reportParser.pushData(value); let event = this.reportParser.popLastEvent(); if (event !== null) { switch (event.name) { case 'ReportStartedEvent': this.channel.notify(IOTileTypes.AdapterEvent.RobustReportStarted, event); break; case 'ReportStalledEvent': this.channel.notify(IOTileTypes.AdapterEvent.RobustReportStalled, event); break; case 'ReportInvalidEvent': this.channel.notify(IOTileTypes.AdapterEvent.RobustReportInvalid, event); break; case 'ReportProgressEvent': this.channel.notify(IOTileTypes.AdapterEvent.RobustReportProgress, event); break; case 'ReportFinishedEvent': this.channel.notify(IOTileTypes.AdapterEvent.RobustReportFinished, event); break; } } for (let i = 0; i < reports.length; ++i) { let report = reports[i]; if (report instanceof iotile_reports_1.IndividualReport) { try { this.channel.notify(IOTileTypes.AdapterEvent.RawRealtimeReading, report); } catch (err) { config_1.catStreaming.error("Error notifying a callback for an individual report", err); } } else if (report instanceof iotile_reports_1.SignedListReport) { try { this.channel.notify(IOTileTypes.AdapterEvent.RawRobustReport, report); } catch (err) { config_1.catStreaming.error("Error notifying a callback for a robust report", err); } } else { config_1.catStreaming.warn('Unknown report type received from ReportParser, ignoring it. Type: ' + report.constructor.name); } } } catch (err) { if (err.name === 'ReportParsingError' || err.name === 'InsufficientSpaceError') { this.channel.notify(IOTileTypes.AdapterEvent.UnrecoverableStreamingError, err); } else if (err.name === 'ReportParsingStoppedError') { } else { config_1.catStreaming.error("Unknown error processing a streamed report", err); } } } } exports.IOTileStreamingInterface = IOTileStreamingInterface; function buf2hex(buffer) { return Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join(''); } //# sourceMappingURL=iotile-iface-streaming.js.map