Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit eb29080

Browse files
facchinmcmaglie
authored andcommitted
Add network control feature
1 parent 72c1a36 commit eb29080

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

handlers_stats.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,26 @@ import (
2424

2525
"github.com/arduino/go-system-stats/disk"
2626
"github.com/arduino/go-system-stats/mem"
27+
"github.com/arduino/go-system-stats/network"
2728
mqtt "github.com/eclipse/paho.mqtt.golang"
29+
"github.com/pkg/errors"
2830
)
2931

32+
// WiFiEvent tries to connect to the specified wifi network
33+
func (s *Status) WiFiEvent(client mqtt.Client, msg mqtt.Message) {
34+
// try registering a new wifi network
35+
var info struct {
36+
SSID string `json:"ssid"`
37+
Password string `json:"password"`
38+
}
39+
err := json.Unmarshal(msg.Payload(), &info)
40+
if err != nil {
41+
s.Error("/wifi", errors.Wrapf(err, "unmarshal %s", msg.Payload()))
42+
return
43+
}
44+
net.AddWirelessConnection(info.SSID, info.Password)
45+
}
46+
3047
// StatsEvent sends statistics about resource used in the system (RAM, Disk, Network, etc...)
3148
func (s *Status) StatsEvent(client mqtt.Client, msg mqtt.Message) {
3249
// Gather all system data metrics
@@ -42,14 +59,22 @@ func (s *Status) StatsEvent(client mqtt.Client, msg mqtt.Message) {
4259
return
4360
}
4461

62+
netStats, err := net.GetNetworkStats()
63+
if err != nil {
64+
s.Error("/stats/error", fmt.Errorf("Retrieving network stats: %s", err))
65+
return
66+
}
67+
4568
type StatsPayload struct {
46-
Memory *mem.Stats `json:"memory"`
47-
Disk []*disk.FSStats `json:"disk"`
69+
Memory *mem.Stats `json:"memory"`
70+
Disk []*disk.FSStats `json:"disk"`
71+
Network *net.Stats `json:"network"`
4872
}
4973

5074
info := StatsPayload{
51-
Memory: memStats,
52-
Disk: diskStats,
75+
Memory: memStats,
76+
Disk: diskStats,
77+
Network: netStats,
5378
}
5479

5580
// Send result

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ func subscribeTopics(mqttClient mqtt.Client, id string, status *Status) {
212212
mqttClient.Subscribe("$aws/things/"+id+"/sketch/post", 1, status.SketchEvent)
213213
mqttClient.Subscribe("$aws/things/"+id+"/update/post", 1, status.UpdateEvent)
214214
mqttClient.Subscribe("$aws/things/"+id+"/stats/post", 1, status.StatsEvent)
215+
mqttClient.Subscribe("$aws/things/"+id+"/wifi/post", 1, status.WiFiEvent)
215216

216217
mqttClient.Subscribe("$aws/things/"+id+"/apt/list/post", 1, status.AptListEvent)
217218
mqttClient.Subscribe("$aws/things/"+id+"/apt/install/post", 1, status.AptInstallEvent)

0 commit comments

Comments
 (0)