-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
135 lines (110 loc) · 3.11 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
package main
// useful: https://github.com/bugst/go-serial/blob/master/serial_windows.go
import (
"fmt"
"time"
"errors"
"sync"
)
var (
globalConnected = false
globalFaults = []string{"not-checked-yet"}
globalSerialPorts = []string{}
globalSelectedSerialPort = ""
globalEcuType = ""
globalUserCommand = ""
globalAlert = "" // pops up on web UI then closes itself
globalError = "" // pops up on web UI and stays until closed
globalDataOutput = map[string] float32{}
globalDataOutputLock = sync.RWMutex{}
globalAgentVersion = "1.4.3"
globalLogLines = []string{}
outgoingData chan string // for pushing data out of the websocket
serialReadChannel = make(chan byte, 1024)
serialReadRoutineRunning = false
)
func main() {
outgoingData = make(chan string, 1000) // buffer on it in case the web browser is slow?
fmt.Println("################################################################################")
fmt.Println("# Rover MEMS Diagnostic Agent version "+globalAgentVersion)
fmt.Println("################################################################################")
fmt.Println("")
fmt.Println("If you have not done so already, open https://rovermems.com/agent/")
fmt.Println("")
go runWebserver()
for true {
err := connectLoop();
if err != nil {
// fmt.Println(err)
globalDataOutputLock.Lock()
globalError = err.Error()
globalDataOutputLock.Unlock()
}
time.Sleep(1 * time.Second)
}
}
func connectLoop() error {
// if globalEcuType == "" {
// return nil
// // return errors.New("No ECU type selected")
// }
portList, err := nativeGetPortsList()
if err != nil {
return err
}
// if len(portList) > 0 {
// fmt.Println("Found the following ports that I can use:")
// fmt.Println(portList)
// }
globalDataOutputLock.Lock()
globalSerialPorts = portList
globalDataOutputLock.Unlock()
portname := ""
if len(portList) == 1 {
// fmt.Println("Only found one port so I'm going to use it")
portname = portList[0]
globalDataOutputLock.Lock()
globalSelectedSerialPort = portname
globalDataOutputLock.Unlock()
} else if len(portList) > 1 {
globalDataOutputLock.Lock()
if globalSelectedSerialPort == "" {
globalDataOutputLock.Unlock()
// return errors.New("Multiple COM ports found, select one")
return nil
} else {
portname = globalSelectedSerialPort
}
globalDataOutputLock.Unlock()
} else {
return errors.New("No serial ports found, check device manager, do you need to install a driver?")
}
// TODO: send normal logging data straight to UI using "outgoingData"
// fmt.Println("Using port:")
// fmt.Println(portname)
switch globalEcuType {
case "1.x":
_, err = readFirstBytesFromPortEcu1x(portname)
break;
case "rc5":
_, err = readFirstBytesFromPortRc5(portname)
break
case "2J":
_, err = readFirstBytesFromPortTwoj(portname)
break
case "1.9":
_, err = readFirstBytesFromPortEcu19(portname)
break
case "3":
_, err = readFirstBytesFromPortEcu3(portname)
break
case "":
return nil
default:
return errors.New("Unknown ECU type set")
}
if err != nil {
return err
}
return errors.New("Connect loop finished")
}