-
Notifications
You must be signed in to change notification settings - Fork 1
/
airrohr-flasher.py
executable file
·526 lines (399 loc) · 18.7 KB
/
airrohr-flasher.py
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#!/usr/bin/env python3
# -* encoding: utf-8 *-
import sys
import os.path
import os
import time
import tempfile
import hashlib
import zlib
import logging
import json
import requests
from esptool import ESPLoader, erase_flash, write_flash
import airrohrFlasher
from airrohrFlasher.qtvariant import QtGui, QtCore, QtWidgets
from airrohrFlasher.utils import QuickThread
from airrohrFlasher.workers import PortDetectThread, FirmwareListThread, \
ZeroconfDiscoveryThread
from gui import mainwindow
from airrohrFlasher.consts import UPDATE_REPOSITORY, ALLOWED_PROTO, \
PREFERED_PORTS, ROLE_DEVICE, DRIVERS_URL
import spiffsGen
from spiffsGen import spiffsgen
#from spiffsgen import _____________
if getattr(sys, 'frozen', False):
RESOURCES_PATH = sys._MEIPASS
else:
RESOURCES_PATH = os.path.dirname(os.path.realpath(__file__))
class MainWindow(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
uploadProgress = QtCore.Signal([str, int])
errorSignal = QtCore.Signal([str])
uploadThread = None
zeroconf_discovery = None
boards_detected = False
jsonFinal = ""
spiffsBinary = b''
def __init__(self, parent=None, app=None):
super(MainWindow, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.Dialog)
# FIXME: dirty hack to solve relative paths in *.ui
oldcwd = os.getcwd()
os.chdir(os.path.join(RESOURCES_PATH, 'assets'))
self.setupUi(self)
os.chdir(oldcwd)
self.app = app
self.translator = QtCore.QTranslator()
self.i18n_init(QtCore.QLocale.system())
self.statusbar.showMessage(self.tr("Loading firmware list..."))
self.versionBox.clear()
self.firmware_list = FirmwareListThread()
self.firmware_list.listLoaded.connect(self.populate_versions)
self.firmware_list.error.connect(self.on_work_error)
self.firmware_list.start()
self.port_detect = PortDetectThread()
self.port_detect.portsUpdate.connect(self.populate_boards)
self.port_detect.error.connect(self.on_work_error)
self.port_detect.start()
self.discovery_start()
self.globalMessage.hide()
# Hide WIP GUI parts...
self.on_expertModeBox_clicked()
self.expertModeBox.hide()
self.tabWidget.removeTab(self.tabWidget.indexOf(self.serialTab))
self.uploadProgress.connect(self.on_work_update)
self.errorSignal.connect(self.on_work_error)
self.cachedir = tempfile.TemporaryDirectory()
self.cachedirjson = tempfile.TemporaryDirectory()
self.cachedirspiffs = tempfile.TemporaryDirectory()
print(self.cachedir.name)
print(self.cachedirjson.name)
print(self.cachedirspiffs.name)
def show_global_message(self, title, message):
self.globalMessage.show()
self.globalMessageTitle.setText(title)
self.globalMessageText.setText(message)
def on_work_update(self, status, progress):
self.statusbar.showMessage(status)
self.progressBar.setValue(progress)
def on_work_error(self, message):
self.statusbar.showMessage(message)
@property
def version(self):
return airrohrFlasher.__version__
@property
def build_id(self):
try:
from airrohrFlasher._buildid import commit, builddate
except ImportError:
import datetime
commit = 'devel'
builddate = datetime.datetime.now().strftime('%Y%m%d')
return '{}-{}/{}'.format(self.version, commit, builddate)
def i18n_init(self, locale):
"""Initializes i18n to specified QLocale"""
self.app.removeTranslator(self.translator)
lang = QtCore.QLocale.languageToString(locale.language())
self.translator.load(os.path.join(
RESOURCES_PATH, 'i18n', lang + '.qm'))
self.app.installTranslator(self.translator)
self.retranslateUi(self)
def retranslateUi(self, win):
super(MainWindow, self).retranslateUi(win)
win.setWindowTitle(win.windowTitle().format(
version=self.version))
win.buildLabel.setText(win.buildLabel.text().format(
build_id=self.build_id))
def populate_versions(self, files):
"""Loads available firmware versions into versionbox widget"""
for fname in files:
if not fname.endswith('.bin'):
continue
item = QtGui.QStandardItem(fname)
item.setData(UPDATE_REPOSITORY + fname, ROLE_DEVICE)
self.versionBox.model().appendRow(item)
self.statusbar.clearMessage()
def populate_boards(self, ports):
"""Populates board selection combobox from list of pyserial
ListPortInfo objects"""
self.boardBox.clear()
prefered, others = self.group_ports(ports)
for b in prefered:
item = QtGui.QStandardItem(
'{0.description} ({0.device})'.format(b))
item.setData(b.device, ROLE_DEVICE)
self.boardBox.model().appendRow(item)
if not prefered:
sep = QtGui.QStandardItem(self.tr('No boards found'))
sep.setEnabled(False)
self.boardBox.model().appendRow(sep)
# No prefered boards has been found so far and there is a
# suggested driver download URL available
if not self.boards_detected and DRIVERS_URL:
self.show_global_message(
self.tr('No boards found'),
self.tr('Have you installed <a href="{drivers_url}">'
'the drivers</a>?').format(drivers_url=DRIVERS_URL))
else:
self.globalMessage.hide()
self.boards_detected = True
if others:
sep = QtGui.QStandardItem(self.tr('Others...'))
sep.setEnabled(False)
self.boardBox.model().appendRow(sep)
for b in others:
item = QtGui.QStandardItem(
'{0.description} ({0.device})'.format(b))
item.setData(b.device, ROLE_DEVICE)
self.boardBox.model().appendRow(item)
def group_ports(self, ports):
prefered = []
others = []
for p in ports:
if (p.vid, p.pid) in PREFERED_PORTS:
prefered.append(p)
else:
others.append(p)
return prefered, others
def is_json(self, myjson):
try:
json_object = json.loads(myjson)
except ValueError as e:
return False
return True
#Credentials saver
@QtCore.Slot()
def on_wifiButton_clicked(self):
self.statusbar.clearMessage()
ssid = self.wifiSSID.text()
pw = self.wifiPW.text()
if not ssid:
self.statusbar.showMessage(self.tr("No SSID typed."))
return
if not pw:
self.statusbar.showMessage(self.tr("No PW type."))
return
esp = ESPLoader.detect_chip(self.boardBox.currentData(ROLE_DEVICE), min(ESPLoader.ESP_ROM_BAUD, 460800), 'default_reset', False)
jsonPart1 = '{"SOFTWARE_VERSION":"NRZ-2020-133","current_lang":"EN","wlanssid":"'
jsonPart2 = '","wlanpwd":"'
jsonPart3 = '","www_username":"admin","www_password":"","fs_ssid":"'
jsonPart4 = '","fs_pwd":"","www_basicauth_enabled":false,"dht_read":false,"htu21d_read":false,"ppd_read":false,"sds_read":true,"pms_read":false,"hpm_read":false,"npm_read":false,"sps30_read":false,"bmp_read":false,"bmx280_read":true,"sht3x_read":false,"ds18b20_read":false,"dnms_read":false,"dnms_correction":"0.0","temp_correction":"0.0","gps_read":false,"send2dusti":true,"ssl_dusti":false,"send2madavi":true,"ssl_madavi":false,"send2sensemap":false,"send2fsapp":false,"send2aircms":false,"send2csv":false,"auto_update":true,"use_beta":false,"has_display":false,"has_sh1106":false,"has_flipped_display":false,"has_lcd1602":false,"has_lcd1602_27":false,"has_lcd2004":false,"has_lcd2004_27":false,"display_wifi_info":true,"display_device_info":true,"debug":3,"sending_intervall_ms":145000,"time_for_wifi_config":600000,"senseboxid":"","send2custom":false,"host_custom":"192.168.234.1","url_custom":"/data.php","port_custom":80,"user_custom":"","pwd_custom":"","ssl_custom":false,"send2influx":false,"host_influx":"influx.server","url_influx":"/write?db=sensorcommunity","port_influx":8086,"user_influx":"","pwd_influx":"","measurement_name_influx":"feinstaub","ssl_influx":false}'
jsonTest = jsonPart1 + ssid + jsonPart2 + pw + jsonPart3 + "airRohr-"+ str(esp.chip_id()) + jsonPart4
if not self.is_json(jsonTest):
self.statusbar.showMessage(self.tr("Created invalid json."))
return
else:
self.jsonFinal = jsonPart1 + ssid + jsonPart2 + pw + jsonPart3 + "airRohr-"+ str(esp.chip_id()) + jsonPart4
self.statusbar.showMessage(self.tr("Created valid json."))
print(self.jsonFinal)
#self.cachedirspiffs = tempfile.TemporaryDirectory()
self.statusbar.showMessage(self.tr("Opening temporary json directory."))
jsonfile = open(self.cachedirjson.name + "/config.json", "w")
self.statusbar.showMessage(self.tr("Write json in temporay json directory."))
jsonfile.write(self.jsonFinal)
jsonfile.close()
#self.cachedirspiffs = tempfile.TemporaryDirectory()
#python3 spiffsgen.py --page-size 256 --block-size 8192 --no-magic-len --aligned-obj-ix-tables --meta-len=0 $((0xFFA000-0x100000)) path/to/files image.bin
#args = ["--page-size 256","--block-size 8192","--no-magic-len","--aligned-obj-ix-tables","--meta-len=0 $((0xFFA000-0x100000))", path/to/files image.bin]
self.statusbar.showMessage(self.tr("Make SPIFFS bin"))
args = []
args.extend(["spiffsgen.py",
"--page-size", "256",
"--block-size", "8192",
"--meta-len=0", "0x100000"])
args.append("--no-magic-len")
args.append("--aligned-obj-ix-tables")
args.extend([self.cachedirjson.name, self.cachedirspiffs.name + "/spiffs.bin"])
sys.argv = args
spiffsgen.main()
self.statusbar.showMessage(self.tr("spiffs.bin done!"))
self.statusbar.clearMessage()
device = self.boardBox.currentData(ROLE_DEVICE)
if not device:
self.statusbar.showMessage(self.tr("No device selected."))
return
if self.write_config.running():
self.statusbar.showMessage(self.tr("Work in progess..."))
return
self.write_config(self.uploadProgress, device, self.cachedirspiffs.name + "/spiffs.bin", error=self.errorSignal)
@QuickThread.wrap
def write_config(self, progress, device, path, baudrate=460800):
progress.emit(self.tr('Connecting...'), 0)
init_baud = min(ESPLoader.ESP_ROM_BAUD, baudrate)
esp = ESPLoader.detect_chip(device, init_baud, 'default_reset', False)
progress.emit(self.tr('Connected. Chip type: {chip_type}').format(
chip_type=esp.get_chip_description()), 0)
esp = esp.run_stub()
esp.change_baud(baudrate)
with open(path, 'rb') as fd:
uncimagespiffs = fd.read()
imagespiffs = zlib.compress(uncimagespiffs, 0)
#address = 0x300000
address = 0x100000
#address = 0x0
blocks = esp.flash_defl_begin(len(uncimagespiffs), len(imagespiffs), address)
seq = 0
written = 0
t = time.time()
while len(imagespiffs) > 0:
current_addr = address + seq * esp.FLASH_WRITE_SIZE
progress.emit(self.tr('Writing at 0x{address:08x}...').format(
address=current_addr),
100 * (seq + 1) // blocks)
block = imagespiffs[0:esp.FLASH_WRITE_SIZE]
esp.flash_defl_block(block, seq, timeout=3.0)
imagespiffs = imagespiffs[esp.FLASH_WRITE_SIZE:]
seq += 1
written += len(block)
t = time.time() - t
progress.emit(self.tr(
'Finished in {time:.2f} seconds. Sensor ID: {sensor_id}').format(
time=t, sensor_id=esp.chip_id()), 100)
esp.flash_finish(True)
@QtCore.Slot()
def on_uploadButton_clicked(self):
self.statusbar.clearMessage()
device = self.boardBox.currentData(ROLE_DEVICE)
version = self.versionBox.currentText()
if not device:
self.statusbar.showMessage(self.tr("No device selected."))
return
if not version:
self.statusbar.showMessage(self.tr("No version selected."))
return
sel = self.versionBox.model().item(
self.versionBox.currentIndex())
if sel:
orig_version = sel.text()
else:
orig_version = ''
if version == orig_version:
# Editable combobox has been unchanged
binary_uri = self.versionBox.currentData(ROLE_DEVICE)
elif version.startswith(ALLOWED_PROTO):
# User has provided a download URL
binary_uri = version
elif os.path.exists(version):
binary_uri = version
else:
self.statusbar.showMessage(self.tr(
"Invalid version / file does not exist"))
return
if self.flash_board.running():
self.statusbar.showMessage(self.tr("Work in progess..."))
return
self.flash_board(self.uploadProgress, device, binary_uri,
error=self.errorSignal)
def cache_download(self, progress, binary_uri):
"""Downloads and caches file with status reports via Qt Signals"""
cache_fname = os.path.join(
self.cachedir.name,
hashlib.sha256(binary_uri.encode('utf-8')).hexdigest())
if os.path.exists(cache_fname):
return cache_fname
with open(cache_fname, 'wb') as fd:
progress.emit(self.tr('Downloading...'), 0)
response = requests.get(binary_uri, stream=True)
total_length = response.headers.get('content-length')
dl = 0
total_length = int(total_length or 0)
for data in response.iter_content(chunk_size=4096):
dl += len(data)
fd.write(data)
if total_length:
progress.emit(self.tr('Downloading...'),
(100*dl) // total_length)
return cache_fname
@QuickThread.wrap
def erase_board(self, progress, device, baudrate=460800):
progress.emit(self.tr('Connecting...'), 0)
init_baud = min(ESPLoader.ESP_ROM_BAUD, baudrate)
esp = ESPLoader.detect_chip(device, init_baud, 'default_reset', False)
progress.emit(self.tr('Connected. Chip type: {chip_type}').format(
chip_type=esp.get_chip_description()), 0)
esp = esp.run_stub()
esp.change_baud(baudrate)
esp.erase_flash()
progress.emit(self.tr('Erasing complete!'), 100)
@QtCore.Slot()
def on_eraseButton_clicked(self):
self.statusbar.clearMessage()
device = self.boardBox.currentData(ROLE_DEVICE)
if not device:
self.statusbar.showMessage(self.tr("No device selected."))
return
if self.erase_board.running():
self.statusbar.showMessage(self.tr("Erasing in progress..."))
return
self.erase_board(self.uploadProgress, device,
error=self.errorSignal)
@QuickThread.wrap
def flash_board(self, progress, device, binary_uri, baudrate=460800):
if binary_uri.startswith(ALLOWED_PROTO):
binary_uri = self.cache_download(progress, binary_uri)
progress.emit(self.tr('Connecting...'), 0)
init_baud = min(ESPLoader.ESP_ROM_BAUD, baudrate)
esp = ESPLoader.detect_chip(device, init_baud, 'default_reset', False)
progress.emit(self.tr('Connected. Chip type: {chip_type}').format(
chip_type=esp.get_chip_description()), 0)
esp = esp.run_stub()
esp.change_baud(baudrate)
with open(binary_uri, 'rb') as fd:
uncimage = fd.read()
image = zlib.compress(uncimage, 9)
address = 0x0
blocks = esp.flash_defl_begin(len(uncimage), len(image), address)
seq = 0
written = 0
t = time.time()
while len(image) > 0:
current_addr = address + seq * esp.FLASH_WRITE_SIZE
progress.emit(self.tr('Writing at 0x{address:08x}...').format(
address=current_addr),
100 * (seq + 1) // blocks)
block = image[0:esp.FLASH_WRITE_SIZE]
esp.flash_defl_block(block, seq, timeout=3.0)
image = image[esp.FLASH_WRITE_SIZE:]
seq += 1
written += len(block)
t = time.time() - t
progress.emit(self.tr(
'Finished in {time:.2f} seconds. Sensor ID: {sensor_id}').format(
time=t, sensor_id=esp.chip_id()), 100)
esp.flash_finish(True)
@QtCore.Slot()
def on_expertModeBox_clicked(self):
self.expertForm.setVisible(self.expertModeBox.checkState())
# self.centralwidget.setFixedHeight(
# self.centralwidget.sizeHint().height())
# self.setFixedHeight(self.sizeHint().height())
# Zeroconf page
def discovery_start(self):
if self.zeroconf_discovery:
self.zeroconf_discovery.stop()
self.zeroconf_discovery = ZeroconfDiscoveryThread()
self.zeroconf_discovery.deviceDiscovered.connect(
self.on_zeroconf_discovered)
self.zeroconf_discovery.start()
def on_zeroconf_discovered(self, name, address, info):
"""Called on every zeroconf discovered device"""
if (name.lower().startswith('feinstaubsensor')
or name.lower().startswith('nam')
or name.lower().startswith('smogomierz')
or name.lower().startswith('airrohr')):
item = QtWidgets.QListWidgetItem('{}: {}'.format(address, name.split('.')[0]))
item.setData(ROLE_DEVICE, 'http://{}:{}'.format(address, info.port))
self.discoveryList.addItem(item)
@QtCore.Slot(QtWidgets.QListWidgetItem)
def on_discoveryList_itemDoubleClicked(self, index):
QtGui.QDesktopServices.openUrl(QtCore.QUrl(index.data(ROLE_DEVICE)))
@QtCore.Slot()
def on_discoveryRefreshButton_clicked(self):
self.discoveryList.clear()
self.discovery_start()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow(app=app)
window.show()
sys.exit(app.exec_())