diff options
-rw-r--r-- | src/rhctl/switch_control.go | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go index 425278e..7ff2ad1 100644 --- a/src/rhctl/switch_control.go +++ b/src/rhctl/switch_control.go @@ -22,6 +22,7 @@ package main import ( + "errors" "fmt" "time" @@ -88,7 +89,7 @@ type SwitchControl struct { sw *AudioSwitch servers []*PlayoutServer state State - startup time.Time + snooze <-chan time.Time Updates *pubsub.PubSub Commands chan *Command } @@ -102,7 +103,7 @@ func (ctrl *SwitchControl) handleCommand(cmd *Command) { cmd.Response <- fmt.Errorf("no server specified") return } - cmd.Response <- ctrl.reconcile(cmd.Args[0]) + cmd.Response <- ctrl.reconcile(cmd.Args[0], false) case CmdSwitch: if len(cmd.Args) == 0 { cmd.Response <- fmt.Errorf("no command specified") @@ -150,15 +151,34 @@ func (ctrl *SwitchControl) checkMissingOrStaleStates(maxAge time.Duration) { } } -func (ctrl *SwitchControl) reconcile(requestedServer string) (result bool) { - rhdl.Printf("SwitchCTRL: reconciling state... (requested server: '%s')", requestedServer) +func (ctrl *SwitchControl) getSwitchServerAssignment() (swsrv, swch string, err error) { + // TODO: implement this + return "", "", errors.New("not implemented") +} + +func (ctrl *SwitchControl) reconcile(requestedServer string, snooze bool) (result bool) { + rhdl.Printf("SwitchCTRL: reconciling state... (requested server: '%s', snooze: %t)", requestedServer, snooze) // send out commands to switch and/or server to get missing infos + // TODO: this shouldn't be called every time, because this will lead to a lot of requests as updates come in... ctrl.checkMissingOrStaleStates(2 * time.Hour) // TODO: hardcoded value - if ctrl.state.Mood == MoodAwakening && time.Since(ctrl.startup) < (time.Minute) { // TODO: hardcoded value - // it's to early to take actions - let's learn i little bit more about current state - return + switch ctrl.state.Mood { + case MoodAwakening: + if !snooze { // this not a wakeup of the snooze timer + // it's too early to take actions - let's learn a little bit more about current state + return + } + swsrv, swch, err := ctrl.getSwitchServerAssignment() + if err != nil { + ctrl.state.Mood = MoodSad + rhl.Printf("SwitchCTRL: switch input assignments are ambigious or outdated -> now in mood: %s", ctrl.state.Mood) + return + } + rhl.Printf("SwitchCTRL: we are awake now... switch is set to server '%s' channel '%s'", swsrv, swch) + + default: + rhl.Printf("SwitchCTRL: error handling of mood %s is not implemented", ctrl.state.Mood) } // TODO: change active server if it fails or on request @@ -173,15 +193,17 @@ func (ctrl *SwitchControl) reconcile(requestedServer string) (result bool) { func (ctrl *SwitchControl) Run() { rhdl.Printf("SwitchCTRL: handler running...") - ctrl.startup = time.Now() + ctrl.snooze = time.After(time.Minute) // TODO: hardcode value serverStateChanges := make(chan ServerState, 8) for _, srv := range ctrl.servers { go handleServer(srv.StateChanges, serverStateChanges) } - ctrl.reconcile("") + ctrl.reconcile("", false) for { select { + case <-ctrl.snooze: + ctrl.reconcile("", true) case update := <-ctrl.sw.Updates: rhdl.Printf("got update from switch: %+v", update) for _, srv := range ctrl.servers { @@ -192,13 +214,13 @@ func (ctrl *SwitchControl) Run() { // rhdl.Printf("got switch state update: %+v", state) ctrl.Updates.Pub(state, "switch:state") ctrl.state.Switch = state - ctrl.reconcile("") + ctrl.reconcile("", false) ctrl.Updates.Pub(ctrl.state, "state") case state := <-serverStateChanges: rhdl.Printf("got server state update: %+v", state) ctrl.Updates.Pub(state, "server:state") ctrl.state.Server[state.Name] = state - ctrl.reconcile("") + ctrl.reconcile("", false) ctrl.Updates.Pub(ctrl.state, "state") case cmd := <-ctrl.Commands: ctrl.handleCommand(cmd) |