UNPKG

@iotile/iotile-device

Version:

A typescript library for interfacing with IOTile BLE devices

153 lines 6.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Devices = require("./devices"); const config_1 = require("../config"); class MockBleService { constructor(Config) { this.devices = []; this.Config = Config; for (var devID in Config.BLE.MOCK_BLE_DEVICES) { let knownDevices = { 'nfc300': Devices.NFC300, 'soilmoisture': Devices.SoilMoisturePOD, 'temperature': Devices.TemperaturePOD, 'stream_test': Devices.StreamTestDevice }; let constructor = knownDevices[Config.BLE.MOCK_BLE_DEVICES[devID].type]; let device = new constructor(devID, Config.BLE.MOCK_BLE_DEVICES[devID].args); this.devices.push(device); } config_1.catMockBLE.debug('Created ' + this.devices.length + ' fake devices'); this.scanInfo = { 'inProgress': false, 'discoveryTimer': null }; this.connectionInfo = { 'inProgress': false, 'connected': false, 'device': null }; } isEnabled(yesCallback, noCallback) { setTimeout(yesCallback, 0); } startScan(services, success, failure) { var i = 0; if (this.scanInfo.inProgress) { config_1.catMockBLE.error('startScan called twice without stopScan being called in between', Error); setTimeout(failure, 10); return; } this.scanInfo.inProgress = true; var that = this; function discoverFunction() { that.scanInfo.discoveryTimer = null; if (!that.scanInfo.inProgress) { config_1.catMockBLE.error('discover callback triggered outside of scanning period. This is probably a race condition.', Error); return; } for (i = 0; i < that.devices.length; ++i) { success(that.devices[i]); } } this.scanInfo.discoveryTimer = setTimeout(discoverFunction, this.Config.BLE.MOCK_BLE_SCAN_DELAY); } stopScan(success, failure) { if (!this.scanInfo.inProgress) { config_1.catMockBLE.error('stopScan called without startScan first being called.', Error); return; } if (this.scanInfo.discoveryTimer !== null) { clearTimeout(this.scanInfo.discoveryTimer); this.scanInfo.discoveryTimer = null; } this.scanInfo.inProgress = false; if (success) { setTimeout(success, 0); } } connect(deviceID, success, failure) { if (this.connectionInfo.connected || this.connectionInfo.inProgress) { config_1.catMockBLE.error('Connect called twice', Error); setTimeout(function () { failure('Connect called twice to id: ' + deviceID); }, 20); return; } for (var i = 0; i < this.devices.length; ++i) { if (this.devices[i].device.iotileID === deviceID) { if (this.devices[i].connected) { setTimeout(function () { failure('Connect called on device with other connected: ' + deviceID); }, 20); } else { var that = this; setTimeout(function () { that.finishConnection(success); }, that.Config.BLE.MOCK_BLE_SCAN_DELAY); this.connectionInfo.inProgress = true; this.connectionInfo.device = this.devices[i]; this.connectionInfo.disconnected = failure; } break; } } if (!this.connectionInfo.inProgress) { setTimeout(function () { failure('Could not find device by id: ' + deviceID); }, 20); } } finishConnection(callback) { if (!this.connectionInfo.inProgress) { config_1.catMockBLE.error('finishConnection called in invalid state.', Error); } this.connectionInfo.inProgress = false; this.connectionInfo.connected = true; callback(this.connectionInfo.device); } isConnected(deviceID, success, failure) { if (this.connectionInfo.connected && this.connectionInfo.device.id === deviceID) { setTimeout(success, 0); } else { setTimeout(failure, 0); } } disconnect(deviceID, success, failure) { if (this.connectionInfo.connected && this.connectionInfo.device.id === deviceID) { this.connectionInfo.device.disconnect(success); this.connectionInfo.connected = false; this.connectionInfo.device = null; } else { setTimeout(failure, 0); } } force_disconnect() { if (this.connectionInfo.connected) { this.connectionInfo.disconnected("Unexpected disconnection"); this.disconnect(this.connectionInfo.device.id, function () { }, function () { }); } } write(deviceID, serviceID, charID, value, success, failure) { if ((!this.connectionInfo.connected) || this.connectionInfo.device.id !== deviceID) { setTimeout(failure, 10); return; } this.connectionInfo.device.write(serviceID, charID, value, success, failure); } writeWithoutResponse(deviceID, serviceID, charID, value, success, failure) { if ((!this.connectionInfo.connected) || this.connectionInfo.device.id !== deviceID) { setTimeout(failure, 10); return; } this.connectionInfo.device.write(serviceID, charID, value, success, failure); } startNotification(deviceID, serviceID, charID, success, failure) { if ((!this.connectionInfo.connected) || this.connectionInfo.device.id !== deviceID) { setTimeout(failure, 10); return; } this.connectionInfo.device.subscribe(serviceID, charID, success, failure); } stopNotification(deviceID, serviceID, charID, success, failure) { if ((!this.connectionInfo.connected) || this.connectionInfo.device.id !== deviceID) { setTimeout(failure, 10); return; } this.connectionInfo.device.unsubscribe(serviceID, charID, success, failure); } } exports.MockBleService = MockBleService; //# sourceMappingURL=mock-ble-serv.js.map