/
go-wink.go
70 lines (55 loc) · 1.12 KB
/
go-wink.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
package main
import (
"fmt"
"os/exec"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
var (
command = "/usr/sbin/aprontest"
ledCommand = "/usr/sbin/set_rgb"
)
func setRGB(args []string) {
_, err := exec.Command(ledCommand, args...).Output()
if err != nil {
fmt.Println("Error setting LED")
}
}
func executeAction(c *gin.Context) {
var args []string
for i := 0; i < 10; i++ {
operation := c.Query(strconv.Itoa(i))
if len(operation) > 0 {
args = append(args, operation)
} else {
break
}
}
fmt.Println(args)
//Turn LED Blue to Indicate event.
setRGB([]string{"0", "255", "255"})
time.Sleep(1 * time.Second)
statusCode := 200
out, err := exec.Command(command, args...).Output()
if err != nil {
statusCode = 500
//Flash LED red to indicate Error
setRGB([]string{"255", "0", "0"})
time.Sleep(1 * time.Second)
}
//Turn LED back to green
setRGB([]string{"0", "255", "0"})
c.JSON(statusCode, gin.H{
"status": "received",
"output": string(out[:]),
})
}
func main() {
router := gin.Default()
v1 := router.Group("/v1")
{
v1.GET("/action", executeAction)
}
router.Run(":4141")
}