-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-esp
executable file
·98 lines (77 loc) · 3.03 KB
/
update-esp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python2
import subprocess, socket
import os , sys
from threading import Thread
dir_path = os.path.dirname(os.path.realpath(__file__))
clients = {}
ip_of_sender = subprocess.check_output("hostname -I" , shell=True).decode('utf-8').rstrip()
esp_respond_sender_port = 3235
firmware = dir_path+'/Arduino/ESP32-miniplayer/ESP32-miniplayer.ino.esp32.bin'
print(ip_of_sender)
version_filter = False
if len(sys.argv) > 1:
version_filter = str(sys.argv[1])
upgradeCount = 0
from zero import *
import time
class ServiceListener(object):
def __init__(self):
self.r = Zeroconf()
def removeService(self, zeroconf, type, name):
print()
print( "Service", name, "removed")
def addService(self, zeroconf, type, name):
#print( " Type is", type)
info = self.r.getServiceInfo(type, name)
if info:
clients[name] = {}
clients[name]['ip'] = str(socket.inet_ntoa(info.getAddress())).rstrip()
clients[name]['port'] = str(info.getPort())
clients[name]['version'] = name.split(' v')[1][:4]
clients[name]['upgrade'] = (not version_filter or clients[name]['version'] != version_filter)
if clients[name]['upgrade']:
global upgradeCount
upgradeCount += 1
print "Device ", len(clients),": Arduino", name, "detected: ", "UPGRADABLE" if clients[name]['upgrade'] else "OK"
#print( " Weight is %d, Priority is %d" % (info.getWeight(),
# info.getPriority()))
#print( " Server is", info.getServer())
#prop = info.getProperties()
#if prop:
# print( " Properties are")
# for key, value in prop.items():
# print( " %s: %s" % (key, value))
class Upgrader(Thread):
def __init__(self, port, info):
Thread.__init__(self)
self.port = port
self.info = info
def run(self):
print "Uploading to ", self.info['ip']
cmd = 'python ~/Arduino/hardware/espressif/esp32/tools/espota.py -i '+self.info['ip']+' -I '+ip_of_sender+' -p '+self.info['port']+' -P '+str(self.port)+' -f '+firmware
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line,
retval = p.wait()
print retval
if __name__ == '__main__':
r = Zeroconf()
type = "_arduino._tcp.local."
listener = ServiceListener()
browser = ServiceBrowser(r, type, listener)
a = raw_input("Press enter to upload...\n\n")
r.close()
#time.sleep(2)
y = raw_input("Upload to "+str(upgradeCount)+" Devices ?... [y/n]\n\n")
if y is not 'y':
process.exit()
threadlist = []
for esp, info in clients.items():
if info['upgrade']:
print "Preparing upload to ", info['ip']
threadlist.append(Upgrader(esp_respond_sender_port, info))
esp_respond_sender_port += 1
for t in threadlist:
t.start()
for t in threadlist:
t.join()