173 lines
4.4 KiB
Go
173 lines
4.4 KiB
Go
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 (
|
|
// 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's saved wifi network
|
|
// Raw messages examples:
|
|
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 (u *Updater) SavedWifiNetworks() ([]*WifiNetwork, error) {
|
|
|
|
var err error
|
|
var wifiNetworks []*WifiNetwork
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
err = u.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) {
|
|
wifiNetworks = wn
|
|
u.conn.Off(eventSavedWifiNetworkResults)
|
|
wg.Done()
|
|
})
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventSavedWifiNetworkResults)
|
|
}
|
|
|
|
u.logf("sending '%s' event", eventGetSavedWifiNetworks)
|
|
if err = u.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil {
|
|
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetSavedWifiNetworks)
|
|
}
|
|
u.logf("'%s' event sent", eventGetSavedWifiNetworks)
|
|
|
|
wg.Wait()
|
|
|
|
return wifiNetworks, err
|
|
|
|
}
|
|
|
|
// AddWifiNetwork asks the ReachRS module to save the given wifi network informations
|
|
func (u *Updater) AddWifiNetwork(ssid string, security WifiSecurity, password string) (bool, error) {
|
|
|
|
var err error
|
|
var wg sync.WaitGroup
|
|
var done bool
|
|
|
|
wg.Add(1)
|
|
|
|
err = u.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
|
|
done = d
|
|
u.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,
|
|
}
|
|
|
|
u.logf("sending '%s' event", eventAddWifiNetwork)
|
|
if err = u.conn.Emit(eventAddWifiNetwork, wn); err != nil {
|
|
return false, errors.Wrapf(err, "error while emitting '%s' event", eventAddWifiNetwork)
|
|
}
|
|
u.logf("'%s' event sent", eventAddWifiNetwork)
|
|
|
|
wg.Wait()
|
|
|
|
return done, err
|
|
|
|
}
|
|
|
|
// RemoveWifiNetwork asks the ReachRS module to remove the given WiFi network
|
|
func (u *Updater) RemoveWifiNetwork(ssid string) (bool, error) {
|
|
|
|
var err error
|
|
var wg sync.WaitGroup
|
|
var done bool
|
|
|
|
wg.Add(1)
|
|
|
|
err = u.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
|
|
done = d
|
|
u.conn.Off(eventRemoveWifiNetworkResults)
|
|
wg.Done()
|
|
})
|
|
if err != nil {
|
|
return false, errors.Wrapf(err, "error while binding to '%s' event", eventRemoveWifiNetworkResults)
|
|
}
|
|
|
|
u.logf("sending '%s' event", eventRemoveWifiNetwork)
|
|
if err := u.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil {
|
|
return false, errors.Wrapf(err, "error while emitting '%s' event", eventRemoveWifiNetwork)
|
|
}
|
|
u.logf("'%s' event sent", eventRemoveWifiNetwork)
|
|
|
|
wg.Wait()
|
|
|
|
return done, nil
|
|
|
|
}
|
|
|
|
// ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network
|
|
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
|
|
}
|