forked from guiguiabloc/rpisurv-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpisurv-api2.go
58 lines (45 loc) · 1.32 KB
/
rpisurv-api2.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/yunginnanet/sendkeys"
)
func pressKey(key string) {
err := sendkeys.Send(key)
if err != nil {
panic(err)
}
}
func handleCamera(w http.ResponseWriter, r *http.Request, camera string, key string) {
rstatus, err := ioutil.ReadFile("/tmp/rpi.status")
if err != nil {
fmt.Fprintf(w, "Error")
return
}
err = ioutil.WriteFile("/tmp/rpi.camera", []byte(camera), 0644)
if err != nil {
fmt.Fprintf(w, "Error")
return
}
pressKey(key)
fmt.Fprintf(w, `{"status": "%s", "camera": "%s"}`, rstatus, camera)
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Server Works!")
})
http.HandleFunc("/camera/1", func(w http.ResponseWriter, r *http.Request) {
handleCamera(w, r, "1", "{F1}")
})
http.HandleFunc("/camera/2", func(w http.ResponseWriter, r *http.Request) {
handleCamera(w, r, "2", "{F2}")
})
http.HandleFunc("/camera/3", func(w http.ResponseWriter, r *http.Request) {
handleCamera(w, r, "3", "{F3}")
})
http.HandleFunc("/camera/4", func(w http.ResponseWriter, r *http.Request) {
handleCamera(w, r, "4", "{F4}")
})
http.ListenAndServe(":5000", nil)
}