-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_server.py
197 lines (174 loc) · 6.77 KB
/
data_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import asyncio
import websockets
import json
import sqlite3
from datetime import datetime, timezone
# Create or connect to the SQLite database
conn = sqlite3.connect('databases/sensors_database55.db') # Adjust path as needed
cursor = conn.cursor()
hostname = "192.168.2.153"
port = 5000
# Create tables if they don't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS raw_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sensor TEXT,
timestamp TEXT,
tvoc REAL,
eco2 REAL,
rawh2 REAL,
rawethanol REAL,
temperature REAL,
humidity REAL,
moisture REAL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS minutely_summary (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sensor TEXT,
timestamp TEXT,
avg_tvoc REAL,
avg_eco2 REAL,
avg_rawh2 REAL,
avg_rawethanol REAL,
avg_temperature REAL,
avg_humidity REAL,
avg_moisture REAL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS hourly_summary (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sensor TEXT,
timestamp TEXT,
avg_tvoc REAL,
avg_eco2 REAL,
avg_rawh2 REAL,
avg_rawethanol REAL,
avg_temperature REAL,
avg_humidity REAL,
avg_moisture REAL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS daily_summary (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sensor TEXT,
timestamp TEXT,
avg_tvoc REAL,
avg_eco2 REAL,
avg_rawh2 REAL,
avg_rawethanol REAL,
avg_temperature REAL,
avg_humidity REAL,
avg_moisture REAL
)
''')
# Function to insert sensor data into the database
def insert_data_into_database(sensor, timestamp, tvoc, eco2, rawh2, rawethanol, temperature, humidity, moisture):
cursor.execute('''
INSERT INTO raw_data (sensor, timestamp, tvoc, eco2, rawh2, rawethanol, temperature, humidity, moisture)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (sensor, timestamp, tvoc, eco2, rawh2, rawethanol, temperature, humidity, moisture))
conn.commit()
# Functions to calculate summaries
def calculate_minutely_summary():
cursor.execute('''
SELECT sensor,
strftime('%Y-%m-%d %H:%M:00', timestamp) as minute,
AVG(tvoc), AVG(eco2), AVG(rawh2), AVG(rawethanol), AVG(temperature), AVG(humidity), AVG(moisture)
FROM raw_data
WHERE timestamp >= datetime('now', '-1 minute')
AND timestamp < datetime('now')
AND minute NOT IN (SELECT timestamp FROM minutely_summary)
GROUP BY sensor, minute
''')
results = cursor.fetchall()
if results:
cursor.executemany('''
INSERT INTO minutely_summary (sensor, timestamp, avg_tvoc, avg_eco2, avg_rawh2, avg_rawethanol, avg_temperature, avg_humidity, avg_moisture)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', results)
conn.commit()
def calculate_hourly_summary():
cursor.execute('''
SELECT sensor,
strftime('%Y-%m-%d %H:00:00', timestamp) as hour,
AVG(tvoc), AVG(eco2), AVG(rawh2), AVG(rawethanol), AVG(temperature), AVG(humidity), AVG(moisture)
FROM raw_data
WHERE timestamp >= datetime('now', '-1 hour')
AND timestamp < datetime('now')
AND hour NOT IN (SELECT timestamp FROM hourly_summary)
GROUP BY sensor, hour
''')
results = cursor.fetchall()
if results:
cursor.executemany('''
INSERT INTO hourly_summary (sensor, timestamp, avg_tvoc, avg_eco2, avg_rawh2, avg_rawethanol, avg_temperature, avg_humidity, avg_moisture)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', results)
conn.commit()
def calculate_daily_summary():
cursor.execute('''
SELECT sensor,
strftime('%Y-%m-%d 00:00:00', timestamp) as day,
AVG(tvoc), AVG(eco2), AVG(rawh2), AVG(rawethanol), AVG(temperature), AVG(humidity), AVG(moisture)
FROM raw_data
WHERE timestamp >= datetime('now', '-1 day')
AND timestamp < datetime('now')
AND day NOT IN (SELECT timestamp FROM daily_summary)
GROUP BY sensor, day
''')
results = cursor.fetchall()
if results:
cursor.executemany('''
INSERT INTO daily_summary (sensor, timestamp, avg_tvoc, avg_eco2, avg_rawh2, avg_rawethanol, avg_temperature, avg_humidity, avg_moisture)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', results)
conn.commit()
# WebSocket handler
async def handle_websocket(websocket, path):
try:
async for message in websocket:
print(f"Received message: {message}")
try:
data = json.loads(message)
name = data["name"]
tvoc = data["TVOC"]
eco2 = data["eCO2"]
rawh2 = data["rawH2"]
rawethanol = data["rawEthanol"]
temperature = data["Temperature"]
humidity = data["Humidity"]
moisture = data["moisture"] # New data field for moisture
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
print(f"Name: {name}, TVOC: {tvoc}, eCO2: {eco2}, rawH2: {rawh2}, rawEthanol: {rawethanol}, Temperature: {temperature}, Humidity: {humidity}, Moisture: {moisture}, Timestamp: {timestamp}")
insert_data_into_database(name, timestamp, tvoc, eco2, rawh2, rawethanol, temperature, humidity, moisture)
except Exception as e:
print(f"Error parsing message: {e}")
await websocket.send("Message received")
except websockets.exceptions.ConnectionClosed as e:
print(f"Connection closed: {e}")
# Timer functions
async def minutely_summary_timer():
while True:
calculate_minutely_summary()
await asyncio.sleep(60) # Trigger every minute
async def hourly_summary_timer():
while True:
calculate_hourly_summary()
await asyncio.sleep(3600) # Trigger every hour
async def daily_summary_timer():
while True:
calculate_daily_summary()
await asyncio.sleep(86400) # Trigger every day
async def main():
minutely_task = asyncio.create_task(minutely_summary_timer())
hourly_task = asyncio.create_task(hourly_summary_timer())
daily_task = asyncio.create_task(daily_summary_timer())
async with websockets.serve(handle_websocket, hostname, port, ping_timeout=20, ping_interval=20):
print("Server started")
await asyncio.gather(minutely_task, hourly_task, daily_task)
if __name__ == "__main__":
asyncio.run(main())