2020-08-13 20:53:35 +02:00
|
|
|
#include "HealthTable.h"
|
|
|
|
#include "../serialize/SerializeAdapter.h"
|
|
|
|
#include "../ipc/MutexFactory.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
HealthTable::HealthTable(object_id_t objectid) :
|
|
|
|
SystemObject(objectid) {
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex = MutexFactory::instance()->createMutex();;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
mapIterator = healthMap.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
HealthTable::~HealthTable() {
|
2018-07-12 16:29:32 +02:00
|
|
|
MutexFactory::instance()->deleteMutex(mutex);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t HealthTable::registerObject(object_id_t object,
|
|
|
|
HasHealthIF::HealthState initilialState) {
|
|
|
|
if (healthMap.count(object) != 0) {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
healthMap.insert(
|
|
|
|
std::pair<object_id_t, HasHealthIF::HealthState>(object,
|
|
|
|
initilialState));
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HealthTable::setHealth(object_id_t object,
|
|
|
|
HasHealthIF::HealthState newState) {
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
|
|
|
iter->second = newState;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HasHealthIF::HealthState HealthTable::getHealth(object_id_t object) {
|
|
|
|
HasHealthIF::HealthState state = HasHealthIF::HEALTHY;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
|
|
|
state = iter->second;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t HealthTable::getPrintSize() {
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
uint32_t size = healthMap.size() * 5 + 2;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HealthTable::hasHealth(object_id_t object) {
|
|
|
|
bool exits = false;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
|
|
|
exits = true;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
return exits;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
void HealthTable::printAll(uint8_t* pointer, size_t maxSize) {
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t size = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
uint16_t count = healthMap.size();
|
2020-04-21 21:34:03 +02:00
|
|
|
ReturnValue_t result = SerializeAdapter::serialize(&count,
|
2020-04-21 22:28:43 +02:00
|
|
|
&pointer, &size, maxSize, SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter;
|
|
|
|
for (iter = healthMap.begin();
|
|
|
|
iter != healthMap.end() && result == HasReturnvaluesIF::RETURN_OK;
|
|
|
|
++iter) {
|
2020-04-21 21:34:03 +02:00
|
|
|
result = SerializeAdapter::serialize(&iter->first,
|
2020-04-21 22:28:43 +02:00
|
|
|
&pointer, &size, maxSize, SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
uint8_t health = iter->second;
|
2020-04-21 21:34:03 +02:00
|
|
|
result = SerializeAdapter::serialize(&health, &pointer, &size,
|
2020-04-21 22:28:43 +02:00
|
|
|
maxSize, SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t HealthTable::iterate(
|
|
|
|
std::pair<object_id_t, HasHealthIF::HealthState> *value, bool reset) {
|
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (reset) {
|
|
|
|
mapIterator = healthMap.begin();
|
|
|
|
}
|
|
|
|
if (mapIterator == healthMap.end()) {
|
|
|
|
result = HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
*value = *mapIterator;
|
|
|
|
mapIterator++;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|