From 72e279c0ba46abbb6075b3161c70cc390a792d38 Mon Sep 17 00:00:00 2001 From: Nicola Peditto Date: Tue, 30 Apr 2019 11:32:11 +0200 Subject: [PATCH] LR v0.4.9dev13: fix connectivity info (MAC); new device RPC functions. Change-Id: Ia41f28deaca5ec79eacd40b638878db085c33a4f --- iotronic_lightningrod/common/utils.py | 12 +- .../modules/device_manager.py | 199 +++++++++++++----- 2 files changed, 146 insertions(+), 65 deletions(-) diff --git a/iotronic_lightningrod/common/utils.py b/iotronic_lightningrod/common/utils.py index 2446f62..b30f6b4 100644 --- a/iotronic_lightningrod/common/utils.py +++ b/iotronic_lightningrod/common/utils.py @@ -141,17 +141,15 @@ def get_socket_info(wport): iface = key for elem in dct[key]: ip_addr = elem.address - if ip_addr == str( - lr_net_iface.laddr.ip): + if ip_addr == str(lr_net_iface.laddr.ip): for snicaddr in dct[iface]: if snicaddr.family == 17: lr_mac = snicaddr.address - else: - lr_mac = "N/A" + sock_bundle = [iface, ip_addr, + lr_mac] + return sock_bundle - sock_bundle = [iface, ip_addr, lr_mac] - - return sock_bundle + return sock_bundle except Exception as e: LOG.warning("Error getting socket info " + str(e)) diff --git a/iotronic_lightningrod/modules/device_manager.py b/iotronic_lightningrod/modules/device_manager.py index e3ce37f..eca14f4 100644 --- a/iotronic_lightningrod/modules/device_manager.py +++ b/iotronic_lightningrod/modules/device_manager.py @@ -97,69 +97,146 @@ class DeviceManager(Module.Module): LOG.info(" --> " + str(meth[0]) + " registered!") - async def DevicePing(self): - rpc_name = utils.getFuncName() - LOG.info("RPC " + rpc_name + " CALLED") - - message = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f') - w_msg = WM.WampSuccess(message) - - return w_msg.serialize() - - async def DeviceReboot(self): - rpc_name = utils.getFuncName() - LOG.info("RPC " + rpc_name + " CALLED") - - def delayBoardReboot(): - time.sleep(3) - subprocess.call("reboot", shell=True) - - threading.Thread(target=delayBoardReboot).start() - - message = "Rebooting board in few seconds @" + \ - str(datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')) - w_msg = WM.WampSuccess(message) - - return w_msg.serialize() - - async def DeviceRestartLR(self): - rpc_name = utils.getFuncName() - LOG.info("RPC " + rpc_name + " CALLED") - - def delayLRrestarting(): - time.sleep(2) - python = sys.executable - os.execl(python, python, *sys.argv) - - threading.Thread(target=delayLRrestarting).start() - - message = "Restarting LR in 5 seconds (" + \ - datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f') + ")..." - w_msg = WM.WampSuccess(message) - - return w_msg.serialize() - - async def DeviceHostname(self): + async def DevicePing(self, parameters=None): rpc_name = utils.getFuncName() LOG.info("RPC " + rpc_name + " CALLED") command = "hostname" - out = subprocess.Popen( - command, - shell=True, - stdout=subprocess.PIPE - ) + try: + out = subprocess.Popen( + command, + shell=True, + stdout=subprocess.PIPE + ) - output = out.communicate()[0].decode('utf-8').strip() + output = out.communicate()[0].decode('utf-8').strip() - message = str(output) + "@" + \ + except Exception as err: + LOG.error("Error in parameters: " + str(err)) + output = "N/A" + + message = str(output) + " @ " + \ str(datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')) w_msg = WM.WampSuccess(message) return w_msg.serialize() - async def DeviceNetConfig(self): + async def DeviceReboot(self, parameters=None): + rpc_name = utils.getFuncName() + LOG.info("RPC " + rpc_name + " CALLED") + + delay = 3 # default delay + + try: + + if parameters['delay'] > 3: + delay = parameters['delay'] + + except Exception as err: + LOG.error("Error in 'delay' parameter: " + str(err)) + LOG.warning("--> default 'delay' parameter set: " + str(delay)) + + LOG.info("--> delay: " + str(delay)) + + def delayBoardReboot(): + time.sleep(delay) + subprocess.call("reboot", shell=True) + + threading.Thread(target=delayBoardReboot).start() + + if parameters == None: + message = "Rebooting board in few seconds @" + \ + str(datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')) + else: + message = "Rebooting board in " + str(delay) + " seconds (" \ + + datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f') \ + + ")..." + + w_msg = WM.WampSuccess(message) + + return w_msg.serialize() + + async def DeviceRestartLR(self, parameters=None): + rpc_name = utils.getFuncName() + LOG.info("RPC " + rpc_name + " CALLED") + + delay = 3 # default delay + + try: + + if parameters['delay'] > 3: + delay = parameters['delay'] + + except Exception as err: + LOG.error("Error in 'delay' parameter: " + str(err)) + LOG.warning("--> default 'delay' parameter set: " + str(delay)) + + LOG.info("--> delay: " + str(delay)) + + def delayLRrestarting(): + time.sleep(delay) + python = sys.executable + os.execl(python, python, *sys.argv) + + threading.Thread(target=delayLRrestarting).start() + + message = "Restarting LR in " + str(delay) \ + + " seconds (" \ + + datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f') + ")..." + + w_msg = WM.WampSuccess(message) + + return w_msg.serialize() + + async def DeviceUpgradeLR(self, parameters=None): + rpc_name = utils.getFuncName() + LOG.info("RPC " + rpc_name + " CALLED") + LOG.info("--> Parameters: " + str(parameters)) + + command = "pip3 install --upgrade iotronic-lightningrod" + + def delayLRupgrading(): + out = subprocess.Popen( + command, + shell=True, + stdout=subprocess.PIPE + ) + + output = out.communicate()[0].decode('utf-8').strip() + LOG.info(str(output)) + + try: + + threading.Thread(target=delayLRupgrading).start() + + except Exception as err: + LOG.error("Error in parameters: " + str(err)) + + w_msg = WM.WampSuccess("LR upgrading...") + + return w_msg.serialize() + + async def DeviceEcho(self, parameters=None): + rpc_name = utils.getFuncName() + LOG.info("RPC " + rpc_name + " CALLED") + LOG.info("--> Parameters: " + str(parameters)) + + try: + message = str(parameters['say']) + " @ " + \ + str(datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')) + LOG.info("--> Echo: " + str(message)) + + except Exception as err: + LOG.warning("Error in parameters: " + str(err)) + LOG.info("--> Echo (no-params): " + str(message)) + message = str(datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')) + + w_msg = WM.WampSuccess(message) + + return w_msg.serialize() + + async def DeviceNetConfig(self, parameters=None): rpc_name = utils.getFuncName() LOG.info("RPC " + rpc_name + " CALLED") @@ -171,14 +248,20 @@ class DeviceManager(Module.Module): def getIfconfig(): - command = "ifconfig" + try: - out = subprocess.Popen( - command, - shell=True, - stdout=subprocess.PIPE - ) + command = "ifconfig" - output = str(out.communicate()[0].decode('utf-8').strip()) + out = subprocess.Popen( + command, + shell=True, + stdout=subprocess.PIPE + ) + + output = str(out.communicate()[0].decode('utf-8').strip()) + + except Exception as err: + LOG.error("Error in 'ifconfig' command: " + str(err)) + output = "N/A" return output