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.

117 lines
2.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "git.aiterp.net/lucifer/new-server/models"
  5. "github.com/olekukonko/tablewriter"
  6. "io"
  7. "strconv"
  8. "strings"
  9. )
  10. func WriteDeviceStateTable(w io.Writer, devices []models.Device) {
  11. table := tablewriter.NewWriter(w)
  12. table.SetHeader([]string{"ID", "NAME", "POWER", "COLOR", "INTENSITY", "TEMPERATURE"})
  13. table.SetReflowDuringAutoWrap(true)
  14. for _, v := range devices {
  15. powerStr := ""
  16. if v.HasCapability(models.DCPower) {
  17. powerStr = strconv.FormatBool(v.State.Power)
  18. }
  19. colorStr := ""
  20. if v.HasCapability(models.DCColorHSK, models.DCColorHS, models.DCColorKelvin) {
  21. colorStr = v.State.Color.String()
  22. }
  23. temperatureString := ""
  24. if v.HasCapability(models.DCTemperatureControl, models.DCTemperatureSensor) {
  25. temperatureString = strconv.FormatFloat(v.State.Temperature, 'f', -1, 64)
  26. }
  27. intensityString := ""
  28. if v.HasCapability(models.DCIntensity) {
  29. intensityString = strconv.FormatFloat(v.State.Intensity, 'f', -1, 64)
  30. }
  31. table.Append([]string{
  32. strconv.Itoa(v.ID),
  33. v.Name,
  34. powerStr,
  35. colorStr,
  36. intensityString,
  37. temperatureString,
  38. })
  39. }
  40. table.Render()
  41. }
  42. func WriteDeviceInfoTable(w io.Writer, devices []models.Device) {
  43. table := tablewriter.NewWriter(w)
  44. table.SetHeader([]string{"ID", "NAME", "ICON", "BUTTONS", "TAGS", "USER PROPERTIES"})
  45. table.SetReflowDuringAutoWrap(false)
  46. for _, v := range devices {
  47. propStr := ""
  48. for key, value := range v.UserProperties {
  49. propStr += key + "=" + value + " "
  50. }
  51. table.Append([]string{
  52. strconv.Itoa(v.ID),
  53. v.Name,
  54. v.Icon,
  55. strings.Join(v.ButtonNames, ","),
  56. strings.Join(v.Tags, ","),
  57. propStr,
  58. })
  59. }
  60. table.Render()
  61. }
  62. func WriteHandlerInfoTable(w io.Writer, handlers []models.EventHandler) {
  63. table := tablewriter.NewWriter(w)
  64. table.SetHeader([]string{"ID", "EVENT NAME", "ONE SHOT", "PRIORITY", "CONDITIONS", "TARGET", "ACTION"})
  65. table.SetReflowDuringAutoWrap(false)
  66. for _, h := range handlers {
  67. condStr := ""
  68. for key, value := range h.Conditions {
  69. condStr += fmt.Sprintf("%s=%s ", key, value)
  70. }
  71. actionStr := ""
  72. if h.Actions.SetPower != nil {
  73. actionStr += fmt.Sprintf("setPower=%t ", *h.Actions.SetPower)
  74. }
  75. if h.Actions.SetColor != nil {
  76. actionStr += fmt.Sprintf("setColor=%s ", *h.Actions.SetColor)
  77. }
  78. if h.Actions.SetIntensity != nil {
  79. actionStr += fmt.Sprintf("setIntensity=%.02f ", *h.Actions.SetIntensity)
  80. }
  81. if h.Actions.AddIntensity != nil {
  82. actionStr += fmt.Sprintf("addIntensity=%.02f ", *h.Actions.AddIntensity)
  83. }
  84. if h.Actions.FireEvent != nil {
  85. actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
  86. }
  87. table.Append([]string{
  88. strconv.Itoa(h.ID),
  89. h.EventName,
  90. strconv.FormatBool(h.OneShot),
  91. strconv.Itoa(h.Priority),
  92. condStr,
  93. fmt.Sprintf("%s:%s", h.TargetKind, h.TargetValue),
  94. actionStr,
  95. })
  96. }
  97. table.Render()
  98. }