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.

185 lines
4.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. "strings"
  10. )
  11. func handlerCmd(
  12. ctx context.Context,
  13. c client.Client,
  14. ) {
  15. cmd := parseCommand(os.Args[2:])
  16. switch cmd.Name {
  17. case "list":
  18. handlers, err := c.GetHandlers(ctx)
  19. if err != nil {
  20. log.Fatalln(err)
  21. }
  22. WriteHandlerInfoTable(os.Stdout, handlers)
  23. case "add":
  24. model := models.EventHandler{}
  25. // Event name
  26. model.EventName = cmd.Params.Get(0).StringOr("")
  27. if len(model.EventName) == 0 {
  28. log.Fatalln("Event name needed!")
  29. }
  30. // Search
  31. fetchStr := cmd.Params.Get(1).String()
  32. if fetchStr == nil {
  33. log.Fatalln("Search filter needed!")
  34. }
  35. model.TargetKind, model.TargetValue = models.ParseFetchString(*fetchStr)
  36. model = applyCmdToHandler(model, cmd)
  37. newHandler, err := c.AddHandler(ctx, model)
  38. if err != nil {
  39. log.Fatalln(err)
  40. }
  41. WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*newHandler})
  42. case "edit":
  43. id := cmd.Params.Get(0).IntOr(0)
  44. if id == 0 {
  45. log.Fatalln("ID missing")
  46. }
  47. model, err := c.GetHandler(ctx, id)
  48. if err != nil {
  49. log.Fatalln(err.Error())
  50. }
  51. eventStr := cmd.Params.Get("event").String()
  52. if eventStr != nil {
  53. model.EventName = *eventStr
  54. }
  55. fetchStr := cmd.Params.Get("search").String()
  56. if fetchStr != nil {
  57. model.TargetKind, model.TargetValue = models.ParseFetchString(*fetchStr)
  58. }
  59. modified := applyCmdToHandler(*model, cmd)
  60. returned, err := c.PutHandler(ctx, &modified)
  61. if err != nil {
  62. log.Fatalln(err.Error())
  63. }
  64. WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*returned})
  65. case "delete":
  66. id := cmd.Params.Get(0).Int()
  67. if id == nil {
  68. log.Fatalln("ID missing")
  69. }
  70. handler, err := c.DeleteHandler(ctx, *id)
  71. if err != nil {
  72. log.Fatalln(err)
  73. }
  74. WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*handler})
  75. default:
  76. if cmd.Name != "help" {
  77. log.Println("Unknown command:", cmd.Name)
  78. }
  79. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  80. }
  81. }
  82. func applyCmdToHandler(model models.EventHandler, cmd Command) models.EventHandler {
  83. // Remove keys
  84. for _, elem := range cmd.Params.Strings(1) {
  85. if !strings.HasPrefix(elem, "-") {
  86. continue
  87. }
  88. keyToRemove := elem[1:]
  89. if strings.HasPrefix(keyToRemove, "conditions.") {
  90. condKeyToRemove := keyToRemove[11:]
  91. delete(model.Conditions, condKeyToRemove)
  92. } else if strings.HasPrefix(keyToRemove, "fire.payload.") && model.Actions.FireEvent != nil {
  93. firePayloadKeyToRemove := keyToRemove[13:]
  94. delete(model.Actions.FireEvent.Payload, firePayloadKeyToRemove)
  95. } else {
  96. switch keyToRemove {
  97. case "set-power":
  98. model.Actions.SetPower = nil
  99. case "set-color":
  100. model.Actions.SetColor = nil
  101. case "set-intensity":
  102. model.Actions.SetIntensity = nil
  103. case "add-intensity":
  104. model.Actions.AddIntensity = nil
  105. case "fire":
  106. fallthrough
  107. case "fire.name":
  108. model.Actions.FireEvent = nil
  109. case "from":
  110. model.From = models.Never
  111. case "to":
  112. model.To = models.Never
  113. case "set-scene":
  114. model.Actions.SetScene = nil
  115. case "push-scene":
  116. model.Actions.PushScene = nil
  117. }
  118. }
  119. }
  120. // From / To
  121. model.From = cmd.Params.Get("from").TimeOfDayOr(models.Never)
  122. model.To = cmd.Params.Get("to").TimeOfDayOr(models.Never)
  123. if model.From.IsNever() != model.To.IsNever() {
  124. log.Fatalln("from and to must be specified together!")
  125. }
  126. // One shot
  127. model.OneShot = cmd.Params.Get("one-shot").StringOr("false") == "true"
  128. // Priority
  129. model.Priority = cmd.Params.Get("priority").IntOr(100)
  130. // Conditions
  131. model.Conditions = cmd.Params.Subset("conditions").EventConditions()
  132. // Power, Color, Intensity, Temperature
  133. nds := cmd.Params.DeviceState("set-")
  134. model.Actions.SetPower = nds.Power
  135. model.Actions.SetColor = nds.Color
  136. model.Actions.SetIntensity = nds.Intensity
  137. // Add intensity
  138. model.Actions.AddIntensity = cmd.Params.Get("add-intensity").Float()
  139. // Fire
  140. fireParams := cmd.Params.Subset("fire")
  141. fireName := fireParams.Get("name").String()
  142. if fireName != nil {
  143. firePayloadParams := fireParams.Subset("payload")
  144. model.Actions.FireEvent = &models.Event{
  145. Name: *fireName,
  146. Payload: make(map[string]string, len(firePayloadParams)),
  147. }
  148. for key, value := range firePayloadParams.StringMap() {
  149. model.Actions.FireEvent.Payload[key] = value
  150. }
  151. }
  152. // Scenes
  153. model.Actions.SetScene = cmd.Params.SceneAssignment("set-")
  154. model.Actions.PushScene = cmd.Params.SceneAssignment("push-")
  155. return model
  156. }