forked from opendata-stuttgart/airrohr-firmware-flasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luftdaten-tool.py
executable file
·374 lines (291 loc) · 12.3 KB
/
luftdaten-tool.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
#!/usr/bin/env python3
# -* encoding: utf-8 *-
import sys
import os.path
import time
import tempfile
import hashlib
import zlib
import logging
import requests
from esptool import ESPLoader, erase_flash
import luftdatentool
from luftdatentool.qtvariant import QtGui, QtCore, QtWidgets
from luftdatentool.utils import QuickThread
from luftdatentool.workers import PortDetectThread, FirmwareListThread, \
ZeroconfDiscoveryThread
from gui import mainwindow
from luftdatentool.consts import UPDATE_REPOSITORY, ALLOWED_PROTO, \
PREFERED_PORTS, ROLE_DEVICE, DRIVERS_URL
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
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()
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 luftdatentool.__version__
@property
def build_id(self):
try:
from luftdatentool._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
@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)
@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.startswith('Feinstaubsensor')
or name.startswith('NAM')
or name.startswith('Smogomierz')
or name.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_())