diff --git a/decoder/formatter.go b/decoder/formatter.go index 3350c0c..cfbf327 100644 --- a/decoder/formatter.go +++ b/decoder/formatter.go @@ -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 diff --git a/main.go b/main.go index b7f30c1..feca01e 100644 --- a/main.go +++ b/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) }