-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (58 loc) · 1.71 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
package main
import (
"fmt"
"github.com/stianeikeland/go-rpio/v4"
"log"
"net"
"net/http"
"net/url"
"strconv"
"strings"
)
func main() {
if err := rpio.Open(); err != nil {
log.Fatal(err)
}
defer rpio.Close()
socket, err := net.Listen("tcp", "localhost:8090")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Listening on http://%s\n", socket.Addr().String())
http.HandleFunc("/pins/", func(w http.ResponseWriter, r *http.Request) {
// http://localhost:8090/pins/17?status=on
pinNr, parsePinErr := strconv.Atoi(r.URL.Path[len("/pins/"):])
if parsePinErr != nil {
log.Printf("an unexpected error occured: %v", parsePinErr)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
queryParams, parseQueryErr := url.ParseQuery(r.URL.RawQuery)
if parseQueryErr != nil {
log.Printf("an unexpected error occured: %v", parseQueryErr)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
var status string
if len(queryParams["status"]) == 0 {
log.Println("'status' query parameter is missing")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
status = queryParams["status"][0]
if !strings.EqualFold(status, "on") && !strings.EqualFold(status, "off") {
log.Printf("'status' query parameter must be 'on' or 'off' but was '%v'\n", status)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
fmt.Printf("turn pin %v %v\n", pinNr, status)
pin := rpio.Pin(pinNr)
pin.Output()
if strings.EqualFold(status, "on") {
pin.Low()
} else {
pin.High()
}
})
log.Fatal(http.Serve(socket, nil))
}