1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-09 03:41:41 +03:00

Fix upload progress and randomize the server port

randomization is good in cases where the previous port is not yet
released by the OS or the server hangs
On OS X it's very noticeable if you need to OTA twice in a short time.
This commit is contained in:
Me No Dev 2015-11-12 03:25:20 +02:00
parent 0213dc34ff
commit d1235f0204

View File

@ -30,18 +30,19 @@ import os
import optparse import optparse
import logging import logging
import hashlib import hashlib
import random
# Commands # Commands
FLASH = 0 FLASH = 0
SPIFFS = 100 SPIFFS = 100
AUTH = 200 AUTH = 200
PROGRESS = 0 PROGRESS = False
# update_progress() : Displays or updates a console progress bar # update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float. ## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'. ## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100% ## A value at 1 or bigger represents 100%
def update_progress(progress): def update_progress(progress):
if (PROGRESS == 1): if (PROGRESS):
barLength = 60 # Modify this to change the length of the progress bar barLength = 60 # Modify this to change the length of the progress bar
status = "" status = ""
if isinstance(progress, int): if isinstance(progress, int):
@ -66,7 +67,7 @@ def update_progress(progress):
def serve(remoteAddr, remotePort, password, filename, command = FLASH): def serve(remoteAddr, remotePort, password, filename, command = FLASH):
# Create a TCP/IP socket # Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverPort = 48266 serverPort = random.randint(10000,60000)
server_address = ('0.0.0.0', serverPort) server_address = ('0.0.0.0', serverPort)
logging.info('Starting on %s:%s', str(server_address[0]), str(server_address[1])) logging.info('Starting on %s:%s', str(server_address[0]), str(server_address[1]))
try: try:
@ -141,11 +142,11 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
try: try:
f = open(filename, "rb") f = open(filename, "rb")
if (PROGRESS == 0): if (PROGRESS):
update_progress(0)
else:
sys.stderr.write('Uploading') sys.stderr.write('Uploading')
sys.stderr.flush() sys.stderr.flush()
else:
update_progress(0)
offset = 0 offset = 0
while True: while True:
chunk = f.read(1460) chunk = f.read(1460)
@ -195,108 +196,108 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
def parser(): def parser():
parser = optparse.OptionParser( parser = optparse.OptionParser(
usage = "%prog [options]", usage = "%prog [options]",
description = "Transmit image over the air to the esp8266 module with OTA support." description = "Transmit image over the air to the esp8266 module with OTA support."
) )
# destination ip and port # destination ip and port
group = optparse.OptionGroup(parser, "Destination") group = optparse.OptionGroup(parser, "Destination")
group.add_option("-i", "--ip", group.add_option("-i", "--ip",
dest = "esp_ip", dest = "esp_ip",
action = "store", action = "store",
help = "ESP8266 IP Address.", help = "ESP8266 IP Address.",
default = False default = False
) )
group.add_option("-p", "--port", group.add_option("-p", "--port",
dest = "esp_port", dest = "esp_port",
type = "int", type = "int",
help = "ESP8266 ota Port. Default 8266", help = "ESP8266 ota Port. Default 8266",
default = 8266 default = 8266
) )
parser.add_option_group(group) parser.add_option_group(group)
# auth # auth
group = optparse.OptionGroup(parser, "Authentication") group = optparse.OptionGroup(parser, "Authentication")
group.add_option("-a", "--auth", group.add_option("-a", "--auth",
dest = "auth", dest = "auth",
help = "Set authentication password.", help = "Set authentication password.",
action = "store", action = "store",
default = "" default = ""
) )
parser.add_option_group(group) parser.add_option_group(group)
# image # image
group = optparse.OptionGroup(parser, "Image") group = optparse.OptionGroup(parser, "Image")
group.add_option("-f", "--file", group.add_option("-f", "--file",
dest = "image", dest = "image",
help = "Image file.", help = "Image file.",
metavar="FILE", metavar="FILE",
default = None default = None
) )
group.add_option("-s", "--spiffs", group.add_option("-s", "--spiffs",
dest = "spiffs", dest = "spiffs",
action = "store_true", action = "store_true",
help = "Use this option to transmit a SPIFFS image and do not flash the module.", help = "Use this option to transmit a SPIFFS image and do not flash the module.",
default = False default = False
) )
parser.add_option_group(group) parser.add_option_group(group)
# output group # output group
group = optparse.OptionGroup(parser, "Output") group = optparse.OptionGroup(parser, "Output")
group.add_option("-d", "--debug", group.add_option("-d", "--debug",
dest = "debug", dest = "debug",
help = "Show debug output. And override loglevel with debug.", help = "Show debug output. And override loglevel with debug.",
action = "store_true", action = "store_true",
default = False default = False
) )
group.add_option("-r", "--progress", group.add_option("-r", "--progress",
dest = "progress", dest = "progress",
help = "Show progress output. Does not work for ArduinoIDE", help = "Show progress output. Does not work for ArduinoIDE",
action = "store_true", action = "store_true",
default = False default = False
) )
parser.add_option_group(group) parser.add_option_group(group)
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
return options return options
# end parser # end parser
def main(args): def main(args):
# get options # get options
options = parser() options = parser()
# adapt log level # adapt log level
loglevel = logging.WARNING loglevel = logging.WARNING
if (options.debug): if (options.debug):
loglevel = logging.DEBUG loglevel = logging.DEBUG
# end if # end if
# logging # logging
logging.basicConfig(level = loglevel, format = '%(asctime)-8s [%(levelname)s]: %(message)s', datefmt = '%H:%M:%S') logging.basicConfig(level = loglevel, format = '%(asctime)-8s [%(levelname)s]: %(message)s', datefmt = '%H:%M:%S')
logging.debug("Options: %s", str(options)) logging.debug("Options: %s", str(options))
# check options # check options
if (options.progress): global PROGRESS
PROGRESS = 1 PROGRESS = options.progress
if (not options.esp_ip or not options.image): if (not options.esp_ip or not options.image):
logging.critical("Not enough arguments.") logging.critical("Not enough arguments.")
return 1 return 1
# end if # end if
command = FLASH command = FLASH
if (options.spiffs): if (options.spiffs):
command = SPIFFS command = SPIFFS
# end if # end if
return serve(options.esp_ip, options.esp_port, options.auth, options.image, command) return serve(options.esp_ip, options.esp_port, options.auth, options.image, command)
# end main # end main
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main(sys.argv)) sys.exit(main(sys.argv))
# end if # end if