Skip to content

Commit

Permalink
messages.go: add new statustouches event
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Jul 25, 2024
1 parent 627a2c0 commit c08b3a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
4 changes: 3 additions & 1 deletion api/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const (
ActionGetSystemConfig = "touches"
ActionGetUserConfig = "get_user_config"
ActionEvent = "xevent"
ActionStatusTouchesChanged = "statustoucheschanged"
// Send this to get real values of resources
ActionStatusTouches = "statustouches"
ActionStatusTouchesChanged = "statustoucheschanged"
)

var ValueToggle = "0x4001"
Expand Down
4 changes: 4 additions & 0 deletions api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type MobileDisplayCell struct {
MaxValue string `json:"Max"`
// Step (aka current value). Display Type TEMP always has this set to
// 0xa005.
//
// Update 25/07/2024: This is literally the *step*, not current value.
//
// To obtain current value, send "touches".
Step string `json:"Sp"`
// Display Type.
DisplayType DisplayType `json:"DT"`
Expand Down
56 changes: 48 additions & 8 deletions cmd/fhome/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"os"
"strconv"
"strings"
"text/tabwriter"

"github.com/adrg/strutil"
Expand Down Expand Up @@ -52,12 +53,16 @@ var configCommand = cli.Command{
Name: "user",
Usage: "Print config set in the client apps",
},
&cli.BoolFlag{
Name: "merged",
Usage: "Print merged config (system + user)",
},
&cli.BoolFlag{
Name: "glance",
Usage: "Print nice, at glance status of system",
},
},
Action: func(c *cli.Context) error {
if c.Bool("system") && c.Bool("user") {
return fmt.Errorf("cannot use both --system and --user")
}

client, err := internal.Connect(config)
if err != nil {
return fmt.Errorf("failed to create api client: %v", err)
Expand Down Expand Up @@ -106,7 +111,7 @@ var configCommand = cli.Command{

fmt.Fprintf(w, "%3d\t%s\t%s\t%s\n", cell.ObjectID, cell.IconName(), cell.Name, p)
}
} else {
} else if c.Bool("merged") {
config, err := api.MergeConfigs(userConfig, sysConfig)
if err != nil {
return fmt.Errorf("failed to merge configs: %v", err)
Expand All @@ -120,6 +125,42 @@ var configCommand = cli.Command{
}
log.Println()
}
} else if c.Bool("glance") {
cells := []struct {
Name string
Value int
}{}
mdCells := sysConfig.Response.MobileDisplayProperties.Cells
for _, cell := range mdCells {
if cell.DisplayType != api.Percentage {
continue
}
if !strings.HasPrefix(cell.Step, "0x60") {
continue
}

slog.Info("remapping lighting value", slog.String("cell", cell.Desc), slog.String("step", cell.Step))
val, err := api.RemapLighting(cell.Step)
if err != nil {
slog.Error(
"error remapping lighting value",
slog.Group("cell", slog.String("id", cell.ID), slog.String("desc", cell.Desc)),
slog.Any("error", err),
)
continue
}

cells = append(cells, struct {
Name string
Value int
}{Name: cell.Desc, Value: val})
}

text := "Oto status oświetlenia:\n"
for _, cell := range cells {
text += fmt.Sprintf("• %s: %d%%\n", cell.Name, cell.Value)
}
fmt.Print(text)
}

return nil
Expand Down Expand Up @@ -286,10 +327,9 @@ var objectCommand = cli.Command{

bestObject, bestScore := bestObjectMatch(object, config)

slog.Info("selected object",
slog.String("name", bestObject.Name),
slog.Int("id", bestObject.ID),
slog.Info("found best match",
slog.Int("confidence", int(bestScore*100)),
slog.Group("object", slog.String("name", bestObject.Name), slog.Int("id", bestObject.ID)),
)

value := api.MapLighting(value)
Expand Down

0 comments on commit c08b3a2

Please sign in to comment.