package main import ( "fmt" "git.aiterp.net/lucifer/new-server/models" "github.com/olekukonko/tablewriter" "io" "strconv" "strings" ) func WriteDeviceStateTable(w io.Writer, devices []models.Device) { table := tablewriter.NewWriter(w) table.SetHeader([]string{"ID", "NAME", "POWER", "COLOR", "INTENSITY", "TEMPERATURE"}) table.SetReflowDuringAutoWrap(true) for _, v := range devices { powerStr := "" if v.HasCapability(models.DCPower) { powerStr = strconv.FormatBool(v.State.Power) } colorStr := "" if v.HasCapability(models.DCColorHSK, models.DCColorHS, models.DCColorKelvin) { colorStr = v.State.Color.String() } temperatureString := "" if v.HasCapability(models.DCTemperatureControl, models.DCTemperatureSensor) { temperatureString = strconv.FormatFloat(v.State.Temperature, 'f', -1, 64) } intensityString := "" if v.HasCapability(models.DCIntensity) { intensityString = strconv.FormatFloat(v.State.Intensity, 'f', -1, 64) } table.Append([]string{ strconv.Itoa(v.ID), v.Name, powerStr, colorStr, intensityString, temperatureString, }) } table.Render() } func WriteDeviceInfoTable(w io.Writer, devices []models.Device) { table := tablewriter.NewWriter(w) table.SetHeader([]string{"ID", "NAME", "ICON", "BUTTONS", "TAGS", "USER PROPERTIES"}) table.SetReflowDuringAutoWrap(false) for _, v := range devices { propStr := "" for key, value := range v.UserProperties { propStr += key + "=" + value + " " } table.Append([]string{ strconv.Itoa(v.ID), v.Name, v.Icon, strings.Join(v.ButtonNames, ","), strings.Join(v.Tags, ","), propStr, }) } table.Render() } func WriteHandlerInfoTable(w io.Writer, handlers []models.EventHandler) { table := tablewriter.NewWriter(w) table.SetHeader([]string{"ID", "EVENT NAME", "PRIORITY", "CONDITIONS", "TARGET", "ACTION"}) table.SetReflowDuringAutoWrap(false) for _, h := range handlers { condStr := "" for key, value := range h.Conditions { condStr += fmt.Sprintf("%s=%s ", key, value) } actionStr := "" if h.Actions.SetPower != nil { actionStr += fmt.Sprintf("setPower=%t ", *h.Actions.SetPower) } if h.Actions.SetColor != nil { actionStr += fmt.Sprintf("setColor=%s ", *h.Actions.SetColor) } if h.Actions.SetIntensity != nil { actionStr += fmt.Sprintf("setIntensity=%.02f ", *h.Actions.SetIntensity) } if h.Actions.AddIntensity != nil { actionStr += fmt.Sprintf("addIntensity=%.02f ", *h.Actions.AddIntensity) } if h.Actions.FireEvent != nil { actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name) } eventName := h.EventName if h.OneShot { eventName += " (one shot)" } table.Append([]string{ strconv.Itoa(h.ID), eventName, strconv.Itoa(h.Priority), condStr, fmt.Sprintf("%s:%s", h.TargetKind, h.TargetValue), actionStr, }) } table.Render() }