Namespaces updaters API in its own client
This commit is contained in:
parent
48db0458c0
commit
dd0a466bd3
|
@ -7,20 +7,20 @@ import (
|
||||||
const (
|
const (
|
||||||
// EventBrowserConnected is emitted after the initial connection to the
|
// EventBrowserConnected is emitted after the initial connection to the
|
||||||
// ReachView endpoint
|
// ReachView endpoint
|
||||||
EventBrowserConnected = "browser connected"
|
eventBrowserConnected = "browser connected"
|
||||||
)
|
)
|
||||||
|
|
||||||
// sendBrowserConnected notifies the ReachView endpoint
|
// sendBrowserConnected notifies the ReachView endpoint
|
||||||
// of a new connection.
|
// of a new connection.
|
||||||
// See misc/reachview/update_main.js line 297
|
// See misc/reachview/update_main.js line 297
|
||||||
func (c *Client) sendBrowserConnected() error {
|
func (c *client) sendBrowserConnected() error {
|
||||||
payload := map[string]string{
|
payload := map[string]string{
|
||||||
"data": "I'm connected",
|
"data": "I'm connected",
|
||||||
}
|
}
|
||||||
c.logf("sending '%s' event", EventBrowserConnected)
|
c.logf("sending '%s' event", eventBrowserConnected)
|
||||||
if err := c.conn.Emit(EventBrowserConnected, payload); err != nil {
|
if err := c.conn.Emit(eventBrowserConnected, payload); err != nil {
|
||||||
return errors.Wrapf(err, "error while emitting '%s' event", EventBrowserConnected)
|
return errors.Wrapf(err, "error while emitting '%s' event", eventBrowserConnected)
|
||||||
}
|
}
|
||||||
c.logf("'%s' event sent", EventBrowserConnected)
|
c.logf("'%s' event sent", eventBrowserConnected)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,26 +2,20 @@ package reach
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||||
"forge.cadoles.com/Pyxis/golang-socketio/transport"
|
"forge.cadoles.com/Pyxis/golang-socketio/transport"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type handshake struct {
|
type client struct {
|
||||||
PingInterval time.Duration `json:"pingInterval"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Client is a ReachView Websocket API client
|
|
||||||
type Client struct {
|
|
||||||
opts *Options
|
opts *Options
|
||||||
conn *gosocketio.Client
|
conn *gosocketio.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect connects the client to the ReachView endpoint
|
// Connect connects the client to the ReachView endpoint
|
||||||
// This method is not safe to call by different coroutines
|
// This method is not safe to call by different coroutines
|
||||||
func (c *Client) Connect() error {
|
func (c *client) Connect() error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
@ -71,7 +65,7 @@ func (c *Client) Connect() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the current connection to the ReachView endpoint
|
// Close closes the current connection to the ReachView endpoint
|
||||||
func (c *Client) Close() {
|
func (c *client) Close() {
|
||||||
if c.conn == nil {
|
if c.conn == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -80,20 +74,19 @@ func (c *Client) Close() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) logf(format string, args ...interface{}) {
|
func (c *client) logf(format string, args ...interface{}) {
|
||||||
if c.opts.Logger == nil {
|
if c.opts.Logger == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.opts.Logger.Printf(format, args...)
|
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()
|
options := DefaultOptions()
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(options)
|
o(options)
|
||||||
}
|
}
|
||||||
return &Client{
|
return &client{
|
||||||
opts: options,
|
opts: options,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ type OPKGUpdateStatus struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// OPKGUpdate asks the ReachRS module to start an OPKG update
|
// 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 err error
|
||||||
var status *OPKGUpdateStatus
|
var status *OPKGUpdateStatus
|
||||||
|
@ -28,24 +28,24 @@ func (c *Client) OPKGUpdate() (*OPKGUpdateStatus, error) {
|
||||||
|
|
||||||
wg.Add(1)
|
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
|
status = st
|
||||||
c.conn.Off(eventOPKGUpdateResult)
|
u.conn.Off(eventOPKGUpdateResult)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventOPKGUpdateResult)
|
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventOPKGUpdateResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logf("sending '%s' event", eventUpdate)
|
u.logf("sending '%s' event", eventUpdate)
|
||||||
if err = c.conn.Emit(eventUpdate, nil); err != nil {
|
if err = u.conn.Emit(eventUpdate, nil); err != nil {
|
||||||
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventUpdate)
|
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()
|
wg.Wait()
|
||||||
|
|
||||||
c.logf(
|
u.logf(
|
||||||
"opkg update result: active: %v, state: %v, locked: %v",
|
"opkg update result: active: %v, state: %v, locked: %v",
|
||||||
status.Active, status.State, status.Locked,
|
status.Active, status.State, status.Locked,
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,11 +6,11 @@ import (
|
||||||
|
|
||||||
func TestClientOPKGUpdate(t *testing.T) {
|
func TestClientOPKGUpdate(t *testing.T) {
|
||||||
|
|
||||||
if !*runIntegrationTests {
|
if !*runUpdaterIntegrationTests {
|
||||||
t.Skip("To run this test, use: go test -integration")
|
t.Skip("To run this test, use: go test -integration")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := NewClient(
|
client := NewUpdaterClient(
|
||||||
WithStandardLogger(),
|
WithStandardLogger(),
|
||||||
WithEndpoint(*reachHost, 80),
|
WithEndpoint(*reachHost, 80),
|
||||||
)
|
)
|
||||||
|
|
|
@ -17,7 +17,7 @@ type reachViewVersion struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReachViewVersion returns the ReachRS module ReachView version
|
// ReachViewVersion returns the ReachRS module ReachView version
|
||||||
func (c *Client) ReachViewVersion() (string, error) {
|
func (u *Updater) ReachViewVersion() (string, error) {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var version string
|
var version string
|
||||||
|
@ -25,24 +25,24 @@ func (c *Client) ReachViewVersion() (string, error) {
|
||||||
|
|
||||||
wg.Add(1)
|
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
|
version = rv.Version
|
||||||
c.conn.Off(eventReachViewVersionResults)
|
u.conn.Off(eventReachViewVersionResults)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrapf(err, "error while binding to '%s' event", eventReachViewVersionResults)
|
return "", errors.Wrapf(err, "error while binding to '%s' event", eventReachViewVersionResults)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logf("sending '%s' event", eventGetReachViewVersion)
|
u.logf("sending '%s' event", eventGetReachViewVersion)
|
||||||
if err = c.conn.Emit(eventGetReachViewVersion, nil); err != nil {
|
if err = u.conn.Emit(eventGetReachViewVersion, nil); err != nil {
|
||||||
return "", errors.Wrapf(err, "error while emitting '%s' event", eventGetReachViewVersion)
|
return "", errors.Wrapf(err, "error while emitting '%s' event", eventGetReachViewVersion)
|
||||||
}
|
}
|
||||||
c.logf("'%s' event sent", eventGetReachViewVersion)
|
u.logf("'%s' event sent", eventGetReachViewVersion)
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
c.logf("reachview version result: %v", version)
|
u.logf("reachview version result: %v", version)
|
||||||
|
|
||||||
return version, err
|
return version, err
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,11 @@ import (
|
||||||
|
|
||||||
func TestClientReachViewVersion(t *testing.T) {
|
func TestClientReachViewVersion(t *testing.T) {
|
||||||
|
|
||||||
if !*runIntegrationTests {
|
if !*runUpdaterIntegrationTests {
|
||||||
t.Skip("To run this test, use: go test -integration")
|
t.Skip("To run this test, use: go test -integration")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := NewClient(
|
client := NewUpdaterClient(
|
||||||
WithStandardLogger(),
|
WithStandardLogger(),
|
||||||
WithEndpoint(*reachHost, 80),
|
WithEndpoint(*reachHost, 80),
|
||||||
)
|
)
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
}
|
|
@ -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()
|
||||||
|
|
||||||
|
}
|
|
@ -18,31 +18,31 @@ type receiverUpgreAvailable struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReceiverUpgradeAvailable checks if an upgrade is avaialable/running for the ReachRS module
|
// 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
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
wg.Add(1)
|
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
|
available = up.Available
|
||||||
running = up.Running
|
running = up.Running
|
||||||
c.conn.Off(eventReceiverUpgradeAvailable)
|
u.conn.Off(eventReceiverUpgradeAvailable)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, false, errors.Wrapf(err, "error while binding to '%s' event", eventReceiverUpgradeAvailable)
|
return false, false, errors.Wrapf(err, "error while binding to '%s' event", eventReceiverUpgradeAvailable)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logf("sending '%s' event", eventIsReceiverUpgradeAvailable)
|
u.logf("sending '%s' event", eventIsReceiverUpgradeAvailable)
|
||||||
if err = c.conn.Emit(eventIsReceiverUpgradeAvailable, nil); err != nil {
|
if err = u.conn.Emit(eventIsReceiverUpgradeAvailable, nil); err != nil {
|
||||||
return false, false, errors.Wrapf(err, "error while emitting '%s' event", eventIsReceiverUpgradeAvailable)
|
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()
|
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
|
return available, running, err
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,11 @@ import (
|
||||||
|
|
||||||
func TestClientReceiverUpgradeAvailable(t *testing.T) {
|
func TestClientReceiverUpgradeAvailable(t *testing.T) {
|
||||||
|
|
||||||
if !*runIntegrationTests {
|
if !*runUpdaterIntegrationTests {
|
||||||
t.Skip("To run this test, use: go test -integration")
|
t.Skip("To run this test, use: go test -integration")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := NewClient(
|
client := NewUpdaterClient(
|
||||||
WithStandardLogger(),
|
WithStandardLogger(),
|
||||||
WithEndpoint(*reachHost, 80),
|
WithEndpoint(*reachHost, 80),
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,9 +2,9 @@ package reach
|
||||||
|
|
||||||
import "flag"
|
import "flag"
|
||||||
|
|
||||||
var runIntegrationTests = flag.Bool(
|
var runUpdaterIntegrationTests = flag.Bool(
|
||||||
"integration", false,
|
"updater-integration", false,
|
||||||
"Run the integration tests (in addition to the unit tests)",
|
"Run the 'Updater' integration tests (in addition to the unit tests)",
|
||||||
)
|
)
|
||||||
|
|
||||||
var reachHost = flag.String(
|
var reachHost = flag.String(
|
||||||
|
|
|
@ -22,7 +22,7 @@ type TestResults struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestResults returns the ReachRS module tests results
|
// TestResults returns the ReachRS module tests results
|
||||||
func (c *Client) TestResults() (*TestResults, error) {
|
func (u *Updater) TestResults() (*TestResults, error) {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var results *TestResults
|
var results *TestResults
|
||||||
|
@ -30,20 +30,20 @@ func (c *Client) TestResults() (*TestResults, error) {
|
||||||
|
|
||||||
wg.Add(1)
|
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
|
results = res
|
||||||
c.conn.Off(eventTestResults)
|
u.conn.Off(eventTestResults)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventTestResults)
|
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventTestResults)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logf("sending '%s' event", eventGetTestResults)
|
u.logf("sending '%s' event", eventGetTestResults)
|
||||||
if err = c.conn.Emit(eventGetTestResults, nil); err != nil {
|
if err = u.conn.Emit(eventGetTestResults, nil); err != nil {
|
||||||
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetTestResults)
|
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()
|
wg.Wait()
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,11 @@ import (
|
||||||
|
|
||||||
func TestClientTestResults(t *testing.T) {
|
func TestClientTestResults(t *testing.T) {
|
||||||
|
|
||||||
if !*runIntegrationTests {
|
if !*runUpdaterIntegrationTests {
|
||||||
t.Skip("To run this test, use: go test -integration")
|
t.Skip("To run this test, use: go test -integration")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := NewClient(
|
client := NewUpdaterClient(
|
||||||
WithStandardLogger(),
|
WithStandardLogger(),
|
||||||
WithEndpoint(*reachHost, 80),
|
WithEndpoint(*reachHost, 80),
|
||||||
)
|
)
|
||||||
|
|
|
@ -17,7 +17,7 @@ type timeSyncStatus struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeSyncStatus returns the ReachRS module time sync status
|
// TimeSyncStatus returns the ReachRS module time sync status
|
||||||
func (c *Client) TimeSyncStatus() (bool, error) {
|
func (u *Updater) TimeSyncStatus() (bool, error) {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var synced bool
|
var synced bool
|
||||||
|
@ -25,24 +25,24 @@ func (c *Client) TimeSyncStatus() (bool, error) {
|
||||||
|
|
||||||
wg.Add(1)
|
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
|
synced = st.Status
|
||||||
c.conn.Off(eventTimeSyncResults)
|
u.conn.Off(eventTimeSyncResults)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.Wrapf(err, "error while binding to '%s' event", eventTimeSyncResults)
|
return false, errors.Wrapf(err, "error while binding to '%s' event", eventTimeSyncResults)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logf("sending '%s' event", eventGetTimeSyncStatus)
|
u.logf("sending '%s' event", eventGetTimeSyncStatus)
|
||||||
if err = c.conn.Emit(eventGetTimeSyncStatus, nil); err != nil {
|
if err = u.conn.Emit(eventGetTimeSyncStatus, nil); err != nil {
|
||||||
return false, errors.Wrapf(err, "error while emitting '%s' event", eventGetTimeSyncStatus)
|
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()
|
wg.Wait()
|
||||||
|
|
||||||
c.logf("time sync result: %v", synced)
|
u.logf("time sync result: %v", synced)
|
||||||
|
|
||||||
return synced, err
|
return synced, err
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,11 @@ import (
|
||||||
|
|
||||||
func TestClientTimeSync(t *testing.T) {
|
func TestClientTimeSync(t *testing.T) {
|
||||||
|
|
||||||
if !*runIntegrationTests {
|
if !*runUpdaterIntegrationTests {
|
||||||
t.Skip("To run this test, use: go test -integration")
|
t.Skip("To run this test, use: go test -integration")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := NewClient(
|
client := NewUpdaterClient(
|
||||||
WithStandardLogger(),
|
WithStandardLogger(),
|
||||||
WithEndpoint(*reachHost, 80),
|
WithEndpoint(*reachHost, 80),
|
||||||
)
|
)
|
||||||
|
|
|
@ -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}
|
||||||
|
}
|
|
@ -42,7 +42,7 @@ type WifiNetwork struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SavedWifiNetworks returns the ReachRS module saved wifi networks
|
// SavedWifiNetworks returns the ReachRS module saved wifi networks
|
||||||
func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) {
|
func (u *Updater) SavedWifiNetworks() ([]*WifiNetwork, error) {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var wifiNetworks []*WifiNetwork
|
var wifiNetworks []*WifiNetwork
|
||||||
|
@ -50,20 +50,20 @@ func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) {
|
||||||
|
|
||||||
wg.Add(1)
|
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
|
wifiNetworks = wn
|
||||||
c.conn.Off(eventSavedWifiNetworkResults)
|
u.conn.Off(eventSavedWifiNetworkResults)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventSavedWifiNetworkResults)
|
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventSavedWifiNetworkResults)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logf("sending '%s' event", eventGetSavedWifiNetworks)
|
u.logf("sending '%s' event", eventGetSavedWifiNetworks)
|
||||||
if err = c.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil {
|
if err = u.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil {
|
||||||
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetSavedWifiNetworks)
|
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()
|
wg.Wait()
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddWifiNetwork asks the ReachRS module to save the given wifi network informations
|
// 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 err error
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
@ -80,9 +80,9 @@ func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password str
|
||||||
|
|
||||||
wg.Add(1)
|
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
|
done = d
|
||||||
c.conn.Off(eventAddWifiNetworkResults)
|
u.conn.Off(eventAddWifiNetworkResults)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -95,11 +95,11 @@ func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password str
|
||||||
Password: password,
|
Password: password,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logf("sending '%s' event", eventAddWifiNetwork)
|
u.logf("sending '%s' event", eventAddWifiNetwork)
|
||||||
if err = c.conn.Emit(eventAddWifiNetwork, wn); err != nil {
|
if err = u.conn.Emit(eventAddWifiNetwork, wn); err != nil {
|
||||||
return false, errors.Wrapf(err, "error while emitting '%s' event", eventAddWifiNetwork)
|
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()
|
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
|
// 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 err error
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
@ -116,20 +116,20 @@ func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) {
|
||||||
|
|
||||||
wg.Add(1)
|
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
|
done = d
|
||||||
c.conn.Off(eventRemoveWifiNetworkResults)
|
u.conn.Off(eventRemoveWifiNetworkResults)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.Wrapf(err, "error while binding to '%s' event", eventRemoveWifiNetworkResults)
|
return false, errors.Wrapf(err, "error while binding to '%s' event", eventRemoveWifiNetworkResults)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logf("sending '%s' event", eventRemoveWifiNetwork)
|
u.logf("sending '%s' event", eventRemoveWifiNetwork)
|
||||||
if err := c.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil {
|
if err := u.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil {
|
||||||
return false, errors.Wrapf(err, "error while emitting '%s' event", eventRemoveWifiNetwork)
|
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()
|
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
|
// ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network
|
||||||
func (c *Client) ConnectToWifiNetwork(ssid string) error {
|
func (u *Updater) ConnectToWifiNetwork(ssid string) error {
|
||||||
c.logf("sending '%s' event", eventConnectToNetwork)
|
u.logf("sending '%s' event", eventConnectToNetwork)
|
||||||
if err := c.conn.Emit(eventConnectToNetwork, ssid); err != nil {
|
if err := u.conn.Emit(eventConnectToNetwork, ssid); err != nil {
|
||||||
return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork)
|
return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork)
|
||||||
}
|
}
|
||||||
c.logf("'%s' event sent", eventConnectToNetwork)
|
u.logf("'%s' event sent", eventConnectToNetwork)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,11 +8,11 @@ import (
|
||||||
|
|
||||||
func TestClientSavedWiFiNetworks(t *testing.T) {
|
func TestClientSavedWiFiNetworks(t *testing.T) {
|
||||||
|
|
||||||
if !*runIntegrationTests {
|
if !*runUpdaterIntegrationTests {
|
||||||
t.Skip("To run this test, use: go test -integration")
|
t.Skip("To run this test, use: go test -integration")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := NewClient(
|
client := NewUpdaterClient(
|
||||||
WithStandardLogger(),
|
WithStandardLogger(),
|
||||||
WithEndpoint(*reachHost, 80),
|
WithEndpoint(*reachHost, 80),
|
||||||
)
|
)
|
||||||
|
@ -31,11 +31,11 @@ func TestClientSavedWiFiNetworks(t *testing.T) {
|
||||||
|
|
||||||
func TestClientCRUDWiFiNetwork(t *testing.T) {
|
func TestClientCRUDWiFiNetwork(t *testing.T) {
|
||||||
|
|
||||||
if !*runIntegrationTests {
|
if !*runUpdaterIntegrationTests {
|
||||||
t.Skip("To run this test, use: go test -integration")
|
t.Skip("To run this test, use: go test -integration")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := NewClient(
|
client := NewUpdaterClient(
|
||||||
WithStandardLogger(),
|
WithStandardLogger(),
|
||||||
WithEndpoint(*reachHost, 80),
|
WithEndpoint(*reachHost, 80),
|
||||||
)
|
)
|
||||||
|
|
Reference in New Issue