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.

131 lines
2.5 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/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. if info := cmd.Params.Get("info").String(); info != nil {
  34. WriteDeviceInfo(os.Stdout, devices, strings.Split(*info, ","))
  35. } else {
  36. WriteDeviceStateTable(os.Stdout, devices)
  37. }
  38. }
  39. case "set":
  40. {
  41. devices, err := c.PutDeviceState(ctx, cmd.Params.Get(0).StringOr("all"), cmd.Params.DeviceState(""))
  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. ctx,
  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(ctx, 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. case "factory":
  84. factoryCmd(ctx, c)
  85. case "handler":
  86. handlerCmd(ctx, c)
  87. case "scene":
  88. sceneCmd(ctx, c)
  89. // EVENT
  90. case "run":
  91. {
  92. name := cmd.Params.Get(0).String()
  93. if name == nil {
  94. return
  95. }
  96. event := models.Event{
  97. Name: *name,
  98. Payload: cmd.Params.StringMap(),
  99. }
  100. err := c.FireEvent(ctx, event)
  101. if err != nil {
  102. log.Fatalln(err)
  103. }
  104. }
  105. default:
  106. if cmd.Name != "help" {
  107. log.Println("Unknown command:", cmd.Name)
  108. }
  109. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  110. }
  111. }