summaryrefslogtreecommitdiff
path: root/cmd/dolmetschctl
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/dolmetschctl')
-rw-r--r--cmd/dolmetschctl/dolmetschctl.go73
1 files changed, 59 insertions, 14 deletions
diff --git a/cmd/dolmetschctl/dolmetschctl.go b/cmd/dolmetschctl/dolmetschctl.go
index bfe9a92..9e878a8 100644
--- a/cmd/dolmetschctl/dolmetschctl.go
+++ b/cmd/dolmetschctl/dolmetschctl.go
@@ -36,6 +36,12 @@ import (
"spreadspace.org/dolmetschctl/pkg/types"
)
+const (
+ blinkTime = 20
+ lang1 = "en"
+ lang2 = "de"
+)
+
func wsReader(ws *websocket.Conn, ch chan<- types.WebSocketResponseFull) {
defer close(ch)
@@ -67,6 +73,53 @@ func wsReader(ws *websocket.Conn, ch chan<- types.WebSocketResponseFull) {
return
}
+func handleControllerEvent(ctrl *controller.Controller, ws *websocket.Conn, ev controller.Event) error {
+ if ev.Type != controller.ButtonPressed {
+ return nil
+ }
+ lang := ""
+ switch ev.Button {
+ case 0:
+ lang = "en"
+ case 1:
+ lang = "de"
+ default:
+ lang = "none"
+ }
+ return ws.WriteJSON(types.WebSocketRequest{Command: "language", Args: []string{lang}})
+}
+
+func ledOnOrBlink(ctrl *controller.Controller, num uint8, state types.State) {
+ if state == types.StateSettled {
+ ctrl.LedOn(num)
+ } else {
+ ctrl.LedBlink(num, blinkTime)
+ }
+}
+
+func handleWebSocketMessage(ctrl *controller.Controller, ws *websocket.Conn, msg types.WebSocketResponseFull) error {
+ if msg.Type == "state" {
+ switch msg.Lang {
+ case lang1:
+ ledOnOrBlink(ctrl, 0, msg.State)
+ ctrl.LedOff(1)
+ ctrl.LedOff(2)
+ ctrl.LedOff(3)
+ case lang2:
+ ctrl.LedOff(0)
+ ledOnOrBlink(ctrl, 1, msg.State)
+ ctrl.LedOff(2)
+ ctrl.LedOff(3)
+ default:
+ ctrl.LedOff(0)
+ ctrl.LedOff(1)
+ ledOnOrBlink(ctrl, 2, msg.State)
+ ledOnOrBlink(ctrl, 3, msg.State)
+ }
+ }
+ return nil
+}
+
func run(ctrl *controller.Controller, ws *websocket.Conn) error {
ctrlCh := make(chan controller.Event, 100)
ctrl.Subscribe(ctrlCh)
@@ -77,6 +130,7 @@ func run(ctrl *controller.Controller, ws *websocket.Conn) error {
if err := ws.WriteJSON(types.WebSocketRequest{Command: "subscribe"}); err != nil {
return err
}
+ log.Printf("subscribed to state changes!")
for {
select {
@@ -84,26 +138,16 @@ func run(ctrl *controller.Controller, ws *websocket.Conn) error {
if !ok {
return fmt.Errorf("controller channel was closed.")
}
- if ev.Type != controller.ButtonPressed {
- continue
- }
- lang := ""
- switch ev.Button {
- case 0:
- lang = "en"
- case 1:
- lang = "de"
- default:
- lang = "none"
- }
- if err := ws.WriteJSON(types.WebSocketRequest{Command: "language", Args: []string{lang}}); err != nil {
+ if err := handleControllerEvent(ctrl, ws, ev); err != nil {
return err
}
case msg, ok := <-wsCh:
if !ok {
return fmt.Errorf("websocket channel was closed.")
}
- log.Printf("got message from websocket: %v", msg)
+ if err := handleWebSocketMessage(ctrl, ws, msg); err != nil {
+ return err
+ }
}
}
return nil
@@ -130,6 +174,7 @@ func main() {
if err != nil {
log.Printf("Error connecting to daemon: %v", err)
} else {
+ log.Printf("successfully conncted!")
err := run(c, ws)
log.Printf("run() returned: %v", err)
}