diff options
author | Christian Pointner <equinox@spreadspace.org> | 2019-02-10 22:19:10 +0100 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2019-02-10 22:19:10 +0100 |
commit | d4513abfcc27e8b7cf4f54f3513cdb865e2dcc76 (patch) | |
tree | eed519ee0bff78220099bb56334ae45cc788c4d0 /cmd/dolmetschctld | |
parent | added getter for ratio and lang (diff) |
added function to get state
Diffstat (limited to 'cmd/dolmetschctld')
-rw-r--r-- | cmd/dolmetschctld/statemachine.go | 34 | ||||
-rw-r--r-- | cmd/dolmetschctld/telnet.go | 10 |
2 files changed, 34 insertions, 10 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go index 5e27359..c7f4ac5 100644 --- a/cmd/dolmetschctld/statemachine.go +++ b/cmd/dolmetschctld/statemachine.go @@ -94,6 +94,10 @@ type getMain2VoiceRatioRequest struct { resultCh chan float32 } +type getStateRequest struct { + resultCh chan State +} + type StateMachine struct { mixer *mixer.Mixer @@ -101,6 +105,7 @@ type StateMachine struct { getLanguageCh chan getLanguageRequest setMain2VoiceRatioCh chan setMain2VoiceRatioRequest getMain2VoiceRatioCh chan getMain2VoiceRatioRequest + getStateCh chan getStateRequest quitCh chan bool exitedCh chan struct{} mixerEventCh chan mixer.Event @@ -110,8 +115,8 @@ type StateMachine struct { main2VoiceRatio float32 - currentState State - language Language + state State + language Language } func (sm *StateMachine) handleMixerEvent(ev mixer.Event) { @@ -157,7 +162,7 @@ func (sm *StateMachine) initMixer() { mcs.voice.target.level = mixer.FaderLevelOff } sm.language = "" - sm.currentState = StateSettled + sm.state = StateSettled } // the "current language" is what is currently spoken on stage @@ -207,18 +212,18 @@ func (sm *StateMachine) reconcile(ticker bool) { } } - if sm.currentState != StateSettled && !ticker { + if sm.state != StateSettled && !ticker { return } - sm.currentState = StateSettled + sm.state = StateSettled for _, mcs := range sm.languages { if mcs.main.target.level != mcs.main.current.level { sm.mixer.SetLevel(mcs.main.num, calcNextLevel(mcs.main.target.level, mcs.main.current.level)) - sm.currentState = StateSettling + sm.state = StateSettling } if mcs.voice.target.level != mcs.voice.current.level { sm.mixer.SetLevel(mcs.voice.num, calcNextLevel(mcs.voice.target.level, mcs.voice.current.level)) - sm.currentState = StateSettling + sm.state = StateSettling } } } @@ -231,7 +236,7 @@ func (sm *StateMachine) run() { for { select { case <-t.C: - if sm.currentState == StateSettling { + if sm.state == StateSettling { sm.reconcile(true) } case req := <-sm.setLanguageCh: @@ -244,6 +249,8 @@ func (sm *StateMachine) run() { sm.reconcile(false) case req := <-sm.getMain2VoiceRatioCh: req.resultCh <- sm.main2VoiceRatio + case req := <-sm.getStateCh: + req.resultCh <- sm.state case ev := <-sm.mixerEventCh: sm.handleMixerEvent(ev) sm.reconcile(false) @@ -259,6 +266,7 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { sm.getLanguageCh = make(chan getLanguageRequest, 10) sm.setMain2VoiceRatioCh = make(chan setMain2VoiceRatioRequest, 10) sm.getMain2VoiceRatioCh = make(chan getMain2VoiceRatioRequest, 10) + sm.getStateCh = make(chan getStateRequest, 10) sm.quitCh = make(chan bool, 1) sm.exitedCh = make(chan struct{}) sm.mixerEventCh = make(chan mixer.Event, 1000) @@ -266,14 +274,14 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { sm.languages = make(map[Language]*MixerChannels) sm.channel2lang = make(map[mixer.Channel]Language) - sm.currentState = StateNew + sm.state = StateNew sm.language = "" return sm, nil } // TODO: currently we can only deal with 2 languages... func (sm *StateMachine) AddLanguage(name Language, main, voice mixer.Channel) error { - if sm.currentState != StateNew { + if sm.state != StateNew { return fmt.Errorf("adding languages is only allowed during startup") } if name == "none" { @@ -331,6 +339,12 @@ func (sm *StateMachine) GetMain2VoiceRatio() float32 { return <-resultCh } +func (sm *StateMachine) GetState() State { + resultCh := make(chan State) + sm.getStateCh <- getStateRequest{resultCh} + return <-resultCh +} + func (sm *StateMachine) Shutdown() { select { case sm.quitCh <- true: diff --git a/cmd/dolmetschctld/telnet.go b/cmd/dolmetschctld/telnet.go index 901c226..d6a5c4a 100644 --- a/cmd/dolmetschctld/telnet.go +++ b/cmd/dolmetschctld/telnet.go @@ -74,6 +74,15 @@ func telnetCmdRatio(c *telgo.Client, args []string, sm *StateMachine) bool { return false } +func telnetCmdState(c *telgo.Client, args []string, sm *StateMachine) bool { + if len(args) != 1 { + c.Sayln("usage: state") + return false + } + c.Sayln("current state: %s", sm.GetState()) + return false +} + func telnetQuit(c *telgo.Client, args []string) bool { return true } @@ -88,6 +97,7 @@ func NewTelnetInterface(addr string, sm *StateMachine) (telnet *TelnetInterface, cmdlist := make(telgo.CmdList) cmdlist["lang"] = func(c *telgo.Client, args []string) bool { return telnetCmdLang(c, args, sm) } cmdlist["ratio"] = func(c *telgo.Client, args []string) bool { return telnetCmdRatio(c, args, sm) } + cmdlist["state"] = func(c *telgo.Client, args []string) bool { return telnetCmdState(c, args, sm) } cmdlist["quit"] = telnetQuit telnet.server, err = telgo.NewServer(addr, "dolmetschctl> ", cmdlist, nil) |