-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
224 lines (188 loc) · 4.72 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"bytes"
"fmt"
"github.com/akamensky/argparse"
"go.bug.st/serial"
"golang.org/x/net/html/charset"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
)
var (
TimeDiff = int64(0)
isPi = false
isSyncTime = false
dataChan = make(chan []byte)
deleteChan = make(chan int)
)
func getNow() string {
// 2006-01-02T15:04:05-0700
return time.Now().Add(time.Duration(TimeDiff) * time.Second).Format(time.RFC3339)
}
func runForPort(i int, portName string, mode *serial.Mode) bool {
port, err := serial.Open(portName, mode)
if err != nil {
//log.Printf("Failed to open port %v: %v", portName, err)
return false
}
defer port.Close()
f, err := os.OpenFile(fmt.Sprintf("differentmind_%v.txt", i), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Printf("error opening file: %v", err)
return false
}
defer f.Close()
_, _ = f.WriteString("---------------- new logging started ----------------\r\n")
if isPi {
exec.Command("gpio", "write", string(rune(8+i)), "1")
}
readError := false
delFile := false
stringBuffer := bytes.NewBufferString("")
stringBuffer.Grow(1024)
buf := make([]byte, 512)
go func() {
for {
toDel := <-deleteChan
//log.Print("received message", toDel)
if toDel == i {
delFile = true
return
}
}
}()
// Must be in another routine because there is no non blocking read
go func() {
for {
n, err := port.Read(buf)
if err != nil || n == 0 || delFile {
readError = true
return
}
stringBuffer.WriteString(string(buf[:n]))
if strings.Contains(stringBuffer.String(), "\n") {
s, err := stringBuffer.ReadString('\n')
if err != nil {
continue
}
cleanText := strings.Replace(s, "\r", "", -1)
cleanText = strings.Replace(cleanText, "\n", "", -1)
if len(cleanText) <= 0 || cleanText == "" {
continue
}
if cleanText[0] == 0x00 {
cleanText = cleanText[1:]
}
withDate := fmt.Sprintf("[%v] %v", getNow(), cleanText)
reader, _ := charset.NewReaderLabel("ascii", bytes.NewReader([]byte(withDate)))
strBytes, _ := io.ReadAll(reader)
_, _ = f.Write(strBytes)
_, _ = f.WriteString("\n")
_ = f.Sync()
wsMsg := fmt.Sprintf("{%v}%v", i, withDate)
wsReader, _ := charset.NewReaderLabel("ascii", bytes.NewReader([]byte(wsMsg)))
wsStrBytes, _ := io.ReadAll(wsReader)
select {
case dataChan <- wsStrBytes:
//fmt.Println("sent message")
default:
//fmt.Println("no message sent")
}
if isSyncTime {
t, err := time.Parse("2006-01-02 15:04:05", strings.Replace(cleanText, "# ", "", -1))
if err == nil {
log.Println("---------------- syncing time ----------------")
TimeDiff = t.Unix() - time.Now().Unix()
log.Println("time difference is ", TimeDiff)
}
}
}
}
}()
for {
if delFile {
f.Close()
_ = os.Remove(fmt.Sprintf("differentmind_%v.txt", i))
return true
}
if readError {
break
}
}
_, _ = f.WriteString("---------------- logging ended ----------------\r\n\r\n\r\n")
if isPi {
exec.Command("gpio", "write", string(rune(8+i)), "0")
}
if readError {
log.Printf("Failed to read port %v", portName)
return false
}
return true
}
func whileRun(i int, port string, config *serial.Mode) {
if isPi {
exec.Command("gpio", "mode", string(rune(8+i)), "out")
}
for {
runForPort(i, port, config)
time.Sleep(5 * time.Second)
}
}
func main() {
parser := argparse.NewParser("seriallogger", "by Niklas Schütrumpf <niklas@mc8051.de>")
ports := parser.List("p", "port", &argparse.Options{Required: true, Help: "COM ports which should be scanned"})
pi := parser.Flag("g", "gpio", &argparse.Options{Required: false, Help: "If RPi GPIO are supported use this flag"})
syncTime := parser.Flag("s", "sync-time", &argparse.Options{Required: false, Help: "Sync time using the output of a serial port"})
err := parser.Parse(os.Args)
if err != nil {
log.Print(parser.Usage(err))
os.Exit(0)
}
isPi = *pi
isSyncTime = *syncTime
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Exit(1)
}()
l, err := net.Listen("tcp", "0.0.0.0:9669")
if err != nil {
log.Print(err)
os.Exit(0)
}
log.Printf("listening on http://%v", l.Addr())
cs := newChatServer(&dataChan, &deleteChan, len(*ports))
s := &http.Server{
Handler: cs,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}
errc := make(chan error, 1)
go func() {
errc <- s.Serve(l)
}()
mode := serial.Mode{
BaudRate: 9600,
DataBits: 8,
Parity: serial.NoParity,
StopBits: serial.OneStopBit,
}
for i, s := range *ports {
if i >= 3 {
continue
}
go whileRun(i, s, &mode)
}
for {
time.Sleep(10 * time.Second)
}
}