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

149 lines
4.2 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 (
// 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 (u *Updater) SavedWifiNetworks() ([]*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) error {
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)
2018-09-19 13:00:08 +02:00
return nil
}