diff options
author | Alvaro Neira Ayuso <aneira@soleta.eu> | 2020-01-13 19:20:13 +0100 |
---|---|---|
committer | Alvaro Neira Ayuso <alvaroneay@gmail.com> | 2020-01-19 19:50:44 +0100 |
commit | 1ced3dd0693fc6d88c0d5ec8432102454e10075e (patch) | |
tree | 7385e7bea7359e10e0470e537c904228b62905a5 | |
parent | 683afa64657bf661f719e4e646457353199f38ec (diff) |
Improve hardware command response behavior
This patch give us a better support in case of error or success execution.
In error cases, the new behavior is to send an Internal Error http message (500).
Otherwise, the server will receive a message with a json with this format:
{ "hardware" : "xyz" }
"xyz" is the output saved in a specific path during the execution of
InventarioHardware.
-rw-r--r-- | src/linux/ogOperations.py | 6 | ||||
-rw-r--r-- | src/ogRest.py | 19 |
2 files changed, 19 insertions, 6 deletions
diff --git a/src/linux/ogOperations.py b/src/linux/ogOperations.py index 0034fd7..447f188 100644 --- a/src/linux/ogOperations.py +++ b/src/linux/ogOperations.py @@ -48,7 +48,11 @@ def procsoftware(httpparser, path): return result.decode('utf-8') def prochardware(path): - result = subprocess.check_output([OG_PATH + 'interfaceAdm/InventarioHardware', path], shell=True) + try: + result = subprocess.check_output([OG_PATH + 'interfaceAdm/InventarioHardware', path], shell=True) + except: + raise ValueError('Error: Incorrect command value') + return result.decode('utf-8') def procsetup(httpparser): diff --git a/src/ogRest.py b/src/ogRest.py index 90f58e7..99ecdbc 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -87,9 +87,19 @@ class ogThread(): client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) # Process hardware - def prochardware(msgqueue, path): - msgqueue.queue.clear() - msgqueue.put(ogOperations.prochardware(path)) + def prochardware(client, path): + try: + ogOperations.prochardware(path) + except ValueError as err: + client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) + return + + jsonResp = jsonResponse() + f = open(path, "r") + lines = f.readlines() + f.close() + jsonResp.addElement('hardware', lines[0]) + client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) # Process setup def procsetup(msgqueue, httpparser): @@ -191,8 +201,7 @@ class ogRest(): def process_hardware(self, client): path = '/tmp/Chrd-' + client.ip - threading.Thread(target=ogThread.prochardware, args=(self.msgqueue, path,)).start() - client.send(restResponse.getResponse(ogResponses.OK)) + threading.Thread(target=ogThread.prochardware, args=(client, path,)).start() def process_schedule(self, client): client.send(restResponse.getResponse(ogResponses.OK)) |