mirror of
https://github.com/dpup/meshstream.git
synced 2026-03-28 17:42:37 +01:00
Add support for map format messages
- Added handling for topics with "map" format (2/map) - Display map data as readable text rather than hex - Added new FormatTopicAndMapData function to formatter.go - Added map format condition in main.go message handler 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -846,6 +846,32 @@ func FormatTopicAndJSONData(topicInfo *TopicInfo, jsonData map[string]interface{
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// FormatTopicAndMapData formats topic information and map data as text
|
||||
func FormatTopicAndMapData(topicInfo *TopicInfo, payload []byte) string {
|
||||
var builder strings.Builder
|
||||
|
||||
// Display topic information
|
||||
builder.WriteString(fmt.Sprintf("Topic: %s\n", topicInfo.FullTopic))
|
||||
builder.WriteString(fmt.Sprintf("Region Path: %s\n", topicInfo.RegionPath))
|
||||
builder.WriteString(fmt.Sprintf("Version: %s\n", topicInfo.Version))
|
||||
builder.WriteString(fmt.Sprintf("Format: %s\n", topicInfo.Format))
|
||||
builder.WriteString(fmt.Sprintf("Channel: %s\n", topicInfo.Channel))
|
||||
if topicInfo.UserID != "" {
|
||||
builder.WriteString(fmt.Sprintf("User ID: %s\n", topicInfo.UserID))
|
||||
}
|
||||
builder.WriteString("\n")
|
||||
|
||||
// Display map data as text
|
||||
builder.WriteString("Map Data:\n")
|
||||
if IsASCII(payload) {
|
||||
builder.WriteString(fmt.Sprintf(" Text Content: %s\n", string(payload)))
|
||||
} else {
|
||||
builder.WriteString(fmt.Sprintf(" Data is not ASCII text, showing hex: %x\n", payload))
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// FormatTopicAndRawData formats topic information and raw data for unsupported formats
|
||||
func FormatTopicAndRawData(topicInfo *TopicInfo, payload []byte) string {
|
||||
var builder strings.Builder
|
||||
|
||||
9
main.go
9
main.go
@@ -18,7 +18,7 @@ const (
|
||||
mqttBroker = "mqtt.bayme.sh"
|
||||
mqttUsername = "meshdev"
|
||||
mqttPassword = "large4cats"
|
||||
mqttTopicPrefix = "msh/US"
|
||||
mqttTopicPrefix = "msh/US/bayarea"
|
||||
)
|
||||
|
||||
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
||||
@@ -37,7 +37,10 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
|
||||
// Binary encoded protobuf message
|
||||
decodedPacket := decoder.DecodeMessage(msg.Payload(), topicInfo)
|
||||
formattedOutput = decoder.FormatTopicAndPacket(topicInfo, decodedPacket)
|
||||
} else if topicInfo.Format == "json" {
|
||||
} else if topicInfo.Format == "map" {
|
||||
// Map format - unencrypted map packets, display as text
|
||||
formattedOutput = decoder.FormatTopicAndMapData(topicInfo, msg.Payload())
|
||||
} else if topicInfo.Format == "json" {
|
||||
// JSON format message
|
||||
jsonData, err := decoder.DecodeJSONMessage(msg.Payload())
|
||||
if err != nil {
|
||||
@@ -50,7 +53,7 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
|
||||
// Unsupported format
|
||||
formattedOutput = decoder.FormatTopicAndRawData(topicInfo, msg.Payload())
|
||||
}
|
||||
|
||||
|
||||
// Print the formatted output
|
||||
fmt.Println(formattedOutput)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user