-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathota_update.py
More file actions
433 lines (356 loc) · 12.4 KB
/
ota_update.py
File metadata and controls
433 lines (356 loc) · 12.4 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
"""
OTA Update System for PSIC-O-TRONIC
Actualiza el firmware desde GitHub
"""
import ujson as json
import os
import gc
import machine
# Configuración del repositorio
GITHUB_USER = "rodillo69"
GITHUB_REPO = "psic-o-tronic"
GITHUB_BRANCH = "main"
GITHUB_RAW_URL = "https://raw.githubusercontent.com/{}/{}/{}/".format(
GITHUB_USER, GITHUB_REPO, GITHUB_BRANCH
)
# Archivo de versión local
VERSION_FILE = "/version.json"
REMOTE_VERSION_FILE = "version.json"
# Versión actual (fallback si no existe version.json)
CURRENT_VERSION = "1.0.0"
def get_local_version():
"""Obtiene la versión instalada localmente"""
try:
with open(VERSION_FILE, "r") as f:
data = json.load(f)
return data.get("version", CURRENT_VERSION)
except:
return CURRENT_VERSION
def save_local_version(version, files_updated=None):
"""Guarda la versión local"""
try:
data = {
"version": version,
"files_updated": files_updated or []
}
with open(VERSION_FILE, "w") as f:
json.dump(data, f)
return True
except Exception as e:
print("[OTA] Error guardando versión:", e)
return False
def fetch_url(url, timeout=15):
"""Descarga contenido de una URL"""
try:
import urequests
gc.collect()
response = urequests.get(url, timeout=timeout)
if response.status_code == 200:
content = response.text
response.close()
return content
else:
response.close()
return None
except Exception as e:
print("[OTA] Error fetch:", e)
return None
def fetch_binary(url, timeout=30):
"""Descarga contenido binario de una URL"""
try:
import urequests
gc.collect()
response = urequests.get(url, timeout=timeout)
if response.status_code == 200:
content = response.content
response.close()
return content
else:
response.close()
return None
except Exception as e:
print("[OTA] Error fetch binary:", e)
return None
def check_for_updates(callback=None):
"""
Verifica si hay actualizaciones disponibles.
Args:
callback: Función opcional para reportar progreso
Returns:
dict con info de actualización o None si no hay/error
{
"available": True/False,
"current_version": "1.0.0",
"new_version": "1.1.0",
"files": ["file1.py", "file2.py"],
"changelog": "Cambios...",
"mandatory": False
}
"""
if callback:
callback("Conectando...")
# Obtener versión remota
url = GITHUB_RAW_URL + REMOTE_VERSION_FILE
print("[OTA] Checking:", url)
content = fetch_url(url)
if not content:
print("[OTA] No se pudo obtener version.json remoto")
return None
try:
remote_data = json.loads(content)
except:
print("[OTA] Error parseando version.json")
return None
current = get_local_version()
remote = remote_data.get("version", "0.0.0")
# Comparar versiones
current_parts = [int(x) for x in current.split(".")]
remote_parts = [int(x) for x in remote.split(".")]
# Padding para comparar
while len(current_parts) < 3:
current_parts.append(0)
while len(remote_parts) < 3:
remote_parts.append(0)
has_update = remote_parts > current_parts
return {
"available": has_update,
"current_version": current,
"new_version": remote,
"files": remote_data.get("files", []),
"changelog": remote_data.get("changelog", ""),
"mandatory": remote_data.get("mandatory", False),
"size_kb": remote_data.get("size_kb", 0)
}
def download_file(filename, callback=None):
"""
Descarga un archivo desde GitHub.
Args:
filename: Nombre del archivo a descargar
callback: Función para reportar progreso
Returns:
True si éxito, False si error
"""
url = GITHUB_RAW_URL + filename
print("[OTA] Downloading:", filename)
if callback:
callback("Bajando " + filename[:12])
gc.collect()
content = fetch_url(url, timeout=30)
if content is None:
print("[OTA] Error descargando:", filename)
return False
# Guardar archivo
try:
# Backup del archivo existente
backup_name = filename + ".bak"
try:
os.rename(filename, backup_name)
except:
pass # No existe el original
# Escribir nuevo archivo
with open(filename, "w") as f:
f.write(content)
# Eliminar backup si todo OK
try:
os.remove(backup_name)
except:
pass
print("[OTA] OK:", filename)
return True
except Exception as e:
print("[OTA] Error guardando:", filename, e)
# Restaurar backup
try:
os.remove(filename)
os.rename(backup_name, filename)
except:
pass
return False
def perform_update(update_info, callback=None):
"""
Realiza la actualización completa.
Args:
update_info: Dict con info de actualización (de check_for_updates)
callback: Función para reportar progreso
Returns:
(success: bool, message: str)
"""
if not update_info or not update_info.get("available"):
return False, "No hay actualizacion"
files = update_info.get("files", [])
if not files:
return False, "Sin archivos"
total = len(files)
success_count = 0
failed_files = []
for i, filename in enumerate(files):
if callback:
callback("({}/{}) {}".format(i + 1, total, filename[:10]))
gc.collect()
if download_file(filename, callback):
success_count += 1
else:
failed_files.append(filename)
if success_count == total:
# Todo OK - guardar nueva versión
save_local_version(update_info["new_version"], files)
return True, "OK v" + update_info["new_version"]
elif success_count > 0:
# Parcial
save_local_version(update_info["new_version"],
[f for f in files if f not in failed_files])
return True, "Parcial {}/{}".format(success_count, total)
else:
return False, "Error descarga"
def reboot():
"""Reinicia el dispositivo"""
print("[OTA] Reiniciando...")
machine.reset()
# === Clase para integración con UI ===
class OTAManager:
"""Manager para integración con la UI del juego"""
def __init__(self):
self.update_info = None
self.last_check = 0
self.check_interval = 3600 # 1 hora entre checks automáticos
self.is_checking = False
self.is_updating = False
self.status_msg = ""
# Estado para actualización paso a paso
self._update_files = []
self._update_index = 0
self._update_success = 0
self._update_failed = []
self._update_started = False
def should_auto_check(self):
"""Determina si es momento de verificar actualizaciones"""
import time
now = time.time()
return (now - self.last_check) > self.check_interval
def check_async(self, callback=None):
"""Inicia verificación de actualizaciones"""
if self.is_checking or self.is_updating:
return
self.is_checking = True
self.status_msg = "Verificando..."
import time
self.last_check = time.time()
try:
self.update_info = check_for_updates(callback)
if self.update_info and self.update_info.get("available"):
self.status_msg = "v" + self.update_info["new_version"] + " disponible"
else:
self.status_msg = "Al dia"
except Exception as e:
print("[OTA] Error check:", e)
self.status_msg = "Error conexion"
self.update_info = None
self.is_checking = False
def has_update(self):
"""Retorna True si hay actualización disponible"""
return self.update_info and self.update_info.get("available", False)
def get_update_info(self):
"""Retorna info de la actualización"""
return self.update_info
def perform_update(self, callback=None):
"""Ejecuta la actualización"""
if self.is_updating or not self.has_update():
return False, "No disponible"
self.is_updating = True
self.status_msg = "Actualizando..."
try:
success, msg = perform_update(self.update_info, callback)
self.status_msg = msg
if success:
self.update_info = None
return success, msg
except Exception as e:
self.status_msg = "Error: " + str(e)[:15]
return False, str(e)
finally:
self.is_updating = False
def get_current_version(self):
"""Retorna versión actual"""
return get_local_version()
# === Métodos para actualización paso a paso ===
def start_update_steps(self):
"""
Inicia actualización paso a paso.
Returns: (total_files, new_version) o (0, None) si error
"""
if not self.has_update():
return 0, None
self._update_files = self.update_info.get("files", [])
self._update_index = 0
self._update_success = 0
self._update_failed = []
self._update_started = True
self.is_updating = True
return len(self._update_files), self.update_info.get("new_version", "?")
def update_next_file(self):
"""
Descarga el siguiente archivo.
Returns: (done, current_index, total, filename, success)
- done: True si terminó (no hay más archivos)
- current_index: índice actual (1-based)
- total: total de archivos
- filename: nombre del archivo descargado
- success: True si este archivo se descargó OK
"""
if not self._update_started or self._update_index >= len(self._update_files):
return True, 0, 0, "", False
filename = self._update_files[self._update_index]
total = len(self._update_files)
current = self._update_index + 1
self.status_msg = f"({current}/{total}) {filename[:12]}"
print(f"[OTA] Downloading {current}/{total}: {filename}")
gc.collect()
success = download_file(filename)
if success:
self._update_success += 1
else:
self._update_failed.append(filename)
self._update_index += 1
done = self._update_index >= len(self._update_files)
return done, current, total, filename, success
def finish_update_steps(self):
"""
Finaliza la actualización paso a paso.
Returns: (success, message)
"""
if not self._update_started:
return False, "No iniciada"
total = len(self._update_files)
success_count = self._update_success
if success_count == total:
save_local_version(self.update_info["new_version"], self._update_files)
msg = "OK v" + self.update_info["new_version"]
result = True
elif success_count > 0:
ok_files = [f for f in self._update_files if f not in self._update_failed]
save_local_version(self.update_info["new_version"], ok_files)
msg = f"Parcial {success_count}/{total}"
result = True
else:
msg = "Error descarga"
result = False
# Reset estado
self._update_started = False
self.is_updating = False
if result:
self.update_info = None
self.status_msg = msg
return result, msg
def get_update_progress(self):
"""
Obtiene progreso actual.
Returns: (current, total, percent)
"""
if not self._update_started:
return 0, 0, 0
total = len(self._update_files)
current = self._update_index
percent = int((current / total) * 100) if total > 0 else 0
return current, total, percent
# Instancia global
ota_manager = OTAManager()