-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (47 loc) · 1.5 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
package main
import (
"github.com/stianeikeland/go-rpio"
"github.com/gorilla/mux"
"time"
"io"
"net/http"
"github.com/GeertJohan/go.rice"
)
// create a map/dictionary to map the door # to the pin #
// This could also be a name or any string
var pinmap map[string]int
func main() {
pinmap = make(map[string]int) // have to "make" the map in memory
pinmap["1"] = 2 // set door 1 to pin 2
//Create a new Gorilla Router
r := mux.NewRouter()
//Add the route to push the button.
//Notice the {id} part. This will be parsed into id returned from mux.Vars
r.HandleFunc("/push/{id}",PushButton).Methods("POST")
//Create a "box" to "rice the static content
r.PathPrefix("/").Handler(http.FileServer(rice.MustFindBox("static").HTTPBox()))
http.ListenAndServe(":8002",r) // start the web server on port 8001
}
func PushButton(w http.ResponseWriter, r *http.Request) {
//Grab the variables
vars := mux.Vars(r)
if p, ok := pinmap[vars["id"]]; ok {
// All our same code here, we are just using a variable now
err := rpio.Open()
if err != nil {
panic(err)
}
pin := rpio.Pin(p)
pin.Output()
pin.Low()
time.Sleep(time.Millisecond * 100)
pin.High()
time.Sleep(time.Millisecond * 100)
rpio.Close()
io.WriteString(w,"pushed!")
} else {
// Pin was not found in pinmap. Set status code 404
w.WriteHeader(http.StatusNotFound)
io.WriteString(w,"unknown pin!")
}
}