75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
"""
|
|
Sensors controller for sensor data and runtime/water counter endpoints.
|
|
"""
|
|
|
|
from flask import Blueprint, jsonify
|
|
from ..services.data_cache import get_data_cache
|
|
from ..utils.logger import get_logger
|
|
from ..utils.error_handler import create_error_response
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
# Create blueprint
|
|
sensors_bp = Blueprint('sensors', __name__)
|
|
|
|
# Initialize services
|
|
cache = get_data_cache()
|
|
|
|
|
|
@sensors_bp.route('/sensors')
|
|
def get_sensors():
|
|
"""Get all sensor data"""
|
|
sensors = cache.get_sensors()
|
|
last_update = cache.get_last_update()
|
|
|
|
return jsonify({
|
|
"sensors": sensors,
|
|
"last_update": last_update,
|
|
"count": len(sensors)
|
|
})
|
|
|
|
|
|
@sensors_bp.route('/sensors/category/<category>')
|
|
def get_sensors_by_category(category):
|
|
"""Get sensors filtered by category"""
|
|
valid_categories = ['system', 'pressure', 'temperature', 'flow', 'quality']
|
|
|
|
if category not in valid_categories:
|
|
return create_error_response(
|
|
"Bad Request",
|
|
f"Invalid category '{category}'. Valid categories: {', '.join(valid_categories)}",
|
|
400
|
|
)
|
|
|
|
filtered_sensors = cache.get_sensors_by_category(category)
|
|
|
|
return jsonify({
|
|
"category": category,
|
|
"sensors": filtered_sensors,
|
|
"count": len(filtered_sensors),
|
|
"last_update": cache.get_last_update()
|
|
})
|
|
|
|
|
|
@sensors_bp.route('/runtime')
|
|
def get_runtime():
|
|
"""Get runtime hours data (IEEE 754 float)"""
|
|
runtime_data = cache.get_runtime()
|
|
|
|
return jsonify({
|
|
"runtime": runtime_data,
|
|
"last_update": cache.get_last_update(),
|
|
"count": len(runtime_data)
|
|
})
|
|
|
|
|
|
@sensors_bp.route('/water_counters')
|
|
def get_water_counters():
|
|
"""Get water production counter data (gallon totals)"""
|
|
water_counter_data = cache.get_water_counters()
|
|
|
|
return jsonify({
|
|
"water_counters": water_counter_data,
|
|
"last_update": cache.get_last_update(),
|
|
"count": len(water_counter_data)
|
|
}) |