Skip to content

Commit

Permalink
update: 2024-10-14_1
Browse files Browse the repository at this point in the history
  • Loading branch information
minsubak committed Oct 14, 2024
1 parent f495cc6 commit 79b04e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
6 changes: 3 additions & 3 deletions fastapi/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ async def init_db(delay=5):
try:
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
print("INFO: Database connection successful and table creation complete.")
print("INFO: Database connection successful and table creation complete.", flush=True)
break
except Exception as e:
print(f"INFO: Database connection failure. \n{e}")
print(f"INFO: Retrying in {delay} seconds...")
print(f"INFO: Database connection failure. \n{e}", flush=True)
print(f"INFO: Retrying in {delay} seconds...", flush=True)
await asyncio.sleep(delay)

async def get_db():
Expand Down
39 changes: 22 additions & 17 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,42 @@ async def verify_token_route(token: str = Depends(OAuth2PasswordBearer(tokenUrl=
# Dictionary to track connected IoT devices
connected_iot_devices = {}

# IoT Device Namespace ("/iot")
@sio.on("connect", namespace="/iot")
async def connect_iot(sid, environ):
# IoT device namespace handlers
@sio.event(namespace="/iot")
async def connect(sid, environ):
device_id = environ.get('HTTP_DEVICE_ID') # Get device ID from headers
connected_iot_devices[device_id] = sid # Track the device's socket ID
print(f"IoT Device {device_id} connected with SID {sid}.")

@sio.on("disconnect", namespace="/iot")
async def disconnect_iot(sid):
@sio.event(namespace="/iot")
async def disconnect(sid):
device_id = next((key for key, value in connected_iot_devices.items() if value == sid), None)
if device_id:
del connected_iot_devices[device_id]
print(f"IoT Device {device_id} disconnected.")
print(f"IoT Device {device_id} disconnected.", flush=True)

@sio.on("iot_event", namespace="/iot")
async def iot_event(sid, data):
print(f"Received data from IoT Device {sid}: {data}")
# Handle data from IoT device if necessary
print(f"INFO: Received data from IoT Device {sid}: {data}", flush=True)
await sio.emit("iot_event", "Test", namespace="/iot")

# Example of emitting data to all connected devices
# async def broadcast_to_all_devices(event, message):
# for sid in connected_iot_devices.items():
# await sio.emit(event, message, to=sid, namespace="/iot")

# AI Server Namespace ("/ai")
@sio.on("connect", namespace="/ai")
@sio.event(namespace="/ai")
async def connect_ai(sid, environ):
print(f"AI Server {sid} connected.")
print(f"AI Server {sid} connected.", flush=True)

@sio.event(namespace="/ai")
async def disconnect_ai(sid):
print(f"AI Server {sid} disconnected.", flush=True)

@sio.on("ai_event", namespace="/ai")
async def ai_event(sid, data):
print(f"Received command from AI Server {sid}: {data}")
print(f"Received command from AI Server {sid}: {data}", flush=True)

# Extract target IoT device and command from data
target_device_id = data.get("device_id")
Expand All @@ -117,13 +126,9 @@ async def ai_event(sid, data):

# Send command to the specified IoT device
await sio.emit("iot_command", {"command": command}, room=target_sid, namespace="/iot")
print(f"Sent command to IoT Device {target_device_id}: {command}")
print(f"Sent command to IoT Device {target_device_id}: {command}", flush=True)
else:
print(f"IoT Device {target_device_id} is not connected.")

@sio.on("disconnect", namespace="/ai")
async def disconnect_ai(sid):
print(f"AI Server {sid} disconnected.")
print(f"IoT Device {target_device_id} is not connected.", flush=True)

# Mount SocketIO app at "/ws" for WebSocket communication
app.mount("/ws", socket_app)

0 comments on commit 79b04e5

Please sign in to comment.