From b9f081d1aeb87186e7118ed796ab51eeac9b8fab Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 13 Feb 2019 19:19:39 +0100 Subject: implemented command to fetch list of languages --- cmd/dolmetschctld/statemachine.go | 22 ++++++++++++++++++++++ cmd/dolmetschctld/telnet.go | 2 +- cmd/dolmetschctld/web-static/socket.html | 23 +++++++++++++++++------ cmd/dolmetschctld/web_socket.go | 16 ++++++++++++++++ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go index 2cea91d..99416a5 100644 --- a/cmd/dolmetschctld/statemachine.go +++ b/cmd/dolmetschctld/statemachine.go @@ -95,6 +95,10 @@ type getLanguageReq struct { resCh chan Language } +type getLanguagesReq struct { + resCh chan []Language +} + type setOriginal2InterpreterRatioReq struct { ratio float32 resCh chan error @@ -119,6 +123,7 @@ type StateMachine struct { setLanguageCh chan setLanguageReq getLanguageCh chan getLanguageReq + getLanguagesCh chan getLanguagesReq setOriginal2InterpreterRatioCh chan setOriginal2InterpreterRatioReq getOriginal2InterpreterRatioCh chan getOriginal2InterpreterRatioReq getStateCh chan getStateReq @@ -181,6 +186,14 @@ func (sm *StateMachine) initMixer() { sm.state = StateSettled } +func (sm *StateMachine) getLanguages() (langs []Language) { + langs = append(langs, "none") + for lang, _ := range sm.languages { + langs = append(langs, lang) + } + return +} + // the "current language" is what is currently spoken on stage func (sm *StateMachine) setLanguage(l Language) error { if l == "none" { @@ -263,6 +276,8 @@ func (sm *StateMachine) run() { sm.reconcile(false) case req := <-sm.getLanguageCh: req.resCh <- sm.language + case req := <-sm.getLanguagesCh: + req.resCh <- sm.getLanguages() case req := <-sm.setOriginal2InterpreterRatioCh: req.resCh <- sm.setOriginal2InterpreterRatio(req.ratio) sm.reconcile(false) @@ -283,6 +298,7 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { sm := &StateMachine{mixer: m} sm.setLanguageCh = make(chan setLanguageReq, 10) sm.getLanguageCh = make(chan getLanguageReq, 10) + sm.getLanguagesCh = make(chan getLanguagesReq, 10) sm.setOriginal2InterpreterRatioCh = make(chan setOriginal2InterpreterRatioReq, 10) sm.getOriginal2InterpreterRatioCh = make(chan getOriginal2InterpreterRatioReq, 10) sm.getStateCh = make(chan getStateReq, 10) @@ -346,6 +362,12 @@ func (sm *StateMachine) GetLanguage() Language { return <-resCh } +func (sm *StateMachine) GetLanguages() []Language { + resCh := make(chan []Language) + sm.getLanguagesCh <- getLanguagesReq{resCh} + return <-resCh +} + func (sm *StateMachine) SetOriginal2InterpreterRatio(r float32) error { resCh := make(chan error) sm.setOriginal2InterpreterRatioCh <- setOriginal2InterpreterRatioReq{r, resCh} diff --git a/cmd/dolmetschctld/telnet.go b/cmd/dolmetschctld/telnet.go index 7bbf552..a5a4555 100644 --- a/cmd/dolmetschctld/telnet.go +++ b/cmd/dolmetschctld/telnet.go @@ -38,7 +38,7 @@ func telnetCmdLang(c *telgo.Client, args []string, sm *StateMachine) bool { return false } if len(args) == 1 { - c.Sayln("current language: %s", sm.GetLanguage()) + c.Sayln("current language: %s, available languages %v", sm.GetLanguage(), sm.GetLanguages()) return false } diff --git a/cmd/dolmetschctld/web-static/socket.html b/cmd/dolmetschctld/web-static/socket.html index b9bebbb..0dd88c5 100644 --- a/cmd/dolmetschctld/web-static/socket.html +++ b/cmd/dolmetschctld/web-static/socket.html @@ -26,13 +26,25 @@ function Controller(url) { this.sock = new WebSocket(url); this.sock_onmessage = function (event) { - $('#rawmsg').text(event.data); + data = JSON.parse(event.data) + if(data.type == 'languages') { + sel = $('#selectlang'); + sel.empty(); + for (var i=0; i < data.languages.length; i++) { + sel.append('') + } + } else { + $('#rawmsg').text(event.data); + } $('#buttonstate').removeAttr('disabled','disabled'); + $('#buttonlang').removeAttr('disabled','disabled'); } this.sock.onmessage = this.sock_onmessage.bind(this); this.sock_onopen = function() { + this.send({ command: "languages" }); $('#buttonstate').removeAttr('disabled','disabled'); + $('#buttonlang').removeAttr('disabled','disabled'); } this.sock.onopen = this.sock_onopen.bind(this); @@ -45,17 +57,16 @@ var ctrl = new Controller("ws://localhost:8234/api/v1/socket"); function state() { - req = { command: "state" }; - ctrl.send(req); + ctrl.send({ command: "state" }); } function set_language() { - req = { command: "language", arguments: [ $('#inputlang').val() ] }; - ctrl.send(req); + ctrl.send({ command: "language", arguments: [ $('#selectlang').val() ] }); } function init() { $('#buttonstate').attr('disabled','disabled') + $('#buttonslang').attr('disabled','disabled') } @@ -66,7 +77,7 @@
- +
diff --git a/cmd/dolmetschctld/web_socket.go b/cmd/dolmetschctld/web_socket.go index 9b5d74e..3879650 100644 --- a/cmd/dolmetschctld/web_socket.go +++ b/cmd/dolmetschctld/web_socket.go @@ -52,6 +52,11 @@ type webSocketResponseState struct { webState } +type webSocketResponseLanguages struct { + webSocketResponseBase + Languages []Language `json:"languages"` +} + func sendWebSocketResponse(ws *websocket.Conn, rd interface{}) { if err := ws.WriteJSON(rd); err != nil { log.Println("Web(socket) client", ws.RemoteAddr(), "write error:", err) @@ -79,6 +84,17 @@ func webSocketHandleCommand(ws *websocket.Conn, sm *StateMachine, req webSocketR result.Type = "state" result.State, result.Ratio, result.Lang = sm.GetState() sendWebSocketResponse(ws, result) + case "languages": + if len(req.Args) != 0 { + sendWebSocketErrorResponse(ws, http.StatusBadRequest, "command 'languages' expects no arguments") + return + } + + var result webSocketResponseLanguages + result.ResponseCode = http.StatusOK + result.Type = "languages" + result.Languages = sm.GetLanguages() + sendWebSocketResponse(ws, result) case "language": if len(req.Args) != 1 { sendWebSocketErrorResponse(ws, http.StatusBadRequest, "command 'language' expects exatly one argument") -- cgit v1.2.3