summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/dolmetschctld/dolmetschctld.go39
-rw-r--r--cmd/dolmetschctld/statemachine.go5
-rw-r--r--cmd/dolmetschctld/web.go89
3 files changed, 129 insertions, 4 deletions
diff --git a/cmd/dolmetschctld/dolmetschctld.go b/cmd/dolmetschctld/dolmetschctld.go
index 4c31dbf..cc75319 100644
--- a/cmd/dolmetschctld/dolmetschctld.go
+++ b/cmd/dolmetschctld/dolmetschctld.go
@@ -40,6 +40,9 @@ func addLanguage(sm *StateMachine, name Language, original, interpreter mixer.Ch
func main() {
log.Println("hello world.")
+ // *************************************
+ // * initialize core
+
m, err := mixer.NewMixer(mixer.Config{DevIn: "ESI MIDIMATE eX MIDI 2", DevOut: "ESI MIDIMATE eX MIDI 1"})
if err != nil {
log.Printf("Error opening the mixer: %v", err)
@@ -71,16 +74,44 @@ func main() {
sm.Start()
defer sm.Shutdown()
+ // *************************************
+ // * initialize control interfaces
+
var telnet *TelnetInterface
if telnet, err = NewTelnetInterface("127.0.0.1:1234", sm); err != nil {
- log.Printf("creating telnet control interface failed: %v", err)
+ log.Printf("creating telnet interface failed: %v", err)
os.Exit(1)
}
log.Printf("telnet interface successfully initialized!")
- if err = telnet.Run(); err != nil {
- log.Printf("running telnet interface failed: %v", err)
+ var web *WebInterface
+ if web, err = NewWebInterface("127.0.0.1:8234", sm); err != nil {
+ log.Printf("creating web interface failed: %v", err)
+ os.Exit(1)
}
+ log.Printf("web interface successfully initialized!")
+
+ // *************************************
+ // * run control interfaces
+
+ stop := make(chan bool)
+
+ go func() {
+ log.Printf("starting telnet interface")
+ if err = telnet.Run(); err != nil {
+ log.Printf("running telnet interface failed: %v", err)
+ }
+ stop <- true
+ }()
+
+ go func() {
+ log.Printf("starting web interface")
+ if err = web.Run(); err != nil {
+ log.Printf("running web interface failed: %v", err)
+ }
+ stop <- true
+ }()
- log.Printf("exiting.")
+ <-stop
+ log.Printf("at least one control interface has stopped - bringing down the whole process")
}
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go
index b1e74c7..7082460 100644
--- a/cmd/dolmetschctld/statemachine.go
+++ b/cmd/dolmetschctld/statemachine.go
@@ -51,6 +51,11 @@ func (s State) String() string {
}
}
+func (s State) MarshalText() (data []byte, err error) {
+ data = []byte(s.String())
+ return
+}
+
type Language string
func (l Language) String() string {
diff --git a/cmd/dolmetschctld/web.go b/cmd/dolmetschctld/web.go
new file mode 100644
index 0000000..ad42429
--- /dev/null
+++ b/cmd/dolmetschctld/web.go
@@ -0,0 +1,89 @@
+//
+// dolmetschctl
+//
+//
+// Copyright (C) 2019 Christian Pointner <equinox@spreadspace.org>
+//
+// This file is part of dolmetschctl.
+//
+// dolmetschctl is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// any later version.
+//
+// dolmetschctl is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with dolmetschctl. If not, see <http://www.gnu.org/licenses/>.
+//
+
+package main
+
+import (
+ "encoding/json"
+ "net/http"
+ "time"
+)
+
+type WebInterface struct {
+ server *http.Server
+}
+
+type webHandler struct {
+ sm *StateMachine
+ H func(*StateMachine, http.ResponseWriter, *http.Request)
+}
+
+func (self webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ self.H(self.sm, w, r)
+}
+
+func webSendResponse(w http.ResponseWriter, status int, respdata interface{}) {
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(status)
+ encoder := json.NewEncoder(w)
+ encoder.Encode(respdata)
+}
+
+type webError struct {
+ ResponseCode int `json:"response-code"`
+ Error string `json:"error"`
+}
+
+func webSendErrorResponse(w http.ResponseWriter, status int, error string) {
+ webSendResponse(w, status, webError{ResponseCode: status, Error: error})
+}
+
+type webState struct {
+ State State `json:"state"`
+ Ratio float32 `json:"original-to-interpreter-ratio"`
+ Lang Language `json:"language"`
+}
+
+func webGetStateHandler(sm *StateMachine, w http.ResponseWriter, r *http.Request) {
+ if r.Method != "GET" {
+ webSendErrorResponse(w, http.StatusMethodNotAllowed, "only GET method is allowed")
+ return
+ }
+
+ var result webState
+ result.State, result.Ratio, result.Lang = sm.GetState()
+ webSendResponse(w, http.StatusOK, result)
+}
+
+func (web *WebInterface) Run() error {
+ return web.server.ListenAndServe()
+}
+
+func NewWebInterface(addr string, sm *StateMachine) (web *WebInterface, err error) {
+ web = &WebInterface{}
+
+ http.Handle("/api/v1/state", webHandler{sm, webGetStateHandler})
+ // TODO: add other handler
+
+ web.server = &http.Server{Addr: addr, ReadTimeout: 2 * time.Hour, WriteTimeout: 2 * time.Hour}
+ return
+}