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.

124 lines
3.0 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", "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.SetTemperature != nil {
  82. actionStr += fmt.Sprintf("setTemperature=%d ", *h.Actions.SetTemperature)
  83. }
  84. if h.Actions.AddIntensity != nil {
  85. actionStr += fmt.Sprintf("addIntensity=%.02f ", *h.Actions.AddIntensity)
  86. }
  87. if h.Actions.FireEvent != nil {
  88. actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
  89. }
  90. eventName := h.EventName
  91. if h.OneShot {
  92. eventName += " (one shot)"
  93. }
  94. table.Append([]string{
  95. strconv.Itoa(h.ID),
  96. eventName,
  97. strconv.Itoa(h.Priority),
  98. condStr,
  99. fmt.Sprintf("%s:%s", h.TargetKind, h.TargetValue),
  100. actionStr,
  101. })
  102. }
  103. table.Render()
  104. }