From da27300196936c58a204722c8521d252c33ec456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sun, 13 Feb 2022 22:03:35 +0100 Subject: [PATCH] Print out how many remote devices are connected In text form for now (some, none, all). --- main.go | 9 ++++++--- status.go | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 50153e8..108a4b4 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,9 @@ import ( ) type Status struct { - Local int `json:"local"` - Remote int `json:"remote"` + Local int `json:"local"` + Remote int `json:"remote"` + ConnectedRemotes string `json:"connectedRemotes"` } func main() { @@ -16,9 +17,11 @@ func main() { var status Status localStatus := client.localStatus() - remoteStatus := client.remoteStatus() status.Local = int(localStatus.Completion) + + remoteStatus := client.remoteStatus() status.Remote = int(remoteStatus.Completion) + status.ConnectedRemotes = remoteStatus.ConnectedRemotes data, err := json.MarshalIndent(status, "", " ") if err != nil { diff --git a/status.go b/status.go index 000a4c7..c4e2b31 100644 --- a/status.go +++ b/status.go @@ -14,7 +14,8 @@ type LocalStatus struct { } type RemoteStatus struct { - Completion float64 `json:"completion"` + Completion float64 `json:"completion"` + ConnectedRemotes string `json:"connectedRemotes"` } type Connection struct { @@ -52,10 +53,15 @@ func (client *Client) remoteStatus() *RemoteStatus { } var sum float64 - for key := range connections.List { + var connectedDevices int + for key, device := range connections.List { if key == systemStatus.MyID { continue } + if device.Connected { + connectedDevices++ + } + path := fmt.Sprintf("/rest/db/completion?device=%s", key) var status RemoteStatus err = client.get(path, &status) @@ -73,5 +79,13 @@ func (client *Client) remoteStatus() *RemoteStatus { totalStatus.Completion = sum / float64(len(connections.List)-1) } + if connectedDevices == 0 { + totalStatus.ConnectedRemotes = "none" + } else if connectedDevices == len(connections.List)-1 { + totalStatus.ConnectedRemotes = "all" + } else { + totalStatus.ConnectedRemotes = "some" + } + return &totalStatus } -- 2.44.0