meshtastic-firmware/src/NodeStatus.h

68 wiersze
1.9 KiB
C

#pragma once
2020-06-29 01:17:52 +00:00
#include "Status.h"
#include "configuration.h"
2023-01-21 13:34:29 +00:00
#include <Arduino.h>
2023-01-21 13:34:29 +00:00
namespace meshtastic
{
2023-01-21 13:34:29 +00:00
/// Describes the state of the NodeDB system.
class NodeStatus : public Status
{
2023-01-21 13:34:29 +00:00
private:
CallbackObserver<NodeStatus, const NodeStatus *> statusObserver =
CallbackObserver<NodeStatus, const NodeStatus *>(this, &NodeStatus::updateStatus);
2023-01-21 13:34:29 +00:00
uint8_t numOnline = 0;
uint8_t numTotal = 0;
2023-01-21 13:34:29 +00:00
uint8_t lastNumTotal = 0;
2023-01-21 13:34:29 +00:00
public:
bool forceUpdate = false;
2023-01-21 13:34:29 +00:00
NodeStatus() { statusType = STATUS_TYPE_NODE; }
NodeStatus(uint8_t numOnline, uint8_t numTotal, bool forceUpdate = false) : Status()
{
this->forceUpdate = forceUpdate;
this->numOnline = numOnline;
this->numTotal = numTotal;
}
NodeStatus(const NodeStatus &);
NodeStatus &operator=(const NodeStatus &);
2023-01-21 13:34:29 +00:00
void observe(Observable<const NodeStatus *> *source) { statusObserver.observe(source); }
2023-01-21 13:34:29 +00:00
uint8_t getNumOnline() const { return numOnline; }
2023-01-21 13:34:29 +00:00
uint8_t getNumTotal() const { return numTotal; }
2023-01-21 13:34:29 +00:00
uint8_t getLastNumTotal() const { return lastNumTotal; }
2023-01-21 13:34:29 +00:00
bool matches(const NodeStatus *newStatus) const
{
return (newStatus->getNumOnline() != numOnline || newStatus->getNumTotal() != numTotal);
}
int updateStatus(const NodeStatus *newStatus)
{
// Only update the status if values have actually changed
lastNumTotal = numTotal;
bool isDirty;
2020-06-29 01:17:52 +00:00
{
2023-01-21 13:34:29 +00:00
isDirty = matches(newStatus);
initialized = true;
numOnline = newStatus->getNumOnline();
numTotal = newStatus->getNumTotal();
2020-06-29 01:17:52 +00:00
}
2023-01-21 13:34:29 +00:00
if (isDirty || newStatus->forceUpdate) {
LOG_DEBUG("Node status update: %d online, %d total\n", numOnline, numTotal);
onNewStatus.notifyObservers(this);
}
2023-01-21 13:34:29 +00:00
return 0;
}
};
2023-01-21 13:34:29 +00:00
} // namespace meshtastic
2020-06-29 01:17:52 +00:00
extern meshtastic::NodeStatus *nodeStatus;