You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.5 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "git.aiterp.net/lucifer/new-server/app/client"
  6. "git.aiterp.net/lucifer/new-server/models"
  7. "log"
  8. "os"
  9. )
  10. func handlerCmd(
  11. ctx context.Context,
  12. c client.Client,
  13. ) {
  14. cmd := parseCommand(os.Args[2:])
  15. switch cmd.Name {
  16. case "list":
  17. handlers, err := c.GetHandlers(ctx)
  18. if err != nil {
  19. log.Fatalln(err)
  20. }
  21. WriteHandlerInfoTable(os.Stdout, handlers)
  22. case "add":
  23. model := models.EventHandler{}
  24. // Event name
  25. model.EventName = cmd.Params.Get(0).StringOr("")
  26. if len(model.EventName) == 0 {
  27. log.Fatalln("Event name needed!")
  28. }
  29. // Search
  30. fetchStr := cmd.Params.Get(1).String()
  31. if fetchStr == nil {
  32. log.Fatalln("Search filter needed!")
  33. }
  34. model.TargetKind, model.TargetValue = models.ParseFetchString(*fetchStr)
  35. // From / To
  36. model.From = cmd.Params.Get("from").TimeOfDayOr(models.Never)
  37. model.To = cmd.Params.Get("to").TimeOfDayOr(models.Never)
  38. if model.From.IsNever() != model.To.IsNever() {
  39. log.Fatalln("from and to must be specified together!")
  40. }
  41. // One shot
  42. model.OneShot = cmd.Params.Get("one-shot").StringOr("false") == "true"
  43. // Priority
  44. model.Priority = cmd.Params.Get("priority").IntOr(100)
  45. // Conditions
  46. model.Conditions = cmd.Params.Subset("conditions").EventConditions()
  47. // Power, Color, Intensity, Temperature
  48. nds := cmd.Params.DeviceState("set-")
  49. model.Actions.SetPower = nds.Power
  50. model.Actions.SetColor = nds.Color
  51. model.Actions.SetIntensity = nds.Intensity
  52. // Add intensity
  53. model.Actions.AddIntensity = cmd.Params.Get("add-intensity").Float()
  54. // Fire
  55. fireParams := cmd.Params.Subset("fire")
  56. fireName := fireParams.Get("name").String()
  57. if fireName != nil {
  58. firePayloadParams := fireParams.Subset("payload")
  59. model.Actions.FireEvent = &models.Event{
  60. Name: *fireName,
  61. Payload: make(map[string]string, len(firePayloadParams)),
  62. }
  63. for key, value := range firePayloadParams.StringMap() {
  64. model.Actions.FireEvent.Payload[key] = value
  65. }
  66. }
  67. newHandler, err := c.AddHandler(ctx, model)
  68. if err != nil {
  69. log.Fatalln(err)
  70. }
  71. WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*newHandler})
  72. case "delete":
  73. id := cmd.Params.Get(0).Int()
  74. if id == nil {
  75. log.Fatalln("ID missing")
  76. }
  77. handler, err := c.DeleteHandler(ctx, *id)
  78. if err != nil {
  79. log.Fatalln(err)
  80. }
  81. WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*handler})
  82. default:
  83. if cmd.Name != "help" {
  84. log.Println("Unknown command:", cmd.Name)
  85. }
  86. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  87. }
  88. }