From a2d1c675d7dfec2c7a4bd39c51769367e1f45247 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 13:00:08 +0200 Subject: [PATCH 01/14] WiFi networks management API --- Makefile | 2 +- go.mod | 2 +- go.sum | 4 +- modd.conf | 1 + reach/test_results.go | 23 +-- .../{client_test.go => test_results_test.go} | 2 +- reach/wifi_networks.go | 148 ++++++++++++++++++ reach/wifi_networks_test.go | 75 +++++++++ 8 files changed, 241 insertions(+), 16 deletions(-) rename reach/{client_test.go => test_results_test.go} (89%) create mode 100644 reach/wifi_networks.go create mode 100644 reach/wifi_networks_test.go diff --git a/Makefile b/Makefile index 09fb3c9..8660d7b 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ test: tidy go test -mod=vendor -v ./... lint: - @PATH=$(PATH):./bin/gometalinter gometalinter -e '.*/pkg/mod' -e ".*/go/src" --vendor ./... + @PATH=$(PATH):./bin/gometalinter gometalinter -e '.*/pkg/mod' -e ".*/go/src" --vendor $(LINT_ARGS) ./... tidy: go mod tidy diff --git a/go.mod b/go.mod index a351cfb..08eb7a8 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module forge.cadoles.com/Pyxis/orion require ( - forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d + forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 github.com/caarlos0/env v3.3.0+incompatible github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-chi/chi v3.3.3+incompatible diff --git a/go.sum b/go.sum index bff80fd..4749ce9 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d h1:gkJw6IeJ+A/EVw48eUnZwbK36fBlDrfB5lNyEPAltLQ= -forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0= +forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 h1:o3G5+9RjczCK1xAYFaRMknk1kY9Ule6PNfiW6N6hEpg= +forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0= github.com/caarlos0/env v3.3.0+incompatible h1:jCfY0ilpzC2FFViyZyDKCxKybDESTwaR+ebh8zm6AOE= github.com/caarlos0/env v3.3.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/modd.conf b/modd.conf index fba3af3..b69c775 100644 --- a/modd.conf +++ b/modd.conf @@ -11,6 +11,7 @@ Makefile { **/*.go modd.conf Makefile { + prep: make lint LINT_ARGS=--fast prep: make test } diff --git a/reach/test_results.go b/reach/test_results.go index 2ceb7bb..7a721f2 100644 --- a/reach/test_results.go +++ b/reach/test_results.go @@ -8,10 +8,8 @@ import ( ) const ( - // EventGetTestResults is a request for the ReachRS module's test results - EventGetTestResults = "get test results" - // EventTestResults is the response of the EventGetTestResults request - EventTestResults = "test results" + eventGetTestResults = "get test results" + eventTestResults = "test results" ) // TestResults are the ReachRS module's test results @@ -32,17 +30,20 @@ func (c *Client) TestResults() (*TestResults, error) { wg.Add(1) - c.conn.On(EventTestResults, func(h *gosocketio.Channel, res *TestResults) { + err = c.conn.On(eventTestResults, func(h *gosocketio.Channel, res *TestResults) { results = res - c.conn.On(EventTestResults, nil) + c.conn.Off(eventTestResults) wg.Done() }) - - c.logf("sending '%s' event", EventGetTestResults) - if err = c.conn.Emit(EventGetTestResults, nil); err != nil { - return nil, errors.Wrapf(err, "error while emitting '%s' event", EventGetTestResults) + if err != nil { + return nil, errors.Wrapf(err, "error while binding to '%s' event", eventTestResults) } - c.logf("'%s' event sent", EventGetTestResults) + + c.logf("sending '%s' event", eventGetTestResults) + if err = c.conn.Emit(eventGetTestResults, nil); err != nil { + return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetTestResults) + } + c.logf("'%s' event sent", eventGetTestResults) wg.Wait() diff --git a/reach/client_test.go b/reach/test_results_test.go similarity index 89% rename from reach/client_test.go rename to reach/test_results_test.go index 658b98e..5dbcf29 100644 --- a/reach/client_test.go +++ b/reach/test_results_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func TestClient(t *testing.T) { +func TestClientTestResults(t *testing.T) { client := NewClient( WithStandardLogger(), diff --git a/reach/wifi_networks.go b/reach/wifi_networks.go new file mode 100644 index 0000000..ce8f2f2 --- /dev/null +++ b/reach/wifi_networks.go @@ -0,0 +1,148 @@ +package reach + +import ( + "sync" + + "forge.cadoles.com/Pyxis/golang-socketio" + "github.com/pkg/errors" +) + +const ( + eventGetSavedWifiNetworks = "get saved wifi networks" + eventSavedWifiNetworkResults = "wifi saved networks results" + eventAddWifiNetwork = "add new network" + eventAddWifiNetworkResults = "add network results" + eventRemoveWifiNetwork = "remove network" + eventRemoveWifiNetworkResults = "remove network results" + eventConnectToNetwork = "connect to network" +) + +// WifiSecurity is a WiFi network security algorithm +type WifiSecurity string + +const ( + // SecurityWPAPSK WPA-PSK wifi network + SecurityWPAPSK WifiSecurity = "wpa-psk" + // SecurityOpen Open wifi network + SecurityOpen WifiSecurity = "open" +) + +// WifiNetwork is a ReachRS module's saved wifi network +// Raw messages examples: +// [{"ssid":"Cadoles","password":"...","security":"wpa-psk","identity":""}] +// [{"is_visible":false,"connected":false,"ssid":"Cadoles","security":"wpa-psk","is_connected":false,"is_added":true}]] +type WifiNetwork struct { + SSID string `json:"ssid"` + Password string `json:"password"` + Security WifiSecurity `json:"security"` + Identity string `json:"identity"` + Visible bool `json:"is_visible"` + Connected bool `json:"is_connected"` + Added bool `json:"is_added"` +} + +// SavedWifiNetworks returns the ReachRS module saved wifi networks +func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) { + + var err error + var wifiNetworks []*WifiNetwork + var wg sync.WaitGroup + + wg.Add(1) + + err = c.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) { + wifiNetworks = wn + c.conn.Off(eventSavedWifiNetworkResults) + wg.Done() + }) + if err != nil { + return nil, errors.Wrapf(err, "error while binding to '%s' event", eventSavedWifiNetworkResults) + } + + c.logf("sending '%s' event", eventGetSavedWifiNetworks) + if err = c.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil { + return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetSavedWifiNetworks) + } + c.logf("'%s' event sent", eventGetSavedWifiNetworks) + + wg.Wait() + + return wifiNetworks, err + +} + +// AddWifiNetwork asks the ReachRS module to save the given wifi network informations +func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password string) (bool, error) { + + var err error + var wg sync.WaitGroup + var done bool + + wg.Add(1) + + err = c.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) { + done = d + c.conn.Off(eventAddWifiNetworkResults) + wg.Done() + }) + if err != nil { + return false, errors.Wrapf(err, "error while binding to '%s' event", eventAddWifiNetworkResults) + } + + wn := &WifiNetwork{ + SSID: ssid, + Security: security, + Password: password, + } + + c.logf("sending '%s' event", eventAddWifiNetwork) + if err = c.conn.Emit(eventAddWifiNetwork, wn); err != nil { + return false, errors.Wrapf(err, "error while emitting '%s' event", eventAddWifiNetwork) + } + c.logf("'%s' event sent", eventAddWifiNetwork) + + wg.Wait() + + return done, err + +} + +// RemoveWifiNetwork asks the ReachRS module to remove the given WiFi network +func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) { + + var err error + var wg sync.WaitGroup + var done bool + + wg.Add(1) + + err = c.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) { + done = d + c.conn.Off(eventRemoveWifiNetworkResults) + wg.Done() + }) + if err != nil { + return false, errors.Wrapf(err, "error while binding to '%s' event", eventRemoveWifiNetworkResults) + } + + c.logf("sending '%s' event", eventRemoveWifiNetwork) + if err := c.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil { + return false, errors.Wrapf(err, "error while emitting '%s' event", eventRemoveWifiNetwork) + } + c.logf("'%s' event sent", eventRemoveWifiNetwork) + + wg.Wait() + + return done, nil + +} + +// ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network +func (c *Client) ConnectToWifiNetwork(ssid string) error { + c.logf("sending '%s' event", eventConnectToNetwork) + if err := c.conn.Emit(eventConnectToNetwork, ssid); err != nil { + return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork) + } + c.logf("'%s' event sent", eventConnectToNetwork) + return nil +} diff --git a/reach/wifi_networks_test.go b/reach/wifi_networks_test.go new file mode 100644 index 0000000..667e669 --- /dev/null +++ b/reach/wifi_networks_test.go @@ -0,0 +1,75 @@ +package reach + +import ( + "fmt" + "math/rand" + "testing" +) + +func TestClientSavedWiFiNetworks(t *testing.T) { + + client := NewClient( + WithStandardLogger(), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + _, err := client.SavedWifiNetworks() + if err != nil { + t.Error(err) + } + + defer client.Close() + +} + +func TestClientCRUDWiFiNetwork(t *testing.T) { + + client := NewClient( + WithStandardLogger(), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32()) + + done, err := client.AddWifiNetwork(ssid, SecurityOpen, "") + if err != nil { + t.Error(err) + } + + if g, e := done, true; g != e { + t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e) + } + + networks, err := client.SavedWifiNetworks() + if err != nil { + t.Error(err) + } + + found := false + for _, n := range networks { + if n.SSID == ssid { + found = true + break + } + } + + if g, e := found, true; g != e { + t.Errorf("wifi network '%s' should exists", ssid) + } + + done, err = client.RemoveWifiNetwork(ssid) + if err != nil { + t.Error(err) + } + + if g, e := done, true; g != e { + t.Errorf("RemoveWifiNetwork() -> done: got '%v', expected '%v'", g, e) + } + + defer client.Close() + +} From 505d462352c3b19c54113e325ef8e6d199168619 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 13:04:56 +0200 Subject: [PATCH 02/14] Remove Probe() method --- reach/probe.go | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 reach/probe.go diff --git a/reach/probe.go b/reach/probe.go deleted file mode 100644 index 802b9f3..0000000 --- a/reach/probe.go +++ /dev/null @@ -1,37 +0,0 @@ -package reach - -import ( - "sync" - - "forge.cadoles.com/Pyxis/golang-socketio" - "github.com/pkg/errors" -) - -const ( - // EventProbe - - EventProbe = "probe" -) - -// Probe - -func (c *Client) Probe() error { - - var wg sync.WaitGroup - - wg.Add(1) - - c.conn.On(EventProbe, func(h *gosocketio.Channel) { - wg.Done() - c.conn.On(EventProbe, nil) - }) - - c.logf("sending '%s' event", EventProbe) - if err := c.conn.Emit(EventProbe, nil); err != nil { - return errors.Wrapf(err, "error while emitting '%s' event", EventProbe) - } - c.logf("'%s' event sent", EventProbe) - - wg.Wait() - - return nil - -} From 23aee0fb68bb50ef74fee7f7578a6d3a361326f7 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 15:29:44 +0200 Subject: [PATCH 03/14] Add flag to execute integration tests --- reach/test.go | 13 +++++++++++++ reach/test_results_test.go | 5 +++++ reach/wifi_networks_test.go | 10 ++++++++++ 3 files changed, 28 insertions(+) create mode 100644 reach/test.go diff --git a/reach/test.go b/reach/test.go new file mode 100644 index 0000000..2142a71 --- /dev/null +++ b/reach/test.go @@ -0,0 +1,13 @@ +package reach + +import "flag" + +var runIntegrationTests = flag.Bool( + "integration", false, + "Run the integration tests (in addition to the unit tests)", +) + +var reachHost = flag.String( + "reach-host", "192.168.42.1", + "The Reach module host to use in integration tests", +) diff --git a/reach/test_results_test.go b/reach/test_results_test.go index 5dbcf29..b7b242a 100644 --- a/reach/test_results_test.go +++ b/reach/test_results_test.go @@ -6,8 +6,13 @@ import ( func TestClientTestResults(t *testing.T) { + if !*runIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + client := NewClient( WithStandardLogger(), + WithEndpoint(*reachHost, 80), ) if err := client.Connect(); err != nil { t.Fatal(err) diff --git a/reach/wifi_networks_test.go b/reach/wifi_networks_test.go index 667e669..0d4e237 100644 --- a/reach/wifi_networks_test.go +++ b/reach/wifi_networks_test.go @@ -8,8 +8,13 @@ import ( func TestClientSavedWiFiNetworks(t *testing.T) { + if !*runIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + client := NewClient( WithStandardLogger(), + WithEndpoint(*reachHost, 80), ) if err := client.Connect(); err != nil { t.Fatal(err) @@ -26,8 +31,13 @@ func TestClientSavedWiFiNetworks(t *testing.T) { func TestClientCRUDWiFiNetwork(t *testing.T) { + if !*runIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + client := NewClient( WithStandardLogger(), + WithEndpoint(*reachHost, 80), ) if err := client.Connect(); err != nil { t.Fatal(err) From 1bf205347e3bb1c635a53c0cc0650710951da8e4 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 15:35:56 +0200 Subject: [PATCH 04/14] Add TimeSyncStatus() API --- reach/time_sync.go | 47 +++++++++++++++++++++++++++++++++++++++++ reach/time_sync_test.go | 31 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 reach/time_sync.go create mode 100644 reach/time_sync_test.go diff --git a/reach/time_sync.go b/reach/time_sync.go new file mode 100644 index 0000000..37f0378 --- /dev/null +++ b/reach/time_sync.go @@ -0,0 +1,47 @@ +package reach + +import ( + "sync" + + "forge.cadoles.com/Pyxis/golang-socketio" + "github.com/pkg/errors" +) + +const ( + eventGetTimeSyncStatus = "get time sync status" + eventTimeSyncResults = "time sync status" +) + +type timeSyncStatus struct { + Status bool `json:"status"` +} + +// TimeSyncStatus returns the ReachRS module time sync status +func (c *Client) TimeSyncStatus() (bool, error) { + + var err error + var synced bool + var wg sync.WaitGroup + + wg.Add(1) + + err = c.conn.On(eventTimeSyncResults, func(h *gosocketio.Channel, st *timeSyncStatus) { + synced = st.Status + c.conn.Off(eventTimeSyncResults) + wg.Done() + }) + if err != nil { + return false, errors.Wrapf(err, "error while binding to '%s' event", eventTimeSyncResults) + } + + c.logf("sending '%s' event", eventGetTimeSyncStatus) + if err = c.conn.Emit(eventGetTimeSyncStatus, nil); err != nil { + return false, errors.Wrapf(err, "error while emitting '%s' event", eventGetTimeSyncStatus) + } + c.logf("'%s' event sent", eventGetTimeSyncStatus) + + wg.Wait() + + return synced, err + +} diff --git a/reach/time_sync_test.go b/reach/time_sync_test.go new file mode 100644 index 0000000..25afa17 --- /dev/null +++ b/reach/time_sync_test.go @@ -0,0 +1,31 @@ +package reach + +import ( + "log" + "testing" +) + +func TestClientTimeSync(t *testing.T) { + + if !*runIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + + client := NewClient( + WithStandardLogger(), + WithEndpoint(*reachHost, 80), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + synced, err := client.TimeSyncStatus() + if err != nil { + t.Error(err) + } + + log.Printf("time sync result: %v", synced) + + defer client.Close() + +} From 055ee0000d75ffb4b05b61d554556c91fc942d1c Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 15:46:15 +0200 Subject: [PATCH 05/14] Add ReceiverUpradeAvailable() API --- reach/receiver_upgrade.go | 47 ++++++++++++++++++++++++++++++++++ reach/receiver_upgrade_test.go | 31 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 reach/receiver_upgrade.go create mode 100644 reach/receiver_upgrade_test.go diff --git a/reach/receiver_upgrade.go b/reach/receiver_upgrade.go new file mode 100644 index 0000000..26fd400 --- /dev/null +++ b/reach/receiver_upgrade.go @@ -0,0 +1,47 @@ +package reach + +import ( + "sync" + + "forge.cadoles.com/Pyxis/golang-socketio" + "github.com/pkg/errors" +) + +const ( + eventIsReceiverUpgradeAvailable = "is receiver upgrade available" + eventReceiverUpgradeAvailable = "receiver upgrade available" +) + +type receiverUpgreAvailable struct { + Available bool `json:"available"` + Running bool `json:"running"` +} + +// ReceiverUpgradeAvailable checks if an upgrade is avaialable/running for the ReachRS module +func (c *Client) ReceiverUpgradeAvailable() (available bool, running bool, err error) { + + var wg sync.WaitGroup + + wg.Add(1) + + err = c.conn.On(eventReceiverUpgradeAvailable, func(h *gosocketio.Channel, up *receiverUpgreAvailable) { + available = up.Available + running = up.Running + c.conn.Off(eventReceiverUpgradeAvailable) + wg.Done() + }) + if err != nil { + return false, false, errors.Wrapf(err, "error while binding to '%s' event", eventReceiverUpgradeAvailable) + } + + c.logf("sending '%s' event", eventIsReceiverUpgradeAvailable) + if err = c.conn.Emit(eventIsReceiverUpgradeAvailable, nil); err != nil { + return false, false, errors.Wrapf(err, "error while emitting '%s' event", eventIsReceiverUpgradeAvailable) + } + c.logf("'%s' event sent", eventIsReceiverUpgradeAvailable) + + wg.Wait() + + return available, running, err + +} diff --git a/reach/receiver_upgrade_test.go b/reach/receiver_upgrade_test.go new file mode 100644 index 0000000..3560ef1 --- /dev/null +++ b/reach/receiver_upgrade_test.go @@ -0,0 +1,31 @@ +package reach + +import ( + "log" + "testing" +) + +func TestClientReceiverUpgradeAvailable(t *testing.T) { + + if !*runIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + + client := NewClient( + WithStandardLogger(), + WithEndpoint(*reachHost, 80), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + available, running, err := client.ReceiverUpgradeAvailable() + if err != nil { + t.Error(err) + } + + log.Printf("receiver upgrade result: available: %v, running: %v", available, running) + + defer client.Close() + +} From 1e16779c5260c2f35644bbebe2772286cd03c6d2 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 15:53:40 +0200 Subject: [PATCH 06/14] Add ReachViewVersion() API --- reach/reachview_version.go | 47 +++++++++++++++++++++++++++++++++ reach/reachview_version_test.go | 35 ++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 reach/reachview_version.go create mode 100644 reach/reachview_version_test.go diff --git a/reach/reachview_version.go b/reach/reachview_version.go new file mode 100644 index 0000000..3bacf46 --- /dev/null +++ b/reach/reachview_version.go @@ -0,0 +1,47 @@ +package reach + +import ( + "sync" + + "forge.cadoles.com/Pyxis/golang-socketio" + "github.com/pkg/errors" +) + +const ( + eventGetReachViewVersion = "get reachview version" + eventReachViewVersionResults = "current reachview version" +) + +type reachViewVersion struct { + Version string `json:"version"` +} + +// ReachViewVersion returns the ReachRS module ReachView version +func (c *Client) ReachViewVersion() (string, error) { + + var err error + var version string + var wg sync.WaitGroup + + wg.Add(1) + + err = c.conn.On(eventReachViewVersionResults, func(h *gosocketio.Channel, rv *reachViewVersion) { + version = rv.Version + c.conn.Off(eventReachViewVersionResults) + wg.Done() + }) + if err != nil { + return "", errors.Wrapf(err, "error while binding to '%s' event", eventReachViewVersionResults) + } + + c.logf("sending '%s' event", eventGetReachViewVersion) + if err = c.conn.Emit(eventGetReachViewVersion, nil); err != nil { + return "", errors.Wrapf(err, "error while emitting '%s' event", eventGetReachViewVersion) + } + c.logf("'%s' event sent", eventGetReachViewVersion) + + wg.Wait() + + return version, err + +} diff --git a/reach/reachview_version_test.go b/reach/reachview_version_test.go new file mode 100644 index 0000000..e384019 --- /dev/null +++ b/reach/reachview_version_test.go @@ -0,0 +1,35 @@ +package reach + +import ( + "log" + "testing" +) + +func TestClientReachViewVersion(t *testing.T) { + + if !*runIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + + client := NewClient( + WithStandardLogger(), + WithEndpoint(*reachHost, 80), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + version, err := client.ReachViewVersion() + if err != nil { + t.Error(err) + } + + if version == "" { + t.Error("version should not be empty") + } + + log.Printf("reachview version result: %v", version) + + defer client.Close() + +} From fc4e50bf087e76a4cd3dfa7fced05ec8b2d20239 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 15:54:11 +0200 Subject: [PATCH 07/14] Use internal logger to display results info --- reach/reachview_version.go | 2 ++ reach/reachview_version_test.go | 3 --- reach/receiver_upgrade.go | 2 ++ reach/receiver_upgrade_test.go | 5 +---- reach/time_sync.go | 2 ++ reach/time_sync_test.go | 5 +---- 6 files changed, 8 insertions(+), 11 deletions(-) diff --git a/reach/reachview_version.go b/reach/reachview_version.go index 3bacf46..e56c4a0 100644 --- a/reach/reachview_version.go +++ b/reach/reachview_version.go @@ -42,6 +42,8 @@ func (c *Client) ReachViewVersion() (string, error) { wg.Wait() + c.logf("reachview version result: %v", version) + return version, err } diff --git a/reach/reachview_version_test.go b/reach/reachview_version_test.go index e384019..0704b65 100644 --- a/reach/reachview_version_test.go +++ b/reach/reachview_version_test.go @@ -1,7 +1,6 @@ package reach import ( - "log" "testing" ) @@ -28,8 +27,6 @@ func TestClientReachViewVersion(t *testing.T) { t.Error("version should not be empty") } - log.Printf("reachview version result: %v", version) - defer client.Close() } diff --git a/reach/receiver_upgrade.go b/reach/receiver_upgrade.go index 26fd400..89371bd 100644 --- a/reach/receiver_upgrade.go +++ b/reach/receiver_upgrade.go @@ -42,6 +42,8 @@ func (c *Client) ReceiverUpgradeAvailable() (available bool, running bool, err e wg.Wait() + c.logf("receiver upgrade result: available: %v, running: %v", available, running) + return available, running, err } diff --git a/reach/receiver_upgrade_test.go b/reach/receiver_upgrade_test.go index 3560ef1..bef2677 100644 --- a/reach/receiver_upgrade_test.go +++ b/reach/receiver_upgrade_test.go @@ -1,7 +1,6 @@ package reach import ( - "log" "testing" ) @@ -19,13 +18,11 @@ func TestClientReceiverUpgradeAvailable(t *testing.T) { t.Fatal(err) } - available, running, err := client.ReceiverUpgradeAvailable() + _, _, err := client.ReceiverUpgradeAvailable() if err != nil { t.Error(err) } - log.Printf("receiver upgrade result: available: %v, running: %v", available, running) - defer client.Close() } diff --git a/reach/time_sync.go b/reach/time_sync.go index 37f0378..229d526 100644 --- a/reach/time_sync.go +++ b/reach/time_sync.go @@ -42,6 +42,8 @@ func (c *Client) TimeSyncStatus() (bool, error) { wg.Wait() + c.logf("time sync result: %v", synced) + return synced, err } diff --git a/reach/time_sync_test.go b/reach/time_sync_test.go index 25afa17..7f79497 100644 --- a/reach/time_sync_test.go +++ b/reach/time_sync_test.go @@ -1,7 +1,6 @@ package reach import ( - "log" "testing" ) @@ -19,13 +18,11 @@ func TestClientTimeSync(t *testing.T) { t.Fatal(err) } - synced, err := client.TimeSyncStatus() + _, err := client.TimeSyncStatus() if err != nil { t.Error(err) } - log.Printf("time sync result: %v", synced) - defer client.Close() } From 48db0458c00d12d2a81704b945f239c37bc43faf Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 16:30:37 +0200 Subject: [PATCH 08/14] Add OPKGUpdate() API --- reach/opkg_update.go | 55 +++++++++++++++++++++++++++++++++++++++ reach/opkg_update_test.go | 28 ++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 reach/opkg_update.go create mode 100644 reach/opkg_update_test.go diff --git a/reach/opkg_update.go b/reach/opkg_update.go new file mode 100644 index 0000000..c264c2c --- /dev/null +++ b/reach/opkg_update.go @@ -0,0 +1,55 @@ +package reach + +import ( + "sync" + + "forge.cadoles.com/Pyxis/golang-socketio" + "github.com/pkg/errors" +) + +const ( + eventUpdate = "update" + eventOPKGUpdateResult = "opkg update result" +) + +// OPKGUpdateStatus embeds informations about OPKG update status +type OPKGUpdateStatus struct { + Active bool `json:"active"` + State string `json:"state"` + Locked bool `json:"locked"` +} + +// OPKGUpdate asks the ReachRS module to start an OPKG update +func (c *Client) OPKGUpdate() (*OPKGUpdateStatus, error) { + + var err error + var status *OPKGUpdateStatus + var wg sync.WaitGroup + + wg.Add(1) + + err = c.conn.On(eventOPKGUpdateResult, func(h *gosocketio.Channel, st *OPKGUpdateStatus) { + status = st + c.conn.Off(eventOPKGUpdateResult) + wg.Done() + }) + if err != nil { + return nil, errors.Wrapf(err, "error while binding to '%s' event", eventOPKGUpdateResult) + } + + c.logf("sending '%s' event", eventUpdate) + if err = c.conn.Emit(eventUpdate, nil); err != nil { + return nil, errors.Wrapf(err, "error while emitting '%s' event", eventUpdate) + } + c.logf("'%s' event sent", eventUpdate) + + wg.Wait() + + c.logf( + "opkg update result: active: %v, state: %v, locked: %v", + status.Active, status.State, status.Locked, + ) + + return status, err + +} diff --git a/reach/opkg_update_test.go b/reach/opkg_update_test.go new file mode 100644 index 0000000..a843dd6 --- /dev/null +++ b/reach/opkg_update_test.go @@ -0,0 +1,28 @@ +package reach + +import ( + "testing" +) + +func TestClientOPKGUpdate(t *testing.T) { + + if !*runIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + + client := NewClient( + WithStandardLogger(), + WithEndpoint(*reachHost, 80), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + _, err := client.OPKGUpdate() + if err != nil { + t.Error(err) + } + + defer client.Close() + +} From dd0a466bd3398aba6c4953ce52a1b65dfda09b9f Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 17:13:45 +0200 Subject: [PATCH 09/14] Namespaces updaters API in its own client --- reach/browser_connected.go | 12 ++++----- reach/client.go | 19 +++++--------- reach/opkg_update.go | 14 +++++------ reach/opkg_update_test.go | 4 +-- reach/reachview_version.go | 14 +++++------ reach/reachview_version_test.go | 4 +-- reach/reboot_now.go | 24 ++++++++++++++++++ reach/reboot_now_test.go | 27 ++++++++++++++++++++ reach/receiver_upgrade.go | 14 +++++------ reach/receiver_upgrade_test.go | 4 +-- reach/test.go | 6 ++--- reach/test_results.go | 12 ++++----- reach/test_results_test.go | 4 +-- reach/time_sync.go | 14 +++++------ reach/time_sync_test.go | 4 +-- reach/updater.go | 12 +++++++++ reach/wifi_networks.go | 44 ++++++++++++++++----------------- reach/wifi_networks_test.go | 8 +++--- 18 files changed, 148 insertions(+), 92 deletions(-) create mode 100644 reach/reboot_now.go create mode 100644 reach/reboot_now_test.go create mode 100644 reach/updater.go diff --git a/reach/browser_connected.go b/reach/browser_connected.go index a859cbb..a773d5b 100644 --- a/reach/browser_connected.go +++ b/reach/browser_connected.go @@ -7,20 +7,20 @@ import ( const ( // EventBrowserConnected is emitted after the initial connection to the // ReachView endpoint - EventBrowserConnected = "browser connected" + eventBrowserConnected = "browser connected" ) // sendBrowserConnected notifies the ReachView endpoint // of a new connection. // See misc/reachview/update_main.js line 297 -func (c *Client) sendBrowserConnected() error { +func (c *client) sendBrowserConnected() error { payload := map[string]string{ "data": "I'm connected", } - c.logf("sending '%s' event", EventBrowserConnected) - if err := c.conn.Emit(EventBrowserConnected, payload); err != nil { - return errors.Wrapf(err, "error while emitting '%s' event", EventBrowserConnected) + c.logf("sending '%s' event", eventBrowserConnected) + if err := c.conn.Emit(eventBrowserConnected, payload); err != nil { + return errors.Wrapf(err, "error while emitting '%s' event", eventBrowserConnected) } - c.logf("'%s' event sent", EventBrowserConnected) + c.logf("'%s' event sent", eventBrowserConnected) return nil } diff --git a/reach/client.go b/reach/client.go index 257cb5b..539df72 100644 --- a/reach/client.go +++ b/reach/client.go @@ -2,26 +2,20 @@ package reach import ( "sync" - "time" "forge.cadoles.com/Pyxis/golang-socketio" "forge.cadoles.com/Pyxis/golang-socketio/transport" "github.com/pkg/errors" ) -type handshake struct { - PingInterval time.Duration `json:"pingInterval"` -} - -// Client is a ReachView Websocket API client -type Client struct { +type client struct { opts *Options conn *gosocketio.Client } // Connect connects the client to the ReachView endpoint // This method is not safe to call by different coroutines -func (c *Client) Connect() error { +func (c *client) Connect() error { var err error var wg sync.WaitGroup @@ -71,7 +65,7 @@ func (c *Client) Connect() error { } // Close closes the current connection to the ReachView endpoint -func (c *Client) Close() { +func (c *client) Close() { if c.conn == nil { return } @@ -80,20 +74,19 @@ func (c *Client) Close() { return } -func (c *Client) logf(format string, args ...interface{}) { +func (c *client) logf(format string, args ...interface{}) { if c.opts.Logger == nil { return } c.opts.Logger.Printf(format, args...) } -// NewClient returns a new ReachView Websocket API client -func NewClient(opts ...OptionFunc) *Client { +func newClient(opts ...OptionFunc) *client { options := DefaultOptions() for _, o := range opts { o(options) } - return &Client{ + return &client{ opts: options, } } diff --git a/reach/opkg_update.go b/reach/opkg_update.go index c264c2c..a200821 100644 --- a/reach/opkg_update.go +++ b/reach/opkg_update.go @@ -20,7 +20,7 @@ type OPKGUpdateStatus struct { } // OPKGUpdate asks the ReachRS module to start an OPKG update -func (c *Client) OPKGUpdate() (*OPKGUpdateStatus, error) { +func (u *Updater) OPKGUpdate() (*OPKGUpdateStatus, error) { var err error var status *OPKGUpdateStatus @@ -28,24 +28,24 @@ func (c *Client) OPKGUpdate() (*OPKGUpdateStatus, error) { wg.Add(1) - err = c.conn.On(eventOPKGUpdateResult, func(h *gosocketio.Channel, st *OPKGUpdateStatus) { + err = u.conn.On(eventOPKGUpdateResult, func(h *gosocketio.Channel, st *OPKGUpdateStatus) { status = st - c.conn.Off(eventOPKGUpdateResult) + u.conn.Off(eventOPKGUpdateResult) wg.Done() }) if err != nil { return nil, errors.Wrapf(err, "error while binding to '%s' event", eventOPKGUpdateResult) } - c.logf("sending '%s' event", eventUpdate) - if err = c.conn.Emit(eventUpdate, nil); err != nil { + u.logf("sending '%s' event", eventUpdate) + if err = u.conn.Emit(eventUpdate, nil); err != nil { return nil, errors.Wrapf(err, "error while emitting '%s' event", eventUpdate) } - c.logf("'%s' event sent", eventUpdate) + u.logf("'%s' event sent", eventUpdate) wg.Wait() - c.logf( + u.logf( "opkg update result: active: %v, state: %v, locked: %v", status.Active, status.State, status.Locked, ) diff --git a/reach/opkg_update_test.go b/reach/opkg_update_test.go index a843dd6..b77749e 100644 --- a/reach/opkg_update_test.go +++ b/reach/opkg_update_test.go @@ -6,11 +6,11 @@ import ( func TestClientOPKGUpdate(t *testing.T) { - if !*runIntegrationTests { + if !*runUpdaterIntegrationTests { t.Skip("To run this test, use: go test -integration") } - client := NewClient( + client := NewUpdaterClient( WithStandardLogger(), WithEndpoint(*reachHost, 80), ) diff --git a/reach/reachview_version.go b/reach/reachview_version.go index e56c4a0..db9b8c6 100644 --- a/reach/reachview_version.go +++ b/reach/reachview_version.go @@ -17,7 +17,7 @@ type reachViewVersion struct { } // ReachViewVersion returns the ReachRS module ReachView version -func (c *Client) ReachViewVersion() (string, error) { +func (u *Updater) ReachViewVersion() (string, error) { var err error var version string @@ -25,24 +25,24 @@ func (c *Client) ReachViewVersion() (string, error) { wg.Add(1) - err = c.conn.On(eventReachViewVersionResults, func(h *gosocketio.Channel, rv *reachViewVersion) { + err = u.conn.On(eventReachViewVersionResults, func(h *gosocketio.Channel, rv *reachViewVersion) { version = rv.Version - c.conn.Off(eventReachViewVersionResults) + u.conn.Off(eventReachViewVersionResults) wg.Done() }) if err != nil { return "", errors.Wrapf(err, "error while binding to '%s' event", eventReachViewVersionResults) } - c.logf("sending '%s' event", eventGetReachViewVersion) - if err = c.conn.Emit(eventGetReachViewVersion, nil); err != nil { + u.logf("sending '%s' event", eventGetReachViewVersion) + if err = u.conn.Emit(eventGetReachViewVersion, nil); err != nil { return "", errors.Wrapf(err, "error while emitting '%s' event", eventGetReachViewVersion) } - c.logf("'%s' event sent", eventGetReachViewVersion) + u.logf("'%s' event sent", eventGetReachViewVersion) wg.Wait() - c.logf("reachview version result: %v", version) + u.logf("reachview version result: %v", version) return version, err diff --git a/reach/reachview_version_test.go b/reach/reachview_version_test.go index 0704b65..b24d528 100644 --- a/reach/reachview_version_test.go +++ b/reach/reachview_version_test.go @@ -6,11 +6,11 @@ import ( func TestClientReachViewVersion(t *testing.T) { - if !*runIntegrationTests { + if !*runUpdaterIntegrationTests { t.Skip("To run this test, use: go test -integration") } - client := NewClient( + client := NewUpdaterClient( WithStandardLogger(), WithEndpoint(*reachHost, 80), ) diff --git a/reach/reboot_now.go b/reach/reboot_now.go new file mode 100644 index 0000000..3ccdcf3 --- /dev/null +++ b/reach/reboot_now.go @@ -0,0 +1,24 @@ +package reach + +import ( + "github.com/pkg/errors" +) + +const ( + eventReboot = "reboot now" +) + +// RebootNow asks the ReachRS module to reboot now +func (u *Updater) RebootNow() error { + + var err error + + u.logf("sending '%s' event", eventReboot) + if err = u.conn.Emit(eventReboot, nil); err != nil { + return errors.Wrapf(err, "error while emitting '%s' event", eventReboot) + } + u.logf("'%s' event sent", eventReboot) + + return err + +} diff --git a/reach/reboot_now_test.go b/reach/reboot_now_test.go new file mode 100644 index 0000000..ffe580c --- /dev/null +++ b/reach/reboot_now_test.go @@ -0,0 +1,27 @@ +package reach + +import ( + "testing" +) + +func TestClientReboutNow(t *testing.T) { + + if !*runUpdaterIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + + client := NewUpdaterClient( + WithStandardLogger(), + WithEndpoint(*reachHost, 80), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + if err := client.RebootNow(); err != nil { + t.Error(err) + } + + defer client.Close() + +} diff --git a/reach/receiver_upgrade.go b/reach/receiver_upgrade.go index 89371bd..79b9630 100644 --- a/reach/receiver_upgrade.go +++ b/reach/receiver_upgrade.go @@ -18,31 +18,31 @@ type receiverUpgreAvailable struct { } // ReceiverUpgradeAvailable checks if an upgrade is avaialable/running for the ReachRS module -func (c *Client) ReceiverUpgradeAvailable() (available bool, running bool, err error) { +func (u *Updater) ReceiverUpgradeAvailable() (available bool, running bool, err error) { var wg sync.WaitGroup wg.Add(1) - err = c.conn.On(eventReceiverUpgradeAvailable, func(h *gosocketio.Channel, up *receiverUpgreAvailable) { + err = u.conn.On(eventReceiverUpgradeAvailable, func(h *gosocketio.Channel, up *receiverUpgreAvailable) { available = up.Available running = up.Running - c.conn.Off(eventReceiverUpgradeAvailable) + u.conn.Off(eventReceiverUpgradeAvailable) wg.Done() }) if err != nil { return false, false, errors.Wrapf(err, "error while binding to '%s' event", eventReceiverUpgradeAvailable) } - c.logf("sending '%s' event", eventIsReceiverUpgradeAvailable) - if err = c.conn.Emit(eventIsReceiverUpgradeAvailable, nil); err != nil { + u.logf("sending '%s' event", eventIsReceiverUpgradeAvailable) + if err = u.conn.Emit(eventIsReceiverUpgradeAvailable, nil); err != nil { return false, false, errors.Wrapf(err, "error while emitting '%s' event", eventIsReceiverUpgradeAvailable) } - c.logf("'%s' event sent", eventIsReceiverUpgradeAvailable) + u.logf("'%s' event sent", eventIsReceiverUpgradeAvailable) wg.Wait() - c.logf("receiver upgrade result: available: %v, running: %v", available, running) + u.logf("receiver upgrade result: available: %v, running: %v", available, running) return available, running, err diff --git a/reach/receiver_upgrade_test.go b/reach/receiver_upgrade_test.go index bef2677..e4d57d5 100644 --- a/reach/receiver_upgrade_test.go +++ b/reach/receiver_upgrade_test.go @@ -6,11 +6,11 @@ import ( func TestClientReceiverUpgradeAvailable(t *testing.T) { - if !*runIntegrationTests { + if !*runUpdaterIntegrationTests { t.Skip("To run this test, use: go test -integration") } - client := NewClient( + client := NewUpdaterClient( WithStandardLogger(), WithEndpoint(*reachHost, 80), ) diff --git a/reach/test.go b/reach/test.go index 2142a71..5343fc6 100644 --- a/reach/test.go +++ b/reach/test.go @@ -2,9 +2,9 @@ package reach import "flag" -var runIntegrationTests = flag.Bool( - "integration", false, - "Run the integration tests (in addition to the unit tests)", +var runUpdaterIntegrationTests = flag.Bool( + "updater-integration", false, + "Run the 'Updater' integration tests (in addition to the unit tests)", ) var reachHost = flag.String( diff --git a/reach/test_results.go b/reach/test_results.go index 7a721f2..0c050b9 100644 --- a/reach/test_results.go +++ b/reach/test_results.go @@ -22,7 +22,7 @@ type TestResults struct { } // TestResults returns the ReachRS module tests results -func (c *Client) TestResults() (*TestResults, error) { +func (u *Updater) TestResults() (*TestResults, error) { var err error var results *TestResults @@ -30,20 +30,20 @@ func (c *Client) TestResults() (*TestResults, error) { wg.Add(1) - err = c.conn.On(eventTestResults, func(h *gosocketio.Channel, res *TestResults) { + err = u.conn.On(eventTestResults, func(h *gosocketio.Channel, res *TestResults) { results = res - c.conn.Off(eventTestResults) + u.conn.Off(eventTestResults) wg.Done() }) if err != nil { return nil, errors.Wrapf(err, "error while binding to '%s' event", eventTestResults) } - c.logf("sending '%s' event", eventGetTestResults) - if err = c.conn.Emit(eventGetTestResults, nil); err != nil { + u.logf("sending '%s' event", eventGetTestResults) + if err = u.conn.Emit(eventGetTestResults, nil); err != nil { return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetTestResults) } - c.logf("'%s' event sent", eventGetTestResults) + u.logf("'%s' event sent", eventGetTestResults) wg.Wait() diff --git a/reach/test_results_test.go b/reach/test_results_test.go index b7b242a..78165f3 100644 --- a/reach/test_results_test.go +++ b/reach/test_results_test.go @@ -6,11 +6,11 @@ import ( func TestClientTestResults(t *testing.T) { - if !*runIntegrationTests { + if !*runUpdaterIntegrationTests { t.Skip("To run this test, use: go test -integration") } - client := NewClient( + client := NewUpdaterClient( WithStandardLogger(), WithEndpoint(*reachHost, 80), ) diff --git a/reach/time_sync.go b/reach/time_sync.go index 229d526..d9f8c96 100644 --- a/reach/time_sync.go +++ b/reach/time_sync.go @@ -17,7 +17,7 @@ type timeSyncStatus struct { } // TimeSyncStatus returns the ReachRS module time sync status -func (c *Client) TimeSyncStatus() (bool, error) { +func (u *Updater) TimeSyncStatus() (bool, error) { var err error var synced bool @@ -25,24 +25,24 @@ func (c *Client) TimeSyncStatus() (bool, error) { wg.Add(1) - err = c.conn.On(eventTimeSyncResults, func(h *gosocketio.Channel, st *timeSyncStatus) { + err = u.conn.On(eventTimeSyncResults, func(h *gosocketio.Channel, st *timeSyncStatus) { synced = st.Status - c.conn.Off(eventTimeSyncResults) + u.conn.Off(eventTimeSyncResults) wg.Done() }) if err != nil { return false, errors.Wrapf(err, "error while binding to '%s' event", eventTimeSyncResults) } - c.logf("sending '%s' event", eventGetTimeSyncStatus) - if err = c.conn.Emit(eventGetTimeSyncStatus, nil); err != nil { + u.logf("sending '%s' event", eventGetTimeSyncStatus) + if err = u.conn.Emit(eventGetTimeSyncStatus, nil); err != nil { return false, errors.Wrapf(err, "error while emitting '%s' event", eventGetTimeSyncStatus) } - c.logf("'%s' event sent", eventGetTimeSyncStatus) + u.logf("'%s' event sent", eventGetTimeSyncStatus) wg.Wait() - c.logf("time sync result: %v", synced) + u.logf("time sync result: %v", synced) return synced, err diff --git a/reach/time_sync_test.go b/reach/time_sync_test.go index 7f79497..fa990a3 100644 --- a/reach/time_sync_test.go +++ b/reach/time_sync_test.go @@ -6,11 +6,11 @@ import ( func TestClientTimeSync(t *testing.T) { - if !*runIntegrationTests { + if !*runUpdaterIntegrationTests { t.Skip("To run this test, use: go test -integration") } - client := NewClient( + client := NewUpdaterClient( WithStandardLogger(), WithEndpoint(*reachHost, 80), ) diff --git a/reach/updater.go b/reach/updater.go new file mode 100644 index 0000000..888790d --- /dev/null +++ b/reach/updater.go @@ -0,0 +1,12 @@ +package reach + +// Updater is a ReachRS Updater client +type Updater struct { + *client +} + +// NewUpdaterClient returns a new ReachRS Updater client +func NewUpdaterClient(opts ...OptionFunc) *Updater { + client := newClient(opts...) + return &Updater{client} +} diff --git a/reach/wifi_networks.go b/reach/wifi_networks.go index ce8f2f2..c895c98 100644 --- a/reach/wifi_networks.go +++ b/reach/wifi_networks.go @@ -42,7 +42,7 @@ type WifiNetwork struct { } // SavedWifiNetworks returns the ReachRS module saved wifi networks -func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) { +func (u *Updater) SavedWifiNetworks() ([]*WifiNetwork, error) { var err error var wifiNetworks []*WifiNetwork @@ -50,20 +50,20 @@ func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) { wg.Add(1) - err = c.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) { + err = u.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) { wifiNetworks = wn - c.conn.Off(eventSavedWifiNetworkResults) + u.conn.Off(eventSavedWifiNetworkResults) wg.Done() }) if err != nil { return nil, errors.Wrapf(err, "error while binding to '%s' event", eventSavedWifiNetworkResults) } - c.logf("sending '%s' event", eventGetSavedWifiNetworks) - if err = c.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil { + u.logf("sending '%s' event", eventGetSavedWifiNetworks) + if err = u.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil { return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetSavedWifiNetworks) } - c.logf("'%s' event sent", eventGetSavedWifiNetworks) + u.logf("'%s' event sent", eventGetSavedWifiNetworks) wg.Wait() @@ -72,7 +72,7 @@ func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) { } // AddWifiNetwork asks the ReachRS module to save the given wifi network informations -func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password string) (bool, error) { +func (u *Updater) AddWifiNetwork(ssid string, security WifiSecurity, password string) (bool, error) { var err error var wg sync.WaitGroup @@ -80,9 +80,9 @@ func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password str wg.Add(1) - err = c.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) { + err = u.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) { done = d - c.conn.Off(eventAddWifiNetworkResults) + u.conn.Off(eventAddWifiNetworkResults) wg.Done() }) if err != nil { @@ -95,11 +95,11 @@ func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password str Password: password, } - c.logf("sending '%s' event", eventAddWifiNetwork) - if err = c.conn.Emit(eventAddWifiNetwork, wn); err != nil { + u.logf("sending '%s' event", eventAddWifiNetwork) + if err = u.conn.Emit(eventAddWifiNetwork, wn); err != nil { return false, errors.Wrapf(err, "error while emitting '%s' event", eventAddWifiNetwork) } - c.logf("'%s' event sent", eventAddWifiNetwork) + u.logf("'%s' event sent", eventAddWifiNetwork) wg.Wait() @@ -108,7 +108,7 @@ func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password str } // RemoveWifiNetwork asks the ReachRS module to remove the given WiFi network -func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) { +func (u *Updater) RemoveWifiNetwork(ssid string) (bool, error) { var err error var wg sync.WaitGroup @@ -116,20 +116,20 @@ func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) { wg.Add(1) - err = c.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) { + err = u.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) { done = d - c.conn.Off(eventRemoveWifiNetworkResults) + u.conn.Off(eventRemoveWifiNetworkResults) wg.Done() }) if err != nil { return false, errors.Wrapf(err, "error while binding to '%s' event", eventRemoveWifiNetworkResults) } - c.logf("sending '%s' event", eventRemoveWifiNetwork) - if err := c.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil { + u.logf("sending '%s' event", eventRemoveWifiNetwork) + if err := u.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil { return false, errors.Wrapf(err, "error while emitting '%s' event", eventRemoveWifiNetwork) } - c.logf("'%s' event sent", eventRemoveWifiNetwork) + u.logf("'%s' event sent", eventRemoveWifiNetwork) wg.Wait() @@ -138,11 +138,11 @@ func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) { } // ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network -func (c *Client) ConnectToWifiNetwork(ssid string) error { - c.logf("sending '%s' event", eventConnectToNetwork) - if err := c.conn.Emit(eventConnectToNetwork, ssid); err != nil { +func (u *Updater) ConnectToWifiNetwork(ssid string) error { + u.logf("sending '%s' event", eventConnectToNetwork) + if err := u.conn.Emit(eventConnectToNetwork, ssid); err != nil { return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork) } - c.logf("'%s' event sent", eventConnectToNetwork) + u.logf("'%s' event sent", eventConnectToNetwork) return nil } diff --git a/reach/wifi_networks_test.go b/reach/wifi_networks_test.go index 0d4e237..5e43f1b 100644 --- a/reach/wifi_networks_test.go +++ b/reach/wifi_networks_test.go @@ -8,11 +8,11 @@ import ( func TestClientSavedWiFiNetworks(t *testing.T) { - if !*runIntegrationTests { + if !*runUpdaterIntegrationTests { t.Skip("To run this test, use: go test -integration") } - client := NewClient( + client := NewUpdaterClient( WithStandardLogger(), WithEndpoint(*reachHost, 80), ) @@ -31,11 +31,11 @@ func TestClientSavedWiFiNetworks(t *testing.T) { func TestClientCRUDWiFiNetwork(t *testing.T) { - if !*runIntegrationTests { + if !*runUpdaterIntegrationTests { t.Skip("To run this test, use: go test -integration") } - client := NewClient( + client := NewUpdaterClient( WithStandardLogger(), WithEndpoint(*reachHost, 80), ) From 998f718354ecc5e90eb40b37ad482cbc824dbfeb Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 17:23:28 +0200 Subject: [PATCH 10/14] Add doc command to Makefile --- Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8660d7b..4ba546a 100644 --- a/Makefile +++ b/Makefile @@ -31,4 +31,8 @@ clean: rm -rf ./bin go clean -i -x -r -modcache -.PHONY: test clean generate vendor install-devtools lint watch tidy \ No newline at end of file +doc: + @echo "Open your browser to http://localhost:6060/pkg/forge.cadoles.com/Pyxis/orion/ to see the documentation" + @godoc -http=:6060 + +.PHONY: test clean generate vendor install-devtools lint watch tidy doc \ No newline at end of file From 2d0c3d9de9f7b0defc5639024a38be9e67a5e1a1 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 17:24:17 +0200 Subject: [PATCH 11/14] Fix flag message to run updater integration tests --- reach/reachview_version_test.go | 2 +- reach/reboot_now_test.go | 2 +- reach/receiver_upgrade_test.go | 2 +- reach/test_results_test.go | 2 +- reach/time_sync_test.go | 2 +- reach/wifi_networks_test.go | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/reach/reachview_version_test.go b/reach/reachview_version_test.go index b24d528..7dba9a8 100644 --- a/reach/reachview_version_test.go +++ b/reach/reachview_version_test.go @@ -7,7 +7,7 @@ import ( func TestClientReachViewVersion(t *testing.T) { if !*runUpdaterIntegrationTests { - t.Skip("To run this test, use: go test -integration") + t.Skip("To run this test, use: go test -updater-integration") } client := NewUpdaterClient( diff --git a/reach/reboot_now_test.go b/reach/reboot_now_test.go index ffe580c..41fbc8f 100644 --- a/reach/reboot_now_test.go +++ b/reach/reboot_now_test.go @@ -7,7 +7,7 @@ import ( func TestClientReboutNow(t *testing.T) { if !*runUpdaterIntegrationTests { - t.Skip("To run this test, use: go test -integration") + t.Skip("To run this test, use: go test -updater-integration") } client := NewUpdaterClient( diff --git a/reach/receiver_upgrade_test.go b/reach/receiver_upgrade_test.go index e4d57d5..a4358d2 100644 --- a/reach/receiver_upgrade_test.go +++ b/reach/receiver_upgrade_test.go @@ -7,7 +7,7 @@ import ( func TestClientReceiverUpgradeAvailable(t *testing.T) { if !*runUpdaterIntegrationTests { - t.Skip("To run this test, use: go test -integration") + t.Skip("To run this test, use: go test -updater-integration") } client := NewUpdaterClient( diff --git a/reach/test_results_test.go b/reach/test_results_test.go index 78165f3..b8691bb 100644 --- a/reach/test_results_test.go +++ b/reach/test_results_test.go @@ -7,7 +7,7 @@ import ( func TestClientTestResults(t *testing.T) { if !*runUpdaterIntegrationTests { - t.Skip("To run this test, use: go test -integration") + t.Skip("To run this test, use: go test -updater-integration") } client := NewUpdaterClient( diff --git a/reach/time_sync_test.go b/reach/time_sync_test.go index fa990a3..c6a6682 100644 --- a/reach/time_sync_test.go +++ b/reach/time_sync_test.go @@ -7,7 +7,7 @@ import ( func TestClientTimeSync(t *testing.T) { if !*runUpdaterIntegrationTests { - t.Skip("To run this test, use: go test -integration") + t.Skip("To run this test, use: go test -updater-integration") } client := NewUpdaterClient( diff --git a/reach/wifi_networks_test.go b/reach/wifi_networks_test.go index 5e43f1b..e7d42e1 100644 --- a/reach/wifi_networks_test.go +++ b/reach/wifi_networks_test.go @@ -9,7 +9,7 @@ import ( func TestClientSavedWiFiNetworks(t *testing.T) { if !*runUpdaterIntegrationTests { - t.Skip("To run this test, use: go test -integration") + t.Skip("To run this test, use: go test -updater-integration") } client := NewUpdaterClient( @@ -32,7 +32,7 @@ func TestClientSavedWiFiNetworks(t *testing.T) { func TestClientCRUDWiFiNetwork(t *testing.T) { if !*runUpdaterIntegrationTests { - t.Skip("To run this test, use: go test -integration") + t.Skip("To run this test, use: go test -updater-integration") } client := NewUpdaterClient( From 403e14c3206db9b92f2bc122559f205117a4cdf8 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 17:24:50 +0200 Subject: [PATCH 12/14] Rename OPKGUpdate() to Update() --- reach/{opkg_update.go => update.go} | 12 ++++++------ reach/{opkg_update_test.go => update_test.go} | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) rename reach/{opkg_update.go => update.go} (77%) rename reach/{opkg_update_test.go => update_test.go} (78%) diff --git a/reach/opkg_update.go b/reach/update.go similarity index 77% rename from reach/opkg_update.go rename to reach/update.go index a200821..1d078a3 100644 --- a/reach/opkg_update.go +++ b/reach/update.go @@ -12,23 +12,23 @@ const ( eventOPKGUpdateResult = "opkg update result" ) -// OPKGUpdateStatus embeds informations about OPKG update status -type OPKGUpdateStatus struct { +// UpdateStatus embeds informations about update status +type UpdateStatus struct { Active bool `json:"active"` State string `json:"state"` Locked bool `json:"locked"` } -// OPKGUpdate asks the ReachRS module to start an OPKG update -func (u *Updater) OPKGUpdate() (*OPKGUpdateStatus, error) { +// Update asks the ReachRS module to start an OPKG update +func (u *Updater) Update() (*UpdateStatus, error) { var err error - var status *OPKGUpdateStatus + var status *UpdateStatus var wg sync.WaitGroup wg.Add(1) - err = u.conn.On(eventOPKGUpdateResult, func(h *gosocketio.Channel, st *OPKGUpdateStatus) { + err = u.conn.On(eventOPKGUpdateResult, func(h *gosocketio.Channel, st *UpdateStatus) { status = st u.conn.Off(eventOPKGUpdateResult) wg.Done() diff --git a/reach/opkg_update_test.go b/reach/update_test.go similarity index 78% rename from reach/opkg_update_test.go rename to reach/update_test.go index b77749e..87161e4 100644 --- a/reach/opkg_update_test.go +++ b/reach/update_test.go @@ -7,7 +7,7 @@ import ( func TestClientOPKGUpdate(t *testing.T) { if !*runUpdaterIntegrationTests { - t.Skip("To run this test, use: go test -integration") + t.Skip("To run this test, use: go test -updater-integration") } client := NewUpdaterClient( @@ -18,7 +18,7 @@ func TestClientOPKGUpdate(t *testing.T) { t.Fatal(err) } - _, err := client.OPKGUpdate() + _, err := client.Update() if err != nil { t.Error(err) } From 582e362aae773c2398d4db39fccccc0759c3ba43 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 17:25:11 +0200 Subject: [PATCH 13/14] Add WEP wifi network security --- reach/wifi_networks.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reach/wifi_networks.go b/reach/wifi_networks.go index c895c98..2abaecf 100644 --- a/reach/wifi_networks.go +++ b/reach/wifi_networks.go @@ -21,7 +21,9 @@ const ( type WifiSecurity string const ( - // SecurityWPAPSK WPA-PSK wifi network + // SecurityWEP WEP wifi network + SecurityWEP WifiSecurity = "wep" + // SecurityWPAPSK WPA(2)-PSK wifi network SecurityWPAPSK WifiSecurity = "wpa-psk" // SecurityOpen Open wifi network SecurityOpen WifiSecurity = "open" @@ -29,8 +31,6 @@ const ( // WifiNetwork is a ReachRS module's saved wifi network // Raw messages examples: -// [{"ssid":"Cadoles","password":"...","security":"wpa-psk","identity":""}] -// [{"is_visible":false,"connected":false,"ssid":"Cadoles","security":"wpa-psk","is_connected":false,"is_added":true}]] type WifiNetwork struct { SSID string `json:"ssid"` Password string `json:"password"` From ee3113ed80873b5f8476f4d8c9ce97310b25e161 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 17:45:38 +0200 Subject: [PATCH 14/14] Add package documentation and usage example of UpdaterClient --- reach/example_test.go | 30 ++++++++++++++++++++++++++++++ reach/reach.go | 4 ++++ 2 files changed, 34 insertions(+) create mode 100644 reach/example_test.go create mode 100644 reach/reach.go diff --git a/reach/example_test.go b/reach/example_test.go new file mode 100644 index 0000000..eebcb4e --- /dev/null +++ b/reach/example_test.go @@ -0,0 +1,30 @@ +package reach + +import "log" + +// ReachRS modules provides an Updater application when reset in factory mode. +// This package provides an API to communicate with this application. +func Example_updaterClientUsage() { + // Create a new Updater client instance + updater := NewUpdaterClient( + WithEndpoint("192.168.42.1", 80), // Define the module endpoint + ) + + // Connect to the ReachRS Updater endpoint + if err := updater.Connect(); err != nil { + log.Fatal(err) + } + + networks, err := updater.SavedWifiNetworks() + if err != nil { + log.Fatal(err) + } + + // Do something with network + for _, n := range networks { + log.Printf("Save WiFi network: SSID: '%s', Security: '%s'", n.SSID, n.Security) + } + + // Dont forget to close the connection when you are done + defer updater.Close() +} diff --git a/reach/reach.go b/reach/reach.go new file mode 100644 index 0000000..6a8aa41 --- /dev/null +++ b/reach/reach.go @@ -0,0 +1,4 @@ +// Package reach is a package to configure EMLID ReachRS modules in Go. +// +// It aims to provide a simple interface to common ReachRS modules management operations. +package reach