summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2019-02-13 19:19:39 +0100
committerChristian Pointner <equinox@spreadspace.org>2019-02-13 19:19:39 +0100
commitb9f081d1aeb87186e7118ed796ab51eeac9b8fab (patch)
tree0a21dece426ecd7f002af26063e444f00bdcb23c
parentimplemented set_language command via web sockets (diff)
implemented command to fetch list of languages
-rw-r--r--cmd/dolmetschctld/statemachine.go22
-rw-r--r--cmd/dolmetschctld/telnet.go2
-rw-r--r--cmd/dolmetschctld/web-static/socket.html23
-rw-r--r--cmd/dolmetschctld/web_socket.go16
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('<option>' + data.languages[i] + '</option>')
+ }
+ } 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')
}
</script>
</head>
@@ -66,7 +77,7 @@
<div>
<button id="buttonstate" onclick="state()">get state</button>
<button id="buttonlang" onclick="set_language()">set language</button>
- <input type="text" id="inputlang" length="10">
+ <select id="selectlang"></select>
<div id="rawmsg" class="data"></div>
</div>
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")