Skip to content

Commit

Permalink
Fix: only create aggregate OPC/UA objects (Relays, LEDs, ...) when th…
Browse files Browse the repository at this point in the history
…ere is at least one such node. (#4)
  • Loading branch information
aentinger authored Jun 13, 2024
1 parent 641af99 commit 4ace327
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 28 deletions.
80 changes: 56 additions & 24 deletions src/ArduinoOpta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,16 @@ namespace opcua
**************************************************************************************/

ArduinoOpta::ArduinoOpta(UA_Server * server, UA_NodeId const & node_id)
: _node_id{node_id}
: _server{server}
, _node_id{node_id}
, _analog_input_mgr{nullptr}
, _digital_input_mgr{nullptr}
, _relay_mgr{nullptr}
, _led_mgr{nullptr}
{
_usr_button = opcua::UserButton::create(server, _node_id);
if (!_usr_button) {
_usr_button = opcua::UserButton::create(_server, _node_id);
if (!_usr_button)
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::Ctor: UserButton::create(...) failed.");
}

_analog_input_mgr = opcua::AnalogInputManager::create(server, _node_id);
if (!_analog_input_mgr) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::Ctor: AnalogInputManager::create(...) failed.");
}

_digital_input_mgr = opcua::DigitalInputManager::create(server, _node_id);
if (!_digital_input_mgr) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::Ctor: DigitalInputManager::create(...) failed.");
}

_relay_mgr = opcua::RelayManager::create(server, _node_id);
if (!_relay_mgr) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::Ctor: RelayManager::create(...) failed.");
}

_led_mgr = opcua::LedManager::create(server, _node_id);
if (!_led_mgr) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::Ctor: LedManager::create(...) failed.");
}
}

/**************************************************************************************
Expand Down Expand Up @@ -148,6 +132,54 @@ ArduinoOpta::SharedPtr ArduinoOpta::create(UA_Server * server, ArduinoOptaVarian
return instance_ptr;
}

AnalogInputManager::SharedPtr ArduinoOpta::analog_input_mgr()
{
if (!_analog_input_mgr)
{
_analog_input_mgr = opcua::AnalogInputManager::create(_server, _node_id);
if (!_analog_input_mgr)
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::analog_input_mgr: AnalogInputManager::create(...) failed.");
}

return _analog_input_mgr;
}

DigitalInputManager::SharedPtr ArduinoOpta::digital_input_mgr()
{
if (!_digital_input_mgr)
{
_digital_input_mgr = opcua::DigitalInputManager::create(_server, _node_id);
if (!_digital_input_mgr)
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::digital_input_mgr: DigitalInputManager::create(...) failed.");
}

return _digital_input_mgr;
}

RelayManager::SharedPtr ArduinoOpta::relay_mgr()
{
if (!_relay_mgr)
{
_relay_mgr = opcua::RelayManager::create(_server, _node_id);
if (!_relay_mgr)
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::relay_mgr: RelayManager::create(...) failed.");
}

return _relay_mgr;
}

LedManager::SharedPtr ArduinoOpta::led_mgr()
{
if (!_led_mgr)
{
_led_mgr = opcua::LedManager::create(_server, _node_id);
if (!_led_mgr)
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "ArduinoOpta::led_mgr: LedManager::create(...) failed.");
}

return _led_mgr;
}

/**************************************************************************************
* NAMESPACE
**************************************************************************************/
Expand Down
10 changes: 6 additions & 4 deletions src/ArduinoOpta.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ class ArduinoOpta

ArduinoOpta(UA_Server * server, UA_NodeId const & node_id);

inline AnalogInputManager::SharedPtr analog_input_mgr() const { return _analog_input_mgr; }
inline DigitalInputManager::SharedPtr digital_input_mgr() const { return _digital_input_mgr; }
inline RelayManager::SharedPtr relay_mgr() const { return _relay_mgr; }
inline LedManager::SharedPtr led_mgr() const { return _led_mgr; }
AnalogInputManager::SharedPtr analog_input_mgr();
DigitalInputManager::SharedPtr digital_input_mgr();
RelayManager::SharedPtr relay_mgr();
LedManager::SharedPtr led_mgr();


private:
UA_Server * _server;
UA_NodeId _node_id;

UserButton::SharedPtr _usr_button;
AnalogInputManager::SharedPtr _analog_input_mgr;
DigitalInputManager::SharedPtr _digital_input_mgr;
Expand Down

0 comments on commit 4ace327

Please sign in to comment.