RoboidControl-cpp/RemoteParticipant.cpp
2025-02-24 09:30:17 +01:00

53 lines
1.7 KiB
C++

#include "RemoteParticipant.h"
namespace RoboidControl {
RemoteParticipant::RemoteParticipant() {}
RemoteParticipant::RemoteParticipant(const char *ipAddress, int port) {
this->ipAddress = ipAddress;
this->port = port;
}
Thing *RemoteParticipant::Get(unsigned char networkId, unsigned char thingId) {
for (Thing *thing : this->things) {
if (thing->networkId == networkId && thing->id == thingId)
return thing;
}
// std::cout << "Could not find thing " << this->ipAddress << ":" << this->port
// << "[" << (int)networkId << "/" << (int)thingId << "]\n";
return nullptr;
}
void RemoteParticipant::Add(Thing *thing) {
Thing *foundThing = Get(thing->networkId, thing->id);
if (foundThing == nullptr) {
this->things.push_back(thing);
std::cout << "Add thing " << this->ipAddress << ":" << this->port << "["
<< (int)thing->networkId << "/" << (int)thing->id << "]\n";
} else
std::cout << "Did not add, existing thing " << this->ipAddress << ":" << this->port << "["
<< (int)thing->networkId << "/" << (int)thing->id << "]\n";
}
void RemoteParticipant::Remove(Thing *thing) {
this->things.remove_if([thing](Thing *obj) { return obj == thing; });
std::cout << "Removing " << thing->networkId << "/" << thing->id
<< " list size = " << this->things.size() << "\n";
}
void RemoteParticipant::UpdateAll(unsigned long currentTimeMs) {
// Not very efficient, but it works for now.
for (Thing *thing : this->things) {
if (thing != nullptr &&
thing->GetParent() == nullptr) { // update all root things
// std::cout << " update " << (int)ix << " thingid " << (int)thing->id
// << "\n";
thing->Update(currentTimeMs);
}
}
}
} // namespace Control