meshtastic-firmware/src/NodeStatus.h

83 wiersze
2.2 KiB
C
Czysty Zwykły widok Historia

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