-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
189 lines (155 loc) · 4.31 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
package main
import (
"encoding/json"
"log"
"os"
"strconv"
"strings"
"time"
somersetcountywrapper "github.com/HelixSpiral/SomersetCountyAPIWrapper"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
// Some initial Twitter setup
consumerKey := os.Getenv("CONSUMER_KEY")
consumerSecret := os.Getenv("CONSUMER_SECRET")
accessToken := os.Getenv("ACCESS_TOKEN")
accessSecret := os.Getenv("ACCESS_SECRET")
// Some initial Mastodon setup
mastodonServer := os.Getenv("MASTODON_SERVER")
mastodonClientID := os.Getenv("MASTODON_CLIENT_ID")
mastodonClientSecret := os.Getenv("MASTODON_CLIENT_SECRET")
mastodonUser := os.Getenv("MASTODON_USERNAME")
mastodonPass := os.Getenv("MASTODON_PASSWORD")
// Some initial MQTT setup
mqttBroker := os.Getenv("MQTT_BROKER")
mqttClientId := os.Getenv("MQTT_CLIENT_ID")
mqttTopic := os.Getenv("MQTT_TOPIC")
options := mqtt.NewClientOptions().AddBroker(mqttBroker).SetClientID(mqttClientId)
options.WriteTimeout = 20 * time.Second
mqttClient := mqtt.NewClient(options)
currentDate := time.Now()
loc, err := time.LoadLocation("EST") // Somerset County API is in EST
if err != nil {
panic(err)
}
var logTmp Cache
var updates []somersetcountywrapper.DispatchLog
// If this errors the file doesn't exist yet, so just set empty values for the cache.
logTmp, err = readCache("/tmp/cache.json")
if err != nil {
logTmp = Cache{
Day: currentDate.In(loc).Day(),
LastProcessed: "00-0",
LogMap: make(map[string][]somersetcountywrapper.DispatchLog),
}
}
// If the day in the cache is before today, clear the cache and remove the file.
if logTmp.Day < currentDate.In(loc).Day() {
err = os.Remove("/tmp/cache.json")
if err != nil {
panic(err)
}
logTmp = Cache{
Day: currentDate.In(loc).Day(),
LastProcessed: "00-00000",
LogMap: make(map[string][]somersetcountywrapper.DispatchLog),
}
}
sw := somersetcountywrapper.NewWrapper()
logs, err := sw.GetDispatch(currentDate.In(loc).Format("20060102"))
if err != nil {
panic(err)
}
lastProcessedString := strings.Split(logTmp.LastProcessed, "-")[1]
lastProcessed, err := strconv.Atoi(lastProcessedString)
if err != nil {
panic(err)
}
var continueOuter bool
for _, y := range logs {
continueOuter = false
currentlyProcessingString := strings.Split(y.CallNum, "-")[1]
currentlyProcessing, err := strconv.Atoi(currentlyProcessingString)
if err != nil {
panic(err)
}
if currentlyProcessing <= lastProcessed {
for _, b := range logTmp.LogMap[y.CallNum] {
if b == y {
continueOuter = true
break
}
}
if continueOuter {
continue
}
}
if currentlyProcessing > lastProcessed {
lastProcessed = currentlyProcessing
logTmp.LastProcessed = y.CallNum
}
updates = append(updates, y)
logTmp.LogMap[y.CallNum] = append(logTmp.LogMap[y.CallNum], y)
}
// Connect to the MQTT broker
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
// We limit to 1 request every 5 seconds
limiter := time.Tick(time.Second * 5)
for _, y := range updates {
<-limiter
message := buildMessage(y)
jsonMsg, err := json.Marshal(&MqttMessage{
MastodonServer: mastodonServer,
MastodonClientID: mastodonClientID,
MastodonClientSecret: mastodonClientSecret,
MastodonUser: mastodonUser,
MastodonPass: mastodonPass,
TwitterConsumerKey: consumerKey,
TwitterConsumerSecret: consumerSecret,
TwitterAccessToken: accessToken,
TwitterAccessSecret: accessSecret,
Message: message,
})
if err != nil {
log.Fatal(err)
}
log.Println("Sending message:", message)
token := mqttClient.Publish(mqttTopic, 2, false, jsonMsg)
_ = token.Wait()
if token.Error() != nil {
panic(err)
}
}
err = writeCache("/tmp/cache.json", logTmp)
if err != nil {
panic(err)
}
mqttClient.Disconnect(250)
}
func readCache(f string) (Cache, error) {
var logTmp Cache
rawdata, err := os.ReadFile(f)
if err != nil {
return Cache{}, err
}
err = json.Unmarshal(rawdata, &logTmp)
if err != nil {
return Cache{}, err
}
return logTmp, nil
}
func writeCache(f string, logTmp Cache) error {
jsonData, err := json.Marshal(logTmp)
if err != nil {
return err
}
_ = jsonData
err = os.WriteFile(f, jsonData, 0644)
if err != nil {
return err
}
return nil
}