-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
146 lines (112 loc) · 4.23 KB
/
main.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
146
# imports needed for tornado
from tornado import web
from tornado import ioloop
from tornado import websocket
from pathlib import Path
import os
import json
import threading
from time import sleep
from datetime import datetime
from psuedoSensor import PsuedoSensor
# array to store all of the generated data that was sent to the frontend
data = []
# function that will return the created server
def create_server():
# list of all of the handlers
handlers = [
(r"/", MainHandler),
(r"/ws", WebSocketHandler)
]
# Dictionary listing some settings, specifically the location of the template and static files are located
settings = {
"template_path" : Path(__file__).parent / "templates",
"static_path" : Path(__file__).parent / "static"
}
# returns the server
return web.Application(handlers, **settings)
# function to store the generated data into an array
def store_data(hum_temp_values):
# get the current time
now = datetime.now()
# format the time how I need to store it
formatted_now = f"{now.month}-{now.day}-{now.year} {now.hour}:{now.minute}:{now.second}"
# create the data point
data_point = {
"data": hum_temp_values,
"datetime": formatted_now
}
# insert the data point into the dictionary
data.append(data_point)
# prints the currently stored values
for dp in data:
print(dp)
print()
# Creating a handler for the path '/'
class MainHandler(web.RequestHandler):
# Creating function to handle the GET request type
def get(self):
# Render index.html
self.render("index.html")
# Creating a websocket handler
class WebSocketHandler(websocket.WebSocketHandler):
# Create an instance of the PsuedoSensor class
ps = PsuedoSensor()
# Function that runs when a connection has been established
def open(self):
# Sends message to the client connected
# self.write_message("Connection Open")
print("Connected to Client")
self.write_message("Hello Client")
# Function that runs when we receive a message
def on_message(self, message):
# Prints the message received
print(f"Message from Client: {message}")
# de-stringify the message
json_message = json.loads(message)
# check what is the packet is about
if (json_message["packet"] == "1 Random Value"):
# packet is asking for 1 random value to be sent to the frontend
# creates the message to send
message = {
"packet": "",
"data": self.ps.generate_values()
}
# stringify the message
stringified_message = json.dumps(message)
# send message to frontend
self.write_message(stringified_message)
# store the data
store_data(message["data"])
elif (json_message["packet"] == "10 Random Values"):
# Start a thread with the purpose of just generating 10 values
threading.Thread(target = self.random10()).start()
# Function that runs when the connection closes
def on_close(self):
# prints Connecton Closed
print("Connection Closed")
# Function to generate 10 values
def random10(self):
for _ in range(10):
# creates the message to send
message = {
"packet": "",
"data": self.ps.generate_values()
}
# stringify the message
stringified_message = json.dumps(message)
# send message to frontend
self.write_message(stringified_message)
# store the data
store_data(message["data"])
# adds a 1 second delay
sleep(1)
# Entrypoint
if __name__ == '__main__':
# Creates server and assigns it to the server variable
server = create_server()
# Tells the server to listen to port 8888
server.listen(8888)
print("Server Running")
# Starts the server in an event loop
ioloop.IOLoop.current().start()