@rentlydev/rently-tuya 0.2.3 → 0.2.5
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/ios/tuya/Tuya.swift +63 -7
- package/package.json +1 -1
package/ios/tuya/Tuya.swift
CHANGED
|
@@ -16,6 +16,12 @@ class Tuya: NSObject, ObservableObject {
|
|
|
16
16
|
var cameraType: ThingSmartCameraType?
|
|
17
17
|
var messages: ThingSmartCameraMessage?
|
|
18
18
|
|
|
19
|
+
private var storedDeviceId: String?
|
|
20
|
+
private var storedHomeId: Int64?
|
|
21
|
+
private var isResyncing: Bool = false
|
|
22
|
+
private var resyncRetryCount: Int = 0
|
|
23
|
+
private let maxResyncRetries: Int = 2
|
|
24
|
+
|
|
19
25
|
@Published var isSettingApplied: Bool = false
|
|
20
26
|
@Published var isConnected: Bool = false
|
|
21
27
|
@Published var isPreviewing: Bool = false
|
|
@@ -157,6 +163,9 @@ class Tuya: NSObject, ObservableObject {
|
|
|
157
163
|
homeId: Int64,
|
|
158
164
|
completion: @escaping (Bool, String?, Error?) -> Void
|
|
159
165
|
) {
|
|
166
|
+
self.storedDeviceId = deviceId
|
|
167
|
+
self.storedHomeId = homeId
|
|
168
|
+
|
|
160
169
|
ThingSmartDevice.syncDeviceInfo(withDevId: deviceId, homeId: homeId) {
|
|
161
170
|
|
|
162
171
|
print("Tuya: Device sync successful.")
|
|
@@ -190,14 +199,51 @@ class Tuya: NSObject, ObservableObject {
|
|
|
190
199
|
return
|
|
191
200
|
}
|
|
192
201
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
202
|
+
let deviceModel = device.deviceModel
|
|
203
|
+
guard let skills = deviceModel.skills,
|
|
204
|
+
let p2pType = skills["p2pType"] else {
|
|
205
|
+
print("Tuya: Device model missing required data, attempting to re-sync device (Retry: \(resyncRetryCount)/\(maxResyncRetries))")
|
|
206
|
+
|
|
207
|
+
guard resyncRetryCount < maxResyncRetries,
|
|
208
|
+
!isResyncing,
|
|
209
|
+
let deviceId = storedDeviceId,
|
|
210
|
+
let homeId = storedHomeId else {
|
|
211
|
+
if resyncRetryCount >= maxResyncRetries {
|
|
212
|
+
print("Tuya: Max re-sync attempts reached. Resetting retry count.")
|
|
213
|
+
resyncRetryCount = 0
|
|
214
|
+
} else {
|
|
215
|
+
print("Tuya: Cannot re-sync - missing stored device info or already resyncing")
|
|
216
|
+
}
|
|
217
|
+
self.updateStatus(isConnected: false, previewLoader: false)
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
isResyncing = true
|
|
222
|
+
resyncRetryCount += 1
|
|
223
|
+
previewLoader = true
|
|
224
|
+
|
|
225
|
+
syncDevice(deviceId: deviceId, homeId: homeId) { success, message, error in
|
|
226
|
+
self.isResyncing = false
|
|
227
|
+
|
|
228
|
+
if success {
|
|
229
|
+
print("Tuya: Re-sync successful, attempting to connect again")
|
|
230
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
231
|
+
self.connect()
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
print("Tuya: Re-sync failed: \(message ?? "Unknown error")")
|
|
235
|
+
self.updateStatus(isConnected: false, previewLoader: false)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
196
238
|
return
|
|
197
239
|
}
|
|
198
240
|
|
|
241
|
+
resyncRetryCount = 0
|
|
242
|
+
|
|
199
243
|
previewLoader = true
|
|
200
244
|
|
|
245
|
+
let deviceId = device.devId
|
|
246
|
+
|
|
201
247
|
device.awake {
|
|
202
248
|
print("Tuya: Device wake success")
|
|
203
249
|
} failure: { error in
|
|
@@ -205,7 +251,7 @@ class Tuya: NSObject, ObservableObject {
|
|
|
205
251
|
}
|
|
206
252
|
|
|
207
253
|
let request = ThingSmartRequest()
|
|
208
|
-
request.request(withApiName: "tuya.m.ipc.config.get", postData: ["devId":
|
|
254
|
+
request.request(withApiName: "tuya.m.ipc.config.get", postData: ["devId": deviceId], version: "2.0") { result in
|
|
209
255
|
guard let configData = result else {
|
|
210
256
|
print("Tuya: Config data missing")
|
|
211
257
|
self.updateStatus(isConnected: false, previewLoader: false)
|
|
@@ -222,14 +268,22 @@ class Tuya: NSObject, ObservableObject {
|
|
|
222
268
|
}
|
|
223
269
|
}
|
|
224
270
|
|
|
225
|
-
// Configure the camera
|
|
226
271
|
private func configureCamera(p2pType: Any, configData: Any) {
|
|
272
|
+
guard let device = self.device else {
|
|
273
|
+
print("Tuya: Camera config creation failed - device is nil")
|
|
274
|
+
self.updateStatus(isConnected: false, previewLoader: false)
|
|
275
|
+
return
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
let deviceModel = device.deviceModel
|
|
279
|
+
let localKey = deviceModel.localKey
|
|
280
|
+
|
|
227
281
|
guard let config = ThingSmartCameraFactory.ipcConfig(
|
|
228
282
|
withUid: ThingSmartUser.sharedInstance().uid,
|
|
229
|
-
localKey:
|
|
283
|
+
localKey: localKey,
|
|
230
284
|
configData: configData as? [AnyHashable: Any]
|
|
231
285
|
) else {
|
|
232
|
-
print("Tuya: Camera config creation failed")
|
|
286
|
+
print("Tuya: Camera config creation failed - config is nil")
|
|
233
287
|
self.updateStatus(isConnected: false, previewLoader: false)
|
|
234
288
|
return
|
|
235
289
|
}
|
|
@@ -393,6 +447,8 @@ class Tuya: NSObject, ObservableObject {
|
|
|
393
447
|
isSdCardPopUpFormatClicked: false
|
|
394
448
|
)
|
|
395
449
|
|
|
450
|
+
self.resyncRetryCount = 0
|
|
451
|
+
self.isResyncing = false
|
|
396
452
|
self.resetFirmwareValues()
|
|
397
453
|
}
|
|
398
454
|
}
|