@@ -61,40 +61,97 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
61
61
if (pb_decode_from_bytes (mp->decoded .payload .bytes , mp->decoded .payload .size , &meshtastic_Telemetry_msg, &scratch)) {
62
62
decoded = &scratch;
63
63
if (decoded->which_variant == meshtastic_Telemetry_device_metrics_tag) {
64
- msgPayload[" battery_level" ] = new JSONValue ((unsigned int )decoded->variant .device_metrics .battery_level );
64
+ // If battery is present, encode the battery level value
65
+ // TODO - Add a condition to send a code for a non-present value
66
+ if (decoded->variant .device_metrics .has_battery_level ) {
67
+ msgPayload[" battery_level" ] = new JSONValue ((int )decoded->variant .device_metrics .battery_level );
68
+ }
65
69
msgPayload[" voltage" ] = new JSONValue (decoded->variant .device_metrics .voltage );
66
70
msgPayload[" channel_utilization" ] = new JSONValue (decoded->variant .device_metrics .channel_utilization );
67
71
msgPayload[" air_util_tx" ] = new JSONValue (decoded->variant .device_metrics .air_util_tx );
68
72
msgPayload[" uptime_seconds" ] = new JSONValue ((unsigned int )decoded->variant .device_metrics .uptime_seconds );
69
73
} else if (decoded->which_variant == meshtastic_Telemetry_environment_metrics_tag) {
70
- msgPayload[" temperature" ] = new JSONValue (decoded->variant .environment_metrics .temperature );
71
- msgPayload[" relative_humidity" ] = new JSONValue (decoded->variant .environment_metrics .relative_humidity );
72
- msgPayload[" barometric_pressure" ] = new JSONValue (decoded->variant .environment_metrics .barometric_pressure );
73
- msgPayload[" gas_resistance" ] = new JSONValue (decoded->variant .environment_metrics .gas_resistance );
74
- msgPayload[" voltage" ] = new JSONValue (decoded->variant .environment_metrics .voltage );
75
- msgPayload[" current" ] = new JSONValue (decoded->variant .environment_metrics .current );
76
- msgPayload[" lux" ] = new JSONValue (decoded->variant .environment_metrics .lux );
77
- msgPayload[" white_lux" ] = new JSONValue (decoded->variant .environment_metrics .white_lux );
78
- msgPayload[" iaq" ] = new JSONValue ((uint)decoded->variant .environment_metrics .iaq );
79
- msgPayload[" wind_speed" ] = new JSONValue (decoded->variant .environment_metrics .wind_speed );
80
- msgPayload[" wind_direction" ] = new JSONValue ((uint)decoded->variant .environment_metrics .wind_direction );
81
- msgPayload[" wind_gust" ] = new JSONValue (decoded->variant .environment_metrics .wind_gust );
82
- msgPayload[" wind_lull" ] = new JSONValue (decoded->variant .environment_metrics .wind_lull );
83
- msgPayload[" radiation" ] = new JSONValue (decoded->variant .environment_metrics .radiation );
74
+ // Avoid sending 0s for sensors that could be 0
75
+ if (decoded->variant .environment_metrics .has_temperature ) {
76
+ msgPayload[" temperature" ] = new JSONValue (decoded->variant .environment_metrics .temperature );
77
+ }
78
+ if (decoded->variant .environment_metrics .has_relative_humidity ) {
79
+ msgPayload[" relative_humidity" ] = new JSONValue (decoded->variant .environment_metrics .relative_humidity );
80
+ }
81
+ if (decoded->variant .environment_metrics .has_barometric_pressure ) {
82
+ msgPayload[" barometric_pressure" ] = new JSONValue (decoded->variant .environment_metrics .barometric_pressure );
83
+ }
84
+ if (decoded->variant .environment_metrics .has_gas_resistance ) {
85
+ msgPayload[" gas_resistance" ] = new JSONValue (decoded->variant .environment_metrics .gas_resistance );
86
+ }
87
+ if (decoded->variant .environment_metrics .has_voltage ) {
88
+ msgPayload[" voltage" ] = new JSONValue (decoded->variant .environment_metrics .voltage );
89
+ }
90
+ if (decoded->variant .environment_metrics .has_current ) {
91
+ msgPayload[" current" ] = new JSONValue (decoded->variant .environment_metrics .current );
92
+ }
93
+ if (decoded->variant .environment_metrics .has_lux ) {
94
+ msgPayload[" lux" ] = new JSONValue (decoded->variant .environment_metrics .lux );
95
+ }
96
+ if (decoded->variant .environment_metrics .has_white_lux ) {
97
+ msgPayload[" white_lux" ] = new JSONValue (decoded->variant .environment_metrics .white_lux );
98
+ }
99
+ if (decoded->variant .environment_metrics .has_iaq ) {
100
+ msgPayload[" iaq" ] = new JSONValue ((uint)decoded->variant .environment_metrics .iaq );
101
+ }
102
+ if (decoded->variant .environment_metrics .has_wind_speed ) {
103
+ msgPayload[" wind_speed" ] = new JSONValue (decoded->variant .environment_metrics .wind_speed );
104
+ }
105
+ if (decoded->variant .environment_metrics .has_wind_direction ) {
106
+ msgPayload[" wind_direction" ] = new JSONValue ((uint)decoded->variant .environment_metrics .wind_direction );
107
+ }
108
+ if (decoded->variant .environment_metrics .has_wind_gust ) {
109
+ msgPayload[" wind_gust" ] = new JSONValue (decoded->variant .environment_metrics .wind_gust );
110
+ }
111
+ if (decoded->variant .environment_metrics .has_wind_lull ) {
112
+ msgPayload[" wind_lull" ] = new JSONValue (decoded->variant .environment_metrics .wind_lull );
113
+ }
114
+ if (decoded->variant .environment_metrics .has_radiation ) {
115
+ msgPayload[" radiation" ] = new JSONValue (decoded->variant .environment_metrics .radiation );
116
+ }
84
117
} else if (decoded->which_variant == meshtastic_Telemetry_air_quality_metrics_tag) {
85
- msgPayload[" pm10" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm10_standard );
86
- msgPayload[" pm25" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm25_standard );
87
- msgPayload[" pm100" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm100_standard );
88
- msgPayload[" pm10_e" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm10_environmental );
89
- msgPayload[" pm25_e" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm25_environmental );
90
- msgPayload[" pm100_e" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm100_environmental );
118
+ if (decoded->variant .air_quality_metrics .has_pm10_standard ) {
119
+ msgPayload[" pm10" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm10_standard );
120
+ }
121
+ if (decoded->variant .air_quality_metrics .has_pm25_standard ) {
122
+ msgPayload[" pm25" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm25_standard );
123
+ }
124
+ if (decoded->variant .air_quality_metrics .has_pm100_standard ) {
125
+ msgPayload[" pm100" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm100_standard );
126
+ }
127
+ if (decoded->variant .air_quality_metrics .has_pm10_environmental ) {
128
+ msgPayload[" pm10_e" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm10_environmental );
129
+ }
130
+ if (decoded->variant .air_quality_metrics .has_pm25_environmental ) {
131
+ msgPayload[" pm25_e" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm25_environmental );
132
+ }
133
+ if (decoded->variant .air_quality_metrics .has_pm100_environmental ) {
134
+ msgPayload[" pm100_e" ] = new JSONValue ((unsigned int )decoded->variant .air_quality_metrics .pm100_environmental );
135
+ }
91
136
} else if (decoded->which_variant == meshtastic_Telemetry_power_metrics_tag) {
92
- msgPayload[" voltage_ch1" ] = new JSONValue (decoded->variant .power_metrics .ch1_voltage );
93
- msgPayload[" current_ch1" ] = new JSONValue (decoded->variant .power_metrics .ch1_current );
94
- msgPayload[" voltage_ch2" ] = new JSONValue (decoded->variant .power_metrics .ch2_voltage );
95
- msgPayload[" current_ch2" ] = new JSONValue (decoded->variant .power_metrics .ch2_current );
96
- msgPayload[" voltage_ch3" ] = new JSONValue (decoded->variant .power_metrics .ch3_voltage );
97
- msgPayload[" current_ch3" ] = new JSONValue (decoded->variant .power_metrics .ch3_current );
137
+ if (decoded->variant .power_metrics .has_ch1_voltage ) {
138
+ msgPayload[" voltage_ch1" ] = new JSONValue (decoded->variant .power_metrics .ch1_voltage );
139
+ }
140
+ if (decoded->variant .power_metrics .has_ch1_current ) {
141
+ msgPayload[" current_ch1" ] = new JSONValue (decoded->variant .power_metrics .ch1_current );
142
+ }
143
+ if (decoded->variant .power_metrics .has_ch2_voltage ) {
144
+ msgPayload[" voltage_ch2" ] = new JSONValue (decoded->variant .power_metrics .ch2_voltage );
145
+ }
146
+ if (decoded->variant .power_metrics .has_ch2_current ) {
147
+ msgPayload[" current_ch2" ] = new JSONValue (decoded->variant .power_metrics .ch2_current );
148
+ }
149
+ if (decoded->variant .power_metrics .has_ch3_voltage ) {
150
+ msgPayload[" voltage_ch3" ] = new JSONValue (decoded->variant .power_metrics .ch3_voltage );
151
+ }
152
+ if (decoded->variant .power_metrics .has_ch3_current ) {
153
+ msgPayload[" current_ch3" ] = new JSONValue (decoded->variant .power_metrics .ch3_current );
154
+ }
98
155
}
99
156
jsonObj[" payload" ] = new JSONValue (msgPayload);
100
157
} else if (shouldLog) {
0 commit comments