add volume control
This commit is contained in:
parent
6c524e2c41
commit
40130a5d2e
46
main.go
46
main.go
|
@ -28,6 +28,11 @@ type PointerMessageData struct {
|
|||
Click string `json:click`
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
Type string `json:"type"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
func getSimpleMessageValue(msg string) string {
|
||||
data := SimpleMessageData{}
|
||||
json.Unmarshal([]byte(msg), &data)
|
||||
|
@ -35,6 +40,11 @@ func getSimpleMessageValue(msg string) string {
|
|||
return data.Value
|
||||
}
|
||||
|
||||
func sendMessageResponse(ws *websocket.Conn, r MessageResponse) {
|
||||
value, _ := json.Marshal(r)
|
||||
websocket.Message.Send(ws, string(value))
|
||||
}
|
||||
|
||||
func ws(c echo.Context) error {
|
||||
var actions = Actions{
|
||||
Functions: make(map[string]func(ws *websocket.Conn, msg string) error),
|
||||
|
@ -123,7 +133,40 @@ func ws(c echo.Context) error {
|
|||
})
|
||||
|
||||
actions.add("volume", func(ws *websocket.Conn, msg string) error {
|
||||
return nil
|
||||
value := getSimpleMessageValue(msg)
|
||||
|
||||
if value == "" {
|
||||
return errors.New("Invalid value")
|
||||
}
|
||||
|
||||
if value == "up" {
|
||||
cmd := exec.Command("amixer", "set", "Master", "2%+")
|
||||
sendMessageResponse(ws, MessageResponse{
|
||||
Type: "response",
|
||||
Value: "Volume up",
|
||||
})
|
||||
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
if value == "down" {
|
||||
cmd := exec.Command("amixer", "set", "Master", "2%-")
|
||||
sendMessageResponse(ws, MessageResponse{
|
||||
Type: "response",
|
||||
Value: "Volume down",
|
||||
})
|
||||
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
cmd := exec.Command("amixer", "set", "Master", fmt.Sprintf("%s%%", value))
|
||||
|
||||
sendMessageResponse(ws, MessageResponse{
|
||||
Type: "response",
|
||||
Value: fmt.Sprintf("Volume set to %s%%", value),
|
||||
})
|
||||
|
||||
return cmd.Run()
|
||||
})
|
||||
|
||||
actions.add("media", func(ws *websocket.Conn, msg string) error {
|
||||
|
@ -236,6 +279,7 @@ func ws(c echo.Context) error {
|
|||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
e.HideBanner = true
|
||||
|
||||
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
|
||||
if subtle.ConstantTimeCompare([]byte(username), []byte("admin")) == 1 &&
|
||||
|
|
BIN
remote-i3wm-go
BIN
remote-i3wm-go
Binary file not shown.
Loading…
Reference in a new issue