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 }