Namespaces updaters API in its own client

This commit is contained in:
wpetit 2018-09-19 17:13:45 +02:00
parent 48db0458c0
commit dd0a466bd3
18 changed files with 148 additions and 92 deletions

View File

@ -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
}

View File

@ -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,
}
}

View File

@ -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,
)

View File

@ -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),
)

View File

@ -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

View File

@ -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),
)

24
reach/reboot_now.go Normal file
View File

@ -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
}

27
reach/reboot_now_test.go Normal file
View File

@ -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()
}

View File

@ -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

View File

@ -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),
)

View File

@ -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(

View File

@ -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()

View File

@ -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),
)

View File

@ -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

View File

@ -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),
)

12
reach/updater.go Normal file
View File

@ -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}
}

View File

@ -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
}

View File

@ -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),
)