AuroraActionEndpoint.vala 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Astralis;
  2. using Invercargill;
  3. using Inversion;
  4. using Spry;
  5. /**
  6. * AuroraActionEndpoint - HTTP endpoint that triggers aurora actions
  7. *
  8. * Handles POST requests to trigger aurora actions, which then broadcast
  9. * updates via the AuroraSseEndpoint to all connected SSE clients.
  10. */
  11. public class AuroraActionEndpoint : Object, Endpoint {
  12. private AuroraSseEndpoint aurora_sse;
  13. public AuroraActionEndpoint(AuroraSseEndpoint aurora_sse) {
  14. this.aurora_sse = aurora_sse;
  15. }
  16. public async HttpResult handle_request(HttpContext http_context, RouteContext route_context) throws Error {
  17. // Get action from route parameter
  18. string? action = null;
  19. route_context.mapped_parameters.try_get("action", out action);
  20. switch (action ?? "") {
  21. case "boost":
  22. aurora_sse.boost_solar_wind.begin();
  23. break;
  24. case "calm":
  25. aurora_sse.calm.begin();
  26. break;
  27. case "shift":
  28. aurora_sse.shift_colors.begin();
  29. break;
  30. case "wave":
  31. aurora_sse.add_wave.begin();
  32. break;
  33. default:
  34. return new HttpStringResult("{\"error\":\"Unknown action\"}", StatusCode.BAD_REQUEST)
  35. .set_header("Content-Type", "application/json");
  36. }
  37. return new HttpStringResult("{\"status\":\"ok\"}")
  38. .set_header("Content-Type", "application/json");
  39. }
  40. }
  41. /**
  42. * CounterActionEndpoint - HTTP endpoint that triggers counter actions
  43. *
  44. * Handles POST requests to trigger counter actions, which then broadcast
  45. * updates via the CounterSseEndpoint to all connected SSE clients.
  46. */
  47. public class CounterActionEndpoint : Object, Endpoint {
  48. private CounterSseEndpoint counter_sse;
  49. public CounterActionEndpoint(CounterSseEndpoint counter_sse) {
  50. this.counter_sse = counter_sse;
  51. }
  52. public async HttpResult handle_request(HttpContext http_context, RouteContext route_context) throws Error {
  53. // Get action from route parameter
  54. string? action = null;
  55. route_context.mapped_parameters.try_get("action", out action);
  56. switch (action ?? "") {
  57. case "increment":
  58. counter_sse.increment.begin();
  59. break;
  60. case "decrement":
  61. counter_sse.decrement.begin();
  62. break;
  63. case "reset":
  64. counter_sse.reset.begin();
  65. break;
  66. default:
  67. return new HttpStringResult("{\"error\":\"Unknown action\"}", StatusCode.BAD_REQUEST)
  68. .set_header("Content-Type", "application/json");
  69. }
  70. return new HttpStringResult("{\"status\":\"ok\"}")
  71. .set_header("Content-Type", "application/json");
  72. }
  73. }