-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoivb.go
187 lines (151 loc) · 3.98 KB
/
goivb.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
package goivb
import (
"fmt"
"github.com/levigross/grequests"
"github.com/tidwall/gjson"
"github.com/BurntSushi/toml"
"log"
"strconv"
"strings"
"time"
"os"
"os/exec"
)
// to change the flags on the default logger
var glog = log.New(os.Stderr, "", log.LstdFlags | log.Lshortfile)
type GoivbConfig struct {
StopHost string `toml:"StopHost"`
PassageHost string `toml:"PassageHost"`
}
type WatchdogConfig struct {
StopUid int `toml:"StopUid"`
Sleep float64 `toml:"Sleep"`
}
type TomlConfig struct {
Goivb GoivbConfig `toml:"goivb"`
Watchdogs map[string]WatchdogConfig `toml:"watchdog"`
IsSet bool
}
var Config TomlConfig
func init () {
if _, err := os.Stat("/etc/goivb.toml"); err == nil {
if _, err := toml.DecodeFile("/etc/goivb.toml", &Config); err != nil {
glog.Fatalln(err)
} else {
Config.IsSet = true
}
}
if _, err := os.Stat("goivb.toml"); err == nil {
if _, err := toml.DecodeFile("goivb.toml", &Config); err != nil {
glog.Fatalln(err)
} else {
Config.IsSet = true
}
}
if !Config.IsSet {
glog.Fatalln("No Config file loaded")
}
}
type GoivbStops gjson.Result
type GoivbSmi gjson.Result
func GetStops () gjson.Result {
if !Config.IsSet {
glog.Fatalln("Goivb Configuration not set")
}
resp, err := grequests.Get(Config.Goivb.StopHost, nil)
if err != nil {
glog.Fatalln("Unable to make request: ", err)
}
data := resp.String()
data = strings.Replace(data, "\\", "", -1)
if !gjson.Valid(data) {
glog.Fatalln("invalid json")
}
return gjson.Parse(data).Get("#.stop")
}
func GetSmartinfo (stopUid int) gjson.Result {
if !Config.IsSet {
glog.Fatalln("Goivb Configuration not set")
}
resp, err := grequests.Post(Config.Goivb.PassageHost + "/?stopUid=" + strconv.Itoa(stopUid), nil)
if err != nil {
glog.Fatalln("Unable to make request: ", err)
}
data := resp.String()
data = strings.Replace(data, "\\", "", -1)
if !gjson.Valid(data) {
glog.Fatalln("invalid json")
}
if (len(data) <= 2) {
glog.Fatalln("No data received. Wrong StopUid?")
}
return gjson.Parse(data)
}
func Printall(parser gjson.Result) {
parser.ForEach(func(key, value gjson.Result) bool {
fmt.Println(value.String())
return true // keep iterating
})
}
func FPrint(parser gjson.Result, num int) {
rowSep := "|---------------------------------------------|\n"
fmt.Printf(rowSep)
fmt.Printf("| Haltestelle:%30s |\n", parser.Get("#.stopidname").Get("0"))
fmt.Printf(rowSep)
smi := parser.Get("#.smartinfo")
nCurr := 0
smi.ForEach(func(key, value gjson.Result) bool {
nCurr++
if nCurr > num {
return false
}
fmt.Printf("| %-5v| %-25v| %-10v|\n", value.Get("route"), value.Get("direction"), value.Get("time"))
return true // keep iterating
})
fmt.Printf(rowSep)
}
var directionMaps = map[string]map[string]string {
//←↑→↓
"Höttinger Auffahrt": map[string]string{
"Sadrach": "< ",
"Rum Sanatorium": "< ",
"J. Kerschb. Str.": "< ",
"J.-Kerschbaumer-Straße": "< ",
"Peerhofsiedlung": " >",
"Technik West": " >",
"Schützenstraße":" >",
"Flughafen": " >",
"Term. Marktplatz": "< ",
"Terminal Marktplatz": "< ",
"Kajetan-Sweth-Straße": "< ",
"Technik": " >",
"Ibk. Hauptbahnhof": "< "}}
func RpiPrint(parser gjson.Result, num int) {
directionMap := directionMaps[parser.Get("#.stopidname").Get("0").String()]
smi := parser.Get("#.smartinfo")
nCurr := 0
smi.ForEach(func(key, value gjson.Result) bool {
nCurr++
if nCurr > num {
return false
}
direction, ok := directionMap[value.Get("direction").String()]
if !ok {
direction = value.Get("direction").String()
}
time := value.Get("time").String()
time = strings.Replace(time, " min", "'", -1)
fmt.Printf("%v %v %v \n", value.Get("route"), direction, time)
return true // keep iterating
})
}
func Watchdog(watchdogId string) {
cfg := Config.Watchdogs[watchdogId]
clearOut, _ := exec.Command("clear").Output()
for true {
smi := GetSmartinfo(cfg.StopUid)
os.Stdout.Write(clearOut)
RpiPrint(smi, 6)
time.Sleep(time.Duration(cfg.Sleep) * time.Second)
}
}