From dd0a466bd3398aba6c4953ce52a1b65dfda09b9f Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 17:13:45 +0200 Subject: [PATCH] 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), )