-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
209 lines (179 loc) · 6.52 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"sync"
"fmt"
"time"
"strconv"
"flag"
rand "math/rand"
mqtt "github.com/eclipse/paho.mqtt.golang"
gpiocdev "github.com/warthog618/go-gpiocdev"
overflow "github.com/JohnCGriffin/overflow"
i64 "github.com/adam-lavrik/go-imath/i64"
)
type CountdownTimer struct {
sync.Mutex
seconds uint64
maxSeconds uint64
}
var gCountdownTimer = CountdownTimer { sync.Mutex {}, 0, 2 * 60 * 60 }
var gpiochipLine *gpiocdev.Line
var gpioChipLineActiveHigh bool = true
var defaultAddedSecondsDuration int64 = 30 * 60
func SetLatchOff() {
if gpioChipLineActiveHigh {
gpiochipLine.SetValue(0)
} else {
gpiochipLine.SetValue(1)
}
}
func SetLatchOn() {
if gpioChipLineActiveHigh {
gpiochipLine.SetValue(1)
} else {
gpiochipLine.SetValue(0)
}
}
func decreaseOneSecond() {
defer gCountdownTimer.Unlock()
gCountdownTimer.Lock()
if gCountdownTimer.seconds == 0 {
return
}
gCountdownTimer.seconds = gCountdownTimer.seconds - 1
if (gCountdownTimer.seconds == 0) {
fmt.Printf("%s: Timer now at 0, will shut off latch\n", time.Now().Format(time.RFC850))
SetLatchOff()
}
}
func countdown() {
SetLatchOff()
for {
time.Sleep(1 * time.Second)
decreaseOneSecond()
}
}
func changeTime(seconds int64) {
gCountdownTimer.Lock()
defer gCountdownTimer.Unlock()
secondsOfTimerBeforeChanging := gCountdownTimer.seconds
if seconds < 0 {
secondsAbs := i64.Abs(seconds)
if uint64(secondsAbs) > gCountdownTimer.seconds {
gCountdownTimer.seconds = 0
SetLatchOff()
} else {
gCountdownTimer.seconds = uint64(int64(gCountdownTimer.seconds) - secondsAbs)
}
fmt.Printf("%s: Subtracted %d seconds so timer is now at %d, was at %d seconds\n", time.Now().Format(time.RFC850), secondsAbs, gCountdownTimer.seconds, secondsOfTimerBeforeChanging)
} else {
gCountdownTimer.seconds = uint64(int64(gCountdownTimer.seconds) + seconds)
if (gCountdownTimer.maxSeconds <= gCountdownTimer.seconds) {
fmt.Printf("%s: Timer truncated to %d seconds\n", time.Now().Format(time.RFC850), gCountdownTimer.maxSeconds)
gCountdownTimer.seconds = gCountdownTimer.maxSeconds
}
fmt.Printf("%s: Added %d seconds so timer is now at %d, was at %d seconds\n", time.Now().Format(time.RFC850), seconds, gCountdownTimer.seconds, secondsOfTimerBeforeChanging)
SetLatchOn()
}
}
func periodicPublishAlerts(client mqtt.Client, topic *string) {
for {
time.Sleep(1 * time.Second)
gCountdownTimer.Lock()
t := client.Publish(*topic, 0, false, fmt.Sprintf("%d\n", gCountdownTimer.seconds))
go func() {
_ = t.Wait()
if t.Error() != nil {
fmt.Printf("%s: %s\n", time.Now().Format(time.RFC850), t.Error())
}
}()
gCountdownTimer.Unlock()
}
}
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
// There are 2 valid inputs - empty message (zero bytes) or a textual ASCII numbers-only message
if len(msg.Payload()) == 0 {
fmt.Printf("%s: Received empty message from topic: %s\n", time.Now().Format(time.RFC850), msg.Topic())
changeTime(defaultAddedSecondsDuration)
return
}
fmt.Printf("%s: Received message: %s from topic: %s\n", time.Now().Format(time.RFC850), msg.Payload(), msg.Topic())
// For "a textual ASCII numbers-only message" - we will try to parse it into a valid number, and if parsing was not
// succesful then we will simply ignore the message.
changedTimeInMinutes, err := strconv.ParseInt(string(msg.Payload()), 10, 64)
if err != nil {
return
}
changedTimeInSeconds, ok := overflow.Mul64(changedTimeInMinutes, 60)
if !ok {
return
}
changeTime(changedTimeInSeconds)
}
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
fmt.Printf("%s: Connected to MQTT broker\n", time.Now().Format(time.RFC850))
}
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
fmt.Printf("%s: Connection lost from MQTT broker: %v\n, aborting!", time.Now().Format(time.RFC850), err)
SetLatchOff()
panic("Connection lost from MQTT broker")
}
func main() {
broker := flag.String("mqtt-broker", "127.0.0.1", "MQTT Broker hostname/IP")
port := flag.Uint64("mqtt-port", 1883, "MQTT Broker Port")
topic := flag.String("mqtt-topic", "test-topic", "MQTT Topic to subscribe")
alertTopic := flag.String("mqtt-alert-topic", "test-alert-topic", "MQTT Topic to send alerts")
username := flag.String("mqtt-username", "", "MQTT Broker username")
password := flag.String("mqtt-password", "", "MQTT Broker password")
gpioChipName := flag.String("gpio-chip", "gpiochip0", "GPIO Chip Name")
gpioChipLineNumber := flag.Int("gpio-chip-line", 1, "GPIO Chip Line for setting latch")
flag.BoolVar(&gpioChipLineActiveHigh, "active-high", false, "Latch is Active High triggered")
flag.Int64Var(&defaultAddedSecondsDuration, "default-added-seconds-duration", 30 * 60, "Default Added Seconds Duration on empty MQTT message")
flag.Parse()
if (*port > uint64(65535)) {
panic("Invalid port number")
}
if (*gpioChipLineNumber < 0) {
panic("Invalid GPIO line number")
}
if (defaultAddedSecondsDuration < 0) {
panic("Invalid default added seconds duration")
}
// This is probably unsafe for anything serious, but since we use
// rand for generating somewhat-random client ID, this is probably OK.
rand.Seed(time.Now().UnixNano())
clientId := fmt.Sprintf("mqtt-relay-module-timer-%d", rand.Int())
fmt.Printf("Connecting to %s:%d, with client id of %s\n", *broker, *port, clientId)
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", *broker, *port))
opts.SetClientID(clientId)
opts.SetUsername(*username)
opts.SetPassword(*password)
opts.SetDefaultPublishHandler(messagePubHandler)
opts.SetPingTimeout(10 * time.Second)
opts.SetKeepAlive(10 * time.Second)
opts.SetAutoReconnect(true)
opts.SetMaxReconnectInterval(10 * time.Second)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
client := mqtt.NewClient(opts)
if token := client.Connect(); !token.WaitTimeout(10 * time.Second) || token.Error() != nil {
if token.Error() != nil {
panic(token.Error())
}
panic("Timeout when initialzing a connection to MQTT broker!\n")
}
token := client.Subscribe(*topic, 1, nil)
if !token.WaitTimeout(10 * time.Second) {
panic("Timeout when subscribing to topic!\n")
}
fmt.Printf("%s: Subscribed to topic %s\n", time.Now().Format(time.RFC850), *topic)
line, err := gpiocdev.RequestLine(*gpioChipName, *gpioChipLineNumber, gpiocdev.AsOutput(1))
if err != nil {
panic(err)
}
gpiochipLine = line
go countdown()
go periodicPublishAlerts(client, alertTopic)
select {} // block forever
}