@iotize/device-com-ble.cordova
Version:
Bluetooth Low Energy (BLE) for IoTize modules Plugin
214 lines • 8.59 kB
JavaScript
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());
});
};
import { ConnectionState } from "@iotize/tap/protocol/api";
import { BleComError, } from "@iotize/tap/protocol/ble/common";
import { BehaviorSubject } from "rxjs";
import { distinctUntilChanged, filter, first, map, shareReplay, } from "rxjs/operators";
import { getIoTizeBleCordovaPlugin } from "./utility";
export class CordovaPeripheralAdapter {
constructor(deviceId, cordovaPlugin = getIoTizeBleCordovaPlugin()) {
this.deviceId = deviceId;
this.cordovaPlugin = cordovaPlugin;
this._stateChange = new BehaviorSubject(ConnectionState.DISCONNECTED);
}
get stateChange() {
return this._stateChange.asObservable().pipe(distinctUntilChanged());
}
get name() {
// TODO device name ?
return this.id;
}
get id() {
return this.deviceId;
}
get state() {
return ConnectionState[this._stateChange.value];
}
discoverServices(serviceUUIDs) {
return __awaiter(this, void 0, void 0, function* () {
this.serviceListCache = yield this.cordovaPlugin.discoverServices(this.deviceId);
const result = {};
if (serviceUUIDs) {
for (const uuid of serviceUUIDs) {
const servicDefinition = this.serviceListCache.find((def) => def.uuid === uuid);
if (servicDefinition) {
result[uuid] = new CordovaServiceAdapter(servicDefinition, this);
}
}
}
else {
for (const serviceDefinition of this.serviceListCache) {
result[serviceDefinition.uuid] = new CordovaServiceAdapter(serviceDefinition, this);
}
}
return result;
});
}
connect() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
(_a = this.connectionStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
const connectObservable = this.cordovaPlugin
.connect(this.deviceId)
.pipe(shareReplay());
this.connectionStateSubscription = connectObservable.subscribe((state) => {
this._stateChange.next(state);
});
yield connectObservable
.pipe(filter((state) => state === ConnectionState.CONNECTED), first())
.toPromise();
});
}
disconnect() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
yield this.cordovaPlugin.disConnect(this.deviceId);
this._stateChange.next(ConnectionState.DISCONNECTED);
(_a = this.connectionStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
});
}
close() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
yield this.cordovaPlugin.close(this.deviceId);
this._stateChange.next(ConnectionState.DISCONNECTED);
(_a = this.connectionStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
});
}
getService(uuid) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.serviceListCache) {
yield this.discoverServices([uuid]);
}
const serviceDefinition = this.serviceListCache.find((s) => s.uuid === uuid);
if (!serviceDefinition) {
throw BleComError.serviceNotFound(uuid);
}
return new CordovaServiceAdapter(serviceDefinition, this);
});
}
}
export class CordovaServiceAdapter {
constructor(config, peripheral) {
this.config = config;
this.peripheral = peripheral;
}
get uuid() {
return this.config.uuid;
}
getCharacteristic(charcUUID) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const characteristicDescription = (_a = this.config.characteristics) === null || _a === void 0 ? void 0 : _a.find((c) => c.uuid === charcUUID);
if (!characteristicDescription) {
throw BleComError.charcacteristicNotFound(charcUUID);
}
return new CordovaCharacteristicAdapter(this, characteristicDescription);
});
}
getCharacteristics() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.config.characteristics) {
return [];
}
return this.config.characteristics.map((c) => new CordovaCharacteristicAdapter(this, c));
});
}
}
export class CordovaCharacteristicAdapter {
constructor(service, config) {
this.service = service;
this.config = config;
}
get uuid() {
return this.config.uuid;
}
get properties() {
return this.config.properties;
}
get data() {
return this.setupDataStreamIfRequired().pipe(map((data) => ({
data,
isNotification: true,
})));
}
get deviceId() {
return this.service.peripheral.deviceId;
}
get pluginInterface() {
return this.service.peripheral.cordovaPlugin;
}
read() {
return __awaiter(this, void 0, void 0, function* () {
return this.pluginInterface.characteristicReadValue(this.deviceId, this.service.uuid, this.uuid);
});
}
getDescriptor() {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`Not implemented yet`);
});
}
getDescriptors() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.config.descriptors) {
return [];
}
return this.config.descriptors.map((descriptor) => new CordovaDescriptorAdapter(descriptor, this));
});
}
write(data, writeWithoutResponse) {
return __awaiter(this, void 0, void 0, function* () {
if (writeWithoutResponse) {
yield this.pluginInterface.characteristicWriteWithoutResponse(this.deviceId, this.service.uuid, this.uuid, data);
}
else {
yield this.pluginInterface.characteristicWrite(this.deviceId, this.service.uuid, this.uuid, data);
}
return data;
});
}
enableNotifications(enabled) {
return __awaiter(this, void 0, void 0, function* () {
if (enabled) {
this.setupDataStreamIfRequired();
yield this.pluginInterface.characteristicStartNotification(this.deviceId, this.service.uuid, this.uuid);
}
else {
yield this.pluginInterface.characteristicStopNotification(this.deviceId, this.service.uuid, this.uuid);
}
});
}
setupDataStreamIfRequired() {
if (!this._dataStream) {
this._dataStream = this.pluginInterface.characteristicChanged(this.deviceId, this.service.uuid, this.uuid);
}
return this._dataStream;
}
}
export class CordovaDescriptorAdapter {
constructor(config, characteristic) {
this.config = config;
this.characteristic = characteristic;
}
get uuid() {
return this.config.uuid;
}
readValue() {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`Reading descriptor value is not implemented yet`);
});
}
writeValue(data) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`Writing descriptor value is not implemented yet`);
});
}
}
//# sourceMappingURL=cordova-service-adapter.js.map