-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
145 lines (125 loc) · 5.04 KB
/
server.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import json
import time
import os, sys
#from aquariumUSB import *
from aquariumv3 import *
#from lcd import *
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
#lcd_string("Index Requested",LCD_LINE_1)
def post (self):
WebCommand = self.get_argument ('command', '')
WebValue = self.get_argument ('value', '')
if WebCommand == 'Pi':
if WebValue == 'Shutdown':
if sys.platform == 'win32':
os.system('shutdown /s')
else:
#lcd_string("Shutting down...",LCD_LINE_2)
os.system('shutdown -h now')
elif WebValue == 'Reboot':
if sys.platform == 'win32':
os.system('shutdown /r')
else:
#lcd_string("Rebooting...",LCD_LINE_2)
os.system('shutdown -r now')
else:
print('No matching Pi Command')
return
elif WebCommand == 'Arduino':
if WebValue == 'Reset':
slave = Aquarium(0)
slave.ResetSlave()
else:
print('No matching Arduino Command')
return
else:
print('Command not recognised')
class TankHandler(tornado.web.RequestHandler):
def get(self, input):
self.render('tank.html')
def post(self, input):
aquarium_id = input
WebCommand = self.get_argument ('command', '')
WebValue = self.get_argument ('value', '')
mintemp_data = self.get_argument('MinTemp', '')
maxtemp_data = self.get_argument('MaxTemp', '')
minph_data = self.get_argument('Minph', '')
maxph_data = self.get_argument('Maxph', '')
self.set_header('Content-Type', 'application/json; charset=UTF-8')
aquarium = Aquarium(aquarium_id)
if WebCommand == 'Heating':
#print(WebCommand + ": " + WebValue)
aquarium.SendCommand(aquarium_id, WebCommand, WebValue)
self.write(json.dumps({"msg":"heater set to " + WebValue}, default=lambda x: None))
return
elif WebCommand == 'Light':
#print(WebCommand + ": " + WebValue)
aquarium.SendCommand(aquarium_id, WebCommand, WebValue)
self.write(json.dumps({"msg":"lights set to " + WebValue}, default=lambda x: None))
return
elif WebCommand == 'Pump':
#print(WebCommand + ": " + WebValue)
aquarium.SendCommand(aquarium_id, WebCommand, WebValue)
self.write(json.dumps({"msg":"water pump set to " + WebValue}, default=lambda x: None))
return
elif WebCommand == 'UpdateValues':
slave_json = aquarium.CurrentStatus(aquarium_id).rstrip("'")[1:]
loaded_json = json.loads(slave_json)
print(loaded_json)
update_response = {}
update_response['TankNo'] = aquarium_id
update_response['msg'] = loaded_json["msg"]
update_response['TempValue'] = loaded_json["Temp"] + " " + chr(176) + "C"
update_response['pHValue'] = loaded_json["pH"]
update_response['LightValue'] = loaded_json["Lights"]
update_response['PumpValue'] = loaded_json["Pump"]
self.write(json.dumps(update_response))
return
else:
self.write('parameter not defined')
class SlaveSocket(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self, input):
aquarium_id = input
print("WebSocket opened")
def on_message(self, message):
self.write_message(u"You said: " + message)
def on_close(self):
print("WebSocket closed")
#def UpdateIPs():
#lcd_string("LAN: " + get_ip_address('eth0'),LCD_LINE_3)
#lcd_string("IP_" + get_ip_address('wlan0'),LCD_LINE_2)
if __name__ == "__main__":
if sys.platform == 'win32':
x=1
else:
#lcd_init()
#lcd_string("Aquariumatic V3 ",LCD_LINE_1)
#lcd_string("IP_" + get_ip_address('wlan0'),LCD_LINE_2)
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
(r"/", IndexHandler),
(r"/ss(\d+)", SlaveSocket),
(r"/tank(\d+)", TankHandler)],
static_path=os.path.join(os.path.dirname(__file__), "static"),
template_path=os.path.join(os.path.dirname(__file__), "templates"))
httpServer = tornado.httpserver.HTTPServer(app)
httpServer.listen(options.port)
print ("Listening on port:", options.port)
main_loop = tornado.ioloop.IOLoop.instance()
# Schedule event (5 seconds from now)
#main_loop.call_later(5, UpdateIPs)
# Start main loop
main_loop.start()