-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# api/websocket_api.py | ||
|
||
from flask import Flask | ||
from flask_socketio import SocketIO, emit | ||
from src.iot.sensor_integration import SensorData | ||
|
||
app = Flask(__name__) | ||
socketio = SocketIO(app) | ||
|
||
# Initialize sensor data | ||
sensor_data = SensorData() | ||
|
||
@socketio.on('connect') | ||
def handle_connect(): | ||
"""Handle new WebSocket connections.""" | ||
print('Client connected') | ||
emit('response', {'message': 'Connected to WebSocket API'}) | ||
|
||
@socketio.on('request_latest_data') | ||
def handle_request_latest_data(): | ||
"""Send the latest climate data to the client.""" | ||
data = sensor_data.get_latest_data() | ||
emit('latest_data', data) | ||
|
||
@socketio.on('disconnect') | ||
def handle_disconnect(): | ||
"""Handle WebSocket disconnections.""" | ||
print('Client disconnected') | ||
|
||
if __name__ == '__main__': | ||
socketio.run(app, debug=True, host='0.0.0.0', port=5001) |