add volume control

This commit is contained in:
Simon Vieille 2023-08-24 20:33:21 +02:00
parent 6c524e2c41
commit 40130a5d2e
Signed by: deblan
GPG key ID: 579388D585F70417
2 changed files with 45 additions and 1 deletions

46
main.go
View file

@ -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 &&

Binary file not shown.