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.

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