Skip to content

Commit

Permalink
#4
Browse files Browse the repository at this point in the history
Check response from the device when configuration.
  • Loading branch information
renakim committed Apr 6, 2018
1 parent d8e53ee commit ed35791
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 33 deletions.
3 changes: 1 addition & 2 deletions FWUploadThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def sendCmd(self, command):
if self.resp is not '':
break
time.sleep(1)

def run(self):
if self.resp is not '':
resp = self.resp.decode('utf-8')
Expand All @@ -126,7 +126,6 @@ def run(self):
self.serverport = int(params[1])

# network reachable check
# os.system("ping " + ("-n 1 " if sys.platform.lower()=="win32" else "-c 1 ") + self.serverip)
os.system("ping " + ("-n 1 " if sys.platform.lower()=="win32" else "-c 1 ") + self.serverip)
ping_reponse = os.system("ping " + ("-n 1 " if sys.platform.lower()=="win32" else "-c 1 ") + self.serverip)
# ping_reponse = os.system('ping -n 1 ' + params[0])
Expand Down
28 changes: 28 additions & 0 deletions WIZMSGHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ def makecommands(self, cmd_list, op_code):
def sendcommands(self):
self.sock.sendto(self.msg)


# Check the response (for setting / reset / factory)
def checkresponse(self):
readready, writeready, errorready = select.select(self.inputs, self.outputs, self.errors, 1)

configreply = None
while True:
for sock in readready:
if sock == self.sock.sock:
data = self.sock.recvfrom()
configreply = data.splitlines()
# print('config reply:', configreply)
readready, writeready, errorready = select.select(self.inputs, self.outputs, self.errors, 1)

if len(readready) == 0:
break

if configreply is not None:
return 1
else:
return -1


def parseresponse(self):
readready, writeready, errorready = select.select(self.inputs, self.outputs, self.errors, 1)

Expand Down Expand Up @@ -250,6 +273,11 @@ def parseresponse(self):

if self.opcode is OP_SEARCHALL:
return len(self.mac_list)
elif self.opcode is OP_SETCOMMAND or self.opcode is OP_SETFILE:
if replylists is not None:
return True
else:
return -1
elif self.opcode is OP_FWUP:
return self.reply
# sys.stdout.write("%s\r\n" % self.mac_list)
Expand Down
70 changes: 39 additions & 31 deletions wizconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ def set_maclist(self, mac_list, devname, version, status, ip_list):
sys.stdout.write(e)
for i in range(len(mac_list)):
print('* Device %d: %s [%s] | %s | %s | Version: %s ' % (i+1, mac_list[i].decode(), devname[i].decode(), ip_list[i].decode(), status[i].decode(), version[i].decode()))
# print('%s|%s|%s|%s' % (mac_list[i].decode(), devname[i].decode(), version[i].decode(), ip_list[i].decode()))
info = "%s\n" % (mac_list[i].decode())
if info in data:
# print('===> already in')
pass
else:
print('New Device: %s' % mac_list[i].decode())
Expand All @@ -152,29 +152,23 @@ def __init__(self, mac_addr, file_name):

self.mac_addr = mac_addr
self.filename = file_name

def run(self):
thread_list = []
update_state = DEV_STATE_IDLE

while update_state <= DEV_STATE_APPUPDATED:

if update_state is DEV_STATE_IDLE:
# print('=================================>> IDLE:', self.mac_addr)
print('[Firmware upload] device %s' % (mac_addr))
# For jump to boot mode
jumpToApp(self.mac_addr)
elif update_state is DEV_STATE_APPBOOT:
time.sleep(2)
# print('=================================>> APPBOOT:', self.mac_addr)
th_fwup = FWUploadThread()
th_fwup.setparam(self.mac_addr, self.filename)
th_fwup.sendCmd('FW')
th_fwup.start()
th_fwup.join()
# elif update_state is DEV_STATE_APPUPDATED:
# print('=================================>> BOOT UPDATED:', self.mac_addr)

update_state += 1

class MultiConfigThread(threading.Thread):
Expand All @@ -187,6 +181,7 @@ def __init__(self, mac_addr, cmd_list, op_code):

self.mac_addr = mac_addr
self.cmd_list = cmd_list
self.configresult = None

self.op_code = OP_SETCOMMAND

Expand All @@ -205,14 +200,23 @@ def SetMultiIP(self, host_ip):
setcmd['OP'] = '1'
self.cmd_list = wizmakecmd.setcommand(mac_addr, list(setcmd.keys()), list(setcmd.values()))

def Checkresult(self):
return self.configresult

def run(self):
# print('multiset cmd_list: ', self.cmd_list)
# print('RUN: Multiconfig device: %s' % (mac_addr))
self.wizmsghangler.makecommands(self.cmd_list, self.op_code)
self.wizmsghangler.sendcommands()
if self.op_code is OP_GETFILE:
self.wizmsghangler.parseresponse()

else:
self.configresult = self.wizmsghangler.checkresponse()
# print('\t%s: %r' % (self.mac_addr, self.configresult))
if self.configresult < 0:
print(' [%s] Configuration failed. Please check the device.' % (self.mac_addr))
else:
print(' [%s] Configuration success!' % (self.mac_addr))

if __name__ == '__main__':
wizmakecmd = WIZMakeCMD()
Expand Down Expand Up @@ -260,7 +264,7 @@ def run(self):
sys.exit(0)

op_code = OP_SETCOMMAND
print('Device configuration start...\n')
print('Devcie configuration start...\n')
# General config

