This repository has been archived on 2024-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
orion/reach/wifi_networks.go

172 lines
4.4 KiB
Go
Raw Normal View History

2018-09-19 13:00:08 +02:00
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 (
2018-09-19 17:25:11 +02:00
// SecurityWEP WEP wifi network
SecurityWEP WifiSecurity = "wep"
// SecurityWPAPSK WPA(2)-PSK wifi network
2018-09-19 13:00:08 +02:00
SecurityWPAPSK WifiSecurity = "wpa-psk"
// SecurityOpen Open wifi network
SecurityOpen WifiSecurity = "open"
)
// WifiNetwork is a ReachRS module wifi network
2018-09-19 13:00:08 +02:00
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 (u *Updater) WifiNetworks() ([]*WifiNetwork, error) {
2018-09-19 13:00:08 +02:00
var err error
var wifiNetworks []*WifiNetwork
var wg sync.WaitGroup
wg.Add(1)
err = u.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) {
2018-09-19 13:00:08 +02:00
wifiNetworks = wn
u.conn.Off(eventSavedWifiNetworkResults)
2018-09-19 13:00:08 +02:00
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 {
2018-09-19 13:00:08 +02:00
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetSavedWifiNetworks)
}
u.logf("'%s' event sent", eventGetSavedWifiNetworks)
2018-09-19 13:00:08 +02:00
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) {
2018-09-19 13:00:08 +02:00
var err error
var wg sync.WaitGroup
var done bool
wg.Add(1)
err = u.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
2018-09-19 13:00:08 +02:00
done = d
u.conn.Off(eventAddWifiNetworkResults)
2018-09-19 13:00:08 +02:00
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 {
2018-09-19 13:00:08 +02:00
return false, errors.Wrapf(err, "error while emitting '%s' event", eventAddWifiNetwork)
}
u.logf("'%s' event sent", eventAddWifiNetwork)
2018-09-19 13:00:08 +02:00
wg.Wait()
return done, err
}
// RemoveWifiNetwork asks the ReachRS module to remove the given WiFi network
func (u *Updater) RemoveWifiNetwork(ssid string) (bool, error) {
2018-09-19 13:00:08 +02:00
var err error
var wg sync.WaitGroup
var done bool
wg.Add(1)
err = u.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
2018-09-19 13:00:08 +02:00
done = d
u.conn.Off(eventRemoveWifiNetworkResults)
2018-09-19 13:00:08 +02:00
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 {
2018-09-19 13:00:08 +02:00
return false, errors.Wrapf(err, "error while emitting '%s' event", eventRemoveWifiNetwork)
}
u.logf("'%s' event sent", eventRemoveWifiNetwork)
2018-09-19 13:00:08 +02:00
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 {
2018-09-19 13:00:08 +02:00
return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork)
}
u.logf("'%s' event sent", eventConnectToNetwork)
if waitDisconnect {
wg.Wait()
}
2018-09-19 13:00:08 +02:00
return nil
}