From f1f120e1cb425943617f302c79cbfd32a85338ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Tue, 8 Feb 2022 13:02:45 +0100 Subject: [PATCH] Add average over remote devices --- main.go | 4 +++- status.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index e005217..50153e8 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,9 @@ func main() { var status Status localStatus := client.localStatus() - status.Local = localStatus.Completion + remoteStatus := client.remoteStatus() + status.Local = int(localStatus.Completion) + status.Remote = int(remoteStatus.Completion) data, err := json.MarshalIndent(status, "", " ") if err != nil { diff --git a/status.go b/status.go index 15a3fc4..a86c304 100644 --- a/status.go +++ b/status.go @@ -1,9 +1,24 @@ package main -import "log" +import ( + "fmt" + "log" +) type LocalStatus struct { - Completion int `json:"completion"` + Completion float64 `json:"completion"` +} + +type RemoteStatus struct { + Completion float64 `json:"completion"` +} + +type Connection struct { + Connected bool `json:"connected"` +} + +type Connections struct { + List map[string]Connection `json:"connections"` } func (client *Client) localStatus() *LocalStatus { @@ -16,3 +31,36 @@ func (client *Client) localStatus() *LocalStatus { return status } + +func (client *Client) remoteStatus() *RemoteStatus { + var totalStatus RemoteStatus + var connections Connections + + err := client.get("/rest/system/connections", &connections) + if err != nil { + log.Fatal(err) + } + + var connectedDevices []string + for key, con := range connections.List { + if con.Connected { + connectedDevices = append(connectedDevices, key) + } + } + + var sum float64 + for _, device := range connectedDevices { + path := fmt.Sprintf("/rest/db/completion?device=%s", device) + var status RemoteStatus + err = client.get(path, &status) + if err != nil { + log.Fatal(err) + } + + sum += status.Completion + } + + totalStatus.Completion = sum / float64(len(connectedDevices)) + + return &totalStatus +} -- 2.44.0