-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (123 loc) · 3.76 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
package main
import (
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"queue"
"time"
//"fmt"
"io"
"net/http"
)
func addPosition(json string) {
log.Info("trying to connect")
valid, riderObject := queue.IsValidGPSLocationJSON(json)
log.Info(json)
if valid {
riderObject.Timestamp = time.Now().UnixNano() // timestamp as soon as we can.
log.Info("adding json to server")
queue.AddNewPosition(*riderObject)
} else {
log.Info("json invalid, nothing added to queue")
}
}
// just add , no response data
func addNoJsonResponse(w http.ResponseWriter, req *http.Request) {
json := req.FormValue("gps")
go addPosition(json)
w.WriteHeader(http.StatusOK)
}
//f08db884-f205-4153-9211-8b29245bbd89
func handleNewEntries(w http.ResponseWriter, req *http.Request) {
log.Info("trying to connect")
json := req.FormValue("gps")
t_param, d_param, err := queue.ConvertTimeDistanceParams(req.FormValue("timespan"), req.FormValue("distance"))
//log.Info("timespan is ", t_param , " distance span is ", d_param)
if err != nil {
log.Info("Invalid time and/or distance params sent from client, using defaults as fallback")
t_param, d_param = queue.GetDefaultParams()
}
valid, riderObject := queue.IsValidGPSLocationJSON(json)
log.Info(json)
if valid {
riderObject.Timestamp = time.Now().UnixNano() // timestamp as soon as we can.
queue.AddNewPosition(*riderObject)
// warninglist := queue.RetrieveCollisionList(*riderObject)
warninglist := queue.RetrieveCollisionList_2(*riderObject, t_param, d_param)
ajsonlist := queue.GetWarninglistJSON(warninglist)
w.WriteHeader(http.StatusOK)
io.WriteString(w, ajsonlist)
} else {
log.Info("Invalid json sent from client")
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "server could not parse json parameter")
}
}
/*
{
"lat":<val> latitude
"long":<val> longitude
"time":<val> timespan in seconds
"dist":<val> distance in meters
}
*/
func handleGPSFence(w http.ResponseWriter, req *http.Request) {
json := req.FormValue("search")
log.Info(json)
valid, searchObject := queue.IsValidSearchstructJSON(json)
if valid {
GPSSearchObject := &queue.GPSLocation{
Location: queue.Locationdata{
Latitude: searchObject.Latitude,
Longitude: searchObject.Longitude,
Accuracy: 1,
Payload: "{}",
},
Gpsobject: 0,
UI: uuid.UUID{},
Timestamp: time.Now().UnixNano(),
}
//timespan := searchObject.Timespan
//distance := searchObject.Distance
list := queue.RetrieveCollisionList_2(*GPSSearchObject, searchObject.Timespan, searchObject.Distance, 30)
json := queue.ConvertToMapBoxFreindlyJSON(list)
if json == "" {
log.Info("Invalid json sent from web client")
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "server could not parse json parameter")
} else {
w.WriteHeader(http.StatusOK)
io.WriteString(w, json)
}
} else {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "server not valid json parameter")
}
}
// to be able to check if it is alive from
func pingHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(" HTTP status(200) OK code returned running: release-1.0.9-demo "))
}
func ServeMap() error {
http.Handle("/", http.FileServer(http.Dir("./static")))
return http.ListenAndServe(":8081", nil)
}
// every 100 ms seconds dispose...
func Dispose() {
for {
queue.Remove()
var sleep = time.Millisecond * 25
time.Sleep(sleep)
}
}
func main() {
log.Info("starting server ...")
go Dispose()
http.HandleFunc("/addposnoret", addNoJsonResponse)
http.HandleFunc("/addposition", handleNewEntries)
http.HandleFunc("/retrieve", handleGPSFence)
http.HandleFunc("/version", pingHandler)
go log.Fatal(ServeMap())
log.Fatal(http.ListenAndServe(":8081", nil))
log.Info("closing server down ... ")
}