@rentlydev/rently-tuya 0.2.3 → 0.2.4

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.
@@ -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.")
@@ -191,11 +200,44 @@ class Tuya: NSObject, ObservableObject {
191
200
  }
192
201
 
193
202
  guard let p2pType = device.deviceModel.skills["p2pType"] else {
194
- print("Tuya: Missing p2pType")
195
- self.updateStatus(isConnected: false, previewLoader: false)
203
+ print("Tuya: Device model missing required data, attempting to re-sync device (Retry: \(resyncRetryCount)/\(maxResyncRetries))")
204
+
205
+ guard resyncRetryCount < maxResyncRetries,
206
+ !isResyncing,
207
+ let deviceId = storedDeviceId,
208
+ let homeId = storedHomeId else {
209
+ if resyncRetryCount >= maxResyncRetries {
210
+ print("Tuya: Max re-sync attempts reached. Resetting retry count.")
211
+ resyncRetryCount = 0
212
+ } else {
213
+ print("Tuya: Cannot re-sync - missing stored device info or already resyncing")
214
+ }
215
+ self.updateStatus(isConnected: false, previewLoader: false)
216
+ return
217
+ }
218
+
219
+ isResyncing = true
220
+ resyncRetryCount += 1
221
+ previewLoader = true
222
+
223
+ syncDevice(deviceId: deviceId, homeId: homeId) { success, message, error in
224
+ self.isResyncing = false
225
+
226
+ if success {
227
+ print("Tuya: Re-sync successful, attempting to connect again")
228
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
229
+ self.connect()
230
+ }
231
+ } else {
232
+ print("Tuya: Re-sync failed: \(message ?? "Unknown error")")
233
+ self.updateStatus(isConnected: false, previewLoader: false)
234
+ }
235
+ }
196
236
  return
197
237
  }
198
238
 
239
+ resyncRetryCount = 0
240
+
199
241
  previewLoader = true
200
242
 
201
243
  device.awake {
@@ -393,6 +435,8 @@ class Tuya: NSObject, ObservableObject {
393
435
  isSdCardPopUpFormatClicked: false
394
436
  )
395
437
 
438
+ self.resyncRetryCount = 0
439
+ self.isResyncing = false
396
440
  self.resetFirmwareValues()
397
441
  }
398
442
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rentlydev/rently-tuya",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "rently-tuya",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/commonjs/index.js",