summaryrefslogtreecommitdiffstats
path: root/src/ogRest.py
diff options
context:
space:
mode:
authorAlvaro Neira Ayuso <aneira@soleta.eu>2019-12-16 11:17:09 +0100
committerAlvaro Neira Ayuso <alvaroneay@gmail.com>2020-01-19 19:50:44 +0100
commitdfc97ffedb9a356299b593f76ce057f5953a77ab (patch)
tree031c174ac09f571b706d228f5f4ce908f15a2ae3 /src/ogRest.py
parent77906be87fc67a49cf4a34fd0a03dfb7a10d74ae (diff)
Merge ogRest and ogProcess to have only one class
Diffstat (limited to 'src/ogRest.py')
-rw-r--r--src/ogRest.py55
1 files changed, 48 insertions, 7 deletions
diff --git a/src/ogRest.py b/src/ogRest.py
index dfc382c..85e961a 100644
--- a/src/ogRest.py
+++ b/src/ogRest.py
@@ -1,14 +1,55 @@
+import threading
+import platform
+import time
from enum import Enum
+if platform.system() == 'Linux':
+ from src.linux import ogOperations
+
class ogResponses(Enum):
BAD_REQUEST=0
IN_PROGRESS=1
OK=2
-def getResponse(response):
- if response == ogResponses.BAD_REQUEST:
- return 'HTTP/1.0 400 Bad request\r\n\r\n'
- if response == ogResponses.IN_PROGRESS:
- return 'HTTP/1.0 202 Accepted\r\n\r\n'
- if response == ogResponses.OK:
- return 'HTTP/1.0 200 OK\r\n\r\n'
+class ogRest():
+ def getResponse(self, response):
+ if response == ogResponses.BAD_REQUEST:
+ return 'HTTP/1.0 400 Bad request\r\n\r\n'
+ if response == ogResponses.IN_PROGRESS:
+ return 'HTTP/1.0 202 Accepted\r\n\r\n'
+ if response == ogResponses.OK:
+ return 'HTTP/1.0 200 OK\r\n\r\n'
+
+ def processOperation(self, op, URI, client):
+ if ("poweroff" in URI):
+ self.process_poweroff(client)
+ elif ("reboot" in URI):
+ self.process_reboot(client)
+ elif ("probe" in URI):
+ self.process_probe(client)
+ else:
+ client.send(self.getResponse(ogResponses.BAD_REQUEST))
+
+ return 0
+
+ def process_reboot(self, client):
+ # Rebooting thread
+ def rebt():
+ ogOperations.reboot()
+
+ client.send(self.getResponse(ogResponses.IN_PROGRESS))
+ client.disconnect()
+ threading.Thread(target=rebt).start()
+
+ def process_poweroff(self, client):
+ # Powering off thread
+ def pwoff():
+ time.sleep(2)
+ ogOperations.poweroff()
+
+ client.send(self.getResponse(ogResponses.IN_PROGRESS))
+ client.disconnect()
+ threading.Thread(target=pwoff).start()
+
+ def process_probe(self, client):
+ client.send(self.getResponse(ogResponses.OK))