|
|
@ -8,14 +8,14 @@ |
|
|
|
# ------------------------------------------------------------------------------- |
|
|
|
from __future__ import print_function |
|
|
|
|
|
|
|
import shutil |
|
|
|
import argparse |
|
|
|
import os |
|
|
|
import re |
|
|
|
import shutil |
|
|
|
import socket |
|
|
|
import subprocess |
|
|
|
import sys |
|
|
|
import time |
|
|
|
import os |
|
|
|
|
|
|
|
from zeroconf import ServiceBrowser, ServiceStateChange, Zeroconf |
|
|
|
|
|
|
@ -33,6 +33,7 @@ description = "ESPurna OTA Manager v0.3" |
|
|
|
devices = [] |
|
|
|
discover_last = 0 |
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------- |
|
|
|
|
|
|
|
def on_service_state_change(zeroconf, service_type, name, state_change): |
|
|
@ -61,7 +62,7 @@ def on_service_state_change(zeroconf, service_type, name, state_change): |
|
|
|
} |
|
|
|
|
|
|
|
for key, item in info.properties.items(): |
|
|
|
device[key.decode('UTF-8')] = item.decode('UTF-8'); |
|
|
|
device[key.decode('UTF-8')] = item.decode('UTF-8') |
|
|
|
|
|
|
|
# rename fields (needed for sorting by name) |
|
|
|
device['app'] = device['app_name'] |
|
|
@ -75,35 +76,35 @@ def list_devices(): |
|
|
|
""" |
|
|
|
Shows the list of discovered devices |
|
|
|
""" |
|
|
|
output_format="{:>3} {:<14} {:<15} {:<17} {:<12} {:<12} {:<25} {:<8} {:<8} {:<10}" |
|
|
|
output_format = "{:>3} {:<14} {:<15} {:<17} {:<12} {:<12} {:<25} {:<8} {:<8} {:<10}" |
|
|
|
print(output_format.format( |
|
|
|
"#", |
|
|
|
"HOSTNAME", |
|
|
|
"IP", |
|
|
|
"MAC", |
|
|
|
"APP", |
|
|
|
"VERSION", |
|
|
|
"DEVICE", |
|
|
|
"MEM_SIZE", |
|
|
|
"SDK_SIZE", |
|
|
|
"FREE_SPACE" |
|
|
|
"#", |
|
|
|
"HOSTNAME", |
|
|
|
"IP", |
|
|
|
"MAC", |
|
|
|
"APP", |
|
|
|
"VERSION", |
|
|
|
"DEVICE", |
|
|
|
"MEM_SIZE", |
|
|
|
"SDK_SIZE", |
|
|
|
"FREE_SPACE" |
|
|
|
)) |
|
|
|
print("-" * 139) |
|
|
|
|
|
|
|
index = 0 |
|
|
|
for device in devices: |
|
|
|
index = index + 1 |
|
|
|
index += 1 |
|
|
|
print(output_format.format( |
|
|
|
index, |
|
|
|
device.get('hostname', ''), |
|
|
|
device.get('ip', ''), |
|
|
|
device.get('mac', ''), |
|
|
|
device.get('app_name', ''), |
|
|
|
device.get('app_version', ''), |
|
|
|
device.get('target_board', ''), |
|
|
|
device.get('mem_size', 0), |
|
|
|
device.get('sdk_size', 0), |
|
|
|
device.get('free_space', 0), |
|
|
|
index, |
|
|
|
device.get('hostname', ''), |
|
|
|
device.get('ip', ''), |
|
|
|
device.get('mac', ''), |
|
|
|
device.get('app_name', ''), |
|
|
|
device.get('app_version', ''), |
|
|
|
device.get('target_board', ''), |
|
|
|
device.get('mem_size', 0), |
|
|
|
device.get('sdk_size', 0), |
|
|
|
device.get('free_space', 0), |
|
|
|
)) |
|
|
|
|
|
|
|
print() |
|
|
@ -120,11 +121,13 @@ def get_boards(): |
|
|
|
boards.append(m.group(1)) |
|
|
|
return sorted(boards) |
|
|
|
|
|
|
|
|
|
|
|
def get_device_size(device): |
|
|
|
if device.get('mem_size', 0) == device.get('sdk_size', 0): |
|
|
|
return int(device.get('mem_size', 0)) / 1024 |
|
|
|
return 0 |
|
|
|
|
|
|
|
|
|
|
|
def get_empty_board(): |
|
|
|
""" |
|
|
|
Returns the empty structure of a board to flash |
|
|
@ -132,12 +135,13 @@ def get_empty_board(): |
|
|
|
board = {'board': '', 'ip': '', 'size': 0, 'auth': '', 'flags': ''} |
|
|
|
return board |
|
|
|
|
|
|
|
|
|
|
|
def get_board_by_index(index): |
|
|
|
""" |
|
|
|
Returns the required data to flash a given board |
|
|
|
""" |
|
|
|
board = {} |
|
|
|
if 1 <= index and index <= len(devices): |
|
|
|
if 1 <= index <= len(devices): |
|
|
|
device = devices[index - 1] |
|
|
|
board['hostname'] = device.get('hostname') |
|
|
|
board['board'] = device.get('target_board', '') |
|
|
@ -145,6 +149,7 @@ def get_board_by_index(index): |
|
|
|
board['size'] = get_device_size(device) |
|
|
|
return board |
|
|
|
|
|
|
|
|
|
|
|
def get_board_by_mac(mac): |
|
|
|
""" |
|
|
|
Returns the required data to flash a given board |
|
|
@ -161,6 +166,7 @@ def get_board_by_mac(mac): |
|
|
|
return board |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
def get_board_by_hostname(hostname): |
|
|
|
""" |
|
|
|
Returns the required data to flash a given board |
|
|
@ -178,6 +184,7 @@ def get_board_by_hostname(hostname): |
|
|
|
return board |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
def input_board(): |
|
|
|
""" |
|
|
|
Grabs info from the user about what device to flash |
|
|
@ -189,10 +196,10 @@ def input_board(): |
|
|
|
except ValueError: |
|
|
|
index = 0 |
|
|
|
if index < 0 or len(devices) < index: |
|
|
|
print("Board number must be between 1 and %s\n" % str(len(devices))) |
|
|
|
print("Board number must be between 1 and {}\n".format(str(len(devices)))) |
|
|
|
return None |
|
|
|
|
|
|
|
board = get_board_by_index(index); |
|
|
|
board = get_board_by_index(index) |
|
|
|
|
|
|
|
# Choose board type if none before |
|
|
|
if len(board.get('board', '')) == 0: |
|
|
@ -201,15 +208,15 @@ def input_board(): |
|
|
|
count = 1 |
|
|
|
boards = get_boards() |
|
|
|
for name in boards: |
|
|
|
print("%3d\t%s" % (count, name)) |
|
|
|
count = count + 1 |
|
|
|
print("{:3d}\t{}".format(count, name)) |
|
|
|
count += 1 |
|
|
|
print() |
|
|
|
try: |
|
|
|
index = int(input("Choose the board type you want to flash: ")) |
|
|
|
except ValueError: |
|
|
|
index = 0 |
|
|
|
if index < 1 or len(boards) < index: |
|
|
|
print("Board number must be between 1 and %s\n" % str(len(boards))) |
|
|
|
print("Board number must be between 1 and {}\n".format(str(len(boards)))) |
|
|
|
return None |
|
|
|
board['board'] = boards[index - 1] |
|
|
|
|
|
|
@ -227,12 +234,14 @@ def input_board(): |
|
|
|
|
|
|
|
return board |
|
|
|
|
|
|
|
|
|
|
|
def boardname(board): |
|
|
|
return board.get('hostname', board['ip']) |
|
|
|
|
|
|
|
|
|
|
|
def store(device, env): |
|
|
|
source = ".pioenvs/%s/firmware.elf" % env |
|
|
|
destination = ".pioenvs/elfs/%s.elf" % boardname(device).lower() |
|
|
|
source = ".pioenvs/{}/firmware.elf".format(env) |
|
|
|
destination = ".pioenvs/elfs/{}.elf".format(boardname(device).lower()) |
|
|
|
|
|
|
|
dst_dir = os.path.dirname(destination) |
|
|
|
if not os.path.exists(dst_dir): |
|
|
@ -240,6 +249,7 @@ def store(device, env): |
|
|
|
|
|
|
|
shutil.move(source, destination) |
|
|
|
|
|
|
|
|
|
|
|
def run(device, env): |
|
|
|
print("Building and flashing image over-the-air...") |
|
|
|
environ = os.environ.copy() |
|
|
@ -253,6 +263,7 @@ def run(device, env): |
|
|
|
|
|
|
|
store(device, env) |
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------- |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
@ -272,13 +283,13 @@ if __name__ == '__main__': |
|
|
|
print(description) |
|
|
|
print() |
|
|
|
|
|
|
|
# Look for sevices |
|
|
|
# Look for services |
|
|
|
zeroconf = Zeroconf() |
|
|
|
browser = ServiceBrowser(zeroconf, "_arduino._tcp.local.", handlers=[on_service_state_change]) |
|
|
|
discover_last = time.time() |
|
|
|
while time.time() < discover_last + DISCOVER_TIMEOUT: |
|
|
|
None |
|
|
|
#zeroconf.close() |
|
|
|
pass |
|
|
|
# zeroconf.close() |
|
|
|
|
|
|
|
if len(devices) == 0: |
|
|
|
print("Nothing found!\n") |
|
|
@ -287,7 +298,7 @@ if __name__ == '__main__': |
|
|
|
# Sort list |
|
|
|
field = args.sort.lower() |
|
|
|
if field not in devices[0]: |
|
|
|
print("Unknown field '%s'\n" % field) |
|
|
|
print("Unknown field '{}'\n".format(field)) |
|
|
|
sys.exit(1) |
|
|
|
devices = sorted(devices, key=lambda device: device.get(field, '')) |
|
|
|
|
|
|
@ -322,23 +333,23 @@ if __name__ == '__main__': |
|
|
|
|
|
|
|
queue = sorted(queue, key=lambda device: device.get('board', '')) |
|
|
|
|
|
|
|
# Flash eash board |
|
|
|
# Flash each board |
|
|
|
for board in queue: |
|
|
|
|
|
|
|
# Flash core version? |
|
|
|
if args.core > 0: |
|
|
|
board['flags'] = "-DESPURNA_CORE " + board['flags'] |
|
|
|
|
|
|
|
env = "esp8266-%dm-ota" % board['size'] |
|
|
|
env = "esp8266-{:d}m-ota".format(board['size']) |
|
|
|
|
|
|
|
# Summary |
|
|
|
print() |
|
|
|
print("HOST = %s" % boardname(board)) |
|
|
|
print("IP = %s" % board['ip']) |
|
|
|
print("BOARD = %s" % board['board']) |
|
|
|
print("AUTH = %s" % board['auth']) |
|
|
|
print("FLAGS = %s" % board['flags']) |
|
|
|
print("ENV = %s" % env) |
|
|
|
print("HOST = {}".format(boardname(board))) |
|
|
|
print("IP = {}".format(board['ip'])) |
|
|
|
print("BOARD = {}".format(board['board'])) |
|
|
|
print("AUTH = {}".format(board['auth'])) |
|
|
|
print("FLAGS = {}".format(board['flags'])) |
|
|
|
print("ENV = {}".format(env)) |
|
|
|
|
|
|
|
response = True |
|
|
|
if args.yes == 0: |
|
|
|