if args.alloc: setcmd['IM'] = args.alloc
Expand Down Expand Up @@ -348,7 +352,6 @@ def run(self):
if wiz752cmdObj.isvalidparameter("LI", host_ip) is False:
sys.stdout.write("Invalid IP address!\r\n")
sys.exit(0)
#################################
for i in range(len(mac_list)):
mac_addr = re.sub('[\r\n]', '', mac_list[i])
# print(mac_addr)
Expand All @@ -360,9 +363,10 @@ def run(self):
fwup_name.start()
else:
if args.multiset:
th_config = MultiConfigThread(mac_addr, cmd_list, OP_SETCOMMAND)
th_config.SetMultiIP(host_ip)
th_config.start()
th_name = 'th%d_config' % (i)
th_name = MultiConfigThread(mac_addr, cmd_list, OP_SETCOMMAND)
th_name.SetMultiIP(host_ip)
th_name.start()
elif args.getfile:
op_code = OP_GETFILE
cmd_list = wizmakecmd.get_value(mac_addr, args.getfile)
Expand All @@ -374,9 +378,8 @@ def run(self):
op_code = OP_SETFILE
print('[Setfile] Device [%s] Config from \'%s\' file.' % (mac_addr, args.setfile))
cmd_list = wizmakecmd.set_value(mac_addr, args.setfile)

wizmsghangler.makecommands(cmd_list, op_code)
wizmsghangler.sendcommands()
th_setfile = MultiConfigThread(mac_addr, cmd_list, OP_SETFILE)
th_setfile.start()
else:
if args.reset:
print('[Multi] Reset devices %d: %s' % (i+1, mac_addr))
Expand All @@ -386,11 +389,13 @@ def run(self):
cmd_list = wizmakecmd.factory_reset(mac_addr)
else:
# op_code = OP_SETCOMMAND
print('[Multi] Setting devices %d: %s' % (i+1, mac_addr))
print('[Multi] Setting devcies %d: %s' % (i+1, mac_addr))
cmd_list = wizmakecmd.setcommand(mac_addr, list(setcmd.keys()), list(setcmd.values()))
get_cmd_list = wizmakecmd.getcommand(mac_addr, list(setcmd.keys()))
th_config = MultiConfigThread(mac_addr, cmd_list, OP_SETCOMMAND)
th_config.start()
th_name = 'th%d_config' % (i)
th_name = MultiConfigThread(mac_addr, cmd_list, OP_SETCOMMAND)
th_name.start()

# print('<ALL> op_code %d, cmd_list: %s\n' % (op_code, cmd_list))
if args.getfile:
print('[Multi][Getfile] Get device [%s] info from \'%s\' commands\n' % (mac_addr, args.getfile))
Expand All @@ -412,9 +417,11 @@ def run(self):
print('Start to Search devices...')
cmd_list = wizmakecmd.search()
elif args.reset:
op_code = OP_SETCOMMAND
print('Device %s Reset' % mac_addr)
cmd_list = wizmakecmd.reset(mac_addr)
elif args.factory:
op_code = OP_SETCOMMAND
print('Device %s Factory reset' % mac_addr)
cmd_list = wizmakecmd.factory_reset(mac_addr)
elif args.setfile:
Expand All @@ -426,7 +433,8 @@ def run(self):
print('[Getfile] Get device [%s] info from \'%s\' commands\n' % (mac_addr, args.getfile))
cmd_list = wizmakecmd.get_value(mac_addr, args.getfile)
else:
print('* Single device config: %s' % mac_addr)
op_code = OP_SETCOMMAND
print('* Single devcie config: %s' % mac_addr)
cmd_list = wizmakecmd.setcommand(mac_addr, list(setcmd.keys()), list(setcmd.values()))
get_cmd_list = wizmakecmd.getcommand(mac_addr, list(setcmd.keys()))
# print('get_cmd_list', get_cmd_list)
Expand All @@ -435,17 +443,17 @@ def run(self):
if args.fwfile or args.factory or args.reset:
pass
else:
print('\nDevice configuration complete!')
print('Configuration result:')
elif args.fwfile:
pass
else:
# print('<SINGLE> op_code %d, cmd_list: %s' % (op_code, cmd_list))
wizmsghangler.makecommands(cmd_list, op_code)
wizmsghangler.sendcommands()
devnum = wizmsghangler.parseresponse()
conf_result = wizmsghangler.parseresponse()

if args.search:
print('\nSearch result: ' + str(devnum) + ' devices are detected')
print('\nSearch result: ' + str(conf_result) + ' devices are detected')
# print(wizmsghangler.mac_list)
dev_name = wizmsghangler.devname
mac_list = wizmsghangler.mac_list
Expand All @@ -462,15 +470,15 @@ def run(self):
elif args.multiset or args.factory or args.reset:
pass
elif op_code is OP_SETCOMMAND:
if args.factory or args.reset:
pass
if conf_result < 0:
print('\nWarning: No response from the device [%s]. Please check the device\'s status.' % mac_addr)
else:
print('\nDevice configuration complete!\n')
print('\nDevice configuration complete!')

# print('get_cmd_list: %s' % get_cmd_list)
wizmsghangler.makecommands(get_cmd_list, OP_GETCOMMAND)
wizmsghangler.sendcommands()
wizmsghangler.parseresponse()
# wizmsghangler.makecommands(get_cmd_list, OP_GETCOMMAND)
# wizmsghangler.sendcommands()
# wizmsghangler.parseresponse()

# wizmsghangler.get_log()


0 comments on commit ed35791

Please sign in to comment.