-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (123 loc) · 2.87 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"github.com/spf13/viper"
"go.bug.st/serial.v1"
)
type parsedMessage struct {
Blue int
Red int
Reset bool
}
var (
scoreKeeper = ScoreKeeperBuilder()
port serial.Port
portOpen bool
matchRed, _ = regexp.Compile("GOAL_RED_[0-9]{1,}")
matchBlue, _ = regexp.Compile("GOAL_BLUE_[0-9]{1,}")
)
/*
This function reads from a serial device and relays parsed messages to the channel given in input
Serial configurations will need to be tweaked accordingly and maybe read from a configuration file
*/
func readFromSerial(messages chan parsedMessage) {
mode := &serial.Mode{
BaudRate: 9600,
}
var err error
port, err = serial.Open(viper.GetString("serialDevice"), mode)
if err != nil {
log.Println("Serial Error")
log.Fatal(err)
}
portOpen = true
buff := make([]byte, 100)
//defer port.Close()
for {
n, err := port.Read(buff)
if err != nil && portOpen {
log.Println("Serial Error")
log.Fatal(err)
break
}
if n != 0 {
messages <- parseMessage(string(buff[:n]))
}
}
}
/*
Parse an input string and returns the corresponding score
*/
func parseMessage(message string) parsedMessage {
message = strings.TrimRight(message, "\r\n")
s := parsedMessage{Red: 0, Blue: 0, Reset: false}
if matchRed.MatchString(message) {
s.Red++
}
if matchBlue.MatchString(message) {
s.Blue++
}
if message == "MATCH_START\r\nStart Game" {
s.Reset = true
}
return s
}
func startWebserver() {
// serve static files from the client folder
fs := http.FileServer(http.Dir("client"))
http.Handle("/", fs)
// websocket endpoint
http.HandleFunc("/ws", serveWs)
log.Println("Listening...")
if err := http.ListenAndServe(":"+viper.GetString("httpServerPort"), nil); err != nil {
log.Fatal(err)
}
}
func initializeConfigurations() {
viper.SetDefault("serialDevice", "/dev/ttyp7")
viper.SetDefault("httpServerPort", "8080")
viper.SetConfigName("config")
viper.AddConfigPath("/etc/hookbot/")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
}
func updateScore(messages chan parsedMessage) {
for msg := range messages {
if msg.Reset {
scoreKeeper.ResetScore()
} else {
scoreKeeper.UpdateScore(Score{Red: msg.Red, Blue: msg.Blue})
}
}
}
func main() {
initializeConfigurations()
messages := make(chan parsedMessage)
go startWebserver()
go readFromSerial(messages)
// for each new parsed message update the score
go updateScore(messages)
// handle interrupts
signalChan := make(chan os.Signal, 1)
cleanupDone := make(chan struct{})
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
log.Println("Received an interrupt, stopping services...")
if port != nil && portOpen {
portOpen = false
port.Close()
}
close(cleanupDone)
}()
<-cleanupDone
}