-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcec-web.go
More file actions
104 lines (83 loc) · 2.21 KB
/
cec-web.go
File metadata and controls
104 lines (83 loc) · 2.21 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
package main
import (
"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
"github.com/chbmuc/cec"
"os"
)
type Options struct {
Host string `short:"i" long:"ip" description:"ip to listen on" default:"127.0.0.1"`
Port string `short:"p" long:"port" description:"tcp port to listen on" default:"8080"`
Adapter string `short:"a" long:"adapter" description:"cec adapter to connect to [RPI, usb, ...]"`
Name string `short:"n" long:"name" description:"OSD name to announce on the cec bus" default:"REST Gateway"`
}
var options Options
var parser = flags.NewParser(&options, flags.Default)
func main() {
if _, err := parser.Parse(); err != nil {
os.Exit(1)
}
cec.Open(options.Adapter, options.Name)
r := gin.Default()
r.GET("/info", info)
r.GET("/power/:device", power_status)
r.PUT("/power/:device", power_on)
r.DELETE("/power/:device", power_off)
r.PUT("/volume/up", vol_up)
r.PUT("/volume/down", vol_down)
r.PUT("/volume/mute", vol_mute)
//r.POST("/key/:device", key_post)
r.PUT("/key/:device/:key", key)
r.POST("/transmit", transmit)
r.Run(options.Host + ":" + options.Port)
}
func info(c *gin.Context) {
c.JSON(200, cec.List())
}
func power_on(c *gin.Context) {
addr := cec.GetLogicalAddressByName(c.Params.ByName("device"))
cec.PowerOn(addr)
c.String(204, "")
}
func power_off(c *gin.Context) {
addr := cec.GetLogicalAddressByName(c.Params.ByName("device"))
cec.Standby(addr)
c.String(204, "")
}
func power_status(c *gin.Context) {
addr := cec.GetLogicalAddressByName(c.Params.ByName("device"))
status := cec.GetDevicePowerStatus(addr)
if status == "on" {
c.String(204, "")
} else if status == "standby" {
c.String(404, "")
} else {
c.String(500, "invalid power state")
}
}
func transmit(c *gin.Context) {
var commands []string
c.Bind(&commands)
for _, val := range commands {
cec.Transmit(val)
}
c.String(204, "")
}
func vol_up(c *gin.Context) {
cec.VolumeUp()
c.String(204, "")
}
func vol_down(c *gin.Context) {
cec.VolumeDown()
c.String(204, "")
}
func vol_mute(c *gin.Context) {
cec.Mute()
c.String(204, "")
}
func key(c *gin.Context) {
addr := cec.GetLogicalAddressByName(c.Params.ByName("device"))
key := c.Params.ByName("key")
cec.Key(addr, key)
c.String(204, "")
}