mirror of
https://github.com/dpup/meshstream.git
synced 2026-05-04 04:22:29 +02:00
Simplify MQTT package API by embedding TopicInfo
- Created Packet to combine DecodedPacket and TopicInfo - Updated MQTT client to return Packet instead of wrapper type - Modified main.go to use the simplified structure - Updated tests to use the new Packet type This change simplifies the API and reduces the cognitive load for consumers of the MQTT package. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
4
main.go
4
main.go
@@ -62,9 +62,9 @@ func main() {
|
||||
// Main event loop
|
||||
for {
|
||||
select {
|
||||
case msg := <-messagesChan:
|
||||
case packet := <-messagesChan:
|
||||
// Format and print the decoded message
|
||||
formattedOutput := decoder.FormatTopicAndPacket(msg.TopicInfo, msg.DecodedPacket)
|
||||
formattedOutput := decoder.FormatTopicAndPacket(packet.TopicInfo, packet.DecodedPacket)
|
||||
fmt.Println(formattedOutput)
|
||||
fmt.Println(strings.Repeat("-", 80))
|
||||
|
||||
|
||||
@@ -23,21 +23,15 @@ type Config struct {
|
||||
type Client struct {
|
||||
config Config
|
||||
client mqtt.Client
|
||||
decodedMessages chan DecodedMessage
|
||||
decodedMessages chan *Packet
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// DecodedMessage contains a decoded packet and its topic info
|
||||
type DecodedMessage struct {
|
||||
TopicInfo *decoder.TopicInfo
|
||||
DecodedPacket *decoder.DecodedPacket
|
||||
}
|
||||
|
||||
// NewClient creates a new MQTT client with the provided configuration
|
||||
func NewClient(config Config) *Client {
|
||||
return &Client{
|
||||
config: config,
|
||||
decodedMessages: make(chan DecodedMessage, 100), // Buffer up to 100 messages
|
||||
decodedMessages: make(chan *Packet, 100), // Buffer up to 100 messages
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
@@ -79,7 +73,7 @@ func (c *Client) Disconnect() {
|
||||
|
||||
// Messages returns a channel of decoded messages
|
||||
// The consumer should read from this channel to receive decoded messages
|
||||
func (c *Client) Messages() <-chan DecodedMessage {
|
||||
func (c *Client) Messages() <-chan *Packet {
|
||||
return c.decodedMessages
|
||||
}
|
||||
|
||||
@@ -103,9 +97,15 @@ func (c *Client) messageHandler(client mqtt.Client, msg mqtt.Message) {
|
||||
// Binary encoded protobuf message
|
||||
decodedPacket := decoder.DecodeMessage(msg.Payload(), topicInfo)
|
||||
|
||||
// Create packet with both the decoded packet and topic info
|
||||
packet := &Packet{
|
||||
DecodedPacket: decodedPacket,
|
||||
TopicInfo: topicInfo,
|
||||
}
|
||||
|
||||
// Send the decoded message to the channel, but don't block if buffer is full
|
||||
select {
|
||||
case c.decodedMessages <- DecodedMessage{TopicInfo: topicInfo, DecodedPacket: decodedPacket}:
|
||||
case c.decodedMessages <- packet:
|
||||
// Message sent successfully
|
||||
case <-c.done:
|
||||
// Client is shutting down
|
||||
|
||||
@@ -56,7 +56,7 @@ func TestMessagesChannel(t *testing.T) {
|
||||
|
||||
// Test we can read from the channel
|
||||
go func() {
|
||||
msg := DecodedMessage{}
|
||||
msg := &Packet{}
|
||||
client.decodedMessages <- msg
|
||||
}()
|
||||
|
||||
|
||||
11
mqtt/packet.go
Normal file
11
mqtt/packet.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package mqtt
|
||||
|
||||
import (
|
||||
"meshstream/decoder"
|
||||
)
|
||||
|
||||
// Packet extends the DecodedPacket with MQTT topic information
|
||||
type Packet struct {
|
||||
*decoder.DecodedPacket
|
||||
*decoder.TopicInfo
|
||||
}
|
||||
Reference in New Issue
Block a user