-
Notifications
You must be signed in to change notification settings - Fork 3
/
webserver.go
182 lines (152 loc) · 4.26 KB
/
webserver.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
package main
import (
"fmt"
"log"
"strings"
"time"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/cors"
"github.com/gorilla/websocket"
)
func runWebserver() {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard // to disable web hits output to console
// gin.DefaultWriter = colorable.NewColorableStdout()
// gin.ForceConsoleColor()
router := gin.Default()
router.Use(cors.Default()) // allow all origins
router.GET("/api", func(c *gin.Context) {
globalDataOutputLock.Lock()
c.JSON(200, gin.H{
"faults": globalFaults,
"connected": globalConnected,
"ecuType": globalEcuType,
"userCommand": globalUserCommand,
"alert": globalAlert,
"error": globalError,
"ecuData": globalDataOutput,
"agentVersion": globalAgentVersion,
})
// clear the error and alert for next time
if globalAlert != "" {
globalAlert = ""
}
if globalError != "" {
globalError = ""
}
globalDataOutputLock.Unlock()
})
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
router.GET("/connected", func(c *gin.Context) {
c.JSON(200, gin.H{
"connected": globalConnected,
})
})
router.GET("/faults", func(c *gin.Context) {
globalDataOutputLock.RLock()
c.JSON(200, gin.H{
"faults": globalFaults,
})
globalDataOutputLock.RUnlock()
})
router.GET("/ecu/:name", func(c *gin.Context) {
globalDataOutputLock.Lock()
name := c.Param("name")
globalEcuType = name
c.String(http.StatusOK, "ECU type set to %s", name)
// globalAlert = "Agent confirms ECU set to "+name
globalDataOutputLock.Unlock()
})
router.GET("/serialPort/:name", func(c *gin.Context) {
globalDataOutputLock.Lock()
name := c.Param("name")
globalSelectedSerialPort = name
c.String(http.StatusOK, "Serial port set to %s", name)
// globalAlert = "Agent confirms ECU set to "+name
globalDataOutputLock.Unlock()
})
router.GET("/command/:name", func(c *gin.Context) {
globalDataOutputLock.Lock()
name := c.Param("name")
globalUserCommand = name
c.String(http.StatusOK, "User command accepted %s", name)
globalDataOutputLock.Unlock()
})
router.GET("/ws", func(c *gin.Context) {
wshandler(c.Writer, c.Request)
})
router.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
var wsupgrader = websocket.Upgrader {
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func wshandler(w http.ResponseWriter, r *http.Request) {
wsupgrader.CheckOrigin = func(r *http.Request) bool {
// TODO: check for localhost/127/rovermems.com ? don't actually care though
return true
}
conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Failed to set websocket upgrade: %+v", err)
return
}
iteration := 0
for {
// TODO: change to channel read then call function to send?
err := wsiteration(conn, iteration)
if err != nil {
break
}
iteration++
}
}
func wsiteration(conn *websocket.Conn, iteration int) error {
// wait for a message from the browser (it is sending "." to request data)
// message type, msg, err
_, message, err := conn.ReadMessage()
if err != nil {
// fmt.Println("WS readmessage failed")
return err
}
var data map[string]interface {} = make(map[string]interface{})
if strings.Compare(string(message), ".") == 0 {
globalDataOutputLock.RLock()
data["faults"] = globalFaults
data["connected"] = globalConnected
data["ecuType"] = globalEcuType
data["userCommand"] = globalUserCommand
data["alert"] = globalAlert
data["error"] = globalError
data["ecuData"] = globalDataOutput
data["agentVersion"] = globalAgentVersion
data["timestamp"] = time.Now().String()
data["serialPorts"] = globalSerialPorts
data["selectedSerialPort"] = globalSelectedSerialPort
data["logLines"] = globalLogLines
if globalAlert != "" {
globalAlert = ""
}
if globalError != "" {
globalError = ""
}
globalDataOutputLock.RUnlock()
} else {
// must be a command if it wasn't . above
log.Printf("recv: %s", message)
data["command"] = "worked"
}
jsondata, err := json.Marshal(data)
if err != nil {
return err
}
conn.WriteMessage(websocket.TextMessage, jsondata)
return nil
}