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/emlid/updater/wifi_networks.go

117 lines
3.1 KiB
Go
Raw Permalink Normal View History

package updater
import (
"context"
"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(ctx context.Context) ([]WifiNetwork, error) {
res := make([]WifiNetwork, 0)
if err := c.ReqResp(ctx, 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(ctx context.Context, ssid string, security WifiSecurity, password string) (bool, error) {
res := false
network := &WifiNetwork{
SSID: ssid,
Security: security,
Password: password,
}
if err := c.ReqResp(ctx, 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(ctx context.Context, ssid string) (bool, error) {
res := false
if err := c.ReqResp(ctx, 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(ctx context.Context, ssid string, waitDisconnect bool) error {
var err error
var wg sync.WaitGroup
if waitDisconnect {
var once sync.Once
done := func() {
c.Off(gosocketio.OnDisconnection)
wg.Done()
}
wg.Add(1)
go func() {
<-ctx.Done()
err = ctx.Err()
once.Do(done)
}()
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
once.Do(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
}