Refactor protos

This commit is contained in:
Daniel Pupius
2025-04-21 15:40:10 -07:00
parent 41ee61b59a
commit 86380cccf9
59 changed files with 1477 additions and 1283 deletions
+4 -8
View File
@@ -8,6 +8,7 @@ import (
mqtt "github.com/eclipse/paho.mqtt.golang"
"meshstream/decoder"
mesh "meshstream/proto/generated"
)
// Config holds configuration for the MQTT client
@@ -32,7 +33,7 @@ type Client struct {
func NewClient(config Config, logger logging.Logger) *Client {
return &Client{
config: config,
decodedMessages: make(chan *Packet, 100), // Buffer up to 100 messages
decodedMessages: make(chan *Packet, 100),
done: make(chan struct{}),
logger: logger.Named("mqtt.client"),
}
@@ -79,8 +80,6 @@ func (c *Client) Messages() <-chan *Packet {
return c.decodedMessages
}
// These are intentionally left empty as we'll use the instance methods below
// messageHandler processes incoming MQTT messages
func (c *Client) messageHandler(client mqtt.Client, msg mqtt.Message) {
c.logger.Debugf("Received message from topic: %s", msg.Topic())
@@ -103,10 +102,7 @@ func (c *Client) messageHandler(client mqtt.Client, msg mqtt.Message) {
decodedPacket := decoder.DecodeMessage(msg.Payload(), topicInfo)
// Create packet with both the decoded packet and topic info
packet := &Packet{
DecodedPacket: decodedPacket,
TopicInfo: topicInfo,
}
packet := NewPacket(decodedPacket, topicInfo)
// Send the decoded message to the channel, but don't block if buffer is full
select {
@@ -138,4 +134,4 @@ func (c *Client) connectHandler(client mqtt.Client) {
// connectionLostHandler is called when the client loses connection
func (c *Client) connectionLostHandler(client mqtt.Client, err error) {
c.logger.Errorw("Connection lost", "error", err)
}
}
+143 -3
View File
@@ -1,11 +1,151 @@
package mqtt
import (
"meshstream/decoder"
mesh "meshstream/proto/generated"
)
// Packet extends the DecodedPacket with MQTT topic information
type Packet struct {
*decoder.DecodedPacket
*decoder.TopicInfo
*mesh.Packet
}
// NewPacket creates a Packet from a decoded packet and topic info
func NewPacket(decoded *mesh.DecodedPacket, topicInfo *mesh.TopicInfo) *Packet {
return &Packet{
Packet: &mesh.Packet{
DecodedPacket: decoded,
TopicInfo: topicInfo,
},
}
}
// Helper accessors to maintain backward compatibility with existing code
func (p *Packet) GetChannelID() string {
if p.DecodedPacket != nil {
return p.DecodedPacket.ChannelId
}
return ""
}
func (p *Packet) GetGatewayID() string {
if p.DecodedPacket != nil {
return p.DecodedPacket.GatewayId
}
return ""
}
func (p *Packet) GetID() uint32 {
if p.DecodedPacket != nil {
return p.DecodedPacket.Id
}
return 0
}
func (p *Packet) GetFrom() uint32 {
if p.DecodedPacket != nil {
return p.DecodedPacket.From
}
return 0
}
func (p *Packet) GetTo() uint32 {
if p.DecodedPacket != nil {
return p.DecodedPacket.To
}
return 0
}
func (p *Packet) GetPortNum() int32 {
if p.DecodedPacket != nil {
return int32(p.DecodedPacket.PortNum)
}
return 0
}
func (p *Packet) GetPortNumString() string {
if p.DecodedPacket != nil {
return p.DecodedPacket.PortNum.String()
}
return "UNKNOWN"
}
func (p *Packet) GetPayload() interface{} {
if p.DecodedPacket == nil {
return nil
}
// Depending on the payload type, return the appropriate value
switch x := p.DecodedPacket.Payload.(type) {
case *mesh.DecodedPacket_TextMessage:
return x.TextMessage
case *mesh.DecodedPacket_BinaryData:
return x.BinaryData
case *mesh.DecodedPacket_Position:
return x.Position
case *mesh.DecodedPacket_NodeInfo:
return x.NodeInfo
case *mesh.DecodedPacket_Telemetry:
return x.Telemetry
case *mesh.DecodedPacket_Waypoint:
return x.Waypoint
case *mesh.DecodedPacket_RouteDiscovery:
return x.RouteDiscovery
case *mesh.DecodedPacket_NeighborInfo:
return x.NeighborInfo
case *mesh.DecodedPacket_CompressedText:
return x.CompressedText
case *mesh.DecodedPacket_MapReport:
return x.MapReport
default:
return nil
}
}
func (p *Packet) GetHopLimit() uint32 {
if p.DecodedPacket != nil {
return p.DecodedPacket.HopLimit
}
return 0
}
func (p *Packet) GetHopStart() uint32 {
if p.DecodedPacket != nil {
return p.DecodedPacket.HopStart
}
return 0
}
func (p *Packet) HasDecodeError() bool {
return p.DecodedPacket != nil && p.DecodedPacket.DecodeError != ""
}
func (p *Packet) GetDecodeError() string {
if p.DecodedPacket != nil {
return p.DecodedPacket.DecodeError
}
return ""
}
// GetFullTopic returns the MQTT topic this packet was received on
func (p *Packet) GetFullTopic() string {
if p.TopicInfo != nil {
return p.TopicInfo.FullTopic
}
return ""
}
// GetRegionPath returns the region path from the topic
func (p *Packet) GetRegionPath() string {
if p.TopicInfo != nil {
return p.TopicInfo.RegionPath
}
return ""
}
// GetChannel returns the channel name from the topic
func (p *Packet) GetChannel() string {
if p.TopicInfo != nil {
return p.TopicInfo.Channel
}
return ""
}