summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2019-02-14 00:36:01 +0100
committerChristian Pointner <equinox@spreadspace.org>2019-02-14 00:36:01 +0100
commit310c1fd10c7f1290c6cbb1e21b8a7007ac82c1f5 (patch)
tree61a91a3d3edd688cd5b0752b7e5990ee8d30a95b /cmd
parentactually send switch language commands (diff)
make code a little nicer
Diffstat (limited to 'cmd')
-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)
}