update examples (#9)

This commit is contained in:
tompzf
2026-04-07 09:11:36 +02:00
committed by GitHub
parent 07cf4f654b
commit 511c1dda51
16 changed files with 112 additions and 116 deletions

View File

@@ -5,6 +5,8 @@ Version = 100
Path = "module_example.sdv"
Class = "class_example"
Name = "object_name_example"
[Component.Parameters]
updatableValue = 13
Message = "It's me"
Id = 42
Pi = 3.1415926

View File

@@ -2,32 +2,41 @@
#include <support/component_impl.h>
#include <support/toml.h>
class DemoConfigurationComponent : public sdv::CSdvObject, public sdv::IObjectControl
class DemoConfigurationComponent : public sdv::CSdvObject
{
public:
BEGIN_SDV_INTERFACE_MAP()
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::Device)
public:
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::device)
DECLARE_OBJECT_CLASS_NAME("Configuration_Example")
// Parameter map
BEGIN_SDV_PARAM_MAP()
SDV_PARAM_ENABLE_LOCKING() // optional, if the parameter should be updatable
SDV_PARAM_ENTRY(m_InitializedValue, "initializedValue", 7, "km/h", "Description for an initialized parameter.")
SDV_PARAM_ENTRY(m_UpdatableValue, "updatableValue", 7, "m/s", "Description for an updatable parameter.")
END_SDV_PARAM_MAP()
/**
* @brief initialize function to register, access the task timer interface from platform abstraction.
* After initialization 'CreateTimer' function is called to execute the task periodically.
* @param[in] rssObjectConfig An object configuration is currently not used by this demo component.
*/
virtual void Initialize([[maybe_unused]] const sdv::u8string& rssObjectConfig) override
* @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
* @return Returns 'true' when the initialization was successful, 'false' when not.
*/
virtual bool OnInitialize() override
{
try
{
sdv::toml::CTOMLParser config(rssObjectConfig.c_str());
sdv::toml::CTOMLParser config(GetObjectConfig());
sdv::toml::CNode messageNode = config.GetDirect("Message");
if (messageNode.GetType() == sdv::toml::ENodeType::node_string)
{
m_Message = static_cast<std::string>(messageNode.GetValue());
}
sdv::toml::CNode jsonNode = config.GetDirect("JSONConfig");
if (jsonNode.GetType() == sdv::toml::ENodeType::node_string)
{
m_JSONConfig = static_cast<std::string>(jsonNode.GetValue());
}
sdv::toml::CNode idNode = config.GetDirect("Id");
if (idNode.GetType() == sdv::toml::ENodeType::node_integer)
{
@@ -76,73 +85,43 @@ class DemoConfigurationComponent : public sdv::CSdvObject, public sdv::IObjectCo
m_DirectTableA = static_cast<uint32_t>(table_a.GetValue());
m_DirectTableB = static_cast<float>(table_b.GetValue());
m_DirectTableC = static_cast<std::string>(table_c.GetValue());
}
catch (const sdv::toml::XTOMLParseException& e)
{
SDV_LOG_ERROR("Parsing error: ", e.what());
m_status = sdv::EObjectStatus::initialization_failure;
return;
return false;
}
PrintAllVariables();
m_status = sdv::EObjectStatus::initialized;
return true;
};
/**
* @brief Gets the current status of the object
* @return EObjectStatus The current status of the object
*/
virtual sdv::EObjectStatus GetStatus() const override
{
return m_status;
};
/**
* @brief Set the component operation mode. Overload of sdv::IObjectControl::SetOperationMode.
* @param[in] eMode The operation mode, the component should run in.
* @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
*/
void SetOperationMode(/*in*/ sdv::EOperationMode eMode)
{
switch (eMode)
{
case sdv::EOperationMode::configuring:
if (m_status == sdv::EObjectStatus::running || m_status == sdv::EObjectStatus::initialized)
m_status = sdv::EObjectStatus::configuring;
break;
case sdv::EOperationMode::running:
if (m_status == sdv::EObjectStatus::configuring || m_status == sdv::EObjectStatus::initialized)
m_status = sdv::EObjectStatus::running;
break;
default:
break;
}
}
/**
* @brief Shutdown function is to shutdown the execution of periodic task.
* Timer ID of the task is used to shutdown the specific task.
*/
virtual void Shutdown() override
{
m_status = sdv::EObjectStatus::destruction_pending;
}
virtual void OnShutdown() override
{}
/**
* @brief Print all global variables to console
*/
void PrintAllVariables()
void PrintAllVariables() const
{
std::cout << "\nValues parsed during initialization:\n" << std::endl;
std::cout << "string: " << m_Message.c_str() << std::endl;
std::cout << "integer: " << std::to_string(m_Id) << std::endl;
std::cout << "float: " << std::to_string(m_Pi) << std::endl;
std::cout << "bool: " << std::to_string(m_IsValid) << std::endl;
std::cout << "table column a: " << std::to_string(m_TableA) << " " << std::to_string(m_DirectTableA) << std::endl;
std::cout << "table column b: " << std::to_string(m_TableB) << " " << std::to_string(m_DirectTableB) << std::endl;
std::cout << "table column c: " << m_TableC.c_str() << " " << m_DirectTableC.c_str() << std::endl;
std::cout << "array: ";
std::cout << "\n----------\nValues from the parameter map:" << std::endl;
std::cout << "Expected 7, got " << "initialized value - not changed, not in configuration file: " << std::to_string(m_InitializedValue) << std::endl;
std::cout << "Expected 13, got " << "updated value - changed, found in configuration file: " << std::to_string(m_UpdatableValue) << std::endl;
std::cout << "\n----------\nValues parsed during initialization:" << std::endl;
std::cout << "Expected 'It's me', got " << "string: " << m_Message.c_str() << std::endl;
std::cout << "multiline string: " << m_JSONConfig.c_str() << std::endl;
std::cout << "Expected 42, got " << "integer: " << std::to_string(m_Id) << std::endl;
std::cout << "Expected 3.141593, got " << "float: " << std::to_string(m_Pi) << std::endl;
std::cout << "Expected 1, got " << "bool: " << std::to_string(m_IsValid) << std::endl;
std::cout << "Expected 77, got " << "table column a: " << std::to_string(m_TableA) << " " << std::to_string(m_DirectTableA) << std::endl;
std::cout << "Expected 1.200000, got " << "table column b: " << std::to_string(m_TableB) << " " << std::to_string(m_DirectTableB) << std::endl;
std::cout << "Expected 'this is a string', got " << "table column c: " << m_TableC.c_str() << " " << m_DirectTableC.c_str() << std::endl;
for (auto counter : m_Counters)
{
std::cout << std::to_string(counter) << ", ";
@@ -151,19 +130,20 @@ class DemoConfigurationComponent : public sdv::CSdvObject, public sdv::IObjectCo
}
private:
std::atomic<sdv::EObjectStatus> m_status = {sdv::EObjectStatus::initialization_pending}; //!< To update the object status when it changes.
std::string m_Message = "";
int32_t m_Id = -1;
float m_Pi = 0.0;
bool m_IsValid = false;
std::vector<uint32_t> m_Counters;
uint32_t m_TableA = 0;
float m_TableB = 0.0;
std::string m_TableC = "";
uint32_t m_DirectTableA = 0;
float m_DirectTableB = 0.0;
std::string m_DirectTableC = "";
std::string m_Message { "" };
std::string m_JSONConfig { "" };
int32_t m_Id { -1 };
float m_Pi { 0.0 };
bool m_IsValid { false };
std::vector<uint32_t> m_Counters{};
uint32_t m_TableA { 0 };
float m_TableB { 0.0 };
std::string m_TableC { "" };
uint32_t m_DirectTableA { 0 };
float m_DirectTableB { 0.0 };
std::string m_DirectTableC { "" };
uint32_t m_InitializedValue { 0 };
uint32_t m_UpdatableValue { 0 };
};
DEFINE_SDV_OBJECT(DemoConfigurationComponent)