Refactor reach package
- Rename reach package to emlid - Create generic Reach websocket client - Add 2 new subpackages 'updater' and 'reachview' to provides specific API
This commit is contained in:
17
emlid/browser_connected.go
Normal file
17
emlid/browser_connected.go
Normal file
@ -0,0 +1,17 @@
|
||||
package emlid
|
||||
|
||||
const (
|
||||
// EventBrowserConnected is emitted after the initial connection to the
|
||||
// ReachView endpoint
|
||||
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 {
|
||||
payload := map[string]string{
|
||||
"data": "I'm connected",
|
||||
}
|
||||
return c.Emit(eventBrowserConnected, payload)
|
||||
}
|
144
emlid/client.go
Normal file
144
emlid/client.go
Normal file
@ -0,0 +1,144 @@
|
||||
package emlid
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||
"forge.cadoles.com/Pyxis/golang-socketio/transport"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Client is a ReachRS websocket API client
|
||||
type Client struct {
|
||||
opts *Options
|
||||
conn *gosocketio.Client
|
||||
}
|
||||
|
||||
// EventHandler is an event handler
|
||||
type EventHandler func(data interface{})
|
||||
|
||||
// Connect connects the client to the ReachView endpoint
|
||||
// This method is not safe to call by different coroutines
|
||||
func (c *Client) Connect() error {
|
||||
|
||||
var err error
|
||||
var wg sync.WaitGroup
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
transport := &transport.WebsocketTransport{
|
||||
PingInterval: c.opts.PingInterval,
|
||||
PingTimeout: c.opts.PingTimeout,
|
||||
ReceiveTimeout: c.opts.ReceiveTimeout,
|
||||
SendTimeout: c.opts.SendTimeout,
|
||||
BufferSize: c.opts.BufferSize,
|
||||
}
|
||||
|
||||
c.Logf("connecting to '%s'", c.opts.Endpoint)
|
||||
conn, err := gosocketio.Dial(c.opts.Endpoint, transport)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error while connecting to endpoint")
|
||||
}
|
||||
|
||||
c.conn = conn
|
||||
|
||||
err = conn.On(gosocketio.OnConnection, func(h *gosocketio.Channel) {
|
||||
conn.Off(gosocketio.OnError)
|
||||
c.Logf("connected with sid '%s'", h.Id())
|
||||
err = c.sendBrowserConnected()
|
||||
wg.Done()
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error while attaching to connection event")
|
||||
}
|
||||
|
||||
err = conn.On(gosocketio.OnError, func(h *gosocketio.Channel) {
|
||||
c.Logf("error")
|
||||
err = errors.Errorf("an unknown error occured")
|
||||
c.conn = nil
|
||||
wg.Done()
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error while attaching to error event")
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Close closes the current connection to the ReachView endpoint
|
||||
func (c *Client) Close() {
|
||||
if c.conn == nil {
|
||||
return
|
||||
}
|
||||
c.conn.Close()
|
||||
c.conn = nil
|
||||
}
|
||||
|
||||
// Emit a new event with the given data
|
||||
func (c *Client) Emit(event string, data interface{}) error {
|
||||
c.Logf("emit '%s' event", event)
|
||||
if err := c.conn.Emit(event, data); err != nil {
|
||||
return errors.Wrapf(err, "error while emitting '%s' event", event)
|
||||
}
|
||||
c.Logf("'%s' event sent", event)
|
||||
return nil
|
||||
}
|
||||
|
||||
// On binds and event handler to the given event
|
||||
func (c *Client) On(event string, handler interface{}) error {
|
||||
return c.conn.On(event, handler)
|
||||
}
|
||||
|
||||
// Off remove the handler bound to the given event
|
||||
func (c *Client) Off(event string) {
|
||||
c.conn.Off(event)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
||||
var err error
|
||||
var wg sync.WaitGroup
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
err = c.conn.On(responseEvent, func(_ *gosocketio.Channel, data interface{}) {
|
||||
err = mapstructure.Decode(data, res)
|
||||
c.conn.Off(responseEvent)
|
||||
wg.Done()
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error while binding to '%s' event", responseEvent)
|
||||
}
|
||||
|
||||
if err := c.Emit(requestEvent, requestData); err != nil {
|
||||
return errors.Wrapf(err, "error while emitting event '%s'", requestEvent)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
// Logf logs the given message with the configured logger
|
||||
func (c *Client) Logf(format string, args ...interface{}) {
|
||||
if c.opts.Logger == nil {
|
||||
return
|
||||
}
|
||||
c.opts.Logger.Printf(format, args...)
|
||||
}
|
||||
|
||||
// NewClient returns a new ReachRS websocket client
|
||||
func NewClient(opts ...OptionFunc) *Client {
|
||||
options := DefaultOptions()
|
||||
for _, o := range opts {
|
||||
o(options)
|
||||
}
|
||||
return &Client{
|
||||
opts: options,
|
||||
}
|
||||
}
|
102
emlid/option.go
Normal file
102
emlid/option.go
Normal file
@ -0,0 +1,102 @@
|
||||
package emlid
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||
)
|
||||
|
||||
// Logger is a logger implementation for the ReachRS client
|
||||
type Logger interface {
|
||||
Printf(format string, args ...interface{})
|
||||
}
|
||||
|
||||
// Options are ReachRS client configuration options
|
||||
type Options struct {
|
||||
Endpoint string
|
||||
PingInterval time.Duration
|
||||
PingTimeout time.Duration
|
||||
ReceiveTimeout time.Duration
|
||||
SendTimeout time.Duration
|
||||
BufferSize int
|
||||
Logger Logger
|
||||
}
|
||||
|
||||
// OptionFunc is an option configuration for the ReachRS client
|
||||
type OptionFunc func(opts *Options)
|
||||
|
||||
// WithEndpoint configures the client to target the given host and port
|
||||
func WithEndpoint(host string, port int) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.Endpoint = gosocketio.GetUrl(host, port, false)
|
||||
}
|
||||
}
|
||||
|
||||
// WithPingInterval configures the client to use the given ping interval
|
||||
func WithPingInterval(interval time.Duration) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.PingInterval = interval
|
||||
}
|
||||
}
|
||||
|
||||
// WithPingTimeout configures the client to use the given ping timeout
|
||||
func WithPingTimeout(timeout time.Duration) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.PingTimeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithReceiveTimeout configures the client to use the given receive timeout
|
||||
func WithReceiveTimeout(timeout time.Duration) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.ReceiveTimeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithSendTimeout configures the client to use the given send timeout
|
||||
func WithSendTimeout(timeout time.Duration) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.SendTimeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithBufferSize configures the client to use the given buffer size
|
||||
func WithBufferSize(size int) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.BufferSize = size
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogger configures the client to use the given logger
|
||||
func WithLogger(logger Logger) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.Logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
// WithStandardLogger configures the client to use the given standard logger
|
||||
func WithStandardLogger() OptionFunc {
|
||||
return func(opts *Options) {
|
||||
logger := log.New(os.Stdout, "[reachrs] ", log.LstdFlags)
|
||||
opts.Logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultOptions returns the default values for the client options
|
||||
func DefaultOptions() *Options {
|
||||
opts := &Options{}
|
||||
defaults := []OptionFunc{
|
||||
WithEndpoint("192.168.42.1", 80),
|
||||
WithPingInterval(30 * time.Second),
|
||||
WithPingTimeout(60 * time.Second),
|
||||
WithReceiveTimeout(60 * time.Second),
|
||||
WithSendTimeout(60 * time.Second),
|
||||
WithBufferSize(1024 * 32),
|
||||
}
|
||||
for _, o := range defaults {
|
||||
o(opts)
|
||||
}
|
||||
return opts
|
||||
}
|
4
emlid/reach.go
Normal file
4
emlid/reach.go
Normal file
@ -0,0 +1,4 @@
|
||||
// Package emlid is a package to configure EMLID ReachRS modules in Go.
|
||||
//
|
||||
// It aims to provide a simple interface to common ReachRS modules management operations.
|
||||
package emlid
|
14
emlid/reachview/client.go
Normal file
14
emlid/reachview/client.go
Normal file
@ -0,0 +1,14 @@
|
||||
package reachview
|
||||
|
||||
import "forge.cadoles.com/Pyxis/orion/emlid"
|
||||
|
||||
// Client is a ReachRS Updater client
|
||||
type Client struct {
|
||||
*emlid.Client
|
||||
}
|
||||
|
||||
// NewClient returns a new ReachRS ReachView client
|
||||
func NewClient(opts ...emlid.OptionFunc) *Client {
|
||||
client := emlid.NewClient(opts...)
|
||||
return &Client{client}
|
||||
}
|
44
emlid/reachview/configuration.go
Normal file
44
emlid/reachview/configuration.go
Normal file
@ -0,0 +1,44 @@
|
||||
package reachview
|
||||
|
||||
const (
|
||||
eventGetConfiguration = "get configuration"
|
||||
eventCurrentConfiguration = "current configuration"
|
||||
)
|
||||
|
||||
// Configuration is a configuration
|
||||
type Configuration struct {
|
||||
RTKSettings *RTKSettings `mapstructure:"rtk settings,omitempty"`
|
||||
}
|
||||
|
||||
// RTKSettings -
|
||||
type RTKSettings struct {
|
||||
GLONASSARMode string `mapstructure:"glonass ar mode"`
|
||||
UpdateRate string `mapstructure:"update rate"`
|
||||
ElevationMaskAngle string `mapstructure:"elevation mask angle"`
|
||||
MaxHorizontalAcceleration string `mapstructure:"max horizontal acceleration"`
|
||||
SNRMask string `mapstructure:"snr mask"`
|
||||
GPSARMode string `mapstructure:"gps ar mode"`
|
||||
PositionningMode string `mapstructure:"positioning mode"`
|
||||
PositioningSystems *PositionningSystems `mapstructure:"positioning systems,omitempty"`
|
||||
MaxVerticalAcceleration string `mapstructure:"max vertical acceleration"`
|
||||
}
|
||||
|
||||
// PositionningSystems -
|
||||
type PositionningSystems struct {
|
||||
GLONASS bool `json:"glonass"`
|
||||
SBAS bool `json:"sbas"`
|
||||
QZS bool `json:"qzs"`
|
||||
QZSS bool `json:"qzss"`
|
||||
Compass bool `json:"compass"`
|
||||
Galileo bool `json:"galileo"`
|
||||
GPS bool `json:"gps"`
|
||||
}
|
||||
|
||||
// Configuration fetches and return the current configuration of the ReachRS module
|
||||
func (r *Client) Configuration() (*Configuration, error) {
|
||||
configuration := &Configuration{}
|
||||
if err := r.ReqResp(eventGetConfiguration, nil, eventCurrentConfiguration, configuration); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return configuration, nil
|
||||
}
|
38
emlid/reachview/configuration_test.go
Normal file
38
emlid/reachview/configuration_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
package reachview
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestReachViewConfiguration(t *testing.T) {
|
||||
|
||||
if !*runReachViewIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -reachview-integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
config, err := client.Configuration()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if config == nil {
|
||||
t.Fatal("config should not be nil")
|
||||
}
|
||||
|
||||
if config.RTKSettings == nil {
|
||||
t.Fatal("config.RTKSettings should not be nil")
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
13
emlid/reachview/test.go
Normal file
13
emlid/reachview/test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package reachview
|
||||
|
||||
import "flag"
|
||||
|
||||
var runReachViewIntegrationTests = flag.Bool(
|
||||
"reachview-integration", false,
|
||||
"Run the 'ReachView' integration tests (in addition to the unit tests)",
|
||||
)
|
||||
|
||||
var reachHost = flag.String(
|
||||
"reach-host", "192.168.42.1",
|
||||
"The Reach module host to use in integration tests",
|
||||
)
|
15
emlid/updater/client.go
Normal file
15
emlid/updater/client.go
Normal file
@ -0,0 +1,15 @@
|
||||
// Package updater provides an API to communicate with the ReachRS modules "Updater" application
|
||||
package updater
|
||||
|
||||
import "forge.cadoles.com/Pyxis/orion/emlid"
|
||||
|
||||
// Client is a ReachRS Updater client
|
||||
type Client struct {
|
||||
*emlid.Client
|
||||
}
|
||||
|
||||
// NewClient returns a new ReachRS Updater client
|
||||
func NewClient(opts ...emlid.OptionFunc) *Client {
|
||||
client := emlid.NewClient(opts...)
|
||||
return &Client{client}
|
||||
}
|
32
emlid/updater/example_test.go
Normal file
32
emlid/updater/example_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func Example_usage() {
|
||||
// Create a new Updater client instance
|
||||
updater := NewClient(
|
||||
emlid.WithEndpoint("192.168.42.1", 80), // Define the module endpoint
|
||||
)
|
||||
|
||||
// Connect to the ReachRS Updater endpoint
|
||||
if err := updater.Connect(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
networks, err := updater.WifiNetworks()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Do something with network
|
||||
for _, n := range networks {
|
||||
log.Printf("Save WiFi network: SSID: '%s', Security: '%s'", n.SSID, n.Security)
|
||||
}
|
||||
|
||||
// Dont forget to close the connection when you are done
|
||||
defer updater.Close()
|
||||
}
|
19
emlid/updater/reachview_version.go
Normal file
19
emlid/updater/reachview_version.go
Normal file
@ -0,0 +1,19 @@
|
||||
package updater
|
||||
|
||||
const (
|
||||
eventGetReachViewVersion = "get reachview version"
|
||||
eventReachViewVersionResults = "current reachview version"
|
||||
)
|
||||
|
||||
type reachViewVersion struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// ReachViewVersion returns the ReachRS module ReachView version
|
||||
func (c *Client) ReachViewVersion() (string, error) {
|
||||
res := &reachViewVersion{}
|
||||
if err := c.ReqResp(eventGetReachViewVersion, nil, eventReachViewVersionResults, res); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return res.Version, nil
|
||||
}
|
34
emlid/updater/reachview_version_test.go
Normal file
34
emlid/updater/reachview_version_test.go
Normal file
@ -0,0 +1,34 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientReachViewVersion(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
version, err := client.ReachViewVersion()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if version == "" {
|
||||
t.Error("version should not be empty")
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
41
emlid/updater/reboot_now.go
Normal file
41
emlid/updater/reboot_now.go
Normal file
@ -0,0 +1,41 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
eventReboot = "reboot now"
|
||||
)
|
||||
|
||||
// RebootNow asks the ReachRS module to reboot now
|
||||
func (c *Client) RebootNow(waitDisconnect bool) error {
|
||||
|
||||
var err error
|
||||
var wg sync.WaitGroup
|
||||
|
||||
if waitDisconnect {
|
||||
wg.Add(1)
|
||||
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
|
||||
c.Off(gosocketio.OnDisconnection)
|
||||
wg.Done()
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
|
||||
}
|
||||
}
|
||||
|
||||
if err = c.Emit(eventReboot, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if waitDisconnect {
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
}
|
33
emlid/updater/reboot_now_test.go
Normal file
33
emlid/updater/reboot_now_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientRebootNow(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
if !*runRebootTest {
|
||||
t.Skip("To run this test, use: go test -updater-reboot-test")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := client.RebootNow(true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
21
emlid/updater/receiver_upgrade.go
Normal file
21
emlid/updater/receiver_upgrade.go
Normal file
@ -0,0 +1,21 @@
|
||||
package updater
|
||||
|
||||
const (
|
||||
eventIsReceiverUpgradeAvailable = "is receiver upgrade available"
|
||||
eventReceiverUpgradeAvailable = "receiver upgrade available"
|
||||
)
|
||||
|
||||
type receiverUpgreAvailable struct {
|
||||
Available bool `json:"available"`
|
||||
Running bool `json:"running"`
|
||||
}
|
||||
|
||||
// ReceiverUpgradeAvailable checks if an upgrade is avaialable/running for the ReachRS module
|
||||
func (c *Client) ReceiverUpgradeAvailable() (bool, bool, error) {
|
||||
res := &receiverUpgreAvailable{}
|
||||
if err := c.ReqResp(eventIsReceiverUpgradeAvailable, nil, eventReceiverUpgradeAvailable, res); err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
c.Logf("receiver upgrade result: available: %v, running: %v", res.Available, res.Running)
|
||||
return res.Available, res.Running, nil
|
||||
}
|
30
emlid/updater/receiver_upgrade_test.go
Normal file
30
emlid/updater/receiver_upgrade_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientReceiverUpgradeAvailable(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, _, err := client.ReceiverUpgradeAvailable()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
23
emlid/updater/test.go
Normal file
23
emlid/updater/test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package updater
|
||||
|
||||
import "flag"
|
||||
|
||||
var runUpdaterIntegrationTests = flag.Bool(
|
||||
"updater-integration", false,
|
||||
"Run the 'Updater' integration tests (in addition to the unit tests)",
|
||||
)
|
||||
|
||||
var runRebootTest = flag.Bool(
|
||||
"updater-reboot-test", false,
|
||||
"Run the updater 'Reboot' test (in addition to the unit tests)",
|
||||
)
|
||||
|
||||
var runJoinNetworkTest = flag.Bool(
|
||||
"updater-join-network-test", false,
|
||||
"Run the updater 'JoinWiFiNetwork' test (in addition to the unit tests)",
|
||||
)
|
||||
|
||||
var reachHost = flag.String(
|
||||
"reach-host", "192.168.42.1",
|
||||
"The Reach module host to use in integration tests",
|
||||
)
|
25
emlid/updater/test_results.go
Normal file
25
emlid/updater/test_results.go
Normal file
@ -0,0 +1,25 @@
|
||||
package updater
|
||||
|
||||
const (
|
||||
eventGetTestResults = "get test results"
|
||||
eventTestResults = "test results"
|
||||
)
|
||||
|
||||
// TestResults are the ReachRS module's test results
|
||||
//
|
||||
type TestResults struct {
|
||||
Device string `json:"device"`
|
||||
Lora bool `json:"lora"`
|
||||
MPU bool `json:"mpu"`
|
||||
STC bool `json:"stc"`
|
||||
UBlox bool `json:"u-blox"`
|
||||
}
|
||||
|
||||
// TestResults returns the ReachRS module tests results
|
||||
func (c *Client) TestResults() (*TestResults, error) {
|
||||
res := &TestResults{}
|
||||
if err := c.ReqResp(eventGetTestResults, nil, eventTestResults, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
34
emlid/updater/test_results_test.go
Normal file
34
emlid/updater/test_results_test.go
Normal file
@ -0,0 +1,34 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientTestResults(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
results, err := client.TestResults()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if g, e := results.Device, "ReachRS"; g != e {
|
||||
t.Errorf("results.Device: got '%s', expected '%s'", g, e)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
21
emlid/updater/time_sync.go
Normal file
21
emlid/updater/time_sync.go
Normal file
@ -0,0 +1,21 @@
|
||||
package updater
|
||||
|
||||
const (
|
||||
eventGetTimeSyncStatus = "get time sync status"
|
||||
eventTimeSyncResults = "time sync status"
|
||||
)
|
||||
|
||||
type timeSyncStatus struct {
|
||||
Status bool `json:"status"`
|
||||
}
|
||||
|
||||
// TimeSynced returns the ReachRS module time synchronization status.
|
||||
// A true response means that the module has synchronized its clock.
|
||||
func (c *Client) TimeSynced() (bool, error) {
|
||||
res := &timeSyncStatus{}
|
||||
if err := c.ReqResp(eventGetTimeSyncStatus, nil, eventTimeSyncResults, res); err != nil {
|
||||
return false, err
|
||||
}
|
||||
c.Logf("time sync result: %v", res.Status)
|
||||
return res.Status, nil
|
||||
}
|
30
emlid/updater/time_sync_test.go
Normal file
30
emlid/updater/time_sync_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientTimeSync(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err := client.TimeSynced()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
26
emlid/updater/update.go
Normal file
26
emlid/updater/update.go
Normal file
@ -0,0 +1,26 @@
|
||||
package updater
|
||||
|
||||
const (
|
||||
eventUpdate = "update"
|
||||
eventOPKGUpdateResult = "opkg update result"
|
||||
)
|
||||
|
||||
// UpdateStatus embeds informations about update status
|
||||
type UpdateStatus struct {
|
||||
Active bool `json:"active"`
|
||||
Locked bool `json:"locked"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
// Update asks the ReachRS module to start an OPKG update
|
||||
func (c *Client) Update() (*UpdateStatus, error) {
|
||||
res := &UpdateStatus{}
|
||||
if err := c.ReqResp(eventUpdate, nil, eventOPKGUpdateResult, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Logf(
|
||||
"opkg update result: active: %v, state: %v, locked: %v",
|
||||
res.Active, res.State, res.Locked,
|
||||
)
|
||||
return res, nil
|
||||
}
|
30
emlid/updater/update_test.go
Normal file
30
emlid/updater/update_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientOPKGUpdate(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err := client.Update()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
101
emlid/updater/wifi_networks.go
Normal file
101
emlid/updater/wifi_networks.go
Normal file
@ -0,0 +1,101 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
eventGetSavedWifiNetworks = "get saved wifi networks"
|
||||
eventSavedWifiNetworkResults = "wifi saved networks results"
|
||||
eventAddWifiNetwork = "add new network"
|
||||
eventAddWifiNetworkResults = "add network results"
|
||||
eventRemoveWifiNetwork = "remove network"
|
||||
eventRemoveWifiNetworkResults = "remove network results"
|
||||
eventConnectToNetwork = "connect to network"
|
||||
)
|
||||
|
||||
// WifiSecurity is a WiFi network security algorithm
|
||||
type WifiSecurity string
|
||||
|
||||
const (
|
||||
// SecurityWEP WEP wifi network
|
||||
SecurityWEP WifiSecurity = "wep"
|
||||
// SecurityWPAPSK WPA(2)-PSK wifi network
|
||||
SecurityWPAPSK WifiSecurity = "wpa-psk"
|
||||
// SecurityOpen Open wifi network
|
||||
SecurityOpen WifiSecurity = "open"
|
||||
)
|
||||
|
||||
// WifiNetwork is a ReachRS module wifi network
|
||||
type WifiNetwork struct {
|
||||
SSID string `json:"ssid"`
|
||||
Password string `json:"password"`
|
||||
Security WifiSecurity `json:"security"`
|
||||
Identity string `json:"identity"`
|
||||
Visible bool `json:"is_visible"`
|
||||
Connected bool `json:"is_connected"`
|
||||
Added bool `json:"is_added"`
|
||||
}
|
||||
|
||||
// WifiNetworks returns the ReachRS module wifi networks
|
||||
func (c *Client) WifiNetworks() ([]WifiNetwork, error) {
|
||||
res := make([]WifiNetwork, 0)
|
||||
if err := c.ReqResp(eventGetSavedWifiNetworks, nil, eventSavedWifiNetworkResults, &res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// AddWifiNetwork asks the ReachRS module to save the given wifi network informations
|
||||
func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password string) (bool, error) {
|
||||
res := false
|
||||
network := &WifiNetwork{
|
||||
SSID: ssid,
|
||||
Security: security,
|
||||
Password: password,
|
||||
}
|
||||
if err := c.ReqResp(eventAddWifiNetwork, network, eventAddWifiNetworkResults, &res); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// RemoveWifiNetwork asks the ReachRS module to remove the given WiFi network
|
||||
func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) {
|
||||
res := false
|
||||
if err := c.ReqResp(eventRemoveWifiNetwork, ssid, eventRemoveWifiNetworkResults, &res); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// JoinWifiNetwork asks the ReachRS module to join the given WiFi network
|
||||
func (c *Client) JoinWifiNetwork(ssid string, waitDisconnect bool) error {
|
||||
|
||||
var err error
|
||||
var wg sync.WaitGroup
|
||||
|
||||
if waitDisconnect {
|
||||
wg.Add(1)
|
||||
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
|
||||
c.Off(gosocketio.OnDisconnection)
|
||||
wg.Done()
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
|
||||
}
|
||||
}
|
||||
|
||||
if err := c.Emit(eventConnectToNetwork, ssid); err != nil {
|
||||
return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork)
|
||||
}
|
||||
|
||||
if waitDisconnect {
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
127
emlid/updater/wifi_networks_test.go
Normal file
127
emlid/updater/wifi_networks_test.go
Normal file
@ -0,0 +1,127 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientSavedWiFiNetworks(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err := client.WifiNetworks()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
||||
|
||||
func TestClientCRUDWiFiNetwork(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
|
||||
|
||||
done, err := client.AddWifiNetwork(ssid, SecurityOpen, "")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if g, e := done, true; g != e {
|
||||
t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
||||
}
|
||||
|
||||
networks, err := client.WifiNetworks()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, n := range networks {
|
||||
if n.SSID == ssid {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if g, e := found, true; g != e {
|
||||
t.Errorf("wifi network '%s' should exists", ssid)
|
||||
}
|
||||
|
||||
done, err = client.RemoveWifiNetwork(ssid)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if g, e := done, true; g != e {
|
||||
t.Errorf("RemoveWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
||||
|
||||
func TestClientWifiNetworkJoin(t *testing.T) {
|
||||
|
||||
if !*runUpdaterIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -updater-integration")
|
||||
}
|
||||
|
||||
if !*runJoinNetworkTest {
|
||||
t.Skip("To run this test, use: go test -updater-join-network-test")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
|
||||
|
||||
done, err := client.AddWifiNetwork(ssid, SecurityOpen, "")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if g, e := done, true; g != e {
|
||||
t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
||||
}
|
||||
|
||||
if err := client.JoinWifiNetwork(ssid, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
Reference in New Issue
Block a user