smart_plugs.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. """API routes for smart plug management."""
  2. import logging
  3. from datetime import datetime, timedelta
  4. from fastapi import APIRouter, Depends, HTTPException, Query
  5. from sqlalchemy.ext.asyncio import AsyncSession
  6. from sqlalchemy import select
  7. from backend.app.core.database import get_db
  8. from backend.app.models.smart_plug import SmartPlug
  9. from backend.app.models.printer import Printer
  10. from backend.app.schemas.smart_plug import (
  11. SmartPlugCreate,
  12. SmartPlugUpdate,
  13. SmartPlugResponse,
  14. SmartPlugControl,
  15. SmartPlugStatus,
  16. SmartPlugTestConnection,
  17. SmartPlugEnergy,
  18. )
  19. from backend.app.services.tasmota import tasmota_service
  20. from backend.app.services.printer_manager import printer_manager
  21. from backend.app.services.notification_service import notification_service
  22. logger = logging.getLogger(__name__)
  23. router = APIRouter(prefix="/smart-plugs", tags=["smart-plugs"])
  24. @router.get("/", response_model=list[SmartPlugResponse])
  25. async def list_smart_plugs(db: AsyncSession = Depends(get_db)):
  26. """List all smart plugs."""
  27. result = await db.execute(select(SmartPlug).order_by(SmartPlug.name))
  28. return list(result.scalars().all())
  29. @router.post("/", response_model=SmartPlugResponse)
  30. async def create_smart_plug(
  31. data: SmartPlugCreate,
  32. db: AsyncSession = Depends(get_db),
  33. ):
  34. """Create a new smart plug."""
  35. # Validate printer_id if provided
  36. if data.printer_id:
  37. result = await db.execute(
  38. select(Printer).where(Printer.id == data.printer_id)
  39. )
  40. if not result.scalar_one_or_none():
  41. raise HTTPException(400, "Printer not found")
  42. # Check if printer already has a plug assigned
  43. result = await db.execute(
  44. select(SmartPlug).where(SmartPlug.printer_id == data.printer_id)
  45. )
  46. if result.scalar_one_or_none():
  47. raise HTTPException(400, "This printer already has a smart plug assigned")
  48. plug = SmartPlug(**data.model_dump())
  49. db.add(plug)
  50. await db.commit()
  51. await db.refresh(plug)
  52. logger.info(f"Created smart plug '{plug.name}' at {plug.ip_address}")
  53. return plug
  54. @router.get("/by-printer/{printer_id}", response_model=SmartPlugResponse | None)
  55. async def get_smart_plug_by_printer(printer_id: int, db: AsyncSession = Depends(get_db)):
  56. """Get the smart plug assigned to a printer."""
  57. result = await db.execute(
  58. select(SmartPlug).where(SmartPlug.printer_id == printer_id)
  59. )
  60. plug = result.scalar_one_or_none()
  61. if not plug:
  62. return None
  63. return plug
  64. @router.get("/{plug_id}", response_model=SmartPlugResponse)
  65. async def get_smart_plug(plug_id: int, db: AsyncSession = Depends(get_db)):
  66. """Get a specific smart plug."""
  67. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  68. plug = result.scalar_one_or_none()
  69. if not plug:
  70. raise HTTPException(404, "Smart plug not found")
  71. return plug
  72. @router.patch("/{plug_id}", response_model=SmartPlugResponse)
  73. async def update_smart_plug(
  74. plug_id: int,
  75. data: SmartPlugUpdate,
  76. db: AsyncSession = Depends(get_db),
  77. ):
  78. """Update a smart plug."""
  79. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  80. plug = result.scalar_one_or_none()
  81. if not plug:
  82. raise HTTPException(404, "Smart plug not found")
  83. update_data = data.model_dump(exclude_unset=True)
  84. # Validate new printer_id if being changed
  85. if "printer_id" in update_data and update_data["printer_id"]:
  86. new_printer_id = update_data["printer_id"]
  87. # Check printer exists
  88. result = await db.execute(
  89. select(Printer).where(Printer.id == new_printer_id)
  90. )
  91. if not result.scalar_one_or_none():
  92. raise HTTPException(400, "Printer not found")
  93. # Check if that printer already has a different plug assigned
  94. result = await db.execute(
  95. select(SmartPlug).where(
  96. SmartPlug.printer_id == new_printer_id,
  97. SmartPlug.id != plug_id,
  98. )
  99. )
  100. if result.scalar_one_or_none():
  101. raise HTTPException(400, "This printer already has a smart plug assigned")
  102. for field, value in update_data.items():
  103. setattr(plug, field, value)
  104. await db.commit()
  105. await db.refresh(plug)
  106. logger.info(f"Updated smart plug '{plug.name}'")
  107. return plug
  108. @router.delete("/{plug_id}")
  109. async def delete_smart_plug(plug_id: int, db: AsyncSession = Depends(get_db)):
  110. """Delete a smart plug."""
  111. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  112. plug = result.scalar_one_or_none()
  113. if not plug:
  114. raise HTTPException(404, "Smart plug not found")
  115. plug_name = plug.name
  116. await db.delete(plug)
  117. await db.commit()
  118. logger.info(f"Deleted smart plug '{plug_name}'")
  119. return {"message": "Smart plug deleted"}
  120. @router.post("/{plug_id}/control")
  121. async def control_smart_plug(
  122. plug_id: int,
  123. control: SmartPlugControl,
  124. db: AsyncSession = Depends(get_db),
  125. ):
  126. """Manual control: on/off/toggle."""
  127. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  128. plug = result.scalar_one_or_none()
  129. if not plug:
  130. raise HTTPException(404, "Smart plug not found")
  131. if control.action == "on":
  132. success = await tasmota_service.turn_on(plug)
  133. expected_state = "ON"
  134. elif control.action == "off":
  135. success = await tasmota_service.turn_off(plug)
  136. expected_state = "OFF"
  137. elif control.action == "toggle":
  138. success = await tasmota_service.toggle(plug)
  139. expected_state = None # Unknown after toggle
  140. else:
  141. raise HTTPException(400, f"Invalid action: {control.action}")
  142. if not success:
  143. raise HTTPException(503, "Failed to communicate with device")
  144. # Update last state and reset auto_off_executed when turning on
  145. if expected_state:
  146. plug.last_state = expected_state
  147. if expected_state == "ON":
  148. plug.auto_off_executed = False # Reset flag when manually turning on
  149. elif expected_state == "OFF" and plug.printer_id:
  150. # Mark printer offline immediately for faster UI update
  151. printer_manager.mark_printer_offline(plug.printer_id)
  152. plug.last_checked = datetime.utcnow()
  153. await db.commit()
  154. return {"success": True, "action": control.action}
  155. @router.get("/{plug_id}/status", response_model=SmartPlugStatus)
  156. async def get_plug_status(plug_id: int, db: AsyncSession = Depends(get_db)):
  157. """Get current plug status from device including energy data."""
  158. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  159. plug = result.scalar_one_or_none()
  160. if not plug:
  161. raise HTTPException(404, "Smart plug not found")
  162. status = await tasmota_service.get_status(plug)
  163. # Update last state in database
  164. if status["reachable"]:
  165. plug.last_state = status["state"]
  166. plug.last_checked = datetime.utcnow()
  167. await db.commit()
  168. # Fetch energy data if device is reachable
  169. energy_data = None
  170. if status["reachable"]:
  171. energy = await tasmota_service.get_energy(plug)
  172. if energy:
  173. energy_data = SmartPlugEnergy(**energy)
  174. # Check power alerts
  175. await check_power_alerts(plug, energy.get("power"), db)
  176. return SmartPlugStatus(
  177. state=status["state"],
  178. reachable=status["reachable"],
  179. device_name=status.get("device_name"),
  180. energy=energy_data,
  181. )
  182. async def check_power_alerts(plug: SmartPlug, current_power: float | None, db: AsyncSession):
  183. """Check if power crosses alert thresholds and send notifications."""
  184. if not plug.power_alert_enabled or current_power is None:
  185. return
  186. # Cooldown: don't alert more than once per 5 minutes
  187. cooldown_minutes = 5
  188. if plug.power_alert_last_triggered:
  189. time_since_last = datetime.utcnow() - plug.power_alert_last_triggered
  190. if time_since_last < timedelta(minutes=cooldown_minutes):
  191. return
  192. alert_triggered = False
  193. alert_type = None
  194. threshold = None
  195. # Check high threshold
  196. if plug.power_alert_high is not None and current_power > plug.power_alert_high:
  197. alert_triggered = True
  198. alert_type = "high"
  199. threshold = plug.power_alert_high
  200. # Check low threshold
  201. if plug.power_alert_low is not None and current_power < plug.power_alert_low:
  202. alert_triggered = True
  203. alert_type = "low"
  204. threshold = plug.power_alert_low
  205. if alert_triggered:
  206. plug.power_alert_last_triggered = datetime.utcnow()
  207. await db.commit()
  208. # Send notification
  209. title = f"Power Alert: {plug.name}"
  210. if alert_type == "high":
  211. message = f"Power consumption is {current_power:.1f}W, above threshold of {threshold:.1f}W"
  212. else:
  213. message = f"Power consumption is {current_power:.1f}W, below threshold of {threshold:.1f}W"
  214. logger.info(f"Power alert triggered for {plug.name}: {message}")
  215. # Use printer_error event type for power alerts (closest match)
  216. await notification_service.send_notification(
  217. event_type="printer_error",
  218. title=title,
  219. message=message,
  220. printer_id=plug.printer_id,
  221. printer_name=plug.name,
  222. context={
  223. "error_type": f"Power {alert_type.title()}",
  224. "error_detail": message,
  225. },
  226. )
  227. @router.post("/test-connection")
  228. async def test_connection(data: SmartPlugTestConnection):
  229. """Test connection to a Tasmota device."""
  230. result = await tasmota_service.test_connection(
  231. data.ip_address,
  232. data.username,
  233. data.password,
  234. )
  235. if not result["success"]:
  236. raise HTTPException(503, result.get("error", "Failed to connect to device"))
  237. return {
  238. "success": True,
  239. "state": result["state"],
  240. "device_name": result.get("device_name"),
  241. }