summaryrefslogtreecommitdiffstats
path: root/src/ogRest.py
blob: 85e961a9e5bc463326a56e9ce8213b3167f949ea (plain)
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
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

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))