ChimeraTK-ControlSystemAdapter-OPCUAAdapter 04.00.05
Loading...
Searching...
No Matches
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
47extern "C" {
48#include <signal.h>
49#include <unistd.h>
50}
51
52#include "ChimeraTK/ControlSystemAdapter/ApplicationBase.h"
53#include "csa_opcua_adapter.h"
54#include "open62541/plugin/log_stdout.h"
55
56#include <atomic>
57#include <iostream>
58#include <string>
59
60boost::shared_ptr<ChimeraTK::ControlSystemPVManager> csManager;
61boost::shared_ptr<ChimeraTK::DevicePVManager> devManager;
63
64std::atomic<bool> terminateMain;
65
66static void SigHandler_Int(int /*sign*/) {
67 UA_LOG_INFO(csaOPCUA->getLogger(), UA_LOGCATEGORY_USERLAND, "Received SIGINT... terminating");
68 terminateMain = true;
69 if(csaOPCUA) {
70 csaOPCUA->stop();
72 }
73 ChimeraTK::ApplicationBase::getInstance().shutdown();
74 std::cout << "OPC UA adapter termianted." << std::endl;
75}
76
77int main() {
78 signal(SIGINT, SigHandler_Int); // Registriert CTRL-C/SIGINT
79 signal(SIGTERM, SigHandler_Int); // Registriert SIGTERM
80
81 /* Block SIGINT until the OPC UA Adapter is running.
82 * So the adapter is in a consistent state when we shut it down. */
83 sigset_t intmask;
84 sigemptyset(&intmask);
85 sigaddset(&intmask, SIGINT);
86 sigprocmask(SIG_BLOCK, &intmask, nullptr);
87
88 std::pair<boost::shared_ptr<ChimeraTK::ControlSystemPVManager>, boost::shared_ptr<ChimeraTK::DevicePVManager>>
89 pvManagers = ChimeraTK::createPVManager();
90
91 devManager = pvManagers.second;
92 csManager = pvManagers.first;
93
94 csManager->enablePersistentDataStorage();
95 ChimeraTK::ApplicationBase::getInstance().setPVManager(devManager);
96 ChimeraTK::ApplicationBase::getInstance().initialise();
97
98 string pathToConfig = ChimeraTK::ApplicationBase::getInstance().getName() + "_mapping.xml";
100
101 UA_LOG_INFO(csaOPCUA->getLogger(), UA_LOGCATEGORY_USERLAND, "Optimize unmapped variables in the application base");
102 ChimeraTK::ApplicationBase::getInstance().optimiseUnmappedVariables(csaOPCUA->getUnusedVariables());
103
104 UA_LOG_INFO(csaOPCUA->getLogger(), UA_LOGCATEGORY_USERLAND, "Run the application instance");
105 ChimeraTK::ApplicationBase::getInstance().run();
106
107 UA_LOG_INFO(csaOPCUA->getLogger(), UA_LOGCATEGORY_USERLAND, "Start the OPC UA Adapter");
108 csaOPCUA->start();
109
110 /* Unblock SIGINT */
111 sigprocmask(SIG_UNBLOCK, &intmask, nullptr);
112
113 while(!terminateMain) sleep(3600); // sleep will be interrupted when signal is received
114 csManager.reset();
115
116 std::cout << "Application termianted." << std::endl;
117}
This class provide the two parts of the OPCUA Adapter.
~csa_opcua_adapter()
Destructor to stop the running thread, hence it stops the OPC UA server.
void start()
Start all objects in single threads for this case only the opc ua server.
const UA_Logger * getLogger()
Get the adapter logger.
const set< string > & getUnusedVariables() const
void stop()
Stop all objects in single threads for this case only the opc ua server.
boost::shared_ptr< ChimeraTK::ControlSystemPVManager > csManager
std::atomic< bool > terminateMain
boost::shared_ptr< ChimeraTK::DevicePVManager > devManager
ChimeraTK::csa_opcua_adapter * csaOPCUA