react-native-gizwits-sdk-v5 0.1.0-rongwei

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 (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -0
  3. package/android/build.gradle +86 -0
  4. package/android/lib/sdk-bluetooth-release.aar +0 -0
  5. package/android/lib/sdk-lan-release.aar +0 -0
  6. package/android/lib/sdk-mqtt-release.aar +0 -0
  7. package/android/lib/sdk-release.aar +0 -0
  8. package/android/src/main/AndroidManifest.xml +23 -0
  9. package/android/src/main/java/com/gizwits/reactnativegizwitssdkv5/GizRNCallbackManager.kt +120 -0
  10. package/android/src/main/java/com/gizwits/reactnativegizwitssdkv5/RNGizDeviceManagerModule.kt +416 -0
  11. package/android/src/main/java/com/gizwits/reactnativegizwitssdkv5/RNGizParamsChecker.kt +58 -0
  12. package/android/src/main/java/com/gizwits/reactnativegizwitssdkv5/RNGizSDKManagerModule.kt +375 -0
  13. package/android/src/main/java/com/gizwits/reactnativegizwitssdkv5/RNGizSDKManagerPackage.kt +24 -0
  14. package/android/src/main/java/com/gizwits/reactnativegizwitssdkv5/RNGizUserManagerModule.kt +86 -0
  15. package/ios/GizRNCallbackManager.swift +31 -0
  16. package/ios/RNGizDeviceManagerModule-Bridging-Header.h +5 -0
  17. package/ios/RNGizDeviceManagerModule.m +26 -0
  18. package/ios/RNGizDeviceManagerModule.swift +479 -0
  19. package/ios/RNGizParamsChecker.swift +28 -0
  20. package/ios/RNGizSDKManagerModule-Bridging-Header.h +5 -0
  21. package/ios/RNGizSDKManagerModule.m +20 -0
  22. package/ios/RNGizSDKManagerModule.swift +139 -0
  23. package/ios/RNGizSDKManagerModule.xcodeproj/project.pbxproj +314 -0
  24. package/ios/RNGizUserManagerModule-Bridging-Header.h +5 -0
  25. package/ios/RNGizUserManagerModule.m +20 -0
  26. package/ios/RNGizUserManagerModule.swift +84 -0
  27. package/lib/Base.d.ts +6 -0
  28. package/lib/Base.js +25 -0
  29. package/lib/Base.js.map +1 -0
  30. package/lib/device.d.ts +25 -0
  31. package/lib/device.js +151 -0
  32. package/lib/device.js.map +1 -0
  33. package/lib/index.d.ts +39 -0
  34. package/lib/index.js +232 -0
  35. package/lib/index.js.map +1 -0
  36. package/lib/types.d.ts +155 -0
  37. package/lib/types.js +2 -0
  38. package/lib/types.js.map +1 -0
  39. package/lib/user.d.ts +13 -0
  40. package/lib/user.js +37 -0
  41. package/lib/user.js.map +1 -0
  42. package/package.json +73 -0
  43. package/react-native-gizwits-sdk-v5.podspec +24 -0
@@ -0,0 +1,416 @@
1
+ package com.gizwits.reactnativegizwitssdkv5
2
+
3
+ import com.facebook.react.bridge.Callback
4
+ import com.facebook.react.bridge.ReactApplicationContext
5
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
6
+ import com.facebook.react.bridge.ReactMethod
7
+ import com.facebook.react.bridge.ReadableMap
8
+ import com.facebook.react.bridge.WritableMap
9
+ import com.facebook.react.modules.core.DeviceEventManagerModule
10
+ import com.gizwits.smart.sdk.GizDevice
11
+ import com.gizwits.smart.sdk.GizSDKManager
12
+ import com.gizwits.smart.sdk.bluetooth.bleCapability
13
+ import com.gizwits.smart.sdk.common.GizConfiguration
14
+ import com.gizwits.smart.sdk.common.ProvideWiFiCredentialsEvent
15
+ import com.gizwits.smart.sdk.core.capacity.CapacityTypes
16
+ import com.gizwits.smart.sdk.exception.GizException
17
+ import com.gizwits.smart.sdk.lan.lanCapability
18
+ import com.gizwits.smart.sdk.mqtt.mqttCapability
19
+ import com.gizwits.smart.sdk.mqtt.queryBoundDevices
20
+ import com.gizwits.smart.sdk.ota.GizOTAFirmwareType
21
+ import com.gizwits.smart.sdk.ota.OTAProgressData
22
+ import com.google.gson.Gson
23
+ import com.google.gson.JsonArray
24
+ import com.google.gson.JsonObject
25
+ import com.google.gson.annotations.SerializedName
26
+ import kotlinx.coroutines.CoroutineScope
27
+ import kotlinx.coroutines.Dispatchers
28
+ import kotlinx.coroutines.launch
29
+ import org.json.JSONObject
30
+
31
+ data class GizBindParams(
32
+ @SerializedName("id")
33
+ override val id: String,
34
+ @SerializedName("alias")
35
+ val alias: String,
36
+ @SerializedName("remark")
37
+ val remark: String,
38
+ ) : DeviceParams
39
+
40
+ data class UpdateDeviceInfoParams(
41
+ @SerializedName("id")
42
+ override val id: String,
43
+ @SerializedName("alias")
44
+ val alias: String,
45
+ @SerializedName("remark")
46
+ val remark: String,
47
+ ) : DeviceParams
48
+
49
+ data class GizGetDPParams(
50
+ @SerializedName("id")
51
+ override val id: String,
52
+ @SerializedName("type")
53
+ val type: CapacityTypes,
54
+ @SerializedName("attrs")
55
+ val attrs: JsonArray?,
56
+ ) : DeviceParams
57
+
58
+ data class GizSendDPParams(
59
+ @SerializedName("id")
60
+ override val id: String,
61
+ @SerializedName("type")
62
+ val type: CapacityTypes,
63
+ @SerializedName("data")
64
+ val data: String,
65
+ ) : DeviceParams
66
+
67
+ data class GizUnBindParams(
68
+ @SerializedName("id")
69
+ override val id: String,
70
+ ): DeviceParams
71
+
72
+ data class GizConnectParams(
73
+ @SerializedName("id")
74
+ override val id: String,
75
+ @SerializedName("type")
76
+ val type: CapacityTypes
77
+ ): DeviceParams
78
+ data class GizDisConnectParams(
79
+ @SerializedName("id")
80
+ override val id: String,
81
+ @SerializedName("type")
82
+ val type: CapacityTypes
83
+ ): DeviceParams
84
+
85
+ data class GizRegisterDeviceParams(
86
+ @SerializedName("id")
87
+ override val id: String,
88
+ ): DeviceParams
89
+
90
+ data class GizProvideWiFiCredentialsParams(
91
+ @SerializedName("id")
92
+ override val id: String,
93
+ @SerializedName("type")
94
+ val type: CapacityTypes,
95
+ @SerializedName("ssid")
96
+ val ssid: String,
97
+ @SerializedName("password")
98
+ val password: String,
99
+ @SerializedName("timeout")
100
+ val timeout: Long,
101
+ ): DeviceParams
102
+ data class GizStopProvideWiFiCredentialsParams(
103
+ @SerializedName("id")
104
+ override val id: String,
105
+ @SerializedName("type")
106
+ val type: CapacityTypes,
107
+ ): DeviceParams
108
+
109
+ data class GizGetDeviceInfoParams(
110
+ @SerializedName("id")
111
+ override val id: String,
112
+ @SerializedName("type")
113
+ val type: CapacityTypes
114
+ ): DeviceParams
115
+ data class GizCheckUpdateParams(
116
+ @SerializedName("id")
117
+ override val id: String,
118
+ @SerializedName("type")
119
+ val type: CapacityTypes,
120
+ @SerializedName("firmwareType")
121
+ val firmwareType: GizOTAFirmwareType
122
+ ): DeviceParams
123
+ data class StopUpgradeParams(
124
+ @SerializedName("id")
125
+ override val id: String,
126
+ @SerializedName("type")
127
+ val type: CapacityTypes,
128
+ ): DeviceParams
129
+
130
+
131
+ class RNGizDeviceManagerModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
132
+ enum class EventName(val value: String) {
133
+ GizOTAProgressEvent("GizOTAProgressEvent"),
134
+ GizProvideWiFiCredentialsEvent("GizProvideWiFiCredentialsEvent"),
135
+ }
136
+
137
+ fun jsonObjectToPairs(jsonObject: JsonObject): Array<Pair<String, Any>> {
138
+ val pairs = mutableListOf<Pair<String, Any>>()
139
+ for ((key, value) in jsonObject.entrySet()) {
140
+ val pair = key to value
141
+ pairs.add(pair)
142
+ }
143
+ return pairs.toTypedArray()
144
+ }
145
+
146
+ override fun getName() = "RNGizDeviceManagerModule"
147
+ @ReactMethod
148
+ fun sendDp(options: ReadableMap, result: Callback) {
149
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizSendDPParams::class.java)
150
+ if (config !=null && device !=null) {
151
+ CoroutineScope(Dispatchers.IO).launch {
152
+ try {
153
+ val gson = Gson()
154
+ val jsonObject = gson.fromJson(config.data, JsonObject::class.java)
155
+ val attrs = jsonObjectToPairs(jsonObject)
156
+ val res = when(config.type) {
157
+ CapacityTypes.BLE -> device.bleCapability.sendDp(*attrs)
158
+ CapacityTypes.LAN -> device.lanCapability.sendDp(*attrs)
159
+ CapacityTypes.MQTT -> device.mqttCapability.sendDp(*attrs)
160
+ }
161
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
162
+ }catch (e: Exception) {
163
+ GizRNCallbackManager.callbackWithResult(callback = result, result = Result.failure<Unit>(e))
164
+ }
165
+ }
166
+ }
167
+ }
168
+ @ReactMethod
169
+ fun getDp(options: ReadableMap, result: Callback) {
170
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizGetDPParams::class.java)
171
+ if (config !=null && device !=null) {
172
+ CoroutineScope(Dispatchers.IO).launch {
173
+ var res = when(config.type) {
174
+ CapacityTypes.BLE -> device.bleCapability.getDp(config.attrs)
175
+ CapacityTypes.LAN -> device.lanCapability.getDp(config.attrs)
176
+ CapacityTypes.MQTT -> device.mqttCapability.getDp(config.attrs)
177
+ }
178
+ if (res.isSuccess) {
179
+ var jsonObject = res.getOrNull() ?: JSONObject()
180
+ val newRes = JSONObject()
181
+ newRes.put("success", true)
182
+ newRes.put("data", JSONObject(jsonObject.toString()))
183
+ result.invoke(null, GizRNCallbackManager.jsonObject2WriteableMap(newRes))
184
+ } else {
185
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
186
+ }
187
+ }
188
+ }
189
+ }
190
+ @ReactMethod
191
+ fun getDeviceInfo(options: ReadableMap, result: Callback) {
192
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizGetDeviceInfoParams::class.java)
193
+ if (config !=null && device !=null) {
194
+ CoroutineScope(Dispatchers.IO).launch {
195
+ val res = when(config.type) {
196
+ CapacityTypes.BLE -> device.bleCapability.getDeviceInfo()
197
+ CapacityTypes.LAN -> device.lanCapability.getDeviceInfo()
198
+ CapacityTypes.MQTT -> device.mqttCapability.getDeviceInfo()
199
+ }
200
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
201
+ }
202
+ }
203
+ }
204
+
205
+ @ReactMethod
206
+ fun updateDeviceInfo(options: ReadableMap, result: Callback) {
207
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, UpdateDeviceInfoParams::class.java)
208
+ if (config !=null && device !=null) {
209
+ CoroutineScope(Dispatchers.IO).launch {
210
+ val res = device.updateDeviceInfo(config.alias, config.remark)
211
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
212
+ // if (res.isSuccess) {
213
+ // GizSDKManager.queryBoundDevices()
214
+ // }
215
+ }
216
+ }
217
+ }
218
+
219
+ @ReactMethod
220
+ fun bind(options: ReadableMap, result: Callback) {
221
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizBindParams::class.java)
222
+ if (config !=null && device !=null) {
223
+ CoroutineScope(Dispatchers.IO).launch {
224
+ val res = device.bind(config.alias, config.remark)
225
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
226
+ // if (res.isSuccess) {
227
+ // GizSDKManager.queryBoundDevices()
228
+ // }
229
+ }
230
+ }
231
+ }
232
+
233
+ @ReactMethod
234
+ fun unBind(options: ReadableMap, result: Callback) {
235
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizUnBindParams::class.java)
236
+ if (config !=null && device !=null) {
237
+ CoroutineScope(Dispatchers.IO).launch {
238
+ val res = device.unBind()
239
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
240
+ if (res.isSuccess) {
241
+ GizSDKManager.queryBoundDevices()
242
+ }
243
+ }
244
+ }
245
+ }
246
+ @ReactMethod
247
+ fun stopProvideWiFiCredentials(options: ReadableMap, result: Callback) {
248
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizStopProvideWiFiCredentialsParams::class.java)
249
+ if (config !=null && device !=null) {
250
+ CoroutineScope(Dispatchers.IO).launch {
251
+ val res = when(config.type) {
252
+ CapacityTypes.BLE -> device.bleCapability.stopProvideWiFiCredentials()
253
+ CapacityTypes.LAN -> device.lanCapability.stopProvideWiFiCredentials()
254
+ CapacityTypes.MQTT -> device.mqttCapability.stopProvideWiFiCredentials()
255
+ }
256
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
257
+ }
258
+ }
259
+ }
260
+
261
+ fun wiFiCredentialsEventHandler(event: ProvideWiFiCredentialsEvent, device: GizDevice) {
262
+ val jsonObject = JSONObject()
263
+ jsonObject.put("data", event.toString())
264
+ jsonObject.put("device", device.toJsonObject())
265
+ sendEvent(
266
+ EventName.GizProvideWiFiCredentialsEvent.name,
267
+ GizRNCallbackManager.jsonObject2WriteableMap(jsonObject)
268
+ )
269
+ }
270
+ @ReactMethod
271
+ fun provideWiFiCredentials(options: ReadableMap, result: Callback) {
272
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizProvideWiFiCredentialsParams::class.java)
273
+ if (config !=null && device !=null) {
274
+ CoroutineScope(Dispatchers.IO).launch {
275
+ val res = when(config.type) {
276
+ CapacityTypes.BLE -> device.bleCapability.provideWiFiCredentials(
277
+ config.ssid,
278
+ config.password,
279
+ config.timeout,
280
+ processHandler = {
281
+ wiFiCredentialsEventHandler(it, device)
282
+ }
283
+ )
284
+ CapacityTypes.LAN -> device.lanCapability.provideWiFiCredentials(
285
+ config.ssid,
286
+ config.password,
287
+ config.timeout,
288
+ processHandler = {
289
+ wiFiCredentialsEventHandler(it, device)
290
+ }
291
+ )
292
+ CapacityTypes.MQTT -> device.mqttCapability.provideWiFiCredentials(
293
+ config.ssid,
294
+ config.password,
295
+ config.timeout,
296
+ processHandler = {
297
+ wiFiCredentialsEventHandler(it, device)
298
+ }
299
+ )
300
+ }
301
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
302
+ }
303
+ }
304
+ }
305
+
306
+ @ReactMethod
307
+ fun register(options: ReadableMap, result: Callback) {
308
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizRegisterDeviceParams::class.java)
309
+ if (config !=null && device !=null) {
310
+ CoroutineScope(Dispatchers.IO).launch {
311
+ val res = device.register()
312
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
313
+ }
314
+ }
315
+ }
316
+
317
+ @ReactMethod
318
+ fun connect(options: ReadableMap, result: Callback) {
319
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizConnectParams::class.java)
320
+ if (config !=null && device !=null) {
321
+ CoroutineScope(Dispatchers.IO).launch {
322
+ val res = when(config.type) {
323
+ CapacityTypes.BLE -> device.bleCapability.connect()
324
+ CapacityTypes.LAN -> device.lanCapability.connect()
325
+ CapacityTypes.MQTT -> device.mqttCapability.connect()
326
+ }
327
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
328
+ }
329
+ }
330
+ }
331
+ @ReactMethod
332
+ fun disConnect(options: ReadableMap, result: Callback) {
333
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizDisConnectParams::class.java)
334
+ if (config !=null && device !=null) {
335
+ CoroutineScope(Dispatchers.IO).launch {
336
+ val res = when(config.type) {
337
+ CapacityTypes.BLE -> device.bleCapability.disConnect()
338
+ CapacityTypes.LAN -> device.lanCapability.disConnect()
339
+ CapacityTypes.MQTT -> device.mqttCapability.disConnect()
340
+ }
341
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
342
+ }
343
+ }
344
+ }
345
+
346
+ @ReactMethod
347
+ fun checkUpdate(options: ReadableMap, result: Callback) {
348
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizCheckUpdateParams::class.java)
349
+ if (config !=null && device !=null) {
350
+ CoroutineScope(Dispatchers.IO).launch {
351
+ val res = when(config.type) {
352
+ CapacityTypes.BLE -> device.bleCapability.checkUpdate(config.firmwareType)
353
+ CapacityTypes.LAN -> device.lanCapability.checkUpdate(config.firmwareType)
354
+ CapacityTypes.MQTT -> device.mqttCapability.checkUpdate(config.firmwareType)
355
+ }
356
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
357
+ }
358
+ }
359
+ }
360
+
361
+ fun handleStartUpgradeProgress(event: OTAProgressData, device: GizDevice) {
362
+ val jsonObject = JSONObject()
363
+ val dataObject = JSONObject()
364
+ dataObject.put("percentage", event.percentage)
365
+ dataObject.put("event", event.event.value)
366
+ jsonObject.put("data", dataObject)
367
+ jsonObject.put("device", device.toJsonObject())
368
+ sendEvent(
369
+ EventName.GizOTAProgressEvent.name,
370
+ GizRNCallbackManager.jsonObject2WriteableMap(jsonObject)
371
+ )
372
+ }
373
+ @ReactMethod
374
+ fun startUpgrade(options: ReadableMap, result: Callback) {
375
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, GizCheckUpdateParams::class.java)
376
+ if (config !=null && device !=null) {
377
+ CoroutineScope(Dispatchers.IO).launch {
378
+ val res = when(config.type) {
379
+ CapacityTypes.BLE -> {
380
+ device.bleCapability.startUpgrade(config.firmwareType, processHandler = {
381
+ handleStartUpgradeProgress(it, device)
382
+ })
383
+ }
384
+ CapacityTypes.LAN -> device.lanCapability.startUpgrade(config.firmwareType, processHandler = {
385
+ handleStartUpgradeProgress(it, device)
386
+ })
387
+ CapacityTypes.MQTT -> device.mqttCapability.startUpgrade(config.firmwareType, processHandler = {
388
+ handleStartUpgradeProgress(it, device)
389
+ })
390
+ }
391
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
392
+ }
393
+ }
394
+ }
395
+ @ReactMethod
396
+ fun cancelUpgrade(options: ReadableMap, result: Callback) {
397
+ val (config, device) = RNGizParamsChecker.checkDeviceAndParams(options, result, StopUpgradeParams::class.java)
398
+ if (config !=null && device !=null) {
399
+ CoroutineScope(Dispatchers.IO).launch {
400
+ val res = when(config.type) {
401
+ CapacityTypes.BLE -> device.bleCapability.cancelUpgrade()
402
+ CapacityTypes.LAN -> device.lanCapability.cancelUpgrade()
403
+ CapacityTypes.MQTT -> device.mqttCapability.cancelUpgrade()
404
+ }
405
+ GizRNCallbackManager.callbackWithResult(callback = result, result = res)
406
+ }
407
+ }
408
+ }
409
+
410
+ fun sendEvent(name:String, data: WritableMap?) {
411
+ reactApplicationContext
412
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
413
+ .emit(name, data)
414
+ }
415
+
416
+ }
@@ -0,0 +1,58 @@
1
+ package com.gizwits.reactnativegizwitssdkv5
2
+ import com.facebook.react.bridge.Callback
3
+ import com.facebook.react.bridge.ReadableMap
4
+ import com.facebook.react.bridge.ReadableNativeMap
5
+ import com.gizwits.smart.sdk.GizDevice
6
+ import com.gizwits.smart.sdk.GizSDKManager
7
+ import com.gizwits.smart.sdk.exception.GizException
8
+ import com.gizwits.smart.sdk.exception.GizParseException
9
+ import com.google.gson.Gson
10
+ import com.google.gson.JsonSyntaxException
11
+ import com.google.gson.annotations.SerializedName
12
+
13
+ interface DeviceParams{
14
+ val id: String
15
+ }
16
+ class RNGizParamsChecker {
17
+ companion object {
18
+
19
+ private fun convertReadableMapToJson(readableMap: ReadableMap): String {
20
+ val readableNativeMap = readableMap as ReadableNativeMap
21
+ val hashMap = readableNativeMap.toHashMap()
22
+ val gson = Gson()
23
+ return gson.toJson(hashMap)
24
+ }
25
+
26
+ fun <T : Any> check(options: ReadableMap, result: Callback, paramsType: Class<T>): T? {
27
+ val gson = Gson()
28
+ val jsonString = convertReadableMapToJson(options)
29
+ try {
30
+ return gson.fromJson(jsonString, paramsType)
31
+ } catch (e: JsonSyntaxException) {
32
+ // 参数错误
33
+ GizRNCallbackManager.callbackWithResult(
34
+ callback = result,
35
+ result = Result.failure<Number>(GizException.GIZ_SDK_PARAM_INVALID)
36
+ )
37
+ return null
38
+ }
39
+ }
40
+
41
+ fun <T : DeviceParams> checkDeviceAndParams(options: ReadableMap, result: Callback, paramsType: Class<T>): Pair<T?, GizDevice?> {
42
+ val config = check(options, result, paramsType)
43
+ var dev: GizDevice? = null
44
+ config?.let {
45
+ dev = GizSDKManager.getRegisteredDevice(config.id)
46
+ if (dev == null) {
47
+ GizRNCallbackManager.callbackWithResult(
48
+ callback = result,
49
+ result = Result.failure<Number>(GizException.GIZ_OPENAPI_DEVICE_NOT_FOUND)
50
+ )
51
+ }
52
+ }
53
+ return Pair(config, dev)
54
+ }
55
+
56
+
57
+ }
58
+ }