Checkpoint
This commit is contained in:
24
broker.go
24
broker.go
@@ -33,13 +33,18 @@ type Broker interface {
|
||||
SubscribeRadios() (<-chan *Radio, error)
|
||||
|
||||
PublishPacket(topic string, packet *protocol.Packet) error
|
||||
SubscribePackets(topic string) (<-chan *protocol.Packet, error)
|
||||
SubscribePackets(topic string) (<-chan *Packet, error)
|
||||
}
|
||||
|
||||
type Receiver interface {
|
||||
Disconnected()
|
||||
}
|
||||
|
||||
type Packet struct {
|
||||
RadioID string
|
||||
*protocol.Packet
|
||||
}
|
||||
|
||||
type BrokerConfig struct {
|
||||
Type string `yaml:"type"`
|
||||
Config yaml.Node `yaml:"conf"`
|
||||
@@ -197,7 +202,7 @@ func (broker *mqttBroker) SubscribeRadios() (<-chan *Radio, error) {
|
||||
}
|
||||
|
||||
radios := make(chan *Radio, 8)
|
||||
token := broker.client.Subscribe("radio/#", 0, func(_ mqtt.Client, message mqtt.Message) {
|
||||
token := broker.client.Subscribe("radio/+", 0, func(_ mqtt.Client, message mqtt.Message) {
|
||||
var radio Radio
|
||||
if err := json.Unmarshal(message.Payload(), &radio); err == nil {
|
||||
select {
|
||||
@@ -232,17 +237,24 @@ func (broker *mqttBroker) PublishPacket(topic string, packet *protocol.Packet) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func (broker *mqttBroker) SubscribePackets(topic string) (<-chan *protocol.Packet, error) {
|
||||
func (broker *mqttBroker) SubscribePackets(topic string) (<-chan *Packet, error) {
|
||||
if broker.client == nil {
|
||||
return nil, ErrBrokerNotStarted
|
||||
}
|
||||
|
||||
packets := make(chan *protocol.Packet, 16)
|
||||
packets := make(chan *Packet, 16)
|
||||
token := broker.client.Subscribe(topic, 0, func(_ mqtt.Client, message mqtt.Message) {
|
||||
var packet protocol.Packet
|
||||
var (
|
||||
part = strings.Split(message.Topic(), "/")
|
||||
id = part[len(part)-1]
|
||||
packet protocol.Packet
|
||||
)
|
||||
if err := json.Unmarshal(message.Payload(), &packet); err == nil {
|
||||
select {
|
||||
case packets <- &packet:
|
||||
case packets <- &Packet{
|
||||
RadioID: id,
|
||||
Packet: &packet,
|
||||
}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user