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
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
err = u.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) {
|
2018-09-19 13:00:08 +02:00
|
|
|
wifiNetworks = wn
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
}
|
2018-09-19 17:13:45 +02:00
|
|
|
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
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
err = u.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
|
2018-09-19 13:00:08 +02:00
|
|
|
done = d
|
2018-09-19 17:13:45 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
}
|
2018-09-19 17:13:45 +02:00
|
|
|
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
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
err = u.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
|
2018-09-19 13:00:08 +02:00
|
|
|
done = d
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
}
|
2018-09-19 17:13:45 +02:00
|
|
|
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
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
}
|
2018-09-19 17:13:45 +02:00
|
|
|
u.logf("'%s' event sent", eventConnectToNetwork)
|
2018-09-19 13:00:08 +02:00
|
|
|
return nil
|
|
|
|
}
|