WiFi networks management API
This commit is contained in:
parent
e7831091ad
commit
a2d1c675d7
2
Makefile
2
Makefile
|
@ -13,7 +13,7 @@ test: tidy
|
||||||
go test -mod=vendor -v ./...
|
go test -mod=vendor -v ./...
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
@PATH=$(PATH):./bin/gometalinter gometalinter -e '.*/pkg/mod' -e ".*/go/src" --vendor ./...
|
@PATH=$(PATH):./bin/gometalinter gometalinter -e '.*/pkg/mod' -e ".*/go/src" --vendor $(LINT_ARGS) ./...
|
||||||
|
|
||||||
tidy:
|
tidy:
|
||||||
go mod tidy
|
go mod tidy
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,7 +1,7 @@
|
||||||
module forge.cadoles.com/Pyxis/orion
|
module forge.cadoles.com/Pyxis/orion
|
||||||
|
|
||||||
require (
|
require (
|
||||||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d
|
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95
|
||||||
github.com/caarlos0/env v3.3.0+incompatible
|
github.com/caarlos0/env v3.3.0+incompatible
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/go-chi/chi v3.3.3+incompatible
|
github.com/go-chi/chi v3.3.3+incompatible
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -1,5 +1,5 @@
|
||||||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d h1:gkJw6IeJ+A/EVw48eUnZwbK36fBlDrfB5lNyEPAltLQ=
|
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 h1:o3G5+9RjczCK1xAYFaRMknk1kY9Ule6PNfiW6N6hEpg=
|
||||||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0=
|
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0=
|
||||||
github.com/caarlos0/env v3.3.0+incompatible h1:jCfY0ilpzC2FFViyZyDKCxKybDESTwaR+ebh8zm6AOE=
|
github.com/caarlos0/env v3.3.0+incompatible h1:jCfY0ilpzC2FFViyZyDKCxKybDESTwaR+ebh8zm6AOE=
|
||||||
github.com/caarlos0/env v3.3.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
|
github.com/caarlos0/env v3.3.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
|
|
@ -11,6 +11,7 @@ Makefile {
|
||||||
**/*.go
|
**/*.go
|
||||||
modd.conf
|
modd.conf
|
||||||
Makefile {
|
Makefile {
|
||||||
|
prep: make lint LINT_ARGS=--fast
|
||||||
prep: make test
|
prep: make test
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,10 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// EventGetTestResults is a request for the ReachRS module's test results
|
eventGetTestResults = "get test results"
|
||||||
EventGetTestResults = "get test results"
|
eventTestResults = "test results"
|
||||||
// EventTestResults is the response of the EventGetTestResults request
|
|
||||||
EventTestResults = "test results"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestResults are the ReachRS module's test results
|
// TestResults are the ReachRS module's test results
|
||||||
|
@ -32,17 +30,20 @@ func (c *Client) TestResults() (*TestResults, error) {
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
c.conn.On(EventTestResults, func(h *gosocketio.Channel, res *TestResults) {
|
err = c.conn.On(eventTestResults, func(h *gosocketio.Channel, res *TestResults) {
|
||||||
results = res
|
results = res
|
||||||
c.conn.On(EventTestResults, nil)
|
c.conn.Off(eventTestResults)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
c.logf("sending '%s' event", EventGetTestResults)
|
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventTestResults)
|
||||||
if err = c.conn.Emit(EventGetTestResults, nil); err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "error while emitting '%s' event", EventGetTestResults)
|
|
||||||
}
|
}
|
||||||
c.logf("'%s' event sent", EventGetTestResults)
|
|
||||||
|
c.logf("sending '%s' event", eventGetTestResults)
|
||||||
|
if err = c.conn.Emit(eventGetTestResults, nil); err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetTestResults)
|
||||||
|
}
|
||||||
|
c.logf("'%s' event sent", eventGetTestResults)
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestClient(t *testing.T) {
|
func TestClientTestResults(t *testing.T) {
|
||||||
|
|
||||||
client := NewClient(
|
client := NewClient(
|
||||||
WithStandardLogger(),
|
WithStandardLogger(),
|
|
@ -0,0 +1,148 @@
|
||||||
|
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 (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) {
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var wifiNetworks []*WifiNetwork
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
err = c.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) {
|
||||||
|
wifiNetworks = wn
|
||||||
|
c.conn.Off(eventSavedWifiNetworkResults)
|
||||||
|
wg.Done()
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventSavedWifiNetworkResults)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logf("sending '%s' event", eventGetSavedWifiNetworks)
|
||||||
|
if err = c.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetSavedWifiNetworks)
|
||||||
|
}
|
||||||
|
c.logf("'%s' event sent", eventGetSavedWifiNetworks)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return wifiNetworks, err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddWifiNetwork asks the ReachRS module to save the given wifi network informations
|
||||||
|
func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password string) (bool, error) {
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
var done bool
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
err = c.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
|
||||||
|
done = d
|
||||||
|
c.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,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logf("sending '%s' event", eventAddWifiNetwork)
|
||||||
|
if err = c.conn.Emit(eventAddWifiNetwork, wn); err != nil {
|
||||||
|
return false, errors.Wrapf(err, "error while emitting '%s' event", eventAddWifiNetwork)
|
||||||
|
}
|
||||||
|
c.logf("'%s' event sent", eventAddWifiNetwork)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return done, err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveWifiNetwork asks the ReachRS module to remove the given WiFi network
|
||||||
|
func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) {
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
var done bool
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
err = c.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
|
||||||
|
done = d
|
||||||
|
c.conn.Off(eventRemoveWifiNetworkResults)
|
||||||
|
wg.Done()
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false, errors.Wrapf(err, "error while binding to '%s' event", eventRemoveWifiNetworkResults)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logf("sending '%s' event", eventRemoveWifiNetwork)
|
||||||
|
if err := c.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil {
|
||||||
|
return false, errors.Wrapf(err, "error while emitting '%s' event", eventRemoveWifiNetwork)
|
||||||
|
}
|
||||||
|
c.logf("'%s' event sent", eventRemoveWifiNetwork)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return done, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network
|
||||||
|
func (c *Client) ConnectToWifiNetwork(ssid string) error {
|
||||||
|
c.logf("sending '%s' event", eventConnectToNetwork)
|
||||||
|
if err := c.conn.Emit(eventConnectToNetwork, ssid); err != nil {
|
||||||
|
return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork)
|
||||||
|
}
|
||||||
|
c.logf("'%s' event sent", eventConnectToNetwork)
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -0,0 +1,75 @@
|
||||||
|
package reach
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestClientSavedWiFiNetworks(t *testing.T) {
|
||||||
|
|
||||||
|
client := NewClient(
|
||||||
|
WithStandardLogger(),
|
||||||
|
)
|
||||||
|
if err := client.Connect(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := client.SavedWifiNetworks()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClientCRUDWiFiNetwork(t *testing.T) {
|
||||||
|
|
||||||
|
client := NewClient(
|
||||||
|
WithStandardLogger(),
|
||||||
|
)
|
||||||
|
if err := client.Connect(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
|
||||||
|
|
||||||
|
done, err := client.AddWifiNetwork(ssid, SecurityOpen, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if g, e := done, true; g != e {
|
||||||
|
t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
networks, err := client.SavedWifiNetworks()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for _, n := range networks {
|
||||||
|
if n.SSID == ssid {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if g, e := found, true; g != e {
|
||||||
|
t.Errorf("wifi network '%s' should exists", ssid)
|
||||||
|
}
|
||||||
|
|
||||||
|
done, err = client.RemoveWifiNetwork(ssid)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if g, e := done, true; g != e {
|
||||||
|
t.Errorf("RemoveWifiNetwork() -> done: got '%v', expected '%v'", g, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
}
|
Reference in New Issue