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 logging
import hashlib
import random
# Commands
FLASH = 0
SPIFFS = 100
AUTH = 200
PROGRESS = 0
PROGRESS = False
# update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100%
def update_progress(progress):
if (PROGRESS == 1):
if (PROGRESS):
barLength = 60 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
@ -66,7 +67,7 @@ def update_progress(progress):
def serve(remoteAddr, remotePort, password, filename, command = FLASH):
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverPort = 48266
serverPort = random.randint(10000,60000)
server_address = ('0.0.0.0', serverPort)
logging.info('Starting on %s:%s', str(server_address[0]), str(server_address[1]))
try:
@ -141,11 +142,11 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
try:
f = open(filename, "rb")
if (PROGRESS == 0):
if (PROGRESS):
update_progress(0)
else:
sys.stderr.write('Uploading')
sys.stderr.flush()
else:
update_progress(0)
offset = 0
while True:
chunk = f.read(1460)
@ -280,8 +281,8 @@ def main(args):
logging.debug("Options: %s", str(options))
# check options
if (options.progress):
PROGRESS = 1
global PROGRESS
PROGRESS = options.progress
if (not options.esp_ip or not options.image):
logging.critical("Not enough arguments.")