Core update
This commit is contained in:
parent
ae81b80dc4
commit
04473faa0d
@ -1,5 +1,6 @@
|
|||||||
#include "CoreThing.h"
|
#include "CoreThing.h"
|
||||||
|
|
||||||
|
#include "Participant.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
CoreThing::CoreThing(unsigned char networkId, unsigned char thingType) {
|
CoreThing::CoreThing(unsigned char networkId, unsigned char thingType) {
|
||||||
@ -8,6 +9,7 @@ CoreThing::CoreThing(unsigned char networkId, unsigned char thingType) {
|
|||||||
this->Init();
|
this->Init();
|
||||||
|
|
||||||
int thingId = CoreThing::Add(this);
|
int thingId = CoreThing::Add(this);
|
||||||
|
|
||||||
if (thingId < 0) {
|
if (thingId < 0) {
|
||||||
std::cout << "ERROR: Thing store is full\n";
|
std::cout << "ERROR: Thing store is full\n";
|
||||||
this->id = 0; // what to do when we cannot store any more things?
|
this->id = 0; // what to do when we cannot store any more things?
|
||||||
@ -15,15 +17,17 @@ CoreThing::CoreThing(unsigned char networkId, unsigned char thingType) {
|
|||||||
this->id = thingId;
|
this->id = thingId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CoreThing::Terminate() { CoreThing::Remove(this); }
|
||||||
|
|
||||||
void CoreThing::Init() {}
|
void CoreThing::Init() {}
|
||||||
|
|
||||||
void CoreThing::SetName(const char *name) { this->name = name; }
|
CoreThing *CoreThing::GetParent() { return this->parent; }
|
||||||
|
|
||||||
// All things
|
// All things
|
||||||
CoreThing *CoreThing::allThings[256] = {nullptr};
|
CoreThing *CoreThing::allThings[THING_STORE_SIZE] = {nullptr};
|
||||||
|
|
||||||
CoreThing *CoreThing::Get(unsigned char networkId, unsigned char thingId) {
|
CoreThing *CoreThing::Get(unsigned char networkId, unsigned char thingId) {
|
||||||
for (unsigned char ix = 0; ix < 256; ix++) {
|
for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
|
||||||
CoreThing *thing = allThings[ix];
|
CoreThing *thing = allThings[ix];
|
||||||
if (thing == nullptr)
|
if (thing == nullptr)
|
||||||
continue;
|
continue;
|
||||||
@ -34,13 +38,27 @@ CoreThing *CoreThing::Get(unsigned char networkId, unsigned char thingId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int CoreThing::Add(CoreThing *newThing) {
|
int CoreThing::Add(CoreThing *newThing) {
|
||||||
for (unsigned char ix = 0; ix < 256; ix++) {
|
for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
|
||||||
CoreThing *thing = allThings[ix];
|
CoreThing *thing = allThings[ix];
|
||||||
if (thing == nullptr) {
|
if (thing == nullptr) {
|
||||||
allThings[ix] = newThing;
|
allThings[ix] = newThing;
|
||||||
|
|
||||||
|
// std::cout << " Add new thing " << (int)ix << "\n";
|
||||||
return ix;
|
return ix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
CoreThing.h
36
CoreThing.h
@ -1,19 +1,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace Passer {
|
namespace Passer {
|
||||||
namespace Control {
|
namespace Control {
|
||||||
|
|
||||||
|
#define THING_STORE_SIZE 256
|
||||||
|
// IMPORTANT: values higher than 256 will need to change the CoreThing::id type
|
||||||
|
// to 16-bit or higher, breaking the networking protocol!
|
||||||
|
|
||||||
class CoreThing {
|
class CoreThing {
|
||||||
public:
|
public:
|
||||||
// Participant *client;
|
// Participant *client;
|
||||||
unsigned char networkId;
|
unsigned char networkId;
|
||||||
/// @char The id of the thing
|
/// @char The id of the thing
|
||||||
unsigned char id;
|
unsigned char id;
|
||||||
// CoreThing *parent;
|
CoreThing *parent;
|
||||||
/// @brief The type of Thing
|
/// @brief The type of Thing
|
||||||
unsigned char type;
|
unsigned char type;
|
||||||
const char *name = nullptr;
|
const char *name = nullptr;
|
||||||
const char *modelUrl = nullptr;
|
const char *modelUrl = nullptr;
|
||||||
|
float modelScale = 1;
|
||||||
// protected Sensor sensor;
|
// protected Sensor sensor;
|
||||||
|
|
||||||
/// @brief Basic Thing types
|
/// @brief Basic Thing types
|
||||||
@ -36,20 +42,32 @@ public:
|
|||||||
public:
|
public:
|
||||||
CoreThing(unsigned char networkId = 0,
|
CoreThing(unsigned char networkId = 0,
|
||||||
unsigned char thingType = (unsigned char)Type::Undetermined);
|
unsigned char thingType = (unsigned char)Type::Undetermined);
|
||||||
|
/// @brief Terminated thins are no longer updated
|
||||||
|
void Terminate();
|
||||||
|
|
||||||
void SetName(const char *name);
|
/// @brief Gets the parent Thing
|
||||||
|
/// @return The parent Thing
|
||||||
|
CoreThing *GetParent();
|
||||||
|
|
||||||
|
/// @brief Updates the state of the thing
|
||||||
|
/// @param currentTimeMs The current clock time in milliseconds
|
||||||
|
virtual void Update(unsigned long currentTimeMs) {};
|
||||||
|
|
||||||
virtual void SendBytes(unsigned char *buffer, unsigned char *ix) {};
|
virtual void SendBytes(unsigned char *buffer, unsigned char *ix) {};
|
||||||
|
virtual void ProcessBytes(unsigned char *bytes) {};
|
||||||
// All things
|
|
||||||
private:
|
|
||||||
static CoreThing *allThings[];
|
|
||||||
|
|
||||||
static CoreThing *Get(unsigned char networkId, unsigned char thingId);
|
|
||||||
static int Add(CoreThing *thing);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void Init();
|
virtual void Init();
|
||||||
|
|
||||||
|
//------------ All things
|
||||||
|
public:
|
||||||
|
static CoreThing *Get(unsigned char networkId, unsigned char thingId);
|
||||||
|
static int Add(CoreThing *thing);
|
||||||
|
static void Remove(CoreThing *thing);
|
||||||
|
static void UpdateAll(unsigned long currentTimeMs);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static CoreThing *allThings[];
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Control
|
} // namespace Control
|
||||||
|
21
Messages.cpp
21
Messages.cpp
@ -26,7 +26,7 @@ unsigned char *IMessage::ReceiveMsg(unsigned char packetSize) {
|
|||||||
bool IMessage::Publish(Participant *participant) {
|
bool IMessage::Publish(Participant *participant) {
|
||||||
return participant->PublishBuffer(Serialize(participant->buffer));
|
return participant->PublishBuffer(Serialize(participant->buffer));
|
||||||
}
|
}
|
||||||
bool IMessage::Send(Participant *participant) {
|
bool IMessage::SendTo(Participant *participant) {
|
||||||
return participant->SendBuffer(Serialize(participant->buffer));
|
return participant->SendBuffer(Serialize(participant->buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,7 +251,6 @@ CustomMsg::CustomMsg(unsigned char networkId, CoreThing *thing) {
|
|||||||
this->thing = thing;
|
this->thing = thing;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
unsigned char CustomMsg::Serialize(unsigned char *buffer) {
|
unsigned char CustomMsg::Serialize(unsigned char *buffer) {
|
||||||
unsigned char ix = this->length;
|
unsigned char ix = this->length;
|
||||||
this->thing->SendBytes(buffer, &ix);
|
this->thing->SendBytes(buffer, &ix);
|
||||||
@ -271,3 +270,21 @@ CustomMsg CustomMsg::Receive(unsigned char *buffer, unsigned char bufferSize) {
|
|||||||
|
|
||||||
// CustomMsg
|
// CustomMsg
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|
||||||
|
#pragma region DestroyMsg
|
||||||
|
|
||||||
|
DestroyMsg::DestroyMsg(unsigned char networkId, CoreThing *thing) {
|
||||||
|
this->networkId = networkId;
|
||||||
|
this->thingId = thing->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char DestroyMsg::Serialize(unsigned char *buffer) {
|
||||||
|
unsigned char ix = 0;
|
||||||
|
buffer[ix++] = this->id;
|
||||||
|
buffer[ix++] = this->networkId;
|
||||||
|
buffer[ix++] = this->thingId;
|
||||||
|
return ix;
|
||||||
|
}
|
||||||
|
|
||||||
|
// DestroyMsg
|
||||||
|
#pragma endregion
|
||||||
|
14
Messages.h
14
Messages.h
@ -18,7 +18,7 @@ public:
|
|||||||
static unsigned char *ReceiveMsg(unsigned char packetSize);
|
static unsigned char *ReceiveMsg(unsigned char packetSize);
|
||||||
|
|
||||||
bool Publish(Participant *participant);
|
bool Publish(Participant *participant);
|
||||||
bool Send(Participant *participant);
|
bool SendTo(Participant *participant);
|
||||||
};
|
};
|
||||||
|
|
||||||
class ClientMsg : public IMessage {
|
class ClientMsg : public IMessage {
|
||||||
@ -147,6 +147,18 @@ public:
|
|||||||
static CustomMsg Receive(unsigned char *buffer, unsigned char bufferSize);
|
static CustomMsg Receive(unsigned char *buffer, unsigned char bufferSize);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class DestroyMsg : public IMessage {
|
||||||
|
public:
|
||||||
|
static const unsigned char id = 0x20;
|
||||||
|
static const unsigned length = 3;
|
||||||
|
unsigned char networkId;
|
||||||
|
unsigned char thingId;
|
||||||
|
|
||||||
|
DestroyMsg(unsigned char networkId, CoreThing *thing);
|
||||||
|
|
||||||
|
virtual unsigned char Serialize(unsigned char *buffer) override;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace Control
|
} // namespace Control
|
||||||
} // namespace Passer
|
} // namespace Passer
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user