Files
firmware/src/NodeStatus.h

68 lines
1.9 KiB
C
Raw Permalink Normal View History

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