-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.py
87 lines (68 loc) · 2.62 KB
/
consumer.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
import socket
import struct
import requests
SERVER_PORT = 8080
SENSOR_DATA_SIZE = 2155
APP_PORT = 5000
# Function to unpack received data into SensorData structure
def unpack_sensor_data(data):
sensor_data = {"rooms":{}}
str_data = data.decode("utf-8")
room_arrays = str_data.split("||") # room_name, co2, humidity, light, pir, temperature
for room_data in room_arrays:
room_data = room_data.split(",")
room_data = [s.replace('\x00', '') for s in room_data] # remove null character if exists
sensor_data["rooms"][room_data[0]] = {
"co2": room_data[1],
"humidity": room_data[2],
"light": room_data[3],
"pir": room_data[4],
"temperature": room_data[5]
}
return sensor_data
# Function to handle the client connection
def handle_client(client_socket, client_address):
try:
while True:
# Receive data from the client
data = client_socket.recv(SENSOR_DATA_SIZE)
if not data:
break
# Unpack the received data into SensorData structure
try:
sensor_data = unpack_sensor_data(data)
response = requests.post(f"http://127.0.0.1:{APP_PORT}", json=sensor_data)
except:
print("START THE WEB SERVER")
room_names = sensor_data["rooms"].keys()
for room in room_names:
room_data = sensor_data["rooms"][room]
# print(f"{room}\tCO2={room_data['co2']}, Humidity={room_data['humidity']}")
except Exception as e:
print(f"Error: {e}")
finally:
# Close the client socket
client_socket.close()
print(f"Connection with {client_address} closed.")
def main():
# Create a socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Bind the socket to a specific address and port
server_socket.bind(('127.0.0.1', SERVER_PORT))
# Listen for incoming connections
server_socket.listen(1)
print(f"Server listening on port {SERVER_PORT}")
# Accept a connection from a client
while True:
client_socket, client_address = server_socket.accept()
print(f"Accepted connection from {client_address}")
# Handle the client connection in a separate function
handle_client(client_socket, client_address)
except KeyboardInterrupt:
print("Server terminated by the user.")
finally:
# Close the server socket
server_socket.close()
if __name__ == "__main__":
main()