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.

155 lines
3.0 KiB

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/internal/color"
  7. "git.aiterp.net/lucifer/new-server/models"
  8. "log"
  9. "os"
  10. "strings"
  11. )
  12. func main() {
  13. cfg, err := loadConfig()
  14. if err != nil {
  15. log.Println("Config at $HOME/.config/lucy.yaml failed:", err)
  16. cfg = &Config{Remote: "http://localhost:8000"}
  17. }
  18. ctx := context.Background()
  19. c := client.Client{APIRoot: cfg.Remote}
  20. cmd := parseCommand(os.Args[1:])
  21. switch cmd.Name {
  22. // DEVICE
  23. case "list":
  24. {
  25. fetchStr := cmd.Params.Get(0).String()
  26. if fetchStr == nil {
  27. s := "all"
  28. fetchStr = &s
  29. }
  30. devices, err := c.GetDevices(ctx, *fetchStr)
  31. if err != nil {
  32. log.Fatalln(err)
  33. }
  34. if info := cmd.Params.Get("info").String(); info != nil {
  35. WriteDeviceInfo(os.Stdout, devices, strings.Split(*info, ","))
  36. } else {
  37. WriteDeviceStateTable(os.Stdout, devices)
  38. }
  39. }
  40. case "set":
  41. {
  42. devices, err := c.PutDeviceState(ctx, cmd.Params.Get(0).StringOr("all"), cmd.Params.DeviceState(""))
  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 "color":
  85. {
  86. col, err := color.Parse(cmd.Params.Get(0).StringOr("hs:0,0"))
  87. if err != nil {
  88. log.Fatalln(err)
  89. }
  90. if col.IsKelvin() {
  91. fmt.Println(col.String())
  92. }
  93. if hs, ok := col.ToHS(); ok {
  94. fmt.Println(hs.String())
  95. }
  96. if hsk, ok := col.ToHSK(); ok {
  97. fmt.Println(hsk.String())
  98. }
  99. if xy, ok := col.ToXY(); ok {
  100. fmt.Println(xy.String())
  101. }
  102. if rgb, ok := col.ToRGB(); ok {
  103. fmt.Println(rgb.String())
  104. }
  105. }
  106. case "factory":
  107. factoryCmd(ctx, c)
  108. case "handler":
  109. handlerCmd(ctx, c)
  110. case "scene":
  111. sceneCmd(ctx, c)
  112. // EVENT
  113. case "run":
  114. {
  115. name := cmd.Params.Get(0).String()
  116. if name == nil {
  117. return
  118. }
  119. event := models.Event{
  120. Name: *name,
  121. Payload: cmd.Params.StringMap(),
  122. }
  123. err := c.FireEvent(ctx, event)
  124. if err != nil {
  125. log.Fatalln(err)
  126. }
  127. }
  128. default:
  129. if cmd.Name != "help" {
  130. log.Println("Unknown command:", cmd.Name)
  131. }
  132. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  133. }
  134. }