package reach 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 ( // SecurityWPAPSK WPA-PSK wifi network SecurityWPAPSK WifiSecurity = "wpa-psk" // SecurityOpen Open wifi network SecurityOpen WifiSecurity = "open" ) // WifiNetwork is a ReachRS module's saved wifi network // Raw messages examples: // [{"ssid":"Cadoles","password":"...","security":"wpa-psk","identity":""}] // [{"is_visible":false,"connected":false,"ssid":"Cadoles","security":"wpa-psk","is_connected":false,"is_added":true}]] 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"` } // SavedWifiNetworks returns the ReachRS module saved wifi networks func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) { var err error var wifiNetworks []*WifiNetwork var wg sync.WaitGroup wg.Add(1) err = c.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) { wifiNetworks = wn c.conn.Off(eventSavedWifiNetworkResults) wg.Done() }) if err != nil { return nil, errors.Wrapf(err, "error while binding to '%s' event", eventSavedWifiNetworkResults) } c.logf("sending '%s' event", eventGetSavedWifiNetworks) if err = c.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil { return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetSavedWifiNetworks) } c.logf("'%s' event sent", eventGetSavedWifiNetworks) wg.Wait() return wifiNetworks, err } // AddWifiNetwork asks the ReachRS module to save the given wifi network informations func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password string) (bool, error) { var err error var wg sync.WaitGroup var done bool wg.Add(1) err = c.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) { done = d c.conn.Off(eventAddWifiNetworkResults) wg.Done() }) if err != nil { return false, errors.Wrapf(err, "error while binding to '%s' event", eventAddWifiNetworkResults) } wn := &WifiNetwork{ SSID: ssid, Security: security, Password: password, } c.logf("sending '%s' event", eventAddWifiNetwork) if err = c.conn.Emit(eventAddWifiNetwork, wn); err != nil { return false, errors.Wrapf(err, "error while emitting '%s' event", eventAddWifiNetwork) } c.logf("'%s' event sent", eventAddWifiNetwork) wg.Wait() return done, err } // RemoveWifiNetwork asks the ReachRS module to remove the given WiFi network func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) { var err error var wg sync.WaitGroup var done bool wg.Add(1) err = c.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) { done = d c.conn.Off(eventRemoveWifiNetworkResults) wg.Done() }) if err != nil { return false, errors.Wrapf(err, "error while binding to '%s' event", eventRemoveWifiNetworkResults) } c.logf("sending '%s' event", eventRemoveWifiNetwork) if err := c.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil { return false, errors.Wrapf(err, "error while emitting '%s' event", eventRemoveWifiNetwork) } c.logf("'%s' event sent", eventRemoveWifiNetwork) wg.Wait() return done, nil } // ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network func (c *Client) ConnectToWifiNetwork(ssid string) error { c.logf("sending '%s' event", eventConnectToNetwork) if err := c.conn.Emit(eventConnectToNetwork, ssid); err != nil { return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork) } c.logf("'%s' event sent", eventConnectToNetwork) return nil }