-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.go
76 lines (59 loc) · 1.72 KB
/
mqtt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"github.com/ninjasphere/go-ninja/api"
nconfig "github.com/ninjasphere/go-ninja/config"
)
func Mqtt() {
conn, err := ninja.Connect("sphere-director")
if err != nil {
log.Errorf("Failed to connect to sphere: %s", err)
panic("MQTT Fail!")
}
topic := "$node/" + nconfig.Serial() + "/module/:task"
log.Infof("Subscribing to %s", topic)
if _, err := conn.Subscribe("$node/"+nconfig.Serial()+"/module/start", func(name *string, data map[string]string) bool {
log.Infof("Received request to start process %s", *name)
p, ok := daemon.children[*name]
if !ok {
log.Infof("%s does not exist.", *name)
return true
}
cp, _, _ := p.find()
if cp != nil {
log.Infof("%s already running.", *name)
return true
}
ch := RunProcess(p)
log.Debugf("%s", <-ch)
return true
}); err != nil {
log.Fatalf("Failed to subscribe to module topic: %s", err)
}
if _, err := conn.Subscribe("$node/"+nconfig.Serial()+"/module/stop", func(name *string, data map[string]string) bool {
log.Infof("Received request to stop process %s", *name)
p, ok := daemon.children[*name]
if !ok {
log.Infof("%s does not exist.", *name)
return true
}
p.find()
p.stop()
return true
}); err != nil {
log.Fatalf("Failed to subscribe to module topic: %s", err)
}
if _, err := conn.Subscribe("$node/"+nconfig.Serial()+"/module/restart", func(name *string, data map[string]string) bool {
log.Infof("Received request to restart process %s", *name)
p, ok := daemon.children[*name]
if !ok {
log.Infof("%s does not exist.", *name)
return true
}
p.find()
ch, _ := p.restart()
log.Debugf("%s", <-ch)
return true
}); err != nil {
log.Fatalf("Failed to subscribe to module topic: %s", err)
}
}