Use context.Context to provide timeout detection to websocket calls
This commit is contained in:
parent
1dcd03e455
commit
9367e5e79e
|
@ -1,6 +1,7 @@
|
||||||
package emlid
|
package emlid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||||
|
@ -98,23 +99,36 @@ func (c *Client) Off(event string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReqResp emits an event with the given data and waits for a response
|
// ReqResp emits an event with the given data and waits for a response
|
||||||
func (c *Client) ReqResp(requestEvent string, requestData interface{}, responseEvent string, res interface{}) error {
|
func (c *Client) ReqResp(ctx context.Context,
|
||||||
|
requestEvent string, requestData interface{},
|
||||||
|
responseEvent string, res interface{}) error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
var once sync.Once
|
||||||
|
|
||||||
|
done := func() {
|
||||||
|
c.conn.Off(responseEvent)
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
err = ctx.Err()
|
||||||
|
once.Do(done)
|
||||||
|
}()
|
||||||
|
|
||||||
err = c.conn.On(responseEvent, func(_ *gosocketio.Channel, data interface{}) {
|
err = c.conn.On(responseEvent, func(_ *gosocketio.Channel, data interface{}) {
|
||||||
err = mapstructure.Decode(data, res)
|
err = mapstructure.Decode(data, res)
|
||||||
c.conn.Off(responseEvent)
|
once.Do(done)
|
||||||
wg.Done()
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error while binding to '%s' event", responseEvent)
|
return errors.Wrapf(err, "error while binding to '%s' event", responseEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Emit(requestEvent, requestData); err != nil {
|
if err = c.Emit(requestEvent, requestData); err != nil {
|
||||||
return errors.Wrapf(err, "error while emitting event '%s'", requestEvent)
|
return errors.Wrapf(err, "error while emitting event '%s'", requestEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||||
)
|
)
|
||||||
|
@ -17,7 +19,12 @@ func Example_usage() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
networks, err := updater.WifiNetworks()
|
// We create a context for the API call with a 10 second delay
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// Retrieve the Wifi networks
|
||||||
|
networks, err := updater.WifiNetworks(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
eventGetReachViewVersion = "get reachview version"
|
eventGetReachViewVersion = "get reachview version"
|
||||||
eventReachViewVersionResults = "current reachview version"
|
eventReachViewVersionResults = "current reachview version"
|
||||||
|
@ -10,9 +12,9 @@ type reachViewVersion struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReachViewVersion returns the ReachRS module ReachView version
|
// ReachViewVersion returns the ReachRS module ReachView version
|
||||||
func (c *Client) ReachViewVersion() (string, error) {
|
func (c *Client) ReachViewVersion(ctx context.Context) (string, error) {
|
||||||
res := &reachViewVersion{}
|
res := &reachViewVersion{}
|
||||||
if err := c.ReqResp(eventGetReachViewVersion, nil, eventReachViewVersionResults, res); err != nil {
|
if err := c.ReqResp(ctx, eventGetReachViewVersion, nil, eventReachViewVersionResults, res); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return res.Version, nil
|
return res.Version, nil
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||||
)
|
)
|
||||||
|
@ -19,8 +21,9 @@ func TestClientReachViewVersion(t *testing.T) {
|
||||||
if err := client.Connect(); err != nil {
|
if err := client.Connect(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
version, err := client.ReachViewVersion()
|
defer cancel()
|
||||||
|
version, err := client.ReachViewVersion(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||||
|
@ -12,16 +13,30 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// RebootNow asks the ReachRS module to reboot now
|
// RebootNow asks the ReachRS module to reboot now
|
||||||
func (c *Client) RebootNow(waitDisconnect bool) error {
|
func (c *Client) RebootNow(ctx context.Context, waitDisconnect bool) error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
if waitDisconnect {
|
if waitDisconnect {
|
||||||
wg.Add(1)
|
|
||||||
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
|
var once sync.Once
|
||||||
|
|
||||||
|
done := func() {
|
||||||
c.Off(gosocketio.OnDisconnection)
|
c.Off(gosocketio.OnDisconnection)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
err = ctx.Err()
|
||||||
|
once.Do(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
|
||||||
|
once.Do(done)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
|
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||||
)
|
)
|
||||||
|
@ -24,7 +26,9 @@ func TestClientRebootNow(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := client.RebootNow(true); err != nil {
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := client.RebootNow(ctx, true); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
eventIsReceiverUpgradeAvailable = "is receiver upgrade available"
|
eventIsReceiverUpgradeAvailable = "is receiver upgrade available"
|
||||||
eventReceiverUpgradeAvailable = "receiver upgrade available"
|
eventReceiverUpgradeAvailable = "receiver upgrade available"
|
||||||
|
@ -11,9 +13,9 @@ 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() (bool, bool, error) {
|
func (c *Client) ReceiverUpgradeAvailable(ctx context.Context) (bool, bool, error) {
|
||||||
res := &receiverUpgreAvailable{}
|
res := &receiverUpgreAvailable{}
|
||||||
if err := c.ReqResp(eventIsReceiverUpgradeAvailable, nil, eventReceiverUpgradeAvailable, res); err != nil {
|
if err := c.ReqResp(ctx, eventIsReceiverUpgradeAvailable, nil, eventReceiverUpgradeAvailable, res); err != nil {
|
||||||
return false, false, err
|
return false, false, err
|
||||||
}
|
}
|
||||||
c.Logf("receiver upgrade result: available: %v, running: %v", res.Available, res.Running)
|
c.Logf("receiver upgrade result: available: %v, running: %v", res.Available, res.Running)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||||
)
|
)
|
||||||
|
@ -20,7 +22,9 @@ func TestClientReceiverUpgradeAvailable(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err := client.ReceiverUpgradeAvailable()
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_, _, err := client.ReceiverUpgradeAvailable(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
eventGetTestResults = "get test results"
|
eventGetTestResults = "get test results"
|
||||||
eventTestResults = "test results"
|
eventTestResults = "test results"
|
||||||
|
@ -16,9 +18,9 @@ type TestResults struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestResults returns the ReachRS module tests results
|
// TestResults returns the ReachRS module tests results
|
||||||
func (c *Client) TestResults() (*TestResults, error) {
|
func (c *Client) TestResults(ctx context.Context) (*TestResults, error) {
|
||||||
res := &TestResults{}
|
res := &TestResults{}
|
||||||
if err := c.ReqResp(eventGetTestResults, nil, eventTestResults, res); err != nil {
|
if err := c.ReqResp(ctx, eventGetTestResults, nil, eventTestResults, res); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||||
)
|
)
|
||||||
|
@ -20,7 +22,9 @@ func TestClientTestResults(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
results, err := client.TestResults()
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
results, err := client.TestResults(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
eventGetTimeSyncStatus = "get time sync status"
|
eventGetTimeSyncStatus = "get time sync status"
|
||||||
eventTimeSyncResults = "time sync status"
|
eventTimeSyncResults = "time sync status"
|
||||||
|
@ -11,9 +13,9 @@ type timeSyncStatus struct {
|
||||||
|
|
||||||
// TimeSynced returns the ReachRS module time synchronization status.
|
// TimeSynced returns the ReachRS module time synchronization status.
|
||||||
// A true response means that the module has synchronized its clock.
|
// A true response means that the module has synchronized its clock.
|
||||||
func (c *Client) TimeSynced() (bool, error) {
|
func (c *Client) TimeSynced(ctx context.Context) (bool, error) {
|
||||||
res := &timeSyncStatus{}
|
res := &timeSyncStatus{}
|
||||||
if err := c.ReqResp(eventGetTimeSyncStatus, nil, eventTimeSyncResults, res); err != nil {
|
if err := c.ReqResp(ctx, eventGetTimeSyncStatus, nil, eventTimeSyncResults, res); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
c.Logf("time sync result: %v", res.Status)
|
c.Logf("time sync result: %v", res.Status)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||||
)
|
)
|
||||||
|
@ -20,7 +22,9 @@ func TestClientTimeSync(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := client.TimeSynced()
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_, err := client.TimeSynced(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
eventUpdate = "update"
|
eventUpdate = "update"
|
||||||
eventOPKGUpdateResult = "opkg update result"
|
eventOPKGUpdateResult = "opkg update result"
|
||||||
|
@ -13,9 +15,9 @@ type UpdateStatus struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update asks the ReachRS module to start an OPKG update
|
// Update asks the ReachRS module to start an OPKG update
|
||||||
func (c *Client) Update() (*UpdateStatus, error) {
|
func (c *Client) Update(ctx context.Context) (*UpdateStatus, error) {
|
||||||
res := &UpdateStatus{}
|
res := &UpdateStatus{}
|
||||||
if err := c.ReqResp(eventUpdate, nil, eventOPKGUpdateResult, res); err != nil {
|
if err := c.ReqResp(ctx, eventUpdate, nil, eventOPKGUpdateResult, res); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c.Logf(
|
c.Logf(
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||||
)
|
)
|
||||||
|
@ -20,7 +22,9 @@ func TestClientOPKGUpdate(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := client.Update()
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_, err := client.Update(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||||
|
@ -41,48 +42,62 @@ type WifiNetwork struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WifiNetworks returns the ReachRS module wifi networks
|
// WifiNetworks returns the ReachRS module wifi networks
|
||||||
func (c *Client) WifiNetworks() ([]WifiNetwork, error) {
|
func (c *Client) WifiNetworks(ctx context.Context) ([]WifiNetwork, error) {
|
||||||
res := make([]WifiNetwork, 0)
|
res := make([]WifiNetwork, 0)
|
||||||
if err := c.ReqResp(eventGetSavedWifiNetworks, nil, eventSavedWifiNetworkResults, &res); err != nil {
|
if err := c.ReqResp(ctx, eventGetSavedWifiNetworks, nil, eventSavedWifiNetworkResults, &res); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 (c *Client) AddWifiNetwork(ctx context.Context, ssid string, security WifiSecurity, password string) (bool, error) {
|
||||||
res := false
|
res := false
|
||||||
network := &WifiNetwork{
|
network := &WifiNetwork{
|
||||||
SSID: ssid,
|
SSID: ssid,
|
||||||
Security: security,
|
Security: security,
|
||||||
Password: password,
|
Password: password,
|
||||||
}
|
}
|
||||||
if err := c.ReqResp(eventAddWifiNetwork, network, eventAddWifiNetworkResults, &res); err != nil {
|
if err := c.ReqResp(ctx, eventAddWifiNetwork, network, eventAddWifiNetworkResults, &res); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 (c *Client) RemoveWifiNetwork(ctx context.Context, ssid string) (bool, error) {
|
||||||
res := false
|
res := false
|
||||||
if err := c.ReqResp(eventRemoveWifiNetwork, ssid, eventRemoveWifiNetworkResults, &res); err != nil {
|
if err := c.ReqResp(ctx, eventRemoveWifiNetwork, ssid, eventRemoveWifiNetworkResults, &res); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// JoinWifiNetwork asks the ReachRS module to join the given WiFi network
|
// JoinWifiNetwork asks the ReachRS module to join the given WiFi network
|
||||||
func (c *Client) JoinWifiNetwork(ssid string, waitDisconnect bool) error {
|
func (c *Client) JoinWifiNetwork(ctx context.Context, ssid string, waitDisconnect bool) error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
if waitDisconnect {
|
if waitDisconnect {
|
||||||
wg.Add(1)
|
|
||||||
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
|
var once sync.Once
|
||||||
|
|
||||||
|
done := func() {
|
||||||
c.Off(gosocketio.OnDisconnection)
|
c.Off(gosocketio.OnDisconnection)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
err = ctx.Err()
|
||||||
|
once.Do(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
|
||||||
|
once.Do(done)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
|
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -23,7 +24,9 @@ func TestClientSavedWiFiNetworks(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := client.WifiNetworks()
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_, err := client.WifiNetworks(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -48,7 +51,11 @@ func TestClientCRUDWiFiNetwork(t *testing.T) {
|
||||||
|
|
||||||
ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
|
ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
|
||||||
|
|
||||||
done, err := client.AddWifiNetwork(ssid, SecurityOpen, "")
|
ctx := context.Background()
|
||||||
|
|
||||||
|
addWifiContext, addWifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer addWifiCancel()
|
||||||
|
done, err := client.AddWifiNetwork(addWifiContext, ssid, SecurityOpen, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -57,7 +64,9 @@ func TestClientCRUDWiFiNetwork(t *testing.T) {
|
||||||
t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
networks, err := client.WifiNetworks()
|
wifiContext, wifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer wifiCancel()
|
||||||
|
networks, err := client.WifiNetworks(wifiContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -74,7 +83,9 @@ func TestClientCRUDWiFiNetwork(t *testing.T) {
|
||||||
t.Errorf("wifi network '%s' should exists", ssid)
|
t.Errorf("wifi network '%s' should exists", ssid)
|
||||||
}
|
}
|
||||||
|
|
||||||
done, err = client.RemoveWifiNetwork(ssid)
|
removeWifiContext, removeWifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer removeWifiCancel()
|
||||||
|
done, err = client.RemoveWifiNetwork(removeWifiContext, ssid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -107,7 +118,12 @@ func TestClientWifiNetworkJoin(t *testing.T) {
|
||||||
|
|
||||||
ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
|
ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
|
||||||
|
|
||||||
done, err := client.AddWifiNetwork(ssid, SecurityOpen, "")
|
ctx := context.Background()
|
||||||
|
|
||||||
|
addWifiContext, addWifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer addWifiCancel()
|
||||||
|
|
||||||
|
done, err := client.AddWifiNetwork(addWifiContext, ssid, SecurityOpen, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -116,7 +132,9 @@ func TestClientWifiNetworkJoin(t *testing.T) {
|
||||||
t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := client.JoinWifiNetwork(ssid, true); err != nil {
|
joinWifiContext, joinWifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer joinWifiCancel()
|
||||||
|
if err := client.JoinWifiNetwork(joinWifiContext, ssid, true); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||||
"forge.cadoles.com/Pyxis/orion/emlid/updater"
|
"forge.cadoles.com/Pyxis/orion/emlid/updater"
|
||||||
|
@ -78,8 +80,13 @@ func configureWifi() {
|
||||||
c := connect()
|
c := connect()
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
log.Println("checking module status")
|
log.Println("checking module status")
|
||||||
results, err := c.TestResults()
|
|
||||||
|
ctx, testResultsCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer testResultsCancel()
|
||||||
|
results, err := c.TestResults(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -92,7 +99,9 @@ func configureWifi() {
|
||||||
|
|
||||||
log.Printf("adding wifi network '%s'", ssid)
|
log.Printf("adding wifi network '%s'", ssid)
|
||||||
|
|
||||||
done, err := c.AddWifiNetwork(ssid, updater.WifiSecurity(security), password)
|
ctx, addWifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer addWifiCancel()
|
||||||
|
done, err := c.AddWifiNetwork(ctx, ssid, updater.WifiSecurity(security), password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -102,7 +111,9 @@ func configureWifi() {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("connecting module to wifi network")
|
log.Println("connecting module to wifi network")
|
||||||
if err := c.JoinWifiNetwork(ssid, true); err != nil {
|
ctx, joinWifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer joinWifiCancel()
|
||||||
|
if err := c.JoinWifiNetwork(ctx, ssid, true); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Printf("you can now switch to the wifi network and start phase '%s'", phaseUpdateThenReboot)
|
log.Printf("you can now switch to the wifi network and start phase '%s'", phaseUpdateThenReboot)
|
||||||
|
@ -114,22 +125,30 @@ func updateThenReboot() {
|
||||||
c := connect()
|
c := connect()
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
log.Println("checking time sync")
|
log.Println("checking time sync")
|
||||||
synced, err := c.TimeSynced()
|
ctx, timeSyncedCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer timeSyncedCancel()
|
||||||
|
synced, err := c.TimeSynced(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Printf("time synced ? %v", synced)
|
log.Printf("time synced ? %v", synced)
|
||||||
|
|
||||||
log.Println("checking reachview version")
|
log.Println("checking reachview version")
|
||||||
version, err := c.ReachViewVersion()
|
ctx, reachviewVersionCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer reachviewVersionCancel()
|
||||||
|
version, err := c.ReachViewVersion(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Printf("reachview version ? '%s'", version)
|
log.Printf("reachview version ? '%s'", version)
|
||||||
|
|
||||||
log.Println("checking for update")
|
log.Println("checking for update")
|
||||||
status, err := c.Update()
|
ctx, updateCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer updateCancel()
|
||||||
|
status, err := c.Update(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -143,7 +162,9 @@ func updateThenReboot() {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("rebooting device")
|
log.Println("rebooting device")
|
||||||
if err := c.RebootNow(true); err != nil {
|
ctx, rebootCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer rebootCancel()
|
||||||
|
if err := c.RebootNow(ctx, true); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue