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.

126 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. "strings"
  10. )
  11. func main() {
  12. cfg, err := loadConfig()
  13. if err != nil {
  14. log.Println("Config at $HOME/.config/lucy.yaml failed:", err)
  15. cfg = &Config{Remote: "http://localhost:8000"}
  16. }
  17. ctx := context.Background()
  18. c := client.Client{APIRoot: cfg.Remote}
  19. cmd := parseCommand(os.Args[1:])
  20. switch cmd.Name {
  21. // DEVICE
  22. case "list":
  23. {
  24. fetchStr := cmd.Params.Get(0).String()
  25. if fetchStr == nil {
  26. s := "all"
  27. fetchStr = &s
  28. }
  29. devices, err := c.GetDevices(ctx, *fetchStr)
  30. if err != nil {
  31. log.Fatalln(err)
  32. }
  33. WriteDeviceStateTable(os.Stdout, devices)
  34. }
  35. case "set":
  36. {
  37. devices, err := c.PutDeviceState(ctx, cmd.Params.Get(0).StringOr("all"), models.NewDeviceState{
  38. Power: cmd.Params.Get("power").Bool(),
  39. Color: cmd.Params.Get("color").String(),
  40. Intensity: cmd.Params.Get("intensity").Float(),
  41. Temperature: cmd.Params.Get("temperature").Int(),
  42. })
  43. if err != nil {
  44. log.Fatalln(err)
  45. }
  46. WriteDeviceStateTable(os.Stdout, devices)
  47. }
  48. case "tag":
  49. {
  50. var addTags []string
  51. var removeTags []string
  52. for _, tag := range cmd.Params.Strings(1) {
  53. if strings.HasPrefix(tag, "+") {
  54. addTags = append(addTags, tag[1:])
  55. } else if strings.HasPrefix(tag, "-") {
  56. removeTags = append(removeTags, tag[1:])
  57. } else {
  58. addTags = append(addTags, tag)
  59. }
  60. }
  61. devices, err := c.PutDeviceTags(
  62. ctx,
  63. cmd.Params.Get(0).StringOr("all"),
  64. addTags,
  65. removeTags,
  66. )
  67. if err != nil {
  68. log.Fatalln(err)
  69. }
  70. WriteDeviceInfoTable(os.Stdout, devices)
  71. }
  72. case "update":
  73. {
  74. devices, err := c.PutDevice(ctx, cmd.Params.Get(0).StringOr("all"), models.DeviceUpdate{
  75. Name: cmd.Params.Get("name").String(),
  76. Icon: cmd.Params.Get("icon").String(),
  77. UserProperties: cmd.Params.Subset("prop.").StringPtrMap(),
  78. })
  79. if err != nil {
  80. log.Fatalln(err)
  81. }
  82. WriteDeviceInfoTable(os.Stdout, devices)
  83. }
  84. case "handler":
  85. handlerCmd(ctx, c)
  86. // EVENT
  87. case "run":
  88. {
  89. name := cmd.Params.Get(0).String()
  90. if name == nil {
  91. return
  92. }
  93. event := models.Event{
  94. Name: *name,
  95. Payload: cmd.Params.StringMap(),
  96. }
  97. err := c.FireEvent(ctx, event)
  98. if err != nil {
  99. log.Fatalln(err)
  100. }
  101. }
  102. default:
  103. if cmd.Name != "help" {
  104. log.Println("Unknown command:", cmd.Name)
  105. }
  106. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  107. }
  108. }