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.

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