Skip to content

Commit

Permalink
implementing Client.SendAction
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Jul 25, 2024
1 parent c08b3a2 commit 0dc5973
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,25 @@ func (c *Client) ReadAnyMessage() (*Message, error) {
return &msg, nil
}

// SendAction sends an action to the server.
func (c *Client) SendAction(actionName string) (*Message, error) {
token := generateRequestToken()

action := Action{
ActionName: actionName,
Login: *c.email,
PasswordHash: *c.resourcePasswordHash,
RequestToken: token,
}

err := c.mainConn.WriteJSON(action)
if err != nil {
return nil, fmt.Errorf("failed to write action %s: %v", action.ActionName, err)
}

return c.ReadMessage(action.ActionName, token)
}

// SendEvent sends an event containing value to the cell.
//
// Events are named "Xevents" in F&Home's terminology.
Expand Down
27 changes: 27 additions & 0 deletions cmd/fhome/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@ var configCommand = cli.Command{
log.Println()
}
} else if c.Bool("glance") {
// We want to see real values of the system resources.
// To do that we need to send "statustouches" action and
// wait for its response.

// Send "statustouches" event.
_, err := client.SendAction(api.ActionStatusTouches)
if err != nil {
return fmt.Errorf("failed to send action: %v", err)
}

msg, err := client.ReadMessage(api.ActionStatusTouchesChanged, "")
if err != nil {
slog.Error("failed to read message", slog.Any("error", err))
return err
}

var resp api.StatusTouchesChangedResponse

err = json.Unmarshal(msg.Raw, &resp)
if err != nil {
slog.Error("failed to unmarshal message", slog.Any("error", err))
return err
}

cellValue := resp.Response.CellValues
printCellData(&cellValue, config)

Check failure on line 154 in cmd/fhome/commands.go

View workflow job for this annotation

GitHub Actions / main

undefined: printCellData (compile)

cells := []struct {
Name string
Value int
Expand Down

0 comments on commit 0dc5973

Please sign in to comment.