-
Notifications
You must be signed in to change notification settings - Fork 4
/
web-handler.go
176 lines (130 loc) · 4.45 KB
/
web-handler.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
package main
import (
"dronecore"
"fmt"
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
const (
SVR_ADDR = "localhost:8080"
ENCODING = "Content-Encoding"
TEXTENC = "text/plain"
)
type actionMessage struct {
Action string
}
type message struct {
Movement movementData `json:"movement"`
}
type movementData struct {
Yaw string `json:"yaw"`
Pitch string `json:"pitch"`
Roll string `json:"roll"`
Gaz string `json:"gaz"`
}
var drones *dronecore.DroneService
var conn *websocket.Conn
//var srv *http.Server
func init() {
drones = dronecore.InitialiseDroneService()
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
newConn, err := websocket.Upgrade(w, r, w.Header(), 1024, 1024)
if err != nil {
http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
}
conn = newConn
}
func sendMessage(yaw string, roll string, pitch string, gaz string) {
m := message{movementData{Yaw: yaw, Roll: roll, Pitch: pitch, Gaz: gaz}}
if conn != nil {
if err := conn.WriteJSON(m); err != nil {
Trace.Println(err)
}
} else {
Info.Println("No websocket connection")
}
}
func sendAction(action string) {
a := actionMessage{Action: action}
if conn != nil {
if err := conn.WriteJSON(a); err != nil {
Trace.Println(err)
}
} else {
Info.Println("No websocket connection")
}
}
func catchAllHandler(w http.ResponseWriter, r *http.Request) {
Info.Println("Default handler Call " + fmt.Sprint(r.Header))
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "-- your body -- %s!", r.URL.Path[1:])
}
// walk through the multiplexer configuration and display the relevant details
func walkMux(r *mux.Router) {
err := r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
pathTemplate, err := route.GetPathTemplate()
if err == nil {
fmt.Println("ROUTE:", pathTemplate)
}
pathRegexp, err := route.GetPathRegexp()
if err == nil {
fmt.Println("Path regexp:", pathRegexp)
}
queriesTemplates, err := route.GetQueriesTemplates()
if err == nil {
fmt.Println("Queries templates:", strings.Join(queriesTemplates, ","))
}
queriesRegexps, err := route.GetQueriesRegexp()
if err == nil {
fmt.Println("Queries regexps:", strings.Join(queriesRegexps, ","))
}
methods, err := route.GetMethods()
if err == nil {
fmt.Println("Methods:", strings.Join(methods, ","))
}
fmt.Println()
return nil
})
if err != nil {
Error.Panicln(err)
}
}
func main() {
Trace.Println("Starting ...")
// going to use Gorrilla MUX - can be obtained by go get -u github.com/gorilla/mux
// http://www.gorillatoolkit.org/pkg/mux
r := mux.NewRouter()
r.HandleFunc("/drone/STOP", droneAllEmergencyStopHandler)
r.HandleFunc("/drone/STOP/", droneAllEmergencyStopHandler)
r.HandleFunc("/drone/{droneId}/STOP", droneEmergencyStopHandler)
r.HandleFunc("/drone/{droneId}/STOP/", droneEmergencyStopHandler)
r.HandleFunc("/drone/register", droneRegistrationHandler)
r.HandleFunc("/drone/register/", droneRegistrationHandler)
r.HandleFunc("/drone/{droneId}/nav/action/{action}", droneActionHandler)
r.HandleFunc("/drone/{droneId}/nav/action/{action}/", droneActionHandler)
r.HandleFunc("/drone/{droneId}/simplenav/pitch/{pitch}", droneSimpleNavPitchHandler)
r.HandleFunc("/drone/{droneId}/simplenav/pitch/{pitch}/", droneSimpleNavPitchHandler)
r.HandleFunc("/drone/{droneId}/simplenav/altitude/{altitude}", droneSimpleNavGazHandler)
r.HandleFunc("/drone/{droneId}/simplenav/altitude/{altitude}/", droneSimpleNavGazHandler)
r.HandleFunc("/drone/{droneId}/simplenav/gaz/{gaz}", droneSimpleNavGazHandler)
r.HandleFunc("/drone/{droneId}/simplenav/gaz/{gaz}/", droneSimpleNavGazHandler)
r.HandleFunc("/drone/{droneId}/simplenav/roll/{roll}", droneSimpleNavRollHandler)
r.HandleFunc("/drone/{droneId}/simplenav/roll/{roll}/", droneSimpleNavRollHandler)
r.HandleFunc("/drone/{droneId}/simplenav/rotation/{yaw}", droneSimpleNavYawHandler)
r.HandleFunc("/drone/{droneId}/simplenav/rotation/{yaw}/", droneSimpleNavYawHandler)
r.HandleFunc("/drone/{droneId}/simplenav/yaw/{yaw}", droneSimpleNavYawHandler)
r.HandleFunc("/drone/{droneId}/simplenav/yaw/{yaw}/", droneSimpleNavYawHandler)
r.HandleFunc("/ws", wsHandler)
r.HandleFunc("/", catchAllHandler)
r.HandleFunc("/drone/", catchAllHandler)
Trace.Println("Multiplexer configured. starting server....")
if IsLogging("Trace") {
walkMux(r)
}
log.Fatal(http.ListenAndServe(SVR_ADDR, r))
Trace.Println("The End")
}