-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweatherserver.go
132 lines (115 loc) · 3.01 KB
/
weatherserver.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
package main
import (
"../ADDS"
"./RockBLOCK"
"database/sql"
"encoding/hex"
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/http"
"strings"
"time"
)
var db *sql.DB
// /metar/{IDENT}
func handleMETARRequest(w http.ResponseWriter, r *http.Request) {
path := strings.Trim(r.URL.Path, "/")
x := strings.Split(path, "/")
if len(x) < 2 {
http.Error(w, "Bad request.", http.StatusBadRequest)
return
}
metar, err := ADDS.GetLatestADDSMETARs(x[1])
if err == nil {
w.Write([]byte(metar.Text + "\n"))
}
}
// /receiveRockBLOCK
func handleRockBLOCKMsg(w http.ResponseWriter, r *http.Request) {
// Decode the form into 'RockBLOCKCOREIncoming'.
var msg RockBLOCK.RockBLOCKCOREIncoming
if err := r.ParseForm(); err != nil {
http.Error(w, "Form could not be decoded", http.StatusBadRequest)
return
}
// Ugly, but both gorilla/schema and ajg/form are unreliable here.
if v, ok := r.Form["imei"]; ok {
msg.IMEI = v[0]
}
if v, ok := r.Form["momsn"]; ok {
msg.MOMSN = v[0]
}
if v, ok := r.Form["transmit_time"]; ok {
msg.TransmitTime = v[0]
}
if v, ok := r.Form["iridium_latitude"]; ok {
msg.IridiumLat = v[0]
}
if v, ok := r.Form["iridium_longitude"]; ok {
msg.IridiumLng = v[0]
}
if v, ok := r.Form["iridium_cep"]; ok {
msg.IridiumCEP = v[0]
}
if v, ok := r.Form["data"]; ok {
d, err := hex.DecodeString(v[0])
if err != nil {
msg.Data = v[0]
} else {
msg.Data = string(d)
}
}
// Process the message.
msg.Process()
x := strings.Split(msg.Data, ",")
var TransmitInitTime time.Time
var GPSLat string
var GPSLng string
if len(x) == 3 {
TransmitInitTime.UnmarshalText([]byte(x[0]))
GPSLat = x[1]
GPSLng = x[2]
}
_, err := db.Exec(`INSERT INTO log SET IMEI=?, MOMSN=?, TransmitTime=?, IridiumLat=?, IridiumLng=?, IridiumCEP=?, InsertTime=NOW(), TransmitInitTime=?, GPSlat=?, GPSLng=?, Data=?`,
msg.IMEI, msg.MOMSN, msg.TransmitTime, msg.IridiumLat, msg.IridiumLng, msg.IridiumCEP, TransmitInitTime, GPSLat, GPSLng, msg.Data)
if err != nil {
fmt.Printf("error inserting stats row to db: %s\n", err.Error())
}
// See if this is a plaintext weather request.
if strings.HasPrefix(msg.Data, "METAR ") {
x := strings.Split(msg.Data, " ")
metar, err := ADDS.GetLatestADDSMETARs(x[1])
if err == nil {
m := new(RockBLOCK.RockBLOCKCOREOutgoing)
m.IMEI = RockBLOCK.TEST_IMEI
m.Data = []byte(metar.Text)
if len(m.Data) > 50 {
m.Data = m.Data[:50]
}
a, err := m.Send()
fmt.Printf("attempt to send METAR %s\n", m.Data)
if err != nil {
fmt.Printf("a=%s, err=%s\n", a, err.Error())
} else {
fmt.Printf("a=%s\n", a)
}
}
}
}
func main() {
db2, err := sql.Open("mysql", "root:@/iridium")
if err != nil {
fmt.Printf("dbWriter(): db connect error: %s\n", err.Error())
return
}
db = db2
http.HandleFunc("/metar/", handleMETARRequest)
http.HandleFunc("/receiveRockBLOCK", handleRockBLOCKMsg)
err = http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Printf("managementInterface ListenAndServe: %s\n", err.Error())
}
for {
time.Sleep(1 * time.Second)
}
}