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.

202 lines
4.8 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
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. if cmd.Params.Get(0).StringOr("_not") == "all" {
  67. handlers, err := c.GetHandlers(ctx)
  68. if err != nil {
  69. log.Fatalln(err)
  70. }
  71. for _, handler := range handlers {
  72. _, err = c.DeleteHandler(ctx, handler.ID)
  73. if err != nil {
  74. log.Fatalln(err)
  75. }
  76. }
  77. log.Printf("Deleted %d handlers", len(handlers))
  78. } else {
  79. id := cmd.Params.Get(0).Int()
  80. if id == nil {
  81. log.Fatalln("ID missing")
  82. }
  83. handler, err := c.DeleteHandler(ctx, *id)
  84. if err != nil {
  85. log.Fatalln(err)
  86. }
  87. WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*handler})
  88. }
  89. default:
  90. if cmd.Name != "help" {
  91. log.Println("Unknown command:", cmd.Name)
  92. }
  93. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  94. }
  95. }
  96. func applyCmdToHandler(model models.EventHandler, cmd Command) models.EventHandler {
  97. // Remove keys
  98. for _, elem := range cmd.Params.Strings(1) {
  99. if !strings.HasPrefix(elem, "-") {
  100. continue
  101. }
  102. keyToRemove := elem[1:]
  103. if strings.HasPrefix(keyToRemove, "conditions.") {
  104. condKeyToRemove := keyToRemove[11:]
  105. delete(model.Conditions, condKeyToRemove)
  106. } else if strings.HasPrefix(keyToRemove, "fire.payload.") && model.Actions.FireEvent != nil {
  107. firePayloadKeyToRemove := keyToRemove[13:]
  108. delete(model.Actions.FireEvent.Payload, firePayloadKeyToRemove)
  109. } else {
  110. switch keyToRemove {
  111. case "set-power":
  112. model.Actions.SetPower = nil
  113. case "set-color":
  114. model.Actions.SetColor = nil
  115. case "set-intensity":
  116. model.Actions.SetIntensity = nil
  117. case "add-intensity":
  118. model.Actions.AddIntensity = nil
  119. case "fire":
  120. fallthrough
  121. case "fire.name":
  122. model.Actions.FireEvent = nil
  123. case "from":
  124. model.From = models.Never
  125. case "to":
  126. model.To = models.Never
  127. case "set-scene":
  128. model.Actions.SetScene = nil
  129. case "push-scene":
  130. model.Actions.PushScene = nil
  131. }
  132. }
  133. }
  134. // From / To
  135. model.From = cmd.Params.Get("from").TimeOfDayOr(models.Never)
  136. model.To = cmd.Params.Get("to").TimeOfDayOr(models.Never)
  137. if model.From.IsNever() != model.To.IsNever() {
  138. log.Fatalln("from and to must be specified together!")
  139. }
  140. // One shot
  141. model.OneShot = cmd.Params.Get("one-shot").StringOr("false") == "true"
  142. // Priority
  143. model.Priority = cmd.Params.Get("priority").IntOr(100)
  144. // Conditions
  145. model.Conditions = cmd.Params.Subset("conditions").EventConditions()
  146. // Power, Color, Intensity, Temperature
  147. nds := cmd.Params.DeviceState("set-")
  148. model.Actions.SetPower = nds.Power
  149. model.Actions.SetColor = nds.Color
  150. model.Actions.SetIntensity = nds.Intensity
  151. model.Actions.SetTemperature = nds.Temperature
  152. // Add intensity
  153. model.Actions.AddIntensity = cmd.Params.Get("add-intensity").Float()
  154. // Fire
  155. fireParams := cmd.Params.Subset("fire")
  156. fireName := fireParams.Get("name").String()
  157. if fireName != nil {
  158. firePayloadParams := fireParams.Subset("payload")
  159. model.Actions.FireEvent = &models.Event{
  160. Name: *fireName,
  161. Payload: make(map[string]string, len(firePayloadParams)),
  162. }
  163. for key, value := range firePayloadParams.StringMap() {
  164. model.Actions.FireEvent.Payload[key] = value
  165. }
  166. }
  167. // Scenes
  168. model.Actions.SetScene = cmd.Params.SceneAssignment("set-")
  169. model.Actions.PushScene = cmd.Params.SceneAssignment("push-")
  170. return model
  171. }