diff --git a/code/ota.py b/code/ota.py index 2a268acf..16b86f82 100755 --- a/code/ota.py +++ b/code/ota.py @@ -8,6 +8,7 @@ # ------------------------------------------------------------------------------- from __future__ import print_function +import shutil import argparse import re import socket @@ -27,7 +28,7 @@ except NameError: DISCOVER_TIMEOUT = 2 -description = "ESPurna OTA Manager v0.2" +description = "ESPurna OTA Manager v0.3" devices = [] discover_last = 0 @@ -111,6 +112,11 @@ 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 @@ -128,9 +134,26 @@ def get_board_by_index(index): board['hostname'] = device.get('hostname') board['board'] = device.get('target_board', '') board['ip'] = device.get('ip', '') - board['size'] = int(device.get('mem_size', 0) if device.get('mem_size', 0) == device.get('sdk_size', 0) else 0) / 1024 + board['size'] = get_device_size(device) return board +def get_board_by_mac(mac): + """ + Returns the required data to flash a given board + """ + hostname = hostname.lower() + for device in devices: + if device.get('mac', '').lower() == mac: + board = {} + board['hostname'] = device.get('hostname') + board['board'] = device.get('device') + board['ip'] = device.get('ip') + board['size'] = get_device_size(device) + if not board['board'] or not board['ip'] or board['size'] == 0: + return None + return board + return None + def get_board_by_hostname(hostname): """ Returns the required data to flash a given board @@ -141,13 +164,9 @@ def get_board_by_hostname(hostname): board = {} board['hostname'] = device.get('hostname') board['board'] = device.get('target_board') - if not board['board']: - return None board['ip'] = device.get('ip') - if not board['ip']: - return None - board['size'] = int(device.get('sdk_size', 0)) / 1024 - if board['size'] == 0: + board['size'] = get_device_size(device) + if not board['board'] or not board['ip'] or board['size'] == 0: return None return board return None @@ -201,13 +220,20 @@ 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() + shutil.move(source, destination) def run(device, env): print("Building and flashing image over-the-air...") command = "export ESPURNA_IP=\"%s\"; export ESPURNA_BOARD=\"%s\"; export ESPURNA_AUTH=\"%s\"; export ESPURNA_FLAGS=\"%s\"; platformio run --silent --environment %s -t upload" command = command % (device['ip'], device['board'], device['auth'], device['flags'], env) subprocess.check_call(command, shell=True) - + store(device, env) # ------------------------------------------------------------------------------- @@ -287,7 +313,7 @@ if __name__ == '__main__': # Summary print() - print("HOST = %s" % board.get('hostname', board['ip'])) + print("HOST = %s" % boardname(board)) print("IP = %s" % board['ip']) print("BOARD = %s" % board['board']) print("AUTH = %s" % board['auth'])