ChimeraTK-ControlSystemAdapter-OPCUAAdapter  04.00.01
csa_opcua_application.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ChimeraTKs ControlSystem-OPC-UA-Adapter.
3  *
4  * ChimeraTKs ControlSystem-OPC-UA-Adapter is free software: you can
5  * redistribute it and/or modify it under the terms of the Lesser GNU
6  * General Public License as published by the Free Software Foundation,
7  * either version 3 of the License, or (at your option) any later version.
8  *
9  * ChimeraTKs ControlSystem-OPC-UA-Adapter is distributed in the hope
10  * that it will be useful, but WITHOUT ANY WARRANTY; without even the
11  * implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the Lesser GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Foobar. If not, see https://www.gnu.org/licenses/lgpl.html
16  *
17  * Copyright (c) 2016 Chris Iatrou <Chris_Paul.Iatrou@tu-dresden.de>
18  * Copyright (c) 2016 Julian Rahm <Julian.Rahm@tu-dresden.de>
19  * Copyright (c) 2023 Andreas Ebner <Andreas.Ebner@iosb.fraunhofer.de>
20  */
21 
46 extern "C" {
47 #include <signal.h>
48 #include <unistd.h>
49 }
50 
51 #include "ChimeraTK/ControlSystemAdapter/ApplicationBase.h"
52 #include "csa_opcua_adapter.h"
53 #include "open62541/plugin/log_stdout.h"
54 
55 #include <atomic>
56 #include <iostream>
57 #include <string>
58 
59 boost::shared_ptr<ChimeraTK::ControlSystemPVManager> csManager;
60 boost::shared_ptr<ChimeraTK::DevicePVManager> devManager;
62 
63 std::atomic<bool> terminateMain;
64 
65 static void SigHandler_Int(int /*sign*/) {
66  UA_LOG_INFO(csaOPCUA->getLogger(), UA_LOGCATEGORY_USERLAND, "Received SIGINT... terminating");
67  terminateMain = true;
68  if(csaOPCUA) {
69  csaOPCUA->stop();
71  }
72  ChimeraTK::ApplicationBase::getInstance().shutdown();
73  std::cout << "OPC UA adapter termianted." << std::endl;
74 }
75 
76 int main() {
77  signal(SIGINT, SigHandler_Int); // Registriert CTRL-C/SIGINT
78  signal(SIGTERM, SigHandler_Int); // Registriert SIGTERM
79 
80  /* Block SIGINT until the OPC UA Adapter is running.
81  * So the adapter is in a consistent state when we shut it down. */
82  sigset_t intmask;
83  sigemptyset(&intmask);
84  sigaddset(&intmask, SIGINT);
85  sigprocmask(SIG_BLOCK, &intmask, nullptr);
86 
87  std::pair<boost::shared_ptr<ChimeraTK::ControlSystemPVManager>, boost::shared_ptr<ChimeraTK::DevicePVManager>>
88  pvManagers = ChimeraTK::createPVManager();
89 
90  devManager = pvManagers.second;
91  csManager = pvManagers.first;
92 
93  csManager->enablePersistentDataStorage();
94  ChimeraTK::ApplicationBase::getInstance().setPVManager(devManager);
95  ChimeraTK::ApplicationBase::getInstance().initialise();
96 
97  string pathToConfig = ChimeraTK::ApplicationBase::getInstance().getName() + "_mapping.xml";
98  csaOPCUA = new ChimeraTK::csa_opcua_adapter(csManager, pathToConfig);
99 
100  UA_LOG_INFO(csaOPCUA->getLogger(), UA_LOGCATEGORY_USERLAND, "Optimize unmapped variables in the application base");
101  ChimeraTK::ApplicationBase::getInstance().optimiseUnmappedVariables(csaOPCUA->getUnusedVariables());
102 
103  UA_LOG_INFO(csaOPCUA->getLogger(), UA_LOGCATEGORY_USERLAND, "Run the application instance");
104  ChimeraTK::ApplicationBase::getInstance().run();
105 
106  UA_LOG_INFO(csaOPCUA->getLogger(), UA_LOGCATEGORY_USERLAND, "Start the OPC UA Adapter");
107  csaOPCUA->start();
108 
109  /* Unblock SIGINT */
110  sigprocmask(SIG_UNBLOCK, &intmask, nullptr);
111 
112  while(!terminateMain) sleep(3600); // sleep will be interrupted when signal is received
113  csManager.reset();
114 
115  std::cout << "Application termianted." << std::endl;
116 }
ChimeraTK::csa_opcua_adapter
This class provide the two parts of the OPCUA Adapter. First of all the OPCUA server starts with a po...
Definition: csa_opcua_adapter.h:44
csa_opcua_adapter.h
ChimeraTK::csa_opcua_adapter::stop
void stop()
Stop all objects in single threads for this case only the opc ua server.
Definition: csa_opcua_adapter.cpp:216
ChimeraTK::csa_opcua_adapter::getUnusedVariables
const set< string > & getUnusedVariables() const
Definition: csa_opcua_adapter.cpp:229
devManager
boost::shared_ptr< ChimeraTK::DevicePVManager > devManager
Definition: csa_opcua_application.cpp:60
main
int main()
Definition: csa_opcua_application.cpp:76
ChimeraTK::csa_opcua_adapter::getLogger
const UA_Logger * getLogger()
Get the adapter logger.
Definition: csa_opcua_adapter.cpp:233
terminateMain
std::atomic< bool > terminateMain
Definition: csa_opcua_application.cpp:63
csaOPCUA
ChimeraTK::csa_opcua_adapter * csaOPCUA
Definition: csa_opcua_application.cpp:61
ChimeraTK::csa_opcua_adapter::~csa_opcua_adapter
~csa_opcua_adapter()
Destructor to stop the running thread, hence it stops the OPC UA server.
Definition: csa_opcua_adapter.cpp:168
csManager
boost::shared_ptr< ChimeraTK::ControlSystemPVManager > csManager
Definition: csa_opcua_application.cpp:59
ChimeraTK::csa_opcua_adapter::start
void start()
Start all objects in single threads for this case only the opc ua server.
Definition: csa_opcua_adapter.cpp:180