diff --git a/reach/wifi_networks.go b/reach/wifi_networks.go index 2abaecf..9c666c0 100644 --- a/reach/wifi_networks.go +++ b/reach/wifi_networks.go @@ -138,11 +138,35 @@ func (u *Updater) RemoveWifiNetwork(ssid string) (bool, error) { } // ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network -func (u *Updater) ConnectToWifiNetwork(ssid string) error { +func (u *Updater) ConnectToWifiNetwork(ssid string, waitDisconnect bool) error { + + var err error + var wg sync.WaitGroup + + if waitDisconnect { + + wg.Add(1) + + err = u.conn.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) { + u.conn.Off(gosocketio.OnDisconnection) + wg.Done() + }) + + if err != nil { + return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection) + } + + } + 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) } u.logf("'%s' event sent", eventConnectToNetwork) + + if waitDisconnect { + wg.Wait() + } + return nil } diff --git a/reach/wifi_networks_test.go b/reach/wifi_networks_test.go index e7d42e1..d18273c 100644 --- a/reach/wifi_networks_test.go +++ b/reach/wifi_networks_test.go @@ -4,6 +4,7 @@ import ( "fmt" "math/rand" "testing" + "time" ) func TestClientSavedWiFiNetworks(t *testing.T) { @@ -83,3 +84,38 @@ func TestClientCRUDWiFiNetwork(t *testing.T) { defer client.Close() } + +func TestClientWifiNetworkConnect(t *testing.T) { + + if !*runUpdaterIntegrationTests { + t.Skip("To run this test, use: go test -updater-integration") + } + + client := NewUpdaterClient( + WithStandardLogger(), + 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.ConnectToWifiNetwork(ssid, true); err != nil { + t.Error(err) + } + + time.Sleep(5 * time.Second) + + defer client.Close() + +}