-
Notifications
You must be signed in to change notification settings - Fork 0
/
messagehandler.go
74 lines (57 loc) · 1.7 KB
/
messagehandler.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
package main
import (
"machine"
)
func HandleMessage(message WireTransmission) {
TurnOnLed() // so we can see transmissions
switch message.Headers.Get("type") {
// TODO: Implement message handling
case "ping":
wt := WireTransmission{
Headers: Headers{
{"type", "pong"},
},
}
_ = SendMessage(wt)
case "http":
url := message.Headers.Get("url")
_, _ = machine.Serial.Write([]byte("\n\rHTTP GET: " + url))
file, err := HttpGetFile(url)
_, _ = machine.Serial.Write([]byte("\n\rHTTP GET: " + url + " done"))
if err != nil {
_, _ = machine.Serial.Write([]byte("\n\rHTTP GET: " + url + " error: " + err.Error() + "\n\n"))
wt := WireTransmission{
Headers: Headers{
{"type", "http"},
{"url", url},
{"status", "error"},
{"responseTo", message.Headers.Get("id")},
},
Body: []byte(err.Error()),
}
_, _ = machine.Serial.Write([]byte("\n\rHTTP GET: " + url + " error response"))
err = SendMessage(wt)
if err != nil {
_, _ = machine.Serial.Write([]byte("\n\rHTTP GET: " + url + " error response error: " + err.Error()))
}
} else {
_, _ = machine.Serial.Write([]byte("\n\rHTTP GET: " + url + " success"))
wt := WireTransmission{
Headers: Headers{
{"type", "http"},
{"url", url},
{"status", "ok"},
{"responseTo", message.Headers.Get("id")},
},
Body: file,
}
_, _ = machine.Serial.Write([]byte("HTTP GET: " + url + " success response\n"))
err = SendMessage(wt)
if err != nil {
_, _ = machine.Serial.Write([]byte("HTTP GET: " + url + " success response error: " + err.Error() + "\n"))
}
_, _ = machine.Serial.Write([]byte("HTTP GET: " + url + " success response sent\n"))
}
}
return
}