-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_dvb.go
143 lines (110 loc) · 3.2 KB
/
request_dvb.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
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"io/ioutil"
MQTT "github.com/eclipse/paho.mqtt.golang"
DVB "github.com/kiliankoe/dvbgo"
)
type Tram struct {
Station string `json:"station"`
Destination string `json:"destination"`
Id string `json:"id"`
Description string `json:"description"`
}
type Config struct {
City string `json:"city"`
MqttBroker string `json:"mqttBroker"`
MqttTopicPrefix string `json:"mqttTopicPrefix"`
Trams []Tram `json:"trams"`
}
type Departure struct {
Station string `json:"station"`
Direction string `json:"direction"`
Time string `json:"time"`
TimeNext string `json:"time_next"`
Tram string `json:"tram"`
Description string `json:"description"`
}
func readConfig(configFile string) Config {
jsonFile, err := os.Open(configFile)
if err != nil {
fmt.Println(err)
}
bValue, _ := ioutil.ReadAll(jsonFile)
var config Config
json.Unmarshal(bValue, &config)
// print what we got
fmt.Println("City:" + config.City)
return config
}
func getDepartures(config Config) []Departure {
var foundDeps = make([]Departure, 0)
for i := 0; i < len(config.Trams); i++ {
city := config.City
stop := config.Trams[i].Station
direction := config.Trams[i].Destination
fmt.Println(" - query from: " + stop + " towards: " + direction )
// find next 2 departures in the specified direction
var foundDepartures [2]string
index := 0
departures, _ := DVB.Monitor(stop, 0, city)
for _, departure := range departures {
if departure.Direction == direction {
json, _ := json.Marshal(departure.RelativeTime)
jsonStr := string(json)
foundDepartures[index] = jsonStr
fmt.Println(" - found departure: " + jsonStr)
index++
if index == 2 {
break
}
}
}
var dep Departure
dep.Station = stop
dep.Direction = direction
dep.Tram = config.Trams[i].Id
dep.Time = foundDepartures[0]
dep.TimeNext = foundDepartures[1]
dep.Description = config.Trams[i].Description
foundDeps = append(foundDeps, dep)
}
return foundDeps
}
func publishDepartures(config Config, departures []Departure) {
mqttBroker := config.MqttBroker
mqttTopicPrefix := config.MqttTopicPrefix
// mqtt client
opts := MQTT.NewClientOptions()
opts.AddBroker(mqttBroker)
opts.SetClientID("dvb")
client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
for _, departure := range departures {
fmt.Println("\n + publish to: " + mqttBroker + "/" + mqttTopicPrefix + departure.Tram)
fmt.Println(" - " + departure.Description + " ::> time: " + departure.Time + ", time_next: " + departure.TimeNext)
b, err := json.Marshal(departure)
if err != nil {
fmt.Println(err)
break
}
token := client.Publish(mqttTopicPrefix + departure.Tram, 0, false, b)
token.Wait()
}
client.Disconnect(250)
}
func main() {
// load config
config := readConfig("config.json")
fmt.Println("+ start")
for true {
departures := getDepartures(config)
publishDepartures(config, departures)
time.Sleep(time.Minute)
}
}