summaryrefslogtreecommitdiff
path: root/src/rhctl/switch_control.go
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-04-19 23:56:25 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-04-19 23:56:25 (GMT)
commit5eae78ec8f85d0d329bd289bed169e5fe3bfd340 (patch)
treedfd470bb62538d458eadb52a843c68119847f12d /src/rhctl/switch_control.go
parentafc65d6cb58e1aa7485c9fe5c95fd9bd6833c259 (diff)
replaced snooze timer with generic settling timer
Diffstat (limited to 'src/rhctl/switch_control.go')
-rw-r--r--src/rhctl/switch_control.go61
1 files changed, 41 insertions, 20 deletions
diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go
index a044124..0c91331 100644
--- a/src/rhctl/switch_control.go
+++ b/src/rhctl/switch_control.go
@@ -57,6 +57,7 @@ type State struct {
Switch SwitchState
ActiveServer string
Server map[string]ServerState
+ Settled bool
}
type CommandType uint8
@@ -89,7 +90,7 @@ type SwitchControl struct {
sw *AudioSwitch
servers []*PlayoutServer
state State
- snooze <-chan time.Time
+ settling *time.Timer
Updates *pubsub.PubSub
Commands chan *Command
}
@@ -103,7 +104,7 @@ func (ctrl *SwitchControl) handleCommand(cmd *Command) {
cmd.Response <- fmt.Errorf("no server specified")
return
}
- cmd.Response <- ctrl.reconcile(cmd.Args[0], false)
+ cmd.Response <- ctrl.reconcile(cmd.Args[0])
case CmdSwitch:
if len(cmd.Args) == 0 {
cmd.Response <- fmt.Errorf("no command specified")
@@ -152,23 +153,39 @@ func (ctrl *SwitchControl) checkMissingOrStaleStates(maxAge time.Duration) {
}
func (ctrl *SwitchControl) getSwitchServerAssignment() (swsrv, swch string, err error) {
- // TODO: implement this
- return "", "", errors.New("not implemented")
+ activeInput := SwitchInputNum(0)
+ cnt := 0
+ for idx, in := range ctrl.state.Switch.Audio[0].Inputs {
+ if in {
+ activeInput = SwitchInputNum(idx + 1)
+ cnt++
+ }
+ }
+ if cnt != 1 || activeInput == SwitchInputNum(0) {
+ return "", "", errors.New("no or more than one input is active")
+ }
+
+ for _, in := range ctrl.sw.Inputs {
+ if in.Number == activeInput {
+ return in.Server, in.Channel, nil
+ }
+ }
+ return "", "", errors.New("unknown input")
}
func (ctrl *SwitchControl) selectServer(server string) {
// TODO: send out commands to switch
}
-func (ctrl *SwitchControl) reconcile(requestedServer string, snooze bool) (result bool) {
- rhdl.Printf("SwitchCTRL: reconciling state... (requested server: '%s', snooze: %t)", requestedServer, snooze)
+func (ctrl *SwitchControl) reconcile(requestedServer string) (result bool) {
+ if !ctrl.state.Settled {
+ return
+ }
+
+ rhdl.Printf("SwitchCTRL: reconciling state... (requested server: '%s')", requestedServer)
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
- }
if len(ctrl.servers) < 1 {
ctrl.state.Mood = MoodHappy
rhdl.Printf("SwitchCTRL: there are no servers configured -> now in mood: %s", ctrl.state.Mood)
@@ -190,12 +207,12 @@ func (ctrl *SwitchControl) reconcile(requestedServer string, snooze bool) (resul
rhl.Printf("SwitchCTRL: just woken up... switch is set to server '%s' channel '%s'", swsrv, swch)
s, exists := ctrl.state.Server[swsrv]
- if !exists || s.Health != ServerAlive {
- rhl.Printf("SwitchCTRL: server '%s' is unknown or dead!", swsrv)
+ if !exists || s.Health != ServerAlive || s.Channel == "" {
+ rhl.Printf("SwitchCTRL: server '%s' is unknown, dead or has invalid channel!", swsrv)
for name, state := range ctrl.state.Server {
- if state.Health == ServerAlive {
+ if state.Health == ServerAlive && state.Channel != "" {
ctrl.state.Mood = MoodNervous
- rhl.Printf("SwitchCTRL: found another alive server -> now in mood: %s", ctrl.state.Mood)
+ rhl.Printf("SwitchCTRL: found another valid server -> now in mood: %s", ctrl.state.Mood)
ctrl.selectServer(name)
return
}
@@ -247,7 +264,9 @@ func (ctrl *SwitchControl) reconcile(requestedServer string, snooze bool) (resul
func (ctrl *SwitchControl) Run() {
rhdl.Printf("SwitchCTRL: handler running...")
- ctrl.snooze = time.After(time.Minute) // TODO: hardcode value
+ ctrl.settling = time.NewTimer(30 * time.Second) // TODO: hardcode value
+ ctrl.state.Settled = false
+
serverStateChanges := make(chan ServerState, 8)
for _, srv := range ctrl.servers {
go handleServer(srv.StateChanges, serverStateChanges)
@@ -257,11 +276,13 @@ func (ctrl *SwitchControl) Run() {
// TODO: this should be called from reconcile but with some sort of rate-limiting...
ctrl.checkMissingOrStaleStates(2 * time.Hour) // TODO: hardcoded value
- ctrl.reconcile("", false)
+ ctrl.reconcile("")
for {
select {
- case <-ctrl.snooze:
- ctrl.reconcile("", true)
+ case <-ctrl.settling.C:
+ ctrl.state.Settled = true
+ ctrl.reconcile("")
+ ctrl.Updates.Pub(ctrl.state, "state")
case update := <-ctrl.sw.Updates:
rhdl.Printf("got update from switch: %+v", update)
for _, srv := range ctrl.servers {
@@ -272,13 +293,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("", false)
+ ctrl.reconcile("")
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("", false)
+ ctrl.reconcile("")
ctrl.Updates.Pub(ctrl.state, "state")
case cmd := <-ctrl.Commands:
ctrl.handleCommand(cmd)