Browse Source

WiFi networks management API

remotes/1537456337028077835/develop
William Petit 11 months ago
parent
commit
a2d1c675d7
8 changed files with 240 additions and 15 deletions
  1. 1
    1
      Makefile
  2. 1
    1
      go.mod
  3. 2
    2
      go.sum
  4. 1
    0
      modd.conf
  5. 11
    10
      reach/test_results.go
  6. 1
    1
      reach/test_results_test.go
  7. 148
    0
      reach/wifi_networks.go
  8. 75
    0
      reach/wifi_networks_test.go

+ 1
- 1
Makefile View File

@@ -13,7 +13,7 @@ test: tidy
13 13
 	go test -mod=vendor -v ./...
14 14
 
15 15
 lint:
16
-	@PATH=$(PATH):./bin/gometalinter gometalinter -e '.*/pkg/mod' -e ".*/go/src" --vendor ./...
16
+	@PATH=$(PATH):./bin/gometalinter gometalinter -e '.*/pkg/mod' -e ".*/go/src" --vendor $(LINT_ARGS) ./...
17 17
 
18 18
 tidy:
19 19
 	go mod tidy

+ 1
- 1
go.mod View File

@@ -1,7 +1,7 @@
1 1
 module forge.cadoles.com/Pyxis/orion
2 2
 
3 3
 require (
4
-	forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d
4
+	forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95
5 5
 	github.com/caarlos0/env v3.3.0+incompatible
6 6
 	github.com/davecgh/go-spew v1.1.1 // indirect
7 7
 	github.com/go-chi/chi v3.3.3+incompatible

+ 2
- 2
go.sum View File

@@ -1,5 +1,5 @@
1
-forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d h1:gkJw6IeJ+A/EVw48eUnZwbK36fBlDrfB5lNyEPAltLQ=
2
-forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919081902-52a9157a070d/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0=
1
+forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 h1:o3G5+9RjczCK1xAYFaRMknk1kY9Ule6PNfiW6N6hEpg=
2
+forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0=
3 3
 github.com/caarlos0/env v3.3.0+incompatible h1:jCfY0ilpzC2FFViyZyDKCxKybDESTwaR+ebh8zm6AOE=
4 4
 github.com/caarlos0/env v3.3.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
5 5
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

+ 1
- 0
modd.conf View File

@@ -11,6 +11,7 @@ Makefile {
11 11
 **/*.go
12 12
 modd.conf
13 13
 Makefile {
14
+  prep: make lint LINT_ARGS=--fast
14 15
   prep: make test
15 16
 }
16 17
 

+ 11
- 10
reach/test_results.go View File

@@ -8,10 +8,8 @@ import (
8 8
 )
9 9
 
10 10
 const (
11
-	// EventGetTestResults is a request for the ReachRS module's test results
12
-	EventGetTestResults = "get test results"
13
-	// EventTestResults is the response of the EventGetTestResults request
14
-	EventTestResults = "test results"
11
+	eventGetTestResults = "get test results"
12
+	eventTestResults    = "test results"
15 13
 )
16 14
 
17 15
 // TestResults are the ReachRS module's test results
@@ -32,17 +30,20 @@ func (c *Client) TestResults() (*TestResults, error) {
32 30
 
33 31
 	wg.Add(1)
34 32
 
35
-	c.conn.On(EventTestResults, func(h *gosocketio.Channel, res *TestResults) {
33
+	err = c.conn.On(eventTestResults, func(h *gosocketio.Channel, res *TestResults) {
36 34
 		results = res
37
-		c.conn.On(EventTestResults, nil)
35
+		c.conn.Off(eventTestResults)
38 36
 		wg.Done()
39 37
 	})
38
+	if err != nil {
39
+		return nil, errors.Wrapf(err, "error while binding to '%s' event", eventTestResults)
40
+	}
40 41
 
41
-	c.logf("sending '%s' event", EventGetTestResults)
42
-	if err = c.conn.Emit(EventGetTestResults, nil); err != nil {
43
-		return nil, errors.Wrapf(err, "error while emitting '%s' event", EventGetTestResults)
42
+	c.logf("sending '%s' event", eventGetTestResults)
43
+	if err = c.conn.Emit(eventGetTestResults, nil); err != nil {
44
+		return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetTestResults)
44 45
 	}
45
-	c.logf("'%s' event sent", EventGetTestResults)
46
+	c.logf("'%s' event sent", eventGetTestResults)
46 47
 
47 48
 	wg.Wait()
48 49
 

reach/client_test.go → reach/test_results_test.go View File

@@ -4,7 +4,7 @@ import (
4 4
 	"testing"
5 5
 )
6 6
 
7
-func TestClient(t *testing.T) {
7
+func TestClientTestResults(t *testing.T) {
8 8
 
9 9
 	client := NewClient(
10 10
 		WithStandardLogger(),

+ 148
- 0
reach/wifi_networks.go View File

@@ -0,0 +1,148 @@
1
+package reach
2
+
3
+import (
4
+	"sync"
5
+
6
+	"forge.cadoles.com/Pyxis/golang-socketio"
7
+	"github.com/pkg/errors"
8
+)
9
+
10
+const (
11
+	eventGetSavedWifiNetworks     = "get saved wifi networks"
12
+	eventSavedWifiNetworkResults  = "wifi saved networks results"
13
+	eventAddWifiNetwork           = "add new network"
14
+	eventAddWifiNetworkResults    = "add network results"
15
+	eventRemoveWifiNetwork        = "remove network"
16
+	eventRemoveWifiNetworkResults = "remove network results"
17
+	eventConnectToNetwork         = "connect to network"
18
+)
19
+
20
+// WifiSecurity is a WiFi network security algorithm
21
+type WifiSecurity string
22
+
23
+const (
24
+	// SecurityWPAPSK WPA-PSK wifi network
25
+	SecurityWPAPSK WifiSecurity = "wpa-psk"
26
+	// SecurityOpen Open wifi network
27
+	SecurityOpen WifiSecurity = "open"
28
+)
29
+
30
+// WifiNetwork is a ReachRS module's saved wifi network
31
+// Raw messages examples:
32
+// [{"ssid":"Cadoles","password":"...","security":"wpa-psk","identity":""}]
33
+// [{"is_visible":false,"connected":false,"ssid":"Cadoles","security":"wpa-psk","is_connected":false,"is_added":true}]]
34
+type WifiNetwork struct {
35
+	SSID      string       `json:"ssid"`
36
+	Password  string       `json:"password"`
37
+	Security  WifiSecurity `json:"security"`
38
+	Identity  string       `json:"identity"`
39
+	Visible   bool         `json:"is_visible"`
40
+	Connected bool         `json:"is_connected"`
41
+	Added     bool         `json:"is_added"`
42
+}
43
+
44
+// SavedWifiNetworks returns the ReachRS module saved wifi networks
45
+func (c *Client) SavedWifiNetworks() ([]*WifiNetwork, error) {
46
+
47
+	var err error
48
+	var wifiNetworks []*WifiNetwork
49
+	var wg sync.WaitGroup
50
+
51
+	wg.Add(1)
52
+
53
+	err = c.conn.On(eventSavedWifiNetworkResults, func(h *gosocketio.Channel, wn []*WifiNetwork) {
54
+		wifiNetworks = wn
55
+		c.conn.Off(eventSavedWifiNetworkResults)
56
+		wg.Done()
57
+	})
58
+	if err != nil {
59
+		return nil, errors.Wrapf(err, "error while binding to '%s' event", eventSavedWifiNetworkResults)
60
+	}
61
+
62
+	c.logf("sending '%s' event", eventGetSavedWifiNetworks)
63
+	if err = c.conn.Emit(eventGetSavedWifiNetworks, nil); err != nil {
64
+		return nil, errors.Wrapf(err, "error while emitting '%s' event", eventGetSavedWifiNetworks)
65
+	}
66
+	c.logf("'%s' event sent", eventGetSavedWifiNetworks)
67
+
68
+	wg.Wait()
69
+
70
+	return wifiNetworks, err
71
+
72
+}
73
+
74
+// AddWifiNetwork asks the ReachRS module to save the given wifi network informations
75
+func (c *Client) AddWifiNetwork(ssid string, security WifiSecurity, password string) (bool, error) {
76
+
77
+	var err error
78
+	var wg sync.WaitGroup
79
+	var done bool
80
+
81
+	wg.Add(1)
82
+
83
+	err = c.conn.On(eventAddWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
84
+		done = d
85
+		c.conn.Off(eventAddWifiNetworkResults)
86
+		wg.Done()
87
+	})
88
+	if err != nil {
89
+		return false, errors.Wrapf(err, "error while binding to '%s' event", eventAddWifiNetworkResults)
90
+	}
91
+
92
+	wn := &WifiNetwork{
93
+		SSID:     ssid,
94
+		Security: security,
95
+		Password: password,
96
+	}
97
+
98
+	c.logf("sending '%s' event", eventAddWifiNetwork)
99
+	if err = c.conn.Emit(eventAddWifiNetwork, wn); err != nil {
100
+		return false, errors.Wrapf(err, "error while emitting '%s' event", eventAddWifiNetwork)
101
+	}
102
+	c.logf("'%s' event sent", eventAddWifiNetwork)
103
+
104
+	wg.Wait()
105
+
106
+	return done, err
107
+
108
+}
109
+
110
+// RemoveWifiNetwork asks the ReachRS module to remove the given WiFi network
111
+func (c *Client) RemoveWifiNetwork(ssid string) (bool, error) {
112
+
113
+	var err error
114
+	var wg sync.WaitGroup
115
+	var done bool
116
+
117
+	wg.Add(1)
118
+
119
+	err = c.conn.On(eventRemoveWifiNetworkResults, func(h *gosocketio.Channel, d bool) {
120
+		done = d
121
+		c.conn.Off(eventRemoveWifiNetworkResults)
122
+		wg.Done()
123
+	})
124
+	if err != nil {
125
+		return false, errors.Wrapf(err, "error while binding to '%s' event", eventRemoveWifiNetworkResults)
126
+	}
127
+
128
+	c.logf("sending '%s' event", eventRemoveWifiNetwork)
129
+	if err := c.conn.Emit(eventRemoveWifiNetwork, ssid); err != nil {
130
+		return false, errors.Wrapf(err, "error while emitting '%s' event", eventRemoveWifiNetwork)
131
+	}
132
+	c.logf("'%s' event sent", eventRemoveWifiNetwork)
133
+
134
+	wg.Wait()
135
+
136
+	return done, nil
137
+
138
+}
139
+
140
+// ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network
141
+func (c *Client) ConnectToWifiNetwork(ssid string) error {
142
+	c.logf("sending '%s' event", eventConnectToNetwork)
143
+	if err := c.conn.Emit(eventConnectToNetwork, ssid); err != nil {
144
+		return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork)
145
+	}
146
+	c.logf("'%s' event sent", eventConnectToNetwork)
147
+	return nil
148
+}

+ 75
- 0
reach/wifi_networks_test.go View File

@@ -0,0 +1,75 @@
1
+package reach
2
+
3
+import (
4
+	"fmt"
5
+	"math/rand"
6
+	"testing"
7
+)
8
+
9
+func TestClientSavedWiFiNetworks(t *testing.T) {
10
+
11
+	client := NewClient(
12
+		WithStandardLogger(),
13
+	)
14
+	if err := client.Connect(); err != nil {
15
+		t.Fatal(err)
16
+	}
17
+
18
+	_, err := client.SavedWifiNetworks()
19
+	if err != nil {
20
+		t.Error(err)
21
+	}
22
+
23
+	defer client.Close()
24
+
25
+}
26
+
27
+func TestClientCRUDWiFiNetwork(t *testing.T) {
28
+
29
+	client := NewClient(
30
+		WithStandardLogger(),
31
+	)
32
+	if err := client.Connect(); err != nil {
33
+		t.Fatal(err)
34
+	}
35
+
36
+	ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
37
+
38
+	done, err := client.AddWifiNetwork(ssid, SecurityOpen, "")
39
+	if err != nil {
40
+		t.Error(err)
41
+	}
42
+
43
+	if g, e := done, true; g != e {
44
+		t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
45
+	}
46
+
47
+	networks, err := client.SavedWifiNetworks()
48
+	if err != nil {
49
+		t.Error(err)
50
+	}
51
+
52
+	found := false
53
+	for _, n := range networks {
54
+		if n.SSID == ssid {
55
+			found = true
56
+			break
57
+		}
58
+	}
59
+
60
+	if g, e := found, true; g != e {
61
+		t.Errorf("wifi network '%s' should exists", ssid)
62
+	}
63
+
64
+	done, err = client.RemoveWifiNetwork(ssid)
65
+	if err != nil {
66
+		t.Error(err)
67
+	}
68
+
69
+	if g, e := done, true; g != e {
70
+		t.Errorf("RemoveWifiNetwork() -> done: got '%v', expected '%v'", g, e)
71
+	}
72
+
73
+	defer client.Close()
74
+
75
+}

Loading…
Cancel
Save