Update src/tracker_dispatcher.py: circuit breaker, weather fix, TB health fix
Browse files- src/tracker_dispatcher.py +15 -7
src/tracker_dispatcher.py
CHANGED
|
@@ -197,12 +197,20 @@ class TrackerDispatcher:
|
|
| 197 |
# poll setAngle/setMode from shared attributes on their update cycle.
|
| 198 |
# RPC requires the device to be online for real-time communication,
|
| 199 |
# which is not guaranteed.
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
# Fallback: try RPC (may timeout if device is offline)
|
| 208 |
try:
|
|
@@ -210,7 +218,7 @@ class TrackerDispatcher:
|
|
| 210 |
tr.method = "rpc"
|
| 211 |
return tr
|
| 212 |
except Exception as rpc_exc:
|
| 213 |
-
tr.error = f"attribute and RPC both failed: {attr_exc}; {rpc_exc}"
|
| 214 |
logger.error("Cannot send to %s: %s", device_name, tr.error)
|
| 215 |
return tr
|
| 216 |
|
|
|
|
| 197 |
# poll setAngle/setMode from shared attributes on their update cycle.
|
| 198 |
# RPC requires the device to be online for real-time communication,
|
| 199 |
# which is not guaranteed.
|
| 200 |
+
# Retry attribute write with exponential backoff (1s, 2s)
|
| 201 |
+
attr_exc = None
|
| 202 |
+
for attempt in range(3):
|
| 203 |
+
try:
|
| 204 |
+
client.set_device_attributes(device_name, {"setAngle": angle, "setMode": "manual"})
|
| 205 |
+
tr.method = "attribute"
|
| 206 |
+
return tr
|
| 207 |
+
except Exception as exc:
|
| 208 |
+
attr_exc = exc
|
| 209 |
+
if attempt < 2:
|
| 210 |
+
time.sleep(1 << attempt) # 1s, 2s
|
| 211 |
+
logger.warning("Attribute write retry %d for %s: %s", attempt + 1, device_name, exc)
|
| 212 |
+
|
| 213 |
+
logger.warning("Attribute write failed for %s after 3 attempts: %s, trying RPC", device_name, attr_exc)
|
| 214 |
|
| 215 |
# Fallback: try RPC (may timeout if device is offline)
|
| 216 |
try:
|
|
|
|
| 218 |
tr.method = "rpc"
|
| 219 |
return tr
|
| 220 |
except Exception as rpc_exc:
|
| 221 |
+
tr.error = f"attribute (3 retries) and RPC both failed: {attr_exc}; {rpc_exc}"
|
| 222 |
logger.error("Cannot send to %s: %s", device_name, tr.error)
|
| 223 |
return tr
|
| 224 |
|