112 lines
3.1 KiB
C++
112 lines
3.1 KiB
C++
#include "Participant.h"
|
|
|
|
#include <string.h>
|
|
#include "Arduino/ArduinoParticipant.h"
|
|
#include "EspIdf/EspIdfParticipant.h"
|
|
#include "Posix/PosixParticipant.h"
|
|
#include "Windows/WindowsParticipant.h"
|
|
|
|
namespace RoboidControl {
|
|
|
|
#pragma region Participant
|
|
|
|
Participant* Participant::LocalParticipant = new Participant();
|
|
|
|
void Participant::ReplaceLocalParticipant(Participant& newParticipant) {
|
|
LocalParticipant = &newParticipant;
|
|
std::cout << "Replaced local participant" << std::endl;
|
|
}
|
|
|
|
Participant::Participant() {
|
|
Thing::CreateRoot(this);
|
|
}
|
|
|
|
Participant::~Participant() {}
|
|
|
|
void Participant::Update(bool recurse) {
|
|
for (Thing* thing : this->things) {
|
|
if (thing != nullptr)
|
|
thing->Update();
|
|
}
|
|
}
|
|
|
|
bool Participant::Send(IMessage* msg) {
|
|
std::cout << "sending message " << (static_cast<int>(this->buffer[0]) & 0xff)
|
|
<< " to base Participant without communcation support "
|
|
<< std::endl;
|
|
|
|
return true;
|
|
}
|
|
|
|
Thing* Participant::Get(unsigned char networkId, unsigned char thingId) {
|
|
for (Thing* thing : this->things) {
|
|
if (thing->owner->networkId == networkId && thing->id == thingId)
|
|
return thing;
|
|
}
|
|
std::cout << "Could not find thing " << "[" << (int)networkId << ": "
|
|
<< (int)thingId << "]\n";
|
|
return nullptr;
|
|
}
|
|
|
|
void Participant::Add(Thing* thing, bool checkId) {
|
|
if (checkId && thing->id == 0) {
|
|
// allocate a new thing ID
|
|
#if defined(NO_STD)
|
|
thing->id = this->thingCount + 1;
|
|
this->things[this->thingCount++] = thing;
|
|
#else
|
|
// find highest id
|
|
int highestIx = 0;
|
|
for (Thing* thing : this->things) {
|
|
if (thing == nullptr)
|
|
continue;
|
|
if (thing->id > highestIx)
|
|
highestIx = thing->id;
|
|
}
|
|
thing->id = highestIx + 1;
|
|
this->things.push_back(thing);
|
|
#endif
|
|
std::cout << "Add thing with generated ID "
|
|
<< "[" << (int)networkId << ": " << (int)thing->id << "]\n";
|
|
} else {
|
|
Thing* foundThing = Get(thing->owner->networkId, thing->id);
|
|
if (foundThing == nullptr) {
|
|
#if defined(NO_STD)
|
|
this->things[this->thingCount++] = thing;
|
|
#else
|
|
this->things.push_back(thing);
|
|
#endif
|
|
std::cout << "Add thing [" << (int)networkId << ": " << (int)thing->id
|
|
<< "]\n";
|
|
} else {
|
|
std::cout << "Did not add, existing thing "
|
|
<< "[" << (int)networkId << ": " << (int)thing->id << "]\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
void Participant::Remove(Thing* thing) {
|
|
#if defined(NO_STD)
|
|
for (unsigned char thingIx = 0; thingIx < this->thingCount; thingIx++)
|
|
if (this->things[thingIx] == thing)
|
|
this->things[thingIx] = nullptr;
|
|
// compacting
|
|
unsigned char lastThingIx = 0;
|
|
for (unsigned char thingIx = 0; thingIx < this->thingCount; thingIx++) {
|
|
if (this->things[thingIx] == nullptr)
|
|
continue;
|
|
this->things[lastThingIx] = this->things[thingIx];
|
|
lastThingIx++;
|
|
}
|
|
this->thingCount = lastThingIx;
|
|
#else
|
|
this->things.remove_if([thing](Thing* obj) { return obj == thing; });
|
|
// std::cout << "Removing [" << (int)thing->networkId << "/" << (int)thing->id
|
|
// << "] list size = " << this->things.size() << "\n";
|
|
#endif
|
|
}
|
|
|
|
#pragma endregion
|
|
|
|
} // namespace RoboidControl
|