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