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.

74 lines
1.6 KiB

  1. package main
  2. import (
  3. "git.aiterp.net/lucifer/new-server/models"
  4. "github.com/olekukonko/tablewriter"
  5. "io"
  6. "strconv"
  7. "strings"
  8. )
  9. func WriteDeviceStateTable(w io.Writer, devices []models.Device) {
  10. table := tablewriter.NewWriter(w)
  11. table.SetHeader([]string{"ID", "NAME", "POWER", "COLOR", "INTENSITY", "TEMPERATURE"})
  12. table.SetReflowDuringAutoWrap(true)
  13. for _, v := range devices {
  14. powerStr := ""
  15. if v.HasCapability(models.DCPower) {
  16. powerStr = strconv.FormatBool(v.State.Power)
  17. }
  18. colorStr := ""
  19. if v.HasCapability(models.DCColorHSK, models.DCColorHS, models.DCColorKelvin) {
  20. colorStr = v.State.Color.String()
  21. }
  22. temperatureString := ""
  23. if v.HasCapability(models.DCTemperatureControl, models.DCTemperatureSensor) {
  24. temperatureString = strconv.FormatFloat(v.State.Temperature, 'f', -1, 64)
  25. }
  26. intensityString := ""
  27. if v.HasCapability(models.DCIntensity) {
  28. intensityString = strconv.FormatFloat(v.State.Intensity, 'f', -1, 64)
  29. }
  30. table.Append([]string{
  31. strconv.Itoa(v.ID),
  32. v.Name,
  33. powerStr,
  34. colorStr,
  35. intensityString,
  36. temperatureString,
  37. })
  38. }
  39. table.Render()
  40. }
  41. func WriteDeviceInfoTable(w io.Writer, devices []models.Device) {
  42. table := tablewriter.NewWriter(w)
  43. table.SetHeader([]string{"ID", "NAME", "ICON", "BUTTONS", "TAGS", "USER PROPERTIES"})
  44. table.SetReflowDuringAutoWrap(false)
  45. for _, v := range devices {
  46. propStr := ""
  47. for key, value := range v.UserProperties {
  48. propStr += key + "=" + value + " "
  49. }
  50. table.Append([]string{
  51. strconv.Itoa(v.ID),
  52. v.Name,
  53. v.Icon,
  54. strings.Join(v.ButtonNames, ","),
  55. strings.Join(v.Tags, ","),
  56. propStr,
  57. })
  58. }
  59. table.Render()
  60. }