-
Notifications
You must be signed in to change notification settings - Fork 0
/
ota
executable file
·245 lines (189 loc) · 7.5 KB
/
ota
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python3
import subprocess, socket
import os , sys, time
from threading import Thread
import netifaces as ni
from operator import itemgetter
import argparse
from zeroconf import ServiceBrowser, Zeroconf
from shutil import which
ip = []
esp_respond_sender_port = 3235
# HELLO
#
print("\n.:: OTA UPLOADER ::.\n")
# ARGUMENTS
#
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', default=False)
parser.add_argument('-t', '--type', default=None)
parser.add_argument('-i', '--ip', default=None)
args = parser.parse_args()
version_filter = args.version
type_filter = args.type
ip_filter = args.ip
# FIND FIRMWARE
#
def findFirmware():
def all_subdirs_of(b='.'):
result = []
for d in os.listdir(b):
bd = os.path.join(b, d)
if os.path.isdir(bd):
result.append(bd)
return result
dir_path = os.path.dirname(os.path.realpath(__file__))
build_dirs = all_subdirs_of( os.path.join(dir_path,'.pio/build/') )
if len(build_dirs) == 0:
print("No build found in ", os.path.join(dir_path,'.pio/build/'))
return False
latest_builddir = max(build_dirs, key=os.path.getmtime)
print("BUILD directory: ", '\t', '.pio/build/'+os.path.basename(latest_builddir))
firm = os.path.join(latest_builddir, 'firmware.bin')
if not os.path.isfile(firm):
print("Firmware not found at ", firm)
return False
print("FIRMWARE selected: ", '\t', os.path.basename(firm), '\t\t', time.ctime(os.path.getmtime(firm)))
return firm
class ServiceListener(object):
def __init__(self):
self.r = Zeroconf()
def remove_service(self, zeroconf, type, name):
print()
print( "Service", name, "removed")
def update_service(self):
pass
def add_service(self, zeroconf, type, name):
#print( " Type is", type)
info = self.r.get_service_info(type, name)
if info:
new = True
cli = {}
for i in range(len(clients)):
if clients[i]['name'] == name:
cli = clients[i]
new = False
break
cli['name'] = name
cli['shortname'] = name.split(type)[0][0:-1]
cli['ip'] = info.parsed_addresses()[0]
cli['port'] = str(info.port)
try:
cli['version'] = name.split('v')[1].split('.')[0].split('-')[0]
except:
cli['version'] = 0
cli['upgradeVersion'] = (not version_filter or cli['version'] != version_filter)
cli['upgradeType'] = (not type_filter or type_filter.lower() in cli['shortname'].lower())
cli['upgradeIP'] = (not ip_filter or ip_filter == cli['ip'])
cli['upgrade'] = cli['upgradeVersion'] and cli['upgradeType'] and cli['upgradeIP']
if new and cli['upgradeType']:
clients.append(cli)
nameSplit = cli['shortname'].split('-')
print("["+("x" if cli['upgrade'] else " ")+"]["+str(len(clients)-1).rjust(2)+"]",
nameSplit[0].rjust(2),
nameSplit[1].ljust(12),
'',
'-'.join( [k for k in nameSplit[2:-1] if not k.startswith('v')] ).ljust(5),
str(cli['version']).ljust(7),
'',
cli['ip']
)
class Upgrader(Thread):
def __init__(self, port, info):
Thread.__init__(self)
self.port = port
self.info = info
def run(self):
def findCloserIp(dest, srcList):
classif = []
for ip in srcList:
classif.append( (ip, len(os.path.commonprefix([ip, dest]))) )
classif = sorted(classif, key=lambda tup: tup[1])
if len(classif) > 0: return classif[-1][0]
else: return None
sender = findCloserIp(self.info['ip'], ip)
stri = "Uploading to "+self.info['shortname']+" "+str(self.info['ip'])+" using "+sender
print(stri)
# Search python
pycandidates = ['py', 'python3', 'python' ]
cmd = None
for py in pycandidates:
if which(py):
cmd = py
break
if not cmd:
print("Can't find python command..", pycandidates)
return
# ESPOTA cmd build
cmd += ' espota.py -i '+self.info['ip']+' -I '+sender+' -p '+self.info['port']+' -P '+str(self.port)+' -f '+firmware
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
res = p.communicate()
if (p.returncode == 0):
print("Updated", self.info['shortname'], self.info['ip'])
else:
print( "Failed", self.info['shortname'], self.info['ip'])
print( "\t", cmd)
print( "\t", res[0], res[1])
if __name__ == '__main__':
while True:
# subprocess.call('clear',shell=True)
# print("LOCAL ip: "+ str(ip_of_sender) +"\n")
print("=============================================")
print("============ ESP32 - OTA updater ============")
print("=============================================")
print("")
firmware = findFirmware()
if not firmware:
exit(0)
upgradeCount = 0
clients = []
r = Zeroconf()
listener = ServiceListener()
browser = ServiceBrowser(r, "_arduino._tcp.local.", listener)
for iface in ni.interfaces():
if 2 in ni.ifaddresses(iface):
if ni.ifaddresses(iface)[2][0]['addr'] != '127.0.0.1':
ip.append(ni.ifaddresses(iface)[2][0]['addr'])
print("SENDER ip: \t\t", ip)
print("")
print("Options:")
print("\t -v version")
print("\t -t type")
print("\t -i ip")
print("")
try:
a = input("Detecting devices... \n\n"+
"You can specify target indexes list, i.e.: 2,6,12 \n"
"Press Enter when ready.\n\n"+
"---------- "+"Type ------------ Version ----- IP ---\n")
except KeyboardInterrupt:
r.close()
browser.cancel();
listener.r.close();
print("Goodbye :)")
os.kill(os.getpid(), signal.SIGKILL)
r.close()
selectedIndexes = a.split(',')
if selectedIndexes[0].isnumeric():
upgradables = []
for a in selectedIndexes:
if a.isnumeric() and int(a) < len(clients):
upgradables.append(clients[int(a)])
else:
upgradables = [cli for cli in clients if cli['upgrade']]
if len(upgradables) == 0: continue
print("\nUpload to "+str(len(upgradables))+" devices :\n")
for u in upgradables:
print("\t"+u['shortname'].ljust(20)+u['ip'])
y = input("\nPlease confirm... [y/n]\n\n")
if y != 'y': continue
threadlist = []
for cli in upgradables:
# print("Preparing upload to ", cli['ip']
threadlist.append(Upgrader(esp_respond_sender_port, cli))
esp_respond_sender_port += 1
for t in threadlist:
t.start()
for t in threadlist:
t.join()
break