This repository was archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
83 lines (72 loc) · 2.48 KB
/
index.ts
File metadata and controls
83 lines (72 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { NativeModules, NativeEventEmitter } from 'react-native'
const { Noke } = NativeModules
const NokeEmitter = new NativeEventEmitter(Noke)
export enum NokeEvent {
Discovered = 'Discovered',
Connecting = 'Connecting',
Connected = 'Connected',
Syncing = 'Syncing',
Unlocked = 'Unlocked',
Shutdown = 'Shutdown',
Disconnected = 'Disconnected',
Uploaded = 'Uploaded',
BluetoothStatusChanged = 'BluetoothStatusChanged',
Error = 'Error',
ServiceConnected = 'ServiceConnected',
ServiceDisconnected = 'ServiceDisconnected'
}
type NokeDevice = {
battery: string
connectionState: string
offlineKey: string
lastSeen: string
lockState: string
mac: string
name: string
serial: string
session: string
trackingKey: string
version: string
}
type INoke = {
isInitialized(): Promise<boolean>
initService(): void
unlock(commands: string[]): Promise<NokeDevice>
unlockOffline(key: string, command: string): Promise<NokeDevice>
change(mac: string): Promise<NokeDevice>
removeAll(): void
startScan(): void
stopScan(): void
disconnectCurrent(): void
getDeviceInfo(): Promise<NokeDevice>
}
type NokeEventData = {
eventName: NokeEvent
info: NokeDevice
}
const RNNoke: INoke = Noke
function fromEvent(cb: (data: NokeEventData) => void) {
const fn = (eventName: NokeEvent) => (info: NokeDevice) => cb({eventName, info})
NokeEmitter.addListener(NokeEvent.Discovered, fn(NokeEvent.Discovered))
NokeEmitter.addListener(NokeEvent.Connecting, fn(NokeEvent.Connecting))
NokeEmitter.addListener(NokeEvent.Connected, fn(NokeEvent.Connected))
NokeEmitter.addListener(NokeEvent.Syncing, fn(NokeEvent.Syncing))
NokeEmitter.addListener(NokeEvent.Unlocked, fn(NokeEvent.Unlocked))
NokeEmitter.addListener(NokeEvent.Disconnected, fn(NokeEvent.Disconnected))
NokeEmitter.addListener(NokeEvent.Error, fn(NokeEvent.Error))
NokeEmitter.addListener(NokeEvent.BluetoothStatusChanged, fn(NokeEvent.BluetoothStatusChanged))
NokeEmitter.addListener(NokeEvent.Uploaded, fn(NokeEvent.Uploaded))
NokeEmitter.addListener(NokeEvent.Shutdown, fn(NokeEvent.Shutdown))
NokeEmitter.addListener(NokeEvent.ServiceConnected, fn(NokeEvent.ServiceConnected))
NokeEmitter.addListener(NokeEvent.ServiceDisconnected, fn(NokeEvent.ServiceDisconnected))
}
const removeAllListeners = NokeEmitter.removeAllListeners
function addListener(eventName: NokeEvent, cb: () => void) {
NokeEmitter.addListener(eventName, cb)
}
export default {
...RNNoke,
fromEvent,
addListener,
removeAllListeners
}