-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
136 lines (108 loc) · 3.29 KB
/
index.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { NativeModules, NativeEventEmitter } from "react-native";
const { Espressif } = NativeModules;
const setCallback = (device) => {
device.startSession = async () => {
return await Espressif.startSession(device.uuid);
};
device.connect = async () => {
return await Espressif.connectTo(device.uuid);
};
device.setCredentials = async (ssid, passphrase) => {
return await Espressif.setCredentials(ssid, passphrase, device.uuid);
};
device.scanWifi = async () => {
return await Espressif.scanWifi(device.uuid);
};
device.networkTest = async () => {
return await Espressif.networkTestStatus(device.uuid);
};
device.disconnect = async () => {
return await Espressif.disconnect(device.uuid);
};
return device;
};
const Wrapper = () => {
let peripherals = [];
const RNEspressifEvent = new NativeEventEmitter(Espressif);
if (Espressif.bluetoothStatusSub) Espressif.bluetoothStatusSub.remove();
if (Espressif.deviceStatus) Espressif.deviceStatus.remove();
if (Espressif.deviceNetworkStatus) Espressif.deviceNetworkStatus.remove();
if (Espressif.devicesStateSub) {
Espressif.devicesStateSub.remove();
}
Espressif.bluetoothStatusSub = RNEspressifEvent.addListener(
"bluetooth-status",
(status) => {
console.info("bluetooth", { status });
Espressif.bluetoothStatus = status;
if (Espressif.OnBluetoothStatusChanged)
Espressif.OnBluetoothStatusChanged(status);
}
);
Espressif.deviceStatus = RNEspressifEvent.addListener(
"device-status",
(dataStr) => {
const data = JSON.parse(dataStr);
const index = peripherals.findIndex(
(peripheral) => data.uuid === peripheral.uuid
);
Object.assign(peripherals[index], data);
peripherals[index].onStatusChanged(peripherals[index]);
}
);
Espressif.deviceNetworkStatus = RNEspressifEvent.addListener(
"network-state",
(dataStr) => {
const data = JSON.parse(dataStr);
const index = peripherals.findIndex(
(peripheral) => data.uuid === peripheral.uuid
);
peripherals[index].onNetworkStatusChanged(data.status, data.components);
}
);
Espressif.devicesStateSub = RNEspressifEvent.addListener(
"devices-state",
(dataStr) => {
const data = JSON.parse(dataStr);
peripherals = data.peripherals;
peripherals.forEach((peripheral) => setCallback(peripheral));
Espressif.OnStateChanged(data.state, peripherals);
}
);
return Espressif;
};
export const ESPTransportType = {
Bluetooth: "bluetooth",
Wifi: "wifi",
};
export const ESPSecurityType = {
Sec0: "sec0",
Sec1: "sec1",
};
export const ESPDeviceState = {
Configured: "CONFIGURED",
SessionEstablished: "SESSION_ESTABLISHED",
NetworkTest: "NETWORK_TEST",
Disconnected: "DISCONNECTED",
};
export const ESPNetworkStatus = {
NotStarted: "NOT_STARTED",
InProgress: "IN_PROGRESS",
Ok: "OK",
Nok: "NOK",
};
export const ESPEventState = {
DeviceNotFound: "DEVICES_NOT_FOUND",
Unknown: "UNKNOWN",
DevicesFounds: "DEVICES_FOUND",
DeviceUpdated: "DEVICE_UPDATED",
};
export const ESPBluetoothState = {
Unknown: "UNKNOWN",
Resetting: "RESETTING",
Unsupported: "UNSUPPORTED",
Unauthorized: "UNAUTHORIZED",
PoweredOff: "POWERED_OFF",
PoweredOn: "POWERED_ON",
};
export default Wrapper;