/
status_change.go
64 lines (57 loc) · 1.76 KB
/
status_change.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
package cmd
import (
"github.com/robmcl4/mycroft/app"
"github.com/robmcl4/mycroft/registry"
"log"
"errors"
"encoding/json"
)
type StatusChange struct {
App *app.App
NewStatus int
Priority int
}
func NewStatusChange(a *app.App, status int, data []byte) (*Command, error) {
sc := new(StatusChange)
sc.App = a
sc.NewStatus = status
sc.Priority = -1
if data != nil {
var parsed interface{}
err := json.Unmarshal(data, &parsed)
if err != nil {
return nil, err
}
m := parsed.(map[string]interface{})
if val, ok := getInt(m, "priority"); ok {
sc.Priority = val
} else {
return nil, errors.New("Priority was missing or not a number")
}
}
ret := new(Command)
ret.Execute = sc.Execute
return ret, nil
}
// change the app's status and notify all those that depend on this app
func (sc *StatusChange) Execute() {
if sc.App.Status != sc.NewStatus {
sc.App.Status = sc.NewStatus
sc.App.Priority = sc.Priority
if sc.App.Manifest != nil {
log.Printf("Changing status of %s to '%s'\n", sc.App.Manifest.InstanceId, sc.App.StatusString())
}
if sc.App.Manifest != nil {
// notify everyone who depends on us
for _, cpb := range sc.App.Manifest.Capabilities {
for _, dependent := range registry.GetDependents(cpb) {
body := make(map[string]interface{})
inner := make(map[string]string)
inner[sc.App.Manifest.InstanceId] = sc.App.StatusString()
body[cpb.Name] = inner
dependent.Send("APP_DEPENDENCY", body)
}
}
}
}
}