forked from DiyadotSaha/SmartRoom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
166 lines (142 loc) · 4.68 KB
/
server.py
File metadata and controls
166 lines (142 loc) · 4.68 KB
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
# # server.py
# from fastapi import FastAPI, WebSocket
# from fastapi.middleware.cors import CORSMiddleware
# import asyncio
# from typing import List
# from threading import Thread
# import json
# from kafka import KafkaConsumer
# app = FastAPI()
# clients: List[WebSocket] = []
# main_loop = asyncio.get_event_loop()
# # In-memory buffer
# shared_buffer = []
# buffer_lock = asyncio.Lock()
# app.add_middleware(
# CORSMiddleware,
# allow_origins=["*"],
# allow_methods=["*"],
# allow_headers=["*"],
# )
# @app.websocket("/ws")
# async def websocket_endpoint(websocket: WebSocket):
# await websocket.accept()
# print("WebSocket client connected")
# clients.append(websocket)
# try:
# while True:
# await asyncio.sleep(0.1)
# except Exception as e:
# print("WebSocket error:", e)
# # finally:
# # clients.remove(websocket)
# def kafka_listener():
# consumer = KafkaConsumer(
# 'room_1_UI', 'room_2_UI',
# bootstrap_servers='localhost:9092',
# value_deserializer=lambda m: json.loads(m.decode('utf-8')),
# auto_offset_reset='latest',
# group_id='dashboard-group',
# )
# for msg in consumer:
# print("IN SERVER: Kafka message received:", msg.value)
# asyncio.run_coroutine_threadsafe(forward_to_clients(msg.topic, msg.value), main_loop)
# async def forward_to_clients(topic, data):
# structured_data = {
# "roomID": topic,
# "time": data["time"],
# "room_temp": data["room_temp"],
# "energy": data["energy"],
# "command": data["command"]
# }
# print("IN SERVER Sending Live to Clients: ", structured_data)
# for ws in clients:
# try:
# await ws.send_json(structured_data)
# except Exception as e:
# print("WebSocket send failed:", e)
# @app.on_event("startup")
# async def startup_event():
# Thread(target=kafka_listener, daemon=True).start()
from fastapi import FastAPI, WebSocket
from fastapi.middleware.cors import CORSMiddleware
import asyncio
from typing import List
from threading import Thread
import json
from kafka import KafkaConsumer
app = FastAPI()
clients: List[WebSocket] = []
main_loop = asyncio.get_event_loop()
# In-memory buffer and lock for thread-safety
shared_buffer = []
buffer_lock = asyncio.Lock()
MAX_BUFFER_SIZE = 100 # adjust as needed
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
print("WebSocket client connected")
# Send all buffered messages immediately upon connection.
async with buffer_lock:
for msg in shared_buffer:
try:
await websocket.send_json(msg)
except Exception as e:
print("Failed to send buffered message:", e)
clients.append(websocket)
try:
while True:
# This loop keeps the connection open.
await asyncio.sleep(0.1)
except Exception as e:
print("WebSocket error:", e)
finally:
if websocket in clients:
clients.remove(websocket)
print("WebSocket client disconnected")
def kafka_listener():
consumer = KafkaConsumer(
'room_1_UI', 'room_2_UI',
bootstrap_servers='localhost:9092',
value_deserializer=lambda m: json.loads(m.decode('utf-8')),
auto_offset_reset='latest',
group_id='dashboard-group',
)
for msg in consumer:
print("IN SERVER: Kafka message received:", msg.value)
asyncio.run_coroutine_threadsafe(forward_to_clients(msg.topic, msg.value), main_loop)
async def forward_to_clients(topic, data):
structured_data = {
"roomID": topic,
"time": data["time"],
"room_temp": data["room_temp"],
"energy": data["energy"],
"command": data["command"]
}
# Buffer the new message.
async with buffer_lock:
shared_buffer.append(structured_data)
# Remove oldest messages if buffer exceeds capacity.
if len(shared_buffer) > MAX_BUFFER_SIZE:
shared_buffer.pop(0)
print("IN SERVER Sending Live to Clients: ", structured_data)
dead_clients = []
for ws in clients:
try:
await ws.send_json(structured_data)
except Exception as e:
print("WebSocket send failed:", e)
dead_clients.append(ws)
# Remove any connections that failed.
for ws in dead_clients:
if ws in clients:
clients.remove(ws)
@app.on_event("startup")
async def startup_event():
Thread(target=kafka_listener, daemon=True).start()