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
4.1 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. table.SetColumnAlignment([]int{
  15. tablewriter.ALIGN_RIGHT,
  16. tablewriter.ALIGN_LEFT,
  17. tablewriter.ALIGN_RIGHT,
  18. tablewriter.ALIGN_LEFT,
  19. tablewriter.ALIGN_RIGHT,
  20. tablewriter.ALIGN_RIGHT,
  21. })
  22. for _, v := range devices {
  23. powerStr := ""
  24. if v.HasCapability(models.DCPower) {
  25. if v.SceneState != nil && v.SceneState.Power != v.State.Power {
  26. powerStr = strconv.FormatBool(v.SceneState.Power) + "*"
  27. } else {
  28. powerStr = strconv.FormatBool(v.State.Power)
  29. }
  30. }
  31. colorStr := ""
  32. if v.HasCapability(models.DCColorHSK, models.DCColorHS, models.DCColorKelvin) {
  33. if v.SceneState != nil && v.SceneState.Color.String() != v.State.Color.String() {
  34. colorStr = v.SceneState.Color.String() + "*"
  35. } else {
  36. colorStr = v.State.Color.String()
  37. }
  38. }
  39. temperatureString := ""
  40. if v.HasCapability(models.DCTemperatureControl, models.DCTemperatureSensor) {
  41. temperatureString = strconv.FormatFloat(v.State.Temperature, 'f', -1, 64)
  42. }
  43. intensityString := ""
  44. if v.HasCapability(models.DCIntensity) {
  45. if v.SceneState != nil && v.SceneState.Intensity != v.State.Intensity {
  46. intensityString = strconv.FormatFloat(v.SceneState.Intensity, 'g', 2, 64) + "*"
  47. } else {
  48. intensityString = strconv.FormatFloat(v.State.Intensity, 'f', -1, 64)
  49. }
  50. }
  51. table.Append([]string{
  52. strconv.Itoa(v.ID),
  53. v.Name,
  54. powerStr,
  55. colorStr,
  56. intensityString,
  57. temperatureString,
  58. })
  59. }
  60. table.Render()
  61. }
  62. func WriteDeviceInfoTable(w io.Writer, devices []models.Device) {
  63. table := tablewriter.NewWriter(w)
  64. table.SetHeader([]string{"ID", "NAME", "ICON", "BUTTONS", "TAGS", "USER PROPERTIES"})
  65. table.SetReflowDuringAutoWrap(false)
  66. for _, v := range devices {
  67. propStr := ""
  68. for key, value := range v.UserProperties {
  69. propStr += key + "=" + value + " "
  70. }
  71. table.Append([]string{
  72. strconv.Itoa(v.ID),
  73. v.Name,
  74. v.Icon,
  75. strings.Join(v.ButtonNames, ","),
  76. strings.Join(v.Tags, ","),
  77. propStr,
  78. })
  79. }
  80. table.Render()
  81. }
  82. func WriteHandlerInfoTable(w io.Writer, handlers []models.EventHandler) {
  83. table := tablewriter.NewWriter(w)
  84. table.SetHeader([]string{"ID", "EVENT NAME", "PRIORITY", "CONDITIONS", "TARGET", "ACTION"})
  85. table.SetReflowDuringAutoWrap(false)
  86. for _, h := range handlers {
  87. condStr := ""
  88. for key, value := range h.Conditions {
  89. condStr += fmt.Sprintf("%s=%s ", key, value)
  90. }
  91. actionStr := ""
  92. if h.Actions.SetPower != nil {
  93. actionStr += fmt.Sprintf("setPower=%t ", *h.Actions.SetPower)
  94. }
  95. if h.Actions.SetColor != nil {
  96. actionStr += fmt.Sprintf("setColor=%s ", *h.Actions.SetColor)
  97. }
  98. if h.Actions.SetIntensity != nil {
  99. actionStr += fmt.Sprintf("setIntensity=%.02f ", *h.Actions.SetIntensity)
  100. }
  101. if h.Actions.SetTemperature != nil {
  102. actionStr += fmt.Sprintf("setTemperature=%d ", *h.Actions.SetTemperature)
  103. }
  104. if h.Actions.AddIntensity != nil {
  105. actionStr += fmt.Sprintf("addIntensity=%.02f ", *h.Actions.AddIntensity)
  106. }
  107. if h.Actions.FireEvent != nil {
  108. actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
  109. }
  110. if h.Actions.FireEvent != nil {
  111. actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
  112. }
  113. if h.Actions.SetScene != nil {
  114. s := h.Actions.SetScene
  115. actionStr += fmt.Sprintf("setScene=(id=%d,group=\"%s\",duration=%d) ", s.SceneID, s.Group, s.DurationMS)
  116. }
  117. if h.Actions.PushScene != nil {
  118. s := h.Actions.PushScene
  119. actionStr += fmt.Sprintf("pushScene=(id=%d,group=\"%s\",duration=%d) ", s.SceneID, s.Group, s.DurationMS)
  120. }
  121. eventName := h.EventName
  122. if h.OneShot {
  123. eventName += " (one shot)"
  124. }
  125. table.Append([]string{
  126. strconv.Itoa(h.ID),
  127. eventName,
  128. strconv.Itoa(h.Priority),
  129. condStr,
  130. fmt.Sprintf("%s:%s", h.TargetKind, h.TargetValue),
  131. actionStr,
  132. })
  133. }
  134. table.Render()
  135. }