-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroundcontrol.go
More file actions
137 lines (110 loc) · 2.94 KB
/
Copy pathgroundcontrol.go
File metadata and controls
137 lines (110 loc) · 2.94 KB
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
package main
import (
"bytes"
"flag"
"fmt"
"image"
"image/color"
"image/jpeg"
"log"
"net/http"
"strconv"
"strings"
)
var (
listenAddr = flag.String("listen", "localhost:8001", "HTTP listen address")
)
func main() {
flag.Parse()
g := new(Groundcontrol)
go g.run()
http.Handle("/tmp/data.png", g)
http.Handle("/", http.FileServer(http.Dir("./web")))
log.Fatal(http.ListenAndServe(*listenAddr, nil))
}
type Groundcontrol struct {
datapoints []string
}
func (g *Groundcontrol) run() {
for {
message, err := reachout()
if err != nil {
log.Printf("Error reading spaceship response: %v", err)
continue
}
g.datapoints = append(g.datapoints, message)
}
}
// reachout to the spaceship.
func reachout() (string, error) {
// --------------------
// START HACKING HERE
// --------------------
// 1. Use the http.Get to access the spaceship
// 2. Don't forget closing the response body using 'defer'
// 3. When the response statuscode isn't a 200 return an error using fmt.Errof()
// 4. Next step is to read the body from the response using ioutil.ReadAll()
// 5. Cast the body to a string and return this function
// 6. That's it, use go run grouncontrol to test this.
}
func (g Groundcontrol) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m := image.NewRGBA(image.Rect(0, 0, 1261, 732))
for _, datapoint := range g.datapoints {
data := strings.Split(datapoint, ";")
col, err := convertDataToColor(data)
if err != nil {
log.Printf("error convert datapoint %v", data)
continue
}
x, err := strconv.Atoi(data[0])
if err != nil {
log.Printf("Unsupported x value %v", data[0])
continue
}
y, err := strconv.Atoi(data[1])
if err != nil {
log.Printf("Unsupported y value %v", data[1])
continue
}
m.Set(y, x, col)
}
var img image.Image = m
writeImage(w, &img)
}
func convertDataToColor(data []string) (color.RGBA, error) {
var c color.RGBA
i, err := strconv.Atoi(data[2])
if err != nil {
return c, fmt.Errorf("Unsupported red value %v", data[2])
}
r := uint8(i >> 8)
i, err = strconv.Atoi(data[3])
if err != nil {
return c, fmt.Errorf("Unsupported green value %v", data[3])
}
g := uint8(i >> 8)
i, err = strconv.Atoi(data[4])
if err != nil {
return c, fmt.Errorf("Unsupported blue value %v", data[4])
}
b := uint8(i >> 8)
i, err = strconv.Atoi(data[5])
if err != nil {
return c, fmt.Errorf("Unsupported alpha value %v", data[5])
}
a := uint8(i >> 8)
c = color.RGBA{r, g, b, a}
return c, nil
}
// writeImage encodes an image 'img' in jpeg format and writes it into ResponseWriter.
func writeImage(w http.ResponseWriter, img *image.Image) {
buffer := new(bytes.Buffer)
if err := jpeg.Encode(buffer, *img, nil); err != nil {
log.Println("unable to encode image.")
}
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))
if _, err := w.Write(buffer.Bytes()); err != nil {
log.Println("unable to write image.")
}
}