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) table.SetColumnAlignment([]int{ tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT, }) for _, v := range devices { powerStr := "" if v.HasCapability(models.DCPower) { if v.SceneState != nil && v.SceneState.Power != v.State.Power { powerStr = strconv.FormatBool(v.SceneState.Power) + "*" } else { powerStr = strconv.FormatBool(v.State.Power) } } colorStr := "" if v.HasCapability(models.DCColorHSK, models.DCColorHS, models.DCColorKelvin) { if v.SceneState != nil && v.SceneState.Color.String() != v.State.Color.String() { colorStr = v.SceneState.Color.String() + "*" } else { colorStr = v.State.Color.String() } } temperatureString := "" if v.HasCapability(models.DCTemperatureControl, models.DCTemperatureSensor) { temperatureString = fmt.Sprintf("%d", v.State.Temperature) } intensityString := "" if v.HasCapability(models.DCIntensity) { if v.SceneState != nil && v.SceneState.Intensity != v.State.Intensity { intensityString = strconv.FormatFloat(v.SceneState.Intensity, 'g', 2, 64) + "*" } else { 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.SetTemperature != nil { actionStr += fmt.Sprintf("setTemperature=%d ", *h.Actions.SetTemperature) } 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) } if h.Actions.FireEvent != nil { actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name) } if h.Actions.SetScene != nil { s := h.Actions.SetScene actionStr += fmt.Sprintf("setScene=(id=%d,group=\"%s\",duration=%d) ", s.SceneID, s.Group, s.DurationMS) } if h.Actions.PushScene != nil { s := h.Actions.PushScene actionStr += fmt.Sprintf("pushScene=(id=%d,group=\"%s\",duration=%d) ", s.SceneID, s.Group, s.DurationMS) } 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() }