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.

173 lines
4.4 KiB

3 years ago
  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 = fmt.Sprintf("%d", v.State.Temperature)
  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 WriteSceneTable(w io.Writer, scenes []models.Scene) {
  83. table := tablewriter.NewWriter(w)
  84. table.SetHeader([]string{"ID", "NAME", "ROLES", "INTERVAL"})
  85. table.SetReflowDuringAutoWrap(false)
  86. for _, v := range scenes {
  87. table.Append([]string{
  88. fmt.Sprint(v.ID),
  89. v.Name,
  90. fmt.Sprint(len(v.Roles)),
  91. fmt.Sprint(v.IntervalMS),
  92. })
  93. }
  94. table.Render()
  95. }
  96. func WriteHandlerInfoTable(w io.Writer, handlers []models.EventHandler) {
  97. table := tablewriter.NewWriter(w)
  98. table.SetHeader([]string{"ID", "EVENT NAME", "PRIORITY", "CONDITIONS", "TARGET", "ACTION"})
  99. table.SetReflowDuringAutoWrap(false)
  100. for _, h := range handlers {
  101. condStr := ""
  102. for key, value := range h.Conditions {
  103. condStr += fmt.Sprintf("%s=%s ", key, value)
  104. }
  105. actionStr := ""
  106. if h.Actions.SetPower != nil {
  107. actionStr += fmt.Sprintf("setPower=%t ", *h.Actions.SetPower)
  108. }
  109. if h.Actions.SetColor != nil {
  110. actionStr += fmt.Sprintf("setColor=%s ", *h.Actions.SetColor)
  111. }
  112. if h.Actions.SetIntensity != nil {
  113. actionStr += fmt.Sprintf("setIntensity=%.02f ", *h.Actions.SetIntensity)
  114. }
  115. if h.Actions.SetTemperature != nil {
  116. actionStr += fmt.Sprintf("setTemperature=%d ", *h.Actions.SetTemperature)
  117. }
  118. if h.Actions.AddIntensity != nil {
  119. actionStr += fmt.Sprintf("addIntensity=%.02f ", *h.Actions.AddIntensity)
  120. }
  121. if h.Actions.FireEvent != nil {
  122. actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
  123. }
  124. if h.Actions.FireEvent != nil {
  125. actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
  126. }
  127. if h.Actions.SetScene != nil {
  128. s := h.Actions.SetScene
  129. actionStr += fmt.Sprintf("setScene=(id=%d,group=\"%s\",duration=%d) ", s.SceneID, s.Group, s.DurationMS)
  130. }
  131. if h.Actions.PushScene != nil {
  132. s := h.Actions.PushScene
  133. actionStr += fmt.Sprintf("pushScene=(id=%d,group=\"%s\",duration=%d) ", s.SceneID, s.Group, s.DurationMS)
  134. }
  135. eventName := h.EventName
  136. if h.OneShot {
  137. eventName += " (one shot)"
  138. }
  139. table.Append([]string{
  140. strconv.Itoa(h.ID),
  141. eventName,
  142. strconv.Itoa(h.Priority),
  143. condStr,
  144. fmt.Sprintf("%s:%s", h.TargetKind, h.TargetValue),
  145. actionStr,
  146. })
  147. }
  148. table.Render()
  149. }