UNPKG

@iotize/tap

Version:

IoTize Device client for Javascript

158 lines (154 loc) 5.74 kB
import { sleep } from '@iotize/common/utility'; import { ConnectionState } from '@iotize/tap/protocol/api'; import { BleConfig, BleComError } from '@iotize/tap/protocol/ble/common'; import { BehaviorSubject, Subject } from 'rxjs'; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; class MockPeripheralAdapter { constructor(options = { state: 'connected', connectDelay: 100, disconnectDelay: 100, name: 'MockPeripheral', id: 'MockPeripheral', }) { this.options = options; this.services = {}; this.stateChange = new BehaviorSubject(this.options.state === 'connected' ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED); this.services[BleConfig.services.lwm2m.service] = new MockServiceAdapter(BleConfig.services.lwm2m.service, [ new MockCharacteristicAdapter(BleConfig.services.lwm2m.charac, { write: true, read: true, authenticatedSignedWrites: false, broadcast: false, indicate: true, notify: true, reliableWrite: false, writableAuxiliaries: false, writeWithoutResponse: true, }), ]); this.services[BleConfig.services.fastLwm2m.service] = new MockServiceAdapter(BleConfig.services.fastLwm2m.service, [ new MockCharacteristicAdapter(BleConfig.services.fastLwm2m.charac, { write: true, read: true, authenticatedSignedWrites: false, broadcast: false, indicate: true, notify: true, reliableWrite: false, writableAuxiliaries: false, writeWithoutResponse: true, }), ]); this.stateChange.subscribe((newState) => { if (newState === ConnectionState.CONNECTED) { this.options.state === 'connected'; } else { this.options.state = 'disconnected'; } }); } get id() { return this.options.id; } get name() { return this.options.name; } get state() { return this.options.state; } connect() { return __awaiter(this, void 0, void 0, function* () { yield sleep(this.options.connectDelay); this.stateChange.next(ConnectionState.CONNECTED); }); } disconnect() { return __awaiter(this, void 0, void 0, function* () { yield sleep(this.options.disconnectDelay); this.stateChange.next(ConnectionState.DISCONNECTED); }); } discoverServices() { return __awaiter(this, void 0, void 0, function* () { return this.services; }); } getService(uuid) { return __awaiter(this, void 0, void 0, function* () { if (!this.services[uuid]) { throw BleComError.serviceNotFound(uuid); } return this.services[uuid]; }); } close() { this.stateChange.complete(); } } class MockServiceAdapter { constructor(uuid, characteristics = []) { this.uuid = uuid; this.characteristics = characteristics; } getCharacteristics() { return __awaiter(this, void 0, void 0, function* () { return this.characteristics; }); } getCharacteristic(charcUUID) { return __awaiter(this, void 0, void 0, function* () { const charac = this.characteristics.find((c) => c.uuid === charcUUID); if (!charac) { throw BleComError.charcacteristicNotFound(charcUUID); } return charac; }); } } class MockCharacteristicAdapter { constructor(uuid, properties, descriptors = []) { this.uuid = uuid; this.properties = properties; this.descriptors = descriptors; this.notificationEnabled = false; this.data = new Subject(); } write(data, writeWithoutResponse) { return __awaiter(this, void 0, void 0, function* () { throw new Error('Method not implemented.'); }); } read() { return __awaiter(this, void 0, void 0, function* () { throw new Error('Method not implemented.'); }); } enableNotifications(enabled) { return __awaiter(this, void 0, void 0, function* () { this.notificationEnabled = enabled; }); } getDescriptors() { return __awaiter(this, void 0, void 0, function* () { return this.descriptors; }); } } /** * Generated bundle index. Do not edit. */ export { MockCharacteristicAdapter, MockPeripheralAdapter, MockServiceAdapter }; //# sourceMappingURL=iotize-tap-protocol-ble-testing.js.map