|
30 | 30 | import optparse
|
31 | 31 | import logging
|
32 | 32 | import hashlib
|
| 33 | +import random |
33 | 34 |
|
34 | 35 | # Commands
|
35 | 36 | FLASH = 0
|
36 | 37 | SPIFFS = 100
|
37 | 38 | AUTH = 200
|
38 |
| - |
| 39 | +PROGRESS = False |
| 40 | +# update_progress() : Displays or updates a console progress bar |
| 41 | +## Accepts a float between 0 and 1. Any int will be converted to a float. |
| 42 | +## A value under 0 represents a 'halt'. |
| 43 | +## A value at 1 or bigger represents 100% |
| 44 | +def update_progress(progress): |
| 45 | + if (PROGRESS): |
| 46 | + barLength = 60 # Modify this to change the length of the progress bar |
| 47 | + status = "" |
| 48 | + if isinstance(progress, int): |
| 49 | + progress = float(progress) |
| 50 | + if not isinstance(progress, float): |
| 51 | + progress = 0 |
| 52 | + status = "error: progress var must be float\r\n" |
| 53 | + if progress < 0: |
| 54 | + progress = 0 |
| 55 | + status = "Halt...\r\n" |
| 56 | + if progress >= 1: |
| 57 | + progress = 1 |
| 58 | + status = "Done...\r\n" |
| 59 | + block = int(round(barLength*progress)) |
| 60 | + text = "\rUploading: [{0}] {1}% {2}".format( "="*block + " "*(barLength-block), int(progress*100), status) |
| 61 | + sys.stderr.write(text) |
| 62 | + sys.stderr.flush() |
| 63 | + else: |
| 64 | + sys.stderr.write('.') |
| 65 | + sys.stderr.flush() |
39 | 66 |
|
40 | 67 | def serve(remoteAddr, remotePort, password, filename, command = FLASH):
|
41 | 68 | # Create a TCP/IP socket
|
42 | 69 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
43 |
| - serverPort = 48266 |
| 70 | + serverPort = random.randint(10000,60000) |
44 | 71 | server_address = ('0.0.0.0', serverPort)
|
45 | 72 | logging.info('Starting on %s:%s', str(server_address[0]), str(server_address[1]))
|
46 | 73 | try:
|
@@ -115,13 +142,17 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
|
115 | 142 |
|
116 | 143 | try:
|
117 | 144 | f = open(filename, "rb")
|
118 |
| - sys.stderr.write('Uploading') |
119 |
| - sys.stderr.flush() |
| 145 | + if (PROGRESS): |
| 146 | + update_progress(0) |
| 147 | + else: |
| 148 | + sys.stderr.write('Uploading') |
| 149 | + sys.stderr.flush() |
| 150 | + offset = 0 |
120 | 151 | while True:
|
121 | 152 | chunk = f.read(1460)
|
122 | 153 | if not chunk: break
|
123 |
| - sys.stderr.write('.') |
124 |
| - sys.stderr.flush() |
| 154 | + offset += len(chunk) |
| 155 | + update_progress(offset/float(content_size)) |
125 | 156 | connection.settimeout(10)
|
126 | 157 | try:
|
127 | 158 | connection.sendall(chunk)
|
@@ -165,100 +196,108 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
|
165 | 196 |
|
166 | 197 |
|
167 | 198 | def parser():
|
168 |
| - parser = optparse.OptionParser( |
169 |
| - usage = "%prog [options]", |
170 |
| - description = "Transmit image over the air to the esp8266 module with OTA support." |
171 |
| - ) |
| 199 | + parser = optparse.OptionParser( |
| 200 | + usage = "%prog [options]", |
| 201 | + description = "Transmit image over the air to the esp8266 module with OTA support." |
| 202 | + ) |
172 | 203 |
|
173 |
| - # destination ip and port |
174 |
| - group = optparse.OptionGroup(parser, "Destination") |
175 |
| - group.add_option("-i", "--ip", |
176 |
| - dest = "esp_ip", |
177 |
| - action = "store", |
178 |
| - help = "ESP8266 IP Address.", |
179 |
| - default = False |
180 |
| - ) |
181 |
| - group.add_option("-p", "--port", |
182 |
| - dest = "esp_port", |
183 |
| - type = "int", |
184 |
| - help = "ESP8266 ota Port. Default 8266", |
185 |
| - default = 8266 |
186 |
| - ) |
187 |
| - parser.add_option_group(group) |
| 204 | + # destination ip and port |
| 205 | + group = optparse.OptionGroup(parser, "Destination") |
| 206 | + group.add_option("-i", "--ip", |
| 207 | + dest = "esp_ip", |
| 208 | + action = "store", |
| 209 | + help = "ESP8266 IP Address.", |
| 210 | + default = False |
| 211 | + ) |
| 212 | + group.add_option("-p", "--port", |
| 213 | + dest = "esp_port", |
| 214 | + type = "int", |
| 215 | + help = "ESP8266 ota Port. Default 8266", |
| 216 | + default = 8266 |
| 217 | + ) |
| 218 | + parser.add_option_group(group) |
188 | 219 |
|
189 |
| - # auth |
190 |
| - group = optparse.OptionGroup(parser, "Authentication") |
191 |
| - group.add_option("-a", "--auth", |
192 |
| - dest = "auth", |
193 |
| - help = "Set authentication password.", |
194 |
| - action = "store", |
195 |
| - default = "" |
196 |
| - ) |
197 |
| - parser.add_option_group(group) |
| 220 | + # auth |
| 221 | + group = optparse.OptionGroup(parser, "Authentication") |
| 222 | + group.add_option("-a", "--auth", |
| 223 | + dest = "auth", |
| 224 | + help = "Set authentication password.", |
| 225 | + action = "store", |
| 226 | + default = "" |
| 227 | + ) |
| 228 | + parser.add_option_group(group) |
198 | 229 |
|
199 |
| - # image |
200 |
| - group = optparse.OptionGroup(parser, "Image") |
201 |
| - group.add_option("-f", "--file", |
202 |
| - dest = "image", |
203 |
| - help = "Image file.", |
204 |
| - metavar="FILE", |
205 |
| - default = None |
206 |
| - ) |
207 |
| - group.add_option("-s", "--spiffs", |
208 |
| - dest = "spiffs", |
209 |
| - action = "store_true", |
210 |
| - help = "Use this option to transmit a SPIFFS image and do not flash the module.", |
211 |
| - default = False |
212 |
| - ) |
213 |
| - parser.add_option_group(group) |
| 230 | + # image |
| 231 | + group = optparse.OptionGroup(parser, "Image") |
| 232 | + group.add_option("-f", "--file", |
| 233 | + dest = "image", |
| 234 | + help = "Image file.", |
| 235 | + metavar="FILE", |
| 236 | + default = None |
| 237 | + ) |
| 238 | + group.add_option("-s", "--spiffs", |
| 239 | + dest = "spiffs", |
| 240 | + action = "store_true", |
| 241 | + help = "Use this option to transmit a SPIFFS image and do not flash the module.", |
| 242 | + default = False |
| 243 | + ) |
| 244 | + parser.add_option_group(group) |
214 | 245 |
|
215 |
| - # output group |
216 |
| - group = optparse.OptionGroup(parser, "Output") |
217 |
| - group.add_option("-d", "--debug", |
218 |
| - dest = "debug", |
219 |
| - help = "Show debug output. And override loglevel with debug.", |
220 |
| - action = "store_true", |
221 |
| - default = False |
222 |
| - ) |
223 |
| - parser.add_option_group(group) |
| 246 | + # output group |
| 247 | + group = optparse.OptionGroup(parser, "Output") |
| 248 | + group.add_option("-d", "--debug", |
| 249 | + dest = "debug", |
| 250 | + help = "Show debug output. And override loglevel with debug.", |
| 251 | + action = "store_true", |
| 252 | + default = False |
| 253 | + ) |
| 254 | + group.add_option("-r", "--progress", |
| 255 | + dest = "progress", |
| 256 | + help = "Show progress output. Does not work for ArduinoIDE", |
| 257 | + action = "store_true", |
| 258 | + default = False |
| 259 | + ) |
| 260 | + parser.add_option_group(group) |
224 | 261 |
|
225 |
| - (options, args) = parser.parse_args() |
| 262 | + (options, args) = parser.parse_args() |
226 | 263 |
|
227 |
| - return options |
| 264 | + return options |
228 | 265 | # end parser
|
229 | 266 |
|
230 | 267 |
|
231 | 268 | def main(args):
|
232 |
| - # get options |
233 |
| - options = parser() |
| 269 | + # get options |
| 270 | + options = parser() |
234 | 271 |
|
235 |
| - # adapt log level |
236 |
| - loglevel = logging.WARNING |
237 |
| - if (options.debug): |
238 |
| - loglevel = logging.DEBUG |
239 |
| - # end if |
| 272 | + # adapt log level |
| 273 | + loglevel = logging.WARNING |
| 274 | + if (options.debug): |
| 275 | + loglevel = logging.DEBUG |
| 276 | + # end if |
240 | 277 |
|
241 |
| - # logging |
242 |
| - logging.basicConfig(level = loglevel, format = '%(asctime)-8s [%(levelname)s]: %(message)s', datefmt = '%H:%M:%S') |
| 278 | + # logging |
| 279 | + logging.basicConfig(level = loglevel, format = '%(asctime)-8s [%(levelname)s]: %(message)s', datefmt = '%H:%M:%S') |
243 | 280 |
|
244 |
| - logging.debug("Options: %s", str(options)) |
| 281 | + logging.debug("Options: %s", str(options)) |
245 | 282 |
|
246 |
| - # check options |
247 |
| - if (not options.esp_ip or not options.image): |
248 |
| - logging.critical("Not enough arguments.") |
| 283 | + # check options |
| 284 | + global PROGRESS |
| 285 | + PROGRESS = options.progress |
| 286 | + if (not options.esp_ip or not options.image): |
| 287 | + logging.critical("Not enough arguments.") |
249 | 288 |
|
250 |
| - return 1 |
251 |
| - # end if |
| 289 | + return 1 |
| 290 | + # end if |
252 | 291 |
|
253 |
| - command = FLASH |
254 |
| - if (options.spiffs): |
255 |
| - command = SPIFFS |
256 |
| - # end if |
| 292 | + command = FLASH |
| 293 | + if (options.spiffs): |
| 294 | + command = SPIFFS |
| 295 | + # end if |
257 | 296 |
|
258 |
| - return serve(options.esp_ip, options.esp_port, options.auth, options.image, command) |
| 297 | + return serve(options.esp_ip, options.esp_port, options.auth, options.image, command) |
259 | 298 | # end main
|
260 | 299 |
|
261 | 300 |
|
262 | 301 | if __name__ == '__main__':
|
263 |
| - sys.exit(main(sys.argv)) |
| 302 | + sys.exit(main(sys.argv)) |
264 | 303 | # end if
|
0 commit comments