64 lines
1.6 KiB
C++
64 lines
1.6 KiB
C++
#include "CoreThing.h"
|
|
|
|
#include "Participant.h"
|
|
#include <iostream>
|
|
|
|
CoreThing::CoreThing(unsigned char networkId, unsigned char thingType) {
|
|
this->type = thingType;
|
|
this->networkId = networkId;
|
|
this->Init();
|
|
|
|
int thingId = CoreThing::Add(this);
|
|
|
|
if (thingId < 0) {
|
|
std::cout << "ERROR: Thing store is full\n";
|
|
this->id = 0; // what to do when we cannot store any more things?
|
|
} else
|
|
this->id = thingId;
|
|
}
|
|
|
|
void CoreThing::Terminate() { CoreThing::Remove(this); }
|
|
|
|
void CoreThing::Init() {}
|
|
|
|
CoreThing *CoreThing::GetParent() { return this->parent; }
|
|
|
|
// All things
|
|
CoreThing *CoreThing::allThings[THING_STORE_SIZE] = {nullptr};
|
|
|
|
CoreThing *CoreThing::Get(unsigned char networkId, unsigned char thingId) {
|
|
for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
|
|
CoreThing *thing = allThings[ix];
|
|
if (thing == nullptr)
|
|
continue;
|
|
if (thing->networkId == networkId && thing->id == thingId)
|
|
return thing;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
int CoreThing::Add(CoreThing *newThing) {
|
|
for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
|
|
CoreThing *thing = allThings[ix];
|
|
if (thing == nullptr) {
|
|
allThings[ix] = newThing;
|
|
|
|
// std::cout << " Add new thing " << (int)ix << "\n";
|
|
return ix;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void CoreThing::Remove(CoreThing *thing) { allThings[thing->id] = nullptr; }
|
|
|
|
void CoreThing::UpdateAll(unsigned long currentTimeMs) {
|
|
// Not very efficient, but it works for now.
|
|
for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
|
|
CoreThing *thing = allThings[ix];
|
|
if (thing != nullptr &&
|
|
thing->parent == nullptr) { // update all root things
|
|
thing->Update(currentTimeMs);
|
|
}
|
|
}
|
|
} |