diff --git a/emlid/reachview/reboot.go b/emlid/reachview/reboot.go new file mode 100644 index 0000000..1a755d2 --- /dev/null +++ b/emlid/reachview/reboot.go @@ -0,0 +1,56 @@ +package reachview + +import ( + "context" + "sync" + + "forge.cadoles.com/Pyxis/golang-socketio" + "github.com/pkg/errors" +) + +const ( + eventReboot = "reboot" +) + +// Reboot asks the ReachRS module to reboot +func (c *Client) Reboot(ctx context.Context, waitDisconnect bool) error { + + var err error + var wg sync.WaitGroup + + if waitDisconnect { + + var once sync.Once + + done := func() { + c.Off(gosocketio.OnDisconnection) + 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 { + 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 + +} diff --git a/emlid/reachview/reboot_test.go b/emlid/reachview/reboot_test.go new file mode 100644 index 0000000..47c18d2 --- /dev/null +++ b/emlid/reachview/reboot_test.go @@ -0,0 +1,33 @@ +package reachview + +import ( + "context" + "testing" + "time" + + "forge.cadoles.com/Pyxis/orion/emlid" +) + +func TestClientReboot(t *testing.T) { + + if !*runRebootTest { + t.Skip("To run this test, use: go test -reboot-test") + } + + client := NewClient( + emlid.WithStandardLogger(), + emlid.WithEndpoint(*reachHost, 80), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := client.Reboot(ctx, true); err != nil { + t.Error(err) + } + + defer client.Close() + +} diff --git a/emlid/reachview/util_test.go b/emlid/reachview/util_test.go index 1dfa823..8fa0a25 100644 --- a/emlid/reachview/util_test.go +++ b/emlid/reachview/util_test.go @@ -7,6 +7,11 @@ var runReachViewIntegrationTests = flag.Bool( "Run the 'ReachView' integration tests (in addition to the unit tests)", ) +var runRebootTest = flag.Bool( + "reboot-test", false, + "Run the updater 'Reboot' 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", diff --git a/emlid/reboot_now.go b/emlid/updater/reboot_now.go similarity index 98% rename from emlid/reboot_now.go rename to emlid/updater/reboot_now.go index b9bf72f..de09855 100644 --- a/emlid/reboot_now.go +++ b/emlid/updater/reboot_now.go @@ -1,4 +1,4 @@ -package emlid +package updater import ( "context" diff --git a/emlid/reboot_now_test.go b/emlid/updater/reboot_now_test.go similarity index 77% rename from emlid/reboot_now_test.go rename to emlid/updater/reboot_now_test.go index cff66a7..610049d 100644 --- a/emlid/reboot_now_test.go +++ b/emlid/updater/reboot_now_test.go @@ -1,9 +1,11 @@ -package emlid +package updater import ( "context" "testing" "time" + + "forge.cadoles.com/Pyxis/orion/emlid" ) func TestClientRebootNow(t *testing.T) { @@ -13,8 +15,8 @@ func TestClientRebootNow(t *testing.T) { } client := NewClient( - WithStandardLogger(), - WithEndpoint(*reachHost, 80), + emlid.WithStandardLogger(), + emlid.WithEndpoint(*reachHost, 80), ) if err := client.Connect(); err != nil { t.Fatal(err) diff --git a/emlid/updater/util_test.go b/emlid/updater/util_test.go index 7bb5361..b95651e 100644 --- a/emlid/updater/util_test.go +++ b/emlid/updater/util_test.go @@ -12,6 +12,11 @@ var runJoinNetworkTest = flag.Bool( "Run the updater 'JoinWiFiNetwork' test (in addition to the unit tests)", ) +var runRebootTest = flag.Bool( + "reboot-test", false, + "Run the updater 'Reboot' 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", diff --git a/emlid/util_test.go b/emlid/util_test.go index d94176b..a21217f 100644 --- a/emlid/util_test.go +++ b/emlid/util_test.go @@ -7,11 +7,6 @@ var runDiscoveryIntegrationTests = flag.Bool( "Run the 'Discovery' integration tests (in addition to the unit tests)", ) -var runRebootTest = flag.Bool( - "reboot-test", false, - "Run the updater 'Reboot' 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", diff --git a/go.mod b/go.mod index 4989193..46d2b42 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ require ( forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 github.com/caarlos0/env v3.4.0+incompatible github.com/cenkalti/backoff v2.0.0+incompatible // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/davecgh/go-spew v1.1.1 github.com/go-chi/chi v3.3.3+incompatible github.com/gorilla/websocket v1.4.0 // indirect github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1