diff options
Diffstat (limited to 'src/rhctl/audio_switch.go')
-rw-r--r-- | src/rhctl/audio_switch.go | 179 |
1 files changed, 158 insertions, 21 deletions
diff --git a/src/rhctl/audio_switch.go b/src/rhctl/audio_switch.go index 3bb4abb..31ecbc4 100644 --- a/src/rhctl/audio_switch.go +++ b/src/rhctl/audio_switch.go @@ -23,6 +23,7 @@ package main import ( "fmt" + "strings" "time" ) @@ -101,6 +102,138 @@ type AudioSwitch struct { Updates chan SwitchUpdate } +func (sw *AudioSwitch) updateStateAudio(data string) { + if len(data) < int(4+2*SwitchInputNumMax) { + rhl.Printf("Audioswitch: invalid audio status update (too short)", data) + return + } + var out SwitchOutputNum + if err := out.FromString(data[3:4]); err != nil { + rhl.Printf("Audioswitch: invalid audio status update (%s)", err) + return + } + ins := strings.Split(data[5:int(4+2*SwitchInputNumMax)], ",") + if len(ins) != int(SwitchInputNumMax) { + rhl.Printf("Audioswitch: invalid audio status update (wrong number of inputs)") + return + } + for i := 0; i < int(SwitchInputNumMax); i++ { + switch ins[i] { + case "0": + sw.state.Audio[out-1].Inputs[i] = false + case "1": + sw.state.Audio[out-1].Inputs[i] = true + default: + rhl.Printf("Audioswitch: invalid audio status update (state must be either \"1\" or \"0\" but is %q)", ins[i]) + } + } + sw.StateChanges <- sw.state +} + +func (sw *AudioSwitch) updateStateGPI(data string) { + if len(data) < 5 { + rhl.Printf("Audioswitch: invalid gpi status update (too short)", data) + return + } + if data[4] != 'A' { + return // TODO: parse single GPI status... + } + + if len(data) < int(5+2*SwitchGPINumMax) { + rhl.Printf("Audioswitch: invalid gpi status update (too short)", data) + return + } + + ins := strings.Split(data[6:int(5+2*SwitchGPINumMax)], ",") + if len(ins) != int(SwitchGPINumMax) { + rhl.Printf("Audioswitch: invalid gpi status update (wrong number of inputs)") + return + } + for i := 0; i < int(SwitchGPINumMax); i++ { + switch ins[i] { + case "0": + sw.state.GPI[i] = false + case "1": + sw.state.GPI[i] = true + default: + rhl.Printf("Audioswitch: invalid gpi status update (state must be either \"1\" or \"0\" but is %q)", ins[i]) + } + } + sw.StateChanges <- sw.state +} + +func (sw *AudioSwitch) updateStateRelay(data string) { + if len(data) < int(3+2*SwitchRelayNumMax) { + rhl.Printf("Audioswitch: invalid relay status update (too short)", data) + return + } + + outs := strings.Split(data[4:int(3+2*SwitchRelayNumMax)], ",") + if len(outs) != int(SwitchRelayNumMax) { + rhl.Printf("Audioswitch: invalid relay status update (wrong number of outputs)") + return + } + for i := 0; i < int(SwitchRelayNumMax); i++ { + switch outs[i] { + case "0": + sw.state.Relay[i] = false + case "1": + sw.state.Relay[i] = true + default: + rhl.Printf("Audioswitch: invalid relay status update (state must be either \"1\" or \"0\" but is %q)", outs[i]) + } + } + sw.StateChanges <- sw.state +} + +func (sw *AudioSwitch) updateStateOC(data string) { + if len(data) < int(3+2*SwitchOCNumMax) { + rhl.Printf("Audioswitch: invalid oc status update (too short)", data) + return + } + + outs := strings.Split(data[4:int(3+2*SwitchOCNumMax)], ",") + if len(outs) != int(SwitchOCNumMax) { + rhl.Printf("Audioswitch: invalid oc status update (wrong number of outputs)") + return + } + for i := 0; i < int(SwitchOCNumMax); i++ { + switch outs[i] { + case "0": + sw.state.OC[i] = false + case "1": + sw.state.OC[i] = true + default: + rhl.Printf("Audioswitch: invalid oc status update (state must be either \"1\" or \"0\" but is %q)", outs[i]) + } + } + sw.StateChanges <- sw.state +} + +func (sw *AudioSwitch) updateStateSilence(data string) { + if len(data) < int(3+2*SwitchOutputNumMax) { + rhl.Printf("Audioswitch: invalid silence status update (too short)", data) + return + } + + outs := strings.Split(data[4:int(3+2*SwitchOutputNumMax)], ",") + if len(outs) != int(SwitchOutputNumMax) { + rhl.Printf("Audioswitch: invalid silence status update (wrong number of outputs)") + return + } + for i := 0; i < int(SwitchOutputNumMax); i++ { + switch outs[i] { + case "0": + sw.state.Audio[i].Silence = false + case "1": + sw.state.Audio[i].Silence = true + default: + rhl.Printf("Audioswitch: invalid silence status update (state must be either \"1\" or \"0\" but is %q)", outs[i]) + } + } + sw.StateChanges <- sw.state +} + func (sw *AudioSwitch) handleData(data string) { if len(data) < 3 { rhl.Printf("Audioswitch: ignoring short line") @@ -108,10 +241,7 @@ func (sw *AudioSwitch) handleData(data string) { } rhdl.Printf("Audioswitch: got data: %q", data) - switch data[0:3] { - case "RRR": - fallthrough - case "EEE": + if data[0:3] == "RRR" || data[0:3] == "EEE" { if sw.current != nil { resp := SwitchResponse{Message: data} resp.Result = SwitchError @@ -125,24 +255,31 @@ func (sw *AudioSwitch) handleData(data string) { } else { rhl.Printf("Audioswitch: ignoring unexpected response: %q", data) } - case "S0L": - sw.Updates <- SwitchUpdate{SwitchAudio, data} - // TODO: update state and send it out - case "S0P": - sw.Updates <- SwitchUpdate{SwitchGPI, data} - // TODO: update state and send it out - case "S0O": - sw.Updates <- SwitchUpdate{SwitchOC, data} - // TODO: update state and send it out - case "S0R": - sw.Updates <- SwitchUpdate{SwitchRelay, data} - // TODO: update state and send it out - case "S0S": - sw.Updates <- SwitchUpdate{SwitchSilence, data} - // TODO: update state and send it out - default: - rhl.Printf("Audioswitch: ignoring invalid data: %q", data) + return } + + if data[0] == 'S' && data[1] == ('0'+byte(sw.unit)) { + switch data[2] { + case 'L': + sw.Updates <- SwitchUpdate{SwitchAudio, data} + sw.updateStateAudio(data) + case 'P': + sw.Updates <- SwitchUpdate{SwitchGPI, data} + sw.updateStateGPI(data) + case 'R': + sw.Updates <- SwitchUpdate{SwitchRelay, data} + sw.updateStateRelay(data) + case 'O': + sw.Updates <- SwitchUpdate{SwitchOC, data} + sw.updateStateOC(data) + case 'S': + sw.Updates <- SwitchUpdate{SwitchSilence, data} + sw.updateStateSilence(data) + } + return + } + + rhl.Printf("Audioswitch: ignoring invalid data: %q", data) } func (sw *AudioSwitch) Run() { |