react-native-flic2 2.0.0-alpha.39 → 2.0.0-beta.1

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.
package/src/index.ts ADDED
@@ -0,0 +1,382 @@
1
+ import { TypedEmitter } from './lib/typedEventEmitter';
2
+ import NativeFlic2, {
3
+ type ButtonEvent,
4
+ type FlicButton,
5
+ type LatencyModeType,
6
+ type ManagerStateChangeEvent,
7
+ type ScanStatusChangeEvent,
8
+ type TriggerModeType,
9
+ } from './NativeFlic2';
10
+
11
+ class Flic2 {
12
+
13
+ private isFlic2ManagerInitialized: boolean = false;
14
+
15
+ private sessionId: string;
16
+
17
+ public eventEmitter: TypedEmitter<{
18
+ buttonEvent: (event: ButtonEvent) => void;
19
+ managerStateChange: (event: ManagerStateChangeEvent) => void;
20
+ scanStatusChange: (event: ScanStatusChangeEvent) => void;
21
+ managerInitialized: () => void;
22
+ }>;
23
+
24
+ /**
25
+ * Constructor.
26
+ *
27
+ * @class
28
+ * @version 2.0.0
29
+ */
30
+ constructor() {
31
+
32
+ // generate a random session ID for the instance
33
+ this.sessionId = Math.random().toString(36).substring(2, 15);
34
+
35
+ console.log('Created new Flic2 instance with sessionId', this.sessionId);
36
+
37
+ // create event emitter
38
+ this.eventEmitter = new TypedEmitter<{
39
+ buttonEvent: (event: ButtonEvent) => void;
40
+ managerStateChange: (event: ManagerStateChangeEvent) => void;
41
+ scanStatusChange: (event: ScanStatusChangeEvent) => void;
42
+ }>();
43
+
44
+ // listen
45
+ NativeFlic2.onButtonEvent(this.onNativeButtonEvent.bind(this));
46
+ NativeFlic2.onManagerStateChange(
47
+ this.onNativeManagerStateChange.bind(this)
48
+ );
49
+
50
+ NativeFlic2.onScanStatusChange(this.onNativeScanStatusChange.bind(this));
51
+
52
+ }
53
+
54
+ // MARK: Public management methods
55
+ /**
56
+ * Initialize the Flic2 manager.
57
+ *
58
+ * @returns A promise that resolves when the Flic2 manager is initialized.
59
+ */
60
+ public async initialize(): Promise<boolean> {
61
+
62
+ // check if the Flic2 manager is already initialized
63
+ if (this.isInitialized()) {
64
+
65
+ throw new Error('Flic2 manager is already initialized');
66
+
67
+ }
68
+
69
+ // initialize the Flic2 manager in background
70
+ const result = await NativeFlic2.initialize(true);
71
+
72
+ if (!result.success) {
73
+
74
+ throw new Error(result.message);
75
+
76
+ }
77
+
78
+ this.onInitialized();
79
+
80
+ return true;
81
+
82
+ }
83
+
84
+ /**
85
+ * Scan for buttons.
86
+ *
87
+ * @returns A promise that resolves when the scan is started. Events will be emitted for the scan process.
88
+ */
89
+ public startScan(): Promise<{ success: boolean; message: string }> {
90
+
91
+ return NativeFlic2.scanForButtons();
92
+
93
+ }
94
+
95
+ /**
96
+ * Stop the scan.
97
+ *
98
+ * @returns A promise that resolves when the scan is stopped.
99
+ */
100
+ public stopScan(): Promise<{ success: boolean; message: string }> {
101
+
102
+ return NativeFlic2.stopScan();
103
+
104
+ }
105
+
106
+ /**
107
+ * Called when the Flic2 manager is initialized.
108
+ */
109
+ public onInitialized(): void {
110
+
111
+ this.isFlic2ManagerInitialized = true;
112
+
113
+ }
114
+
115
+ /**
116
+ * Check if the Flic2 manager is initialized.
117
+ *
118
+ * @returns True if the Flic2 manager is initialized, false otherwise.
119
+ */
120
+ public isInitialized(): boolean {
121
+
122
+ return this.isFlic2ManagerInitialized;
123
+
124
+ }
125
+
126
+ /**
127
+ * Connect all known buttons.
128
+ *
129
+ * @returns A promise that resolves when the all known buttons are connected.
130
+ */
131
+ public connectAllKnownButtons(): Promise<{
132
+ success: boolean;
133
+ message: string;
134
+ }> {
135
+
136
+ return NativeFlic2.connectAllKnownButtons();
137
+
138
+ }
139
+
140
+ /**
141
+ * Disconnect all known buttons.
142
+ *
143
+ * @returns A promise that resolves when the all known buttons are disconnected.
144
+ */
145
+ public disconnectAllKnownButtons(): Promise<{
146
+ success: boolean;
147
+ message: string;
148
+ }> {
149
+
150
+ return NativeFlic2.disconnectAllKnownButtons();
151
+
152
+ }
153
+
154
+ /**
155
+ * Forget all buttons.
156
+ *
157
+ * @returns A promise that resolves when the all buttons are forgotten.
158
+ */
159
+ public forgetAllButtons(): Promise<{ success: boolean; message: string }> {
160
+
161
+ return NativeFlic2.forgetAllButtons();
162
+
163
+ }
164
+
165
+ /**
166
+ * Check if a scan is currently running.
167
+ *
168
+ * @returns A promise that resolves to true if scanning, false otherwise.
169
+ */
170
+ public isScanning(): Promise<boolean> {
171
+
172
+ return NativeFlic2.isScanning();
173
+
174
+ }
175
+
176
+ /**
177
+ * Forget a specific button by UUID.
178
+ *
179
+ * @param uuid - The UUID of the button to forget.
180
+ * @returns A promise that resolves when the button is forgotten.
181
+ */
182
+ public forgetButton(
183
+ uuid: string
184
+ ): Promise<{ success: boolean; message: string }> {
185
+
186
+ return NativeFlic2.forgetButton(uuid);
187
+
188
+ }
189
+
190
+ // MARK: Public button methods
191
+ /**
192
+ * Connect a button.
193
+ *
194
+ * @param uuid - The UUID of the button to connect.
195
+ * @returns A promise that resolves when the button is connected.
196
+ */
197
+ public buttonConnect(
198
+ uuid: string
199
+ ): Promise<{ success: boolean; message: string }> {
200
+
201
+ return NativeFlic2.connectButton(uuid);
202
+
203
+ }
204
+
205
+ /**
206
+ * Disconnect a button.
207
+ *
208
+ * @param uuid - The UUID of the button to disconnect.
209
+ * @returns A promise that resolves when the button is disconnected.
210
+ */
211
+ public buttonDisconnect(
212
+ uuid: string
213
+ ): Promise<{ success: boolean; message: string }> {
214
+
215
+ return NativeFlic2.disconnectButton(uuid);
216
+
217
+ }
218
+
219
+ /**
220
+ * Set the trigger mode of a button.
221
+ *
222
+ * @param uuid - The UUID of the button to set the trigger mode of.
223
+ * @param mode - The trigger mode to set.
224
+ * @returns A promise that resolves when the trigger mode is set.
225
+ */
226
+ public buttonSetTriggerMode(
227
+ uuid: string,
228
+ mode: TriggerModeType
229
+ ): Promise<{ success: boolean; message: string }> {
230
+
231
+ return NativeFlic2.setTriggerMode(uuid, mode);
232
+
233
+ }
234
+
235
+ /**
236
+ * Set the latency mode of a button.
237
+ *
238
+ * @param uuid - The UUID of the button to set the latency mode of.
239
+ * @param mode - The latency mode to set.
240
+ * @returns A promise that resolves when the latency mode is set.
241
+ */
242
+ public buttonSetLatencyMode(
243
+ uuid: string,
244
+ mode: LatencyModeType
245
+ ): Promise<{ success: boolean; message: string }> {
246
+
247
+ return NativeFlic2.setLatencyMode(uuid, mode);
248
+
249
+ }
250
+
251
+ /**
252
+ * Set the nickname of a button.
253
+ *
254
+ * @param uuid - The UUID of the button to set the nickname of.
255
+ * @param nickname - The nickname to set.
256
+ * @returns A promise that resolves when the nickname is set.
257
+ */
258
+ public buttonSetNickname(
259
+ uuid: string,
260
+ nickname: string
261
+ ): Promise<{ success: boolean; message: string }> {
262
+
263
+ return NativeFlic2.setNickname(uuid, nickname);
264
+
265
+ }
266
+
267
+ /**
268
+ * Get all buttons.
269
+ *
270
+ * @returns A promise that resolves with an array of FlicButton instances.
271
+ */
272
+ public getButtons(): Promise<FlicButton[]> {
273
+
274
+ return NativeFlic2.getButtons();
275
+
276
+ }
277
+
278
+ /**
279
+ * Get a button by UUID.
280
+ *
281
+ * @param uuid - The UUID of the button to get.
282
+ * @returns A promise that resolves with the FlicButton instance or null if the button is not found.
283
+ */
284
+ public async getButton(uuid: string): Promise<FlicButton | null> {
285
+
286
+ const buttons = await NativeFlic2.getButtons();
287
+ const button = buttons.find((item: FlicButton) => item.uuid === uuid);
288
+
289
+ return button ?? null;
290
+
291
+ }
292
+
293
+ /**
294
+ * Get the battery health status of a button.
295
+ *
296
+ * @param uuid - The UUID of the button to check.
297
+ * @returns A promise that resolves to true if battery is OK (voltage > 2.65V), false otherwise.
298
+ * @throws Error if the button is not found.
299
+ */
300
+ public async getBatteryHealth(uuid: string): Promise<boolean> {
301
+
302
+ const button = await this.getButton(uuid);
303
+
304
+ if (!button) {
305
+
306
+ throw new Error(`Button with UUID ${uuid} not found`);
307
+
308
+ }
309
+
310
+ return this.isBatteryVoltageOk(button.batteryVoltage);
311
+
312
+ }
313
+
314
+ // MARK: Private Methods
315
+ /**
316
+ * Check if battery voltage is OK based on the 2.65V threshold.
317
+ *
318
+ * @param voltage - The battery voltage in volts.
319
+ * @returns True if voltage is above 2.65V (battery is OK), false otherwise.
320
+ */
321
+ private isBatteryVoltageOk(voltage: number): boolean {
322
+
323
+ return voltage * 1000 > 2650;
324
+
325
+ }
326
+
327
+ /**
328
+ * Called when a button event is received from the native side.
329
+ *
330
+ * @param event - The button event.
331
+ */
332
+ private onNativeButtonEvent(event: ButtonEvent): void {
333
+
334
+ // Enrich batteryUpdate events with batteryVoltageOk
335
+ if (event.event === 'batteryUpdate') {
336
+
337
+ const voltage = typeof event.voltage === 'number' ? event.voltage : event.button?.batteryVoltage;
338
+
339
+ const enrichedEvent: ButtonEvent = {
340
+ ...event,
341
+ batteryVoltageOk: this.isBatteryVoltageOk(voltage ?? 0),
342
+ };
343
+
344
+ this.eventEmitter.emit('buttonEvent', enrichedEvent);
345
+
346
+ return;
347
+
348
+ }
349
+
350
+ this.eventEmitter.emit('buttonEvent', event);
351
+
352
+ }
353
+
354
+ /**
355
+ * Called when a manager state change event is received from the native side.
356
+ *
357
+ * @param event - The manager state change event.
358
+ */
359
+ private onNativeManagerStateChange(event: ManagerStateChangeEvent): void {
360
+
361
+ this.eventEmitter.emit('managerStateChange', event);
362
+
363
+ }
364
+
365
+ /**
366
+ * Called when a scan status change event is received from the native side.
367
+ *
368
+ * @param event - The scan status change event.
369
+ */
370
+ private onNativeScanStatusChange(event: ScanStatusChangeEvent): void {
371
+
372
+ this.eventEmitter.emit('scanStatusChange', event);
373
+
374
+ }
375
+
376
+ }
377
+
378
+ // export as singleton
379
+ export default new Flic2();
380
+
381
+ // re-export types
382
+ export type * from './NativeFlic2';
@@ -0,0 +1,63 @@
1
+ type EventMap = Record<string, (...args: any[]) => void>;
2
+
3
+ export class TypedEmitter<E extends EventMap> {
4
+
5
+ private listeners: { [K in keyof E]?: Set<E[K]> } = {};
6
+
7
+ on<K extends keyof E>(event: K, fn: E[K]): { remove: () => void } {
8
+
9
+ (this.listeners[event] ??= new Set()).add(fn);
10
+
11
+ // return shorthand for removing the listener
12
+ return { remove: () => this.off(event, fn) };
13
+
14
+ }
15
+
16
+ off<K extends keyof E>(event: K, fn: E[K]): this {
17
+
18
+ this.listeners[event]?.delete(fn);
19
+ return this;
20
+
21
+ }
22
+
23
+ once<K extends keyof E>(event: K, fn: E[K]): { remove: () => void } {
24
+
25
+ const wrapped: E[K] = ((...args: Parameters<E[K]>) => {
26
+
27
+ this.off(event, wrapped);
28
+ fn(...args);
29
+
30
+ }) as E[K];
31
+
32
+ return this.on(event, wrapped);
33
+
34
+ }
35
+
36
+ emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): boolean {
37
+
38
+ const ls = this.listeners[event];
39
+
40
+ if (!ls || ls.size === 0) {
41
+
42
+ return false;
43
+
44
+ }
45
+
46
+ for (const fn of [...ls]) {
47
+
48
+ fn(...args);
49
+
50
+ }
51
+
52
+ return true;
53
+
54
+ }
55
+
56
+ clear(): this {
57
+
58
+ this.listeners = {};
59
+ return this;
60
+
61
+ }
62
+
63
+ }
package/src/index.tsx DELETED
@@ -1,159 +0,0 @@
1
- import type { CodegenTypes } from 'react-native';
2
- import type {
3
- MultiplyEvent,
4
- ManagerStateChangeEvent,
5
- ScanStatusChangeEvent,
6
- ButtonEvent,
7
- FlicButton,
8
- TriggerModeType,
9
- LatencyModeType,
10
- } from './NativeFlic2';
11
- import NativeFlic2 from './NativeFlic2';
12
-
13
- // MARK: - Example Functions (keeping for compatibility)
14
-
15
- export function multiply(a: number, b: number): number {
16
- return NativeFlic2.multiply(a, b);
17
- }
18
-
19
- export const onMultiply =
20
- NativeFlic2.onMultiply as CodegenTypes.EventEmitter<MultiplyEvent>;
21
-
22
- // MARK: - Manager Functions
23
-
24
- export function initialize(
25
- background: boolean
26
- ): Promise<{ success: boolean; message: string }> {
27
- return NativeFlic2.initialize(background);
28
- }
29
-
30
- export function getButtons(): Promise<FlicButton[]> {
31
- return NativeFlic2.getButtons();
32
- }
33
-
34
- export function scanForButtons(): Promise<{
35
- success: boolean;
36
- message: string;
37
- }> {
38
- return NativeFlic2.scanForButtons();
39
- }
40
-
41
- export function stopScan(): Promise<{ success: boolean; message: string }> {
42
- return NativeFlic2.stopScan();
43
- }
44
-
45
- export function forgetButton(
46
- uuid: string
47
- ): Promise<{ success: boolean; message: string }> {
48
- return NativeFlic2.forgetButton(uuid);
49
- }
50
-
51
- export function connectAllKnownButtons(): Promise<{
52
- success: boolean;
53
- message: string;
54
- }> {
55
- return NativeFlic2.connectAllKnownButtons();
56
- }
57
-
58
- export function disconnectAllKnownButtons(): Promise<{
59
- success: boolean;
60
- message: string;
61
- }> {
62
- return NativeFlic2.disconnectAllKnownButtons();
63
- }
64
-
65
- export function forgetAllButtons(): Promise<{
66
- success: boolean;
67
- message: string;
68
- }> {
69
- return NativeFlic2.forgetAllButtons();
70
- }
71
-
72
- export function isScanning(): Promise<boolean> {
73
- return NativeFlic2.isScanning();
74
- }
75
-
76
- // MARK: - Button Functions
77
-
78
- export function connectButton(
79
- uuid: string
80
- ): Promise<{ success: boolean; message: string }> {
81
- return NativeFlic2.connectButton(uuid);
82
- }
83
-
84
- export function disconnectButton(
85
- uuid: string
86
- ): Promise<{ success: boolean; message: string }> {
87
- return NativeFlic2.disconnectButton(uuid);
88
- }
89
-
90
- /**
91
- * Sets the trigger mode for a button.
92
- *
93
- * @platform iOS
94
- * @param uuid - Button UUID
95
- * @param mode - Trigger mode (0-3)
96
- * @returns Promise that resolves on iOS, rejects with NOT_SUPPORTED_ON_ANDROID on Android
97
- *
98
- * **Note:** This feature is only available on iOS. On Android, this will reject due to
99
- * limitations in the Android Flic2 library v1.1.0+.
100
- */
101
- export function setTriggerMode(
102
- uuid: string,
103
- mode: TriggerModeType
104
- ): Promise<{ success: boolean; message: string }> {
105
- return NativeFlic2.setTriggerMode(uuid, mode);
106
- }
107
-
108
- /**
109
- * Sets the latency mode for a button.
110
- *
111
- * @platform iOS
112
- * @param uuid - Button UUID
113
- * @param mode - Latency mode (0-1)
114
- * @returns Promise that resolves on iOS, rejects with NOT_SUPPORTED_ON_ANDROID on Android
115
- *
116
- * **Note:** This feature is only available on iOS. On Android, this will reject due to
117
- * limitations in the Android Flic2 library v1.1.0+.
118
- */
119
- export function setLatencyMode(
120
- uuid: string,
121
- mode: LatencyModeType
122
- ): Promise<{ success: boolean; message: string }> {
123
- return NativeFlic2.setLatencyMode(uuid, mode);
124
- }
125
-
126
- export function setNickname(
127
- uuid: string,
128
- nickname: string
129
- ): Promise<{ success: boolean; message: string }> {
130
- return NativeFlic2.setNickname(uuid, nickname);
131
- }
132
-
133
- // MARK: - Event Emitters
134
-
135
- export const onManagerStateChange =
136
- NativeFlic2.onManagerStateChange as CodegenTypes.EventEmitter<ManagerStateChangeEvent>;
137
-
138
- export const onScanStatusChange =
139
- NativeFlic2.onScanStatusChange as CodegenTypes.EventEmitter<ScanStatusChangeEvent>;
140
-
141
- export const onButtonEvent =
142
- NativeFlic2.onButtonEvent as CodegenTypes.EventEmitter<ButtonEvent>;
143
-
144
- // MARK: - Re-export Types
145
-
146
- export type {
147
- MultiplyEvent,
148
- ManagerStateChangeEvent,
149
- ScanStatusChangeEvent,
150
- ButtonEvent,
151
- FlicButton,
152
- FlicManagerState,
153
- FlicButtonState,
154
- FlicTriggerMode,
155
- FlicLatencyMode,
156
- FlicScannerEvent,
157
- TriggerModeType,
158
- LatencyModeType,
159
- } from './NativeFlic2';