-
Notifications
You must be signed in to change notification settings - Fork 7
/
gleam.go
159 lines (143 loc) · 3.64 KB
/
gleam.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gleam
import (
"crypto/tls"
"log"
"os"
"syscall"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/mikespook/golib/signal"
"github.com/mikespook/schego"
)
type Gleam struct {
lua *luaEnv
config Config
mqttClient mqtt.Client
scheduler *schego.Scheduler
}
func NewGleam(workdir string) *Gleam {
return &Gleam{
lua: newLuaEnv(workdir),
}
}
func (g *Gleam) Init() error {
if err := g.lua.Init(&g.config); err != nil {
return err
}
if err := g.lua.onSeat(InitFunc); err != nil {
return err
}
g.initMQTT()
g.initSchedule()
return g.lua.onEvent(AfterInitFunc, g.mqttClient)
}
func (g *Gleam) initSchedule() {
if g.config.Schedule.Tick == 0 {
log.Printf("Scheduler not init")
return
}
g.scheduler = schego.New(g.config.Schedule.Tick * time.Millisecond)
g.scheduler.ErrorFunc = g.lua.onError
for name, interval := range g.config.Schedule.Tasks {
f := g.lua.newOnSchedule(name, &g.mqttClient)
g.scheduler.Add(name, time.Now(), interval*time.Millisecond, schego.ForEver, f)
fn := "Skip"
if f != nil {
fn = "Serve"
}
log.Printf("Schedule: %s (%d) => %s", name, interval, fn)
}
}
func (g *Gleam) initMQTT() {
if g.config.ClientId == "" {
log.Printf("MQTT not init")
return
}
opts := mqtt.NewClientOptions()
for _, broker := range g.config.MQTT {
opts.AddBroker(broker.Addr)
log.Printf("Add Broker: %s@%s", broker.Username, broker.Addr)
if broker.Username != "" {
opts.SetUsername(broker.Username).SetPassword(broker.Password)
}
}
opts.SetClientID(g.config.ClientId)
log.Printf("ClientId: %s", g.config.ClientId)
opts.SetDefaultPublishHandler(g.lua.newOnMessage(MessageFunc))
opts.SetAutoReconnect(true)
if g.config.NotVerifyTLS {
opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: true})
}
opts.SetOnConnectHandler(g.newOnConnect())
opts.SetConnectionLostHandler(g.newOnLostConnect())
g.mqttClient = mqtt.NewClient(opts)
for {
token := g.mqttClient.Connect()
if !token.Wait() || token.Error() == nil {
break
}
log.Printf("Conn Error: %s", token.Error())
time.Sleep(5 * time.Second)
}
}
func (g *Gleam) newOnConnect() mqtt.OnConnectHandler {
return func(client mqtt.Client) {
for name, task := range g.config.Tasks {
for topic, qos := range task {
f := g.lua.newOnMessage(name)
if f == nil {
name = "{default}"
}
log.Printf("Subscribe: %s (%d) => %s", topic, qos, name)
if token := client.Subscribe(topic, qos, f); token.Wait() && token.Error() != nil {
log.Printf("Subscribe Error: %s %s", topic, token.Error())
}
}
}
}
}
func (g *Gleam) newOnLostConnect() mqtt.ConnectionLostHandler {
return func(client mqtt.Client, err error) {
log.Printf("Conn Error: %s", err)
}
}
func (g *Gleam) Serve() {
if g.scheduler != nil {
go g.scheduler.Serve()
}
sh := signal.New(nil)
sh.Bind(os.Interrupt, func() uint {
return signal.BreakExit
})
sh.Bind(syscall.SIGTERM, func() uint {
return signal.BreakExit
})
sh.Wait()
}
func (g *Gleam) Final() error {
if g.scheduler != nil {
if err := g.scheduler.Close(); err != nil {
return err
}
}
for topic, _ := range g.config.Tasks {
if token := g.mqttClient.Unsubscribe(topic); token.Wait() && token.Error() != nil {
log.Printf("Unsubscribe error: %s => [%s]", topic, token.Error())
}
log.Printf("Unsubscribe: %s", topic)
}
if err := g.lua.onEvent(BeforeFinalizeFunc, g.mqttClient); err != nil {
log.Printf("BeforeFinalize: %s", err)
}
if g.config.FinalTick != 0 {
time.Sleep(g.config.FinalTick * time.Millisecond)
}
if g.mqttClient != nil {
g.mqttClient.Disconnect(500)
}
if err := g.lua.onSeat(FinalizeFunc); err != nil {
return err
}
g.lua.Final()
return nil
}