2022-03-27 14:55:35 +00:00
|
|
|
#include "DeviceTelemetry.h"
|
|
|
|
|
#include "../mesh/generated/telemetry.pb.h"
|
|
|
|
|
#include "MeshService.h"
|
|
|
|
|
#include "NodeDB.h"
|
2022-05-07 20:31:21 +10:00
|
|
|
#include "PowerFSM.h"
|
2022-03-27 14:55:35 +00:00
|
|
|
#include "RTC.h"
|
|
|
|
|
#include "Router.h"
|
|
|
|
|
#include "configuration.h"
|
|
|
|
|
#include "main.h"
|
|
|
|
|
#include <OLEDDisplay.h>
|
|
|
|
|
#include <OLEDDisplayUi.h>
|
|
|
|
|
|
|
|
|
|
int32_t DeviceTelemetryModule::runOnce()
|
|
|
|
|
{
|
2022-07-31 07:11:47 -05:00
|
|
|
#ifndef ARCH_PORTDUINO
|
2022-10-11 10:21:30 -05:00
|
|
|
uint32_t now = millis();
|
|
|
|
|
if ((lastSentToMesh == 0 || (now - lastSentToMesh) >= getConfiguredOrDefaultMs(moduleConfig.telemetry.device_update_interval))
|
|
|
|
|
&& airTime->channelUtilizationPercent() < max_channel_util_percent) {
|
|
|
|
|
sendTelemetry();
|
|
|
|
|
lastSentToMesh = now;
|
|
|
|
|
} else {
|
|
|
|
|
// Just send to phone when it's not our time to send to mesh yet
|
|
|
|
|
sendTelemetry(NODENUM_BROADCAST, true);
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
2022-10-11 10:21:30 -05:00
|
|
|
return sendToPhoneIntervalMs;
|
2022-03-27 14:55:35 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceTelemetryModule::handleReceivedProtobuf(const MeshPacket &mp, Telemetry *t)
|
|
|
|
|
{
|
|
|
|
|
if (t->which_variant == Telemetry_device_metrics_tag) {
|
2022-04-24 16:12:25 -05:00
|
|
|
const char *sender = getSenderShortName(mp);
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
|
DEBUG_MSG("-----------------------------------------\n");
|
|
|
|
|
DEBUG_MSG("Device Telemetry: Received data from %s\n", sender);
|
|
|
|
|
DEBUG_MSG("Telemetry->time: %i\n", t->time);
|
2022-05-07 20:31:21 +10:00
|
|
|
DEBUG_MSG("Telemetry->air_util_tx: %f\n", t->variant.device_metrics.air_util_tx);
|
2022-03-27 14:55:35 +00:00
|
|
|
DEBUG_MSG("Telemetry->battery_level: %i\n", t->variant.device_metrics.battery_level);
|
|
|
|
|
DEBUG_MSG("Telemetry->channel_utilization: %f\n", t->variant.device_metrics.channel_utilization);
|
|
|
|
|
DEBUG_MSG("Telemetry->voltage: %f\n", t->variant.device_metrics.voltage);
|
|
|
|
|
|
|
|
|
|
lastMeasurementPacket = packetPool.allocCopy(mp);
|
|
|
|
|
|
|
|
|
|
nodeDB.updateTelemetry(getFrom(&mp), *t, RX_SRC_RADIO);
|
|
|
|
|
}
|
|
|
|
|
return false; // Let others look at this message also if they want
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:21:30 -05:00
|
|
|
bool DeviceTelemetryModule::sendTelemetry(NodeNum dest, bool phoneOnly)
|
2022-03-27 14:55:35 +00:00
|
|
|
{
|
2022-03-28 17:13:22 +00:00
|
|
|
Telemetry t;
|
2022-03-27 14:55:35 +00:00
|
|
|
|
2022-03-28 17:13:22 +00:00
|
|
|
t.time = getTime();
|
|
|
|
|
t.which_variant = Telemetry_device_metrics_tag;
|
2022-03-27 14:55:35 +00:00
|
|
|
|
2022-03-28 17:13:22 +00:00
|
|
|
t.variant.device_metrics.air_util_tx = myNodeInfo.air_util_tx;
|
|
|
|
|
t.variant.device_metrics.battery_level = powerStatus->getBatteryChargePercent();
|
|
|
|
|
t.variant.device_metrics.channel_utilization = myNodeInfo.channel_utilization;
|
|
|
|
|
t.variant.device_metrics.voltage = powerStatus->getBatteryVoltageMv() / 1000.0;
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
|
DEBUG_MSG("-----------------------------------------\n");
|
|
|
|
|
DEBUG_MSG("Device Telemetry: Read data\n");
|
|
|
|
|
|
2022-03-28 17:13:22 +00:00
|
|
|
DEBUG_MSG("Telemetry->time: %i\n", t.time);
|
|
|
|
|
DEBUG_MSG("Telemetry->air_util_tx: %f\n", t.variant.device_metrics.air_util_tx);
|
|
|
|
|
DEBUG_MSG("Telemetry->battery_level: %i\n", t.variant.device_metrics.battery_level);
|
|
|
|
|
DEBUG_MSG("Telemetry->channel_utilization: %f\n", t.variant.device_metrics.channel_utilization);
|
|
|
|
|
DEBUG_MSG("Telemetry->voltage: %f\n", t.variant.device_metrics.voltage);
|
2022-03-27 14:55:35 +00:00
|
|
|
|
2022-03-28 17:13:22 +00:00
|
|
|
MeshPacket *p = allocDataProtobuf(t);
|
2022-03-27 14:55:35 +00:00
|
|
|
p->to = dest;
|
2022-10-11 10:21:30 -05:00
|
|
|
p->decoded.want_response = false;
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
|
lastMeasurementPacket = packetPool.allocCopy(*p);
|
2022-03-28 17:13:22 +00:00
|
|
|
nodeDB.updateTelemetry(nodeDB.getNodeNum(), t, RX_SRC_LOCAL);
|
2022-10-11 10:21:30 -05:00
|
|
|
if (phoneOnly) {
|
|
|
|
|
DEBUG_MSG("Device Telemetry: Sending packet to phone\n");
|
|
|
|
|
service.sendToPhone(p);
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_MSG("Device Telemetry: Sending packet to mesh\n");
|
|
|
|
|
service.sendToMesh(p, RX_SRC_LOCAL, true);
|
|
|
|
|
}
|
2022-03-27 14:55:35 +00:00
|
|
|
return true;
|
|
|
|
|
}
|