summaryrefslogtreecommitdiff
path: root/src/rhctl/audio_switch.go
blob: f3821d82d3c887ebe399d359b4c5883e9283b4b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
//  rhctl
//
//  Copyright (C) 2009-2016 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of rhctl.
//
//  rhctl 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.
//
//  rhctl 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 rhctl. If not, see <http://www.gnu.org/licenses/>.
//

package main

import (
	"fmt"
	"time"
)

type SwitchResult uint8

const (
	SwitchOK SwitchResult = iota
	SwitchError
)

func (c SwitchResult) String() string {
	switch c {
	case SwitchOK:
		return "OK"
	case SwitchError:
		return "error"
	}
	return "unknown"
}

type SwitchResponse struct {
	Result  SwitchResult
	Message string
}

type SwitchUpdateType uint8

const (
	SwitchAudio SwitchUpdateType = iota
	SwitchGPI
	SwitchOC
	SwitchRelay
	SwitchSilence
)

func (c SwitchUpdateType) String() string {
	switch c {
	case SwitchAudio:
		return "audio"
	case SwitchGPI:
		return "gpi"
	case SwitchOC:
		return "oc"
	case SwitchRelay:
		return "relay"
	case SwitchSilence:
		return "silence"
	}
	return "unknown"
}

type SwitchUpdate struct {
	Type SwitchUpdateType
	Data string
}

type SwitchState struct {
	audio [SwitchOutputNumMax]struct {
		inputs  [SwitchInputNumMax]bool
		silence bool
	}
	gpi   [SwitchGPINumMax]bool
	relay [SwitchRelayNumMax]bool
	oc    [SwitchOCNumMax]bool
}

type AudioSwitch struct {
	port         *SerialPort
	timeout      time.Duration
	current      *SwitchCommand
	timer        *time.Timer
	unit         SwitchUnitID
	state        SwitchState
	StateChanges chan SwitchState
	Commands     chan *SwitchCommand
	Updates      chan SwitchUpdate
}

func (sw *AudioSwitch) handleData(data string) {
	if len(data) < 3 {
		rhl.Printf("Audioswitch: ignoring short line")
		return
	}

	rhdl.Printf("Audioswitch: got data: %q", data)
	switch data[0:3] {
	case "RRR":
		fallthrough
	case "EEE":
		if sw.current != nil {
			resp := SwitchResponse{Message: data}
			resp.Result = SwitchError
			if data[0] == 'R' {
				resp.Result = SwitchOK
			}
			sw.current.Response <- resp
			sw.current = nil
			sw.timer.Stop()
			sw.timer = nil
		} 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)
	}
}

func (sw *AudioSwitch) Run() {
	stop := make(chan bool)
	sw.port.Run(stop)
	sw.current = nil
	sw.timer = nil

	rhdl.Printf("Audioswitch: handler running...")
	for {
		if sw.current != nil {
			select {
			case <-stop:
				return
			case <-sw.timer.C:
				sw.current.Response <- fmt.Errorf("command timed out")
				sw.current = nil
				sw.timer = nil
			case data := <-sw.port.rx:
				sw.handleData(data)
			}
		} else {
			select {
			case <-stop:
				return
			case cmd := <-sw.Commands:
				c, err := cmd.Cmd.Generate(append(cmd.Args, sw.unit)...)
				if err != nil {
					cmd.Response <- err
				} else {
					rhdl.Printf("sending '%s' to switch", c)
					sw.current = cmd
					sw.port.tx <- c
					sw.timer = time.NewTimer(sw.timeout)
				}
			case data := <-sw.port.rx:
				sw.handleData(data)
			}
		}
	}
}

func SwitchInit(conf *Config) (sw *AudioSwitch, err error) {
	sw = &AudioSwitch{}
	sw.timeout = time.Second
	if conf.Audioswitch.Timeout.Duration > 0 {
		sw.timeout = conf.Audioswitch.Timeout.Duration
	}
	sw.unit = conf.Audioswitch.Unit
	sw.StateChanges = make(chan SwitchState, 16)
	sw.Commands = make(chan *SwitchCommand, 8)
	sw.Updates = make(chan SwitchUpdate, 32)

	if sw.port, err = SerialOpen(conf.Audioswitch.Device, conf.Audioswitch.Baudrate, ""); err != nil {
		err = fmt.Errorf("Audioswitch: error opening serial port: %s", err)
		return
	}
	return
}