incyclist-devices 2.3.12 → 2.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/lib/ble/adapter-factory.d.ts +34 -0
  2. package/lib/ble/adapter-factory.js +110 -0
  3. package/lib/ble/base/comms-utils.d.ts +7 -0
  4. package/lib/ble/base/comms-utils.js +90 -0
  5. package/lib/ble/base/comms.d.ts +75 -0
  6. package/lib/ble/base/comms.js +599 -0
  7. package/lib/ble/ble-interface.d.ts +84 -0
  8. package/lib/ble/ble-interface.js +622 -0
  9. package/lib/ble/ble-peripheral.d.ts +39 -0
  10. package/lib/ble/ble-peripheral.js +252 -0
  11. package/lib/ble/cp/comm.d.ts +30 -0
  12. package/lib/ble/cp/comm.js +126 -0
  13. package/lib/ble/elite/adapter.d.ts +21 -0
  14. package/lib/ble/elite/adapter.js +118 -0
  15. package/lib/ble/elite/comms.d.ts +31 -0
  16. package/lib/ble/elite/comms.js +127 -0
  17. package/lib/ble/elite/index.d.ts +3 -0
  18. package/lib/ble/elite/index.js +10 -0
  19. package/lib/ble/fm/comms.d.ts +49 -0
  20. package/lib/ble/fm/comms.js +506 -0
  21. package/lib/ble/fm/sensor.js +3 -3
  22. package/lib/ble/hr/comm.d.ts +19 -0
  23. package/lib/ble/hr/comm.js +65 -0
  24. package/lib/ble/peripheral-cache.d.ts +45 -0
  25. package/lib/ble/peripheral-cache.js +109 -0
  26. package/lib/ble/tacx/comms.d.ts +59 -0
  27. package/lib/ble/tacx/comms.js +634 -0
  28. package/lib/ble/wahoo/comms.d.ts +64 -0
  29. package/lib/ble/wahoo/comms.js +399 -0
  30. package/lib/direct-connect/base/comms.d.ts +3 -0
  31. package/lib/direct-connect/base/comms.js +7 -0
  32. package/lib/direct-connect/base/sensor.d.ts +3 -0
  33. package/lib/direct-connect/base/sensor.js +7 -0
  34. package/lib/direct-connect/utils.d.ts +5 -0
  35. package/lib/direct-connect/utils.js +73 -0
  36. package/lib/factories/index.d.ts +3 -0
  37. package/lib/factories/index.js +10 -0
  38. package/lib/modes/power-base.js +8 -5
  39. package/lib/utils/operation.d.ts +17 -0
  40. package/lib/utils/operation.js +20 -0
  41. package/package.json +1 -1
@@ -0,0 +1,34 @@
1
+ import BleAdapter from "./base/adapter";
2
+ import { BleDeviceSettings, BleProtocol } from "./types";
3
+ import { DeviceProperties } from "../types";
4
+ import { BleComms } from "./base/comms";
5
+ import { BleDeviceData } from "./base/types";
6
+ export interface BleAdapterInfo {
7
+ protocol: BleProtocol;
8
+ Adapter: typeof BleAdapter<BleDeviceData, BleComms>;
9
+ Comm: typeof BleComms;
10
+ }
11
+ export default class BleAdapterFactory {
12
+ static _instance: BleAdapterFactory;
13
+ implementations: BleAdapterInfo[];
14
+ instances: Array<BleAdapter<BleDeviceData, BleComms>>;
15
+ static getInstance(): BleAdapterFactory;
16
+ constructor();
17
+ getAdapterInfo(protocol: BleProtocol): BleAdapterInfo;
18
+ getAll(): BleAdapterInfo[];
19
+ createInstance(settings: BleDeviceSettings, props?: DeviceProperties): BleAdapter<BleDeviceData, BleComms>;
20
+ removeInstance(query: {
21
+ settings?: BleDeviceSettings;
22
+ adapter?: BleAdapter<BleDeviceData, BleComms>;
23
+ }): void;
24
+ find(settings?: BleDeviceSettings): BleAdapter<BleDeviceData, BleComms>;
25
+ register(protocol: BleProtocol, Adapter: typeof BleAdapter<BleDeviceData, BleComms>, Comm: typeof BleComms): void;
26
+ getAllInstances(): Array<BleAdapter<BleDeviceData, BleComms>>;
27
+ getAllSupportedComms(): (typeof BleComms)[];
28
+ getAllSupportedAdapters(): Array<(typeof BleAdapter<BleDeviceData, BleComms>)>;
29
+ getAllSupportedServices(): string[];
30
+ getDeviceClasses(peripheral: any, props?: {
31
+ protocol?: BleProtocol;
32
+ services?: string[];
33
+ }): (typeof BleComms)[];
34
+ }
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const comms_utils_1 = require("./base/comms-utils");
4
+ const utils_1 = require("./utils");
5
+ class BleAdapterFactory {
6
+ static getInstance() {
7
+ if (!BleAdapterFactory._instance)
8
+ BleAdapterFactory._instance = new BleAdapterFactory();
9
+ return BleAdapterFactory._instance;
10
+ }
11
+ constructor() {
12
+ this.implementations = [];
13
+ this.instances = [];
14
+ }
15
+ getAdapterInfo(protocol) {
16
+ return this.implementations.find(a => a.protocol === protocol);
17
+ }
18
+ getAll() {
19
+ return this.implementations;
20
+ }
21
+ createInstance(settings, props) {
22
+ let { profile, protocol } = settings;
23
+ const adapterSettings = Object.assign({}, settings);
24
+ if (profile) {
25
+ try {
26
+ const mapping = (0, utils_1.mapLegacyProfile)(profile);
27
+ protocol = adapterSettings.protocol = mapping.protocol;
28
+ delete adapterSettings.profile;
29
+ }
30
+ catch (_a) {
31
+ delete settings.profile;
32
+ }
33
+ }
34
+ const existing = this.find(adapterSettings);
35
+ if (existing) {
36
+ existing.setProperties(props);
37
+ return existing;
38
+ }
39
+ const info = this.getAdapterInfo(protocol);
40
+ if (!info || !info.Adapter)
41
+ return;
42
+ const adapter = new info.Adapter(adapterSettings, props);
43
+ this.instances.push(adapter);
44
+ return adapter;
45
+ }
46
+ removeInstance(query) {
47
+ let idx = -1;
48
+ if (query.settings) {
49
+ idx = this.instances.findIndex(a => a.isEqual(query.settings));
50
+ }
51
+ else if (query.adapter) {
52
+ idx = this.instances.findIndex(a => a.isEqual(query.adapter.getSettings()));
53
+ }
54
+ if (idx !== -1)
55
+ this.instances.splice(idx);
56
+ }
57
+ find(settings) {
58
+ return this.instances.find(a => a.isEqual(settings));
59
+ }
60
+ register(protocol, Adapter, Comm) {
61
+ const info = Object.assign({}, { protocol, Adapter, Comm });
62
+ const existing = this.implementations.findIndex(a => a.protocol === protocol);
63
+ if (existing !== -1)
64
+ this.implementations[existing] = info;
65
+ else
66
+ this.implementations.push(info);
67
+ }
68
+ getAllInstances() {
69
+ return this.instances;
70
+ }
71
+ getAllSupportedComms() {
72
+ const supported = BleAdapterFactory.getInstance().getAll();
73
+ return supported.map(info => info.Comm);
74
+ }
75
+ getAllSupportedAdapters() {
76
+ const supported = BleAdapterFactory.getInstance().getAll();
77
+ return supported.map(info => info.Adapter);
78
+ }
79
+ getAllSupportedServices() {
80
+ const supported = BleAdapterFactory.getInstance().getAll();
81
+ const res = [];
82
+ if (supported && supported.length > 0) {
83
+ supported.forEach(info => {
84
+ if (info && info.Comm && info.Comm.services) {
85
+ info.Comm.services.forEach(s => {
86
+ if (!res.includes(s))
87
+ res.push(s);
88
+ });
89
+ }
90
+ });
91
+ }
92
+ return res;
93
+ }
94
+ getDeviceClasses(peripheral, props = {}) {
95
+ let DeviceClasses;
96
+ const { protocol, services = peripheral.advertisement.serviceUuids } = props;
97
+ const classes = this.getAllSupportedComms();
98
+ DeviceClasses = (0, comms_utils_1.getDevicesFromServices)(classes, services);
99
+ if (protocol && DeviceClasses && DeviceClasses.length > 0) {
100
+ DeviceClasses = DeviceClasses.filter((C) => {
101
+ const device = new C({ peripheral });
102
+ if (device.getProtocol() !== protocol)
103
+ return false;
104
+ return true;
105
+ });
106
+ }
107
+ return DeviceClasses;
108
+ }
109
+ }
110
+ exports.default = BleAdapterFactory;
@@ -0,0 +1,7 @@
1
+ import { BleProtocol } from "../types";
2
+ import { BleComms } from "./comms";
3
+ export declare function getBestDeviceMatch(DeviceClasses: (typeof BleComms)[]): typeof BleComms;
4
+ export declare function getDevicesFromServices(deviceTypes: (typeof BleComms)[], services: string | string[]): (typeof BleComms)[];
5
+ export declare function getServicesFromDeviceTypes(deviceTypes: (typeof BleComms)[]): string[];
6
+ export declare function getServicesFromProtocols(protocols: BleProtocol[]): string[];
7
+ export declare function getServicesFromDevice(device: BleComms): string[];
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getBestDeviceMatch = getBestDeviceMatch;
7
+ exports.getDevicesFromServices = getDevicesFromServices;
8
+ exports.getServicesFromDeviceTypes = getServicesFromDeviceTypes;
9
+ exports.getServicesFromProtocols = getServicesFromProtocols;
10
+ exports.getServicesFromDevice = getServicesFromDevice;
11
+ const adapter_factory_1 = __importDefault(require("../adapter-factory"));
12
+ const utils_1 = require("../utils");
13
+ function getBestDeviceMatch(DeviceClasses) {
14
+ if (!DeviceClasses || DeviceClasses.length === 0)
15
+ return;
16
+ const details = DeviceClasses.map(c => ({ name: c.prototype.constructor.name, priority: c.detectionPriority || 0, class: c }));
17
+ details.sort((a, b) => b.priority - a.priority);
18
+ return details[0].class;
19
+ }
20
+ function getDevicesFromServices(deviceTypes, services) {
21
+ if (!deviceTypes || !Array.isArray(deviceTypes) || deviceTypes.length === 0) {
22
+ return [];
23
+ }
24
+ const get = (deviceTypes, fnCompare) => {
25
+ const types = deviceTypes.filter(DeviceType => {
26
+ const C = DeviceType;
27
+ let found = false;
28
+ if (C.services)
29
+ found = C.services.find((s) => fnCompare(s));
30
+ return found;
31
+ });
32
+ return types;
33
+ };
34
+ if (typeof services === 'string') {
35
+ return get(deviceTypes, (s) => (0, utils_1.matches)(s, services));
36
+ }
37
+ if (Array.isArray(services)) {
38
+ const sids = services.map(utils_1.uuid);
39
+ return get(deviceTypes, s => {
40
+ const res = sids.find((service) => (0, utils_1.matches)(s, service));
41
+ return res !== undefined;
42
+ });
43
+ }
44
+ return [];
45
+ }
46
+ function getServicesFromDeviceTypes(deviceTypes) {
47
+ let services = [];
48
+ try {
49
+ if (!deviceTypes || !Array.isArray(deviceTypes) || deviceTypes.length === 0) {
50
+ return [];
51
+ }
52
+ deviceTypes.forEach(DeviceType => {
53
+ if (DeviceType.services) {
54
+ const dtServices = DeviceType.services;
55
+ dtServices.forEach(s => {
56
+ if (!services.find(s2 => s2 === s))
57
+ services.push(s);
58
+ });
59
+ }
60
+ });
61
+ }
62
+ catch (err) {
63
+ console.log(err);
64
+ }
65
+ return services;
66
+ }
67
+ function getServicesFromProtocols(protocols) {
68
+ const services = [];
69
+ const comms = adapter_factory_1.default.getInstance().getAllSupportedComms();
70
+ comms
71
+ .filter((C) => protocols.find(p => p === C.protocol) !== undefined)
72
+ .forEach((C) => {
73
+ C.services.forEach(s => {
74
+ if (!services.find(s2 => s2 === s))
75
+ services.push(s);
76
+ });
77
+ });
78
+ return services;
79
+ }
80
+ function getServicesFromDevice(device) {
81
+ if (!device)
82
+ return [];
83
+ const services = [];
84
+ const dServices = device.getServiceUUids();
85
+ dServices.forEach(s => {
86
+ if (!services.find(s2 => s2 === s))
87
+ services.push(s);
88
+ });
89
+ return services;
90
+ }
@@ -0,0 +1,75 @@
1
+ import EventEmitter from "events";
2
+ import { EventLogger } from "gd-eventlog";
3
+ import { LegacyProfile } from "../../antv2/types";
4
+ import BleInterface from "../ble-interface";
5
+ import BlePeripheralConnector from "../ble-peripheral";
6
+ import { BleCharacteristic, BleCommsConnectProps, BleDeviceConstructProps, BleDeviceInfo, BleDeviceSettings, BlePeripheral, BleProtocol, BleWriteProps, ConnectState, IBlePeripheralConnector } from "../types";
7
+ type CommandQueueItem = {
8
+ uuid: string;
9
+ data: Buffer;
10
+ timeout: number;
11
+ resolve: any;
12
+ reject: any;
13
+ };
14
+ export interface MessageLog {
15
+ uuid: string;
16
+ timestamp: any;
17
+ data: string;
18
+ }
19
+ export declare class BleComms extends EventEmitter {
20
+ static services: string[];
21
+ static protocol: BleProtocol;
22
+ id: string;
23
+ paused: boolean;
24
+ address: string;
25
+ name: string;
26
+ services: string[];
27
+ ble: BleInterface;
28
+ peripheral?: BlePeripheral;
29
+ characteristics: BleCharacteristic[];
30
+ state?: string;
31
+ logger?: EventLogger;
32
+ deviceInfo: BleDeviceInfo;
33
+ isInitialized: boolean;
34
+ subscribedCharacteristics: string[];
35
+ writeQueue: CommandQueueItem[];
36
+ workerIv: NodeJS.Timeout;
37
+ prevMessages: MessageLog[];
38
+ connectState: ConnectState;
39
+ constructor(props?: BleDeviceConstructProps);
40
+ getConnectState(): ConnectState;
41
+ isConnected(): boolean;
42
+ pause(): void;
43
+ resume(): void;
44
+ getServiceUUids(): string[];
45
+ getProfile(): LegacyProfile;
46
+ getProtocol(): BleProtocol;
47
+ getSettings(): BleDeviceSettings;
48
+ getServices(): string[];
49
+ logEvent(event: any): void;
50
+ setLogger(logger: EventLogger): void;
51
+ setInterface(ble: BleInterface): void;
52
+ static isMatching(characteristics: string[]): boolean;
53
+ reset(): void;
54
+ cleanupListeners(): void;
55
+ onDisconnect(): Promise<void>;
56
+ waitForConnectFinished(timeout: any): Promise<unknown>;
57
+ hasService(serviceUuid: any): boolean;
58
+ init(): Promise<boolean>;
59
+ initDevice(): Promise<boolean>;
60
+ connectPeripheral(peripheral: BlePeripheral): Promise<boolean>;
61
+ subscribeMultiple(characteristics: string[], conn?: IBlePeripheralConnector): Promise<void>;
62
+ subscribeAll(conn?: BlePeripheralConnector): Promise<void>;
63
+ unsubscribeAll(conn?: BlePeripheralConnector): void;
64
+ connect(props?: BleCommsConnectProps): Promise<boolean>;
65
+ disconnect(): Promise<boolean>;
66
+ checkForDuplicate(characteristic: string, data: Buffer): boolean;
67
+ onData(characteristic: string, _data: Buffer): boolean;
68
+ timeoutCheck(): void;
69
+ startWorker(): void;
70
+ stopWorker(): void;
71
+ write(characteristicUuid: string, data: Buffer, props?: BleWriteProps): Promise<ArrayBuffer>;
72
+ read(characteristicUuid: string): Promise<Uint8Array>;
73
+ getDeviceInfo(): Promise<BleDeviceInfo>;
74
+ }
75
+ export {};