From 9db80ca15d6d6de0e50950db9b4336c1fd5c38b7 Mon Sep 17 00:00:00 2001 From: Mattia Mancini <mancini@astron.nl> Date: Wed, 29 May 2019 13:43:59 +0000 Subject: [PATCH] OSB-12: merge to master branch --- .gitattributes | 25 ++ LCS/WinCCWrapper/include/CMakeLists.txt | 3 +- LCS/WinCCWrapper/include/WinCCManager.h | 25 +- LCS/WinCCWrapper/include/WinCCResources.h | 7 +- LCS/WinCCWrapper/include/WinCCWrapper.h | 25 +- LCS/WinCCWrapper/include/exceptions.h | 23 ++ LCS/WinCCWrapper/src/ConnectWaitForAnswer.cc | 41 ++- LCS/WinCCWrapper/src/ConnectWaitForAnswer.h | 6 + LCS/WinCCWrapper/src/WinCCManager.cc | 260 +++++++++++++++++- LCS/WinCCWrapper/src/WinCCResources.cc | 59 +++- LCS/WinCCWrapper/src/WinCCWrapper.cc | 55 +++- LCS/WinCCWrapper/src/exceptions.h | 12 + LCS/WinCCWrapper/test/CMakeLists.txt | 9 + LCS/WinCCWrapper/test/WinCCGet.cc | 10 +- LCS/WinCCWrapper/test/WinCCQuery.cc | 68 +++++ MAC/CMakeLists.txt | 4 + MAC/WinCCDBBridge/CMakeLists.txt | 20 ++ MAC/WinCCDBBridge/DB.ini | 7 + MAC/WinCCDBBridge/Docker/Dockerfile | 55 ++++ MAC/WinCCDBBridge/Docker/LOFAR.tar.gz | Bin 0 -> 568 bytes MAC/WinCCDBBridge/Docker/run.sh | 4 + MAC/WinCCDBBridge/etc/CMakeLists.txt | 0 MAC/WinCCDBBridge/etc/DB.ini | 7 + .../etc/Datapoints/antennaStatuses.xml | 34 +++ .../etc/Datapoints/antennaStatuses.xsd | 63 +++++ MAC/WinCCDBBridge/src/CMakeLists.txt | 25 ++ MAC/WinCCDBBridge/src/DatapointAttribute.cpp | 180 ++++++++++++ MAC/WinCCDBBridge/src/DatapointAttribute.hpp | 76 +++++ MAC/WinCCDBBridge/src/DatapointIndex.cpp | 52 ++++ MAC/WinCCDBBridge/src/DatapointIndex.hpp | 40 +++ MAC/WinCCDBBridge/src/DatapointRule.cpp | 199 ++++++++++++++ MAC/WinCCDBBridge/src/DatapointRule.hpp | 77 ++++++ MAC/WinCCDBBridge/src/Program.cpp | 249 +++++++++++++++++ MAC/WinCCDBBridge/src/Program.hpp | 50 ++++ MAC/WinCCDBBridge/src/main.cpp | 69 +++++ MAC/WinCCDBBridge/test/CMakeLists.txt | 27 ++ .../test/t_DatapointAttribute.cpp | 98 +++++++ MAC/WinCCDBBridge/test/t_DatapointIndex.cpp | 11 + 38 files changed, 1955 insertions(+), 20 deletions(-) create mode 100644 LCS/WinCCWrapper/include/exceptions.h create mode 100644 LCS/WinCCWrapper/src/exceptions.h create mode 100644 LCS/WinCCWrapper/test/WinCCQuery.cc create mode 100644 MAC/WinCCDBBridge/CMakeLists.txt create mode 100644 MAC/WinCCDBBridge/DB.ini create mode 100644 MAC/WinCCDBBridge/Docker/Dockerfile create mode 100644 MAC/WinCCDBBridge/Docker/LOFAR.tar.gz create mode 100755 MAC/WinCCDBBridge/Docker/run.sh create mode 100644 MAC/WinCCDBBridge/etc/CMakeLists.txt create mode 100644 MAC/WinCCDBBridge/etc/DB.ini create mode 100644 MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xml create mode 100644 MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xsd create mode 100644 MAC/WinCCDBBridge/src/CMakeLists.txt create mode 100644 MAC/WinCCDBBridge/src/DatapointAttribute.cpp create mode 100644 MAC/WinCCDBBridge/src/DatapointAttribute.hpp create mode 100644 MAC/WinCCDBBridge/src/DatapointIndex.cpp create mode 100644 MAC/WinCCDBBridge/src/DatapointIndex.hpp create mode 100644 MAC/WinCCDBBridge/src/DatapointRule.cpp create mode 100644 MAC/WinCCDBBridge/src/DatapointRule.hpp create mode 100644 MAC/WinCCDBBridge/src/Program.cpp create mode 100644 MAC/WinCCDBBridge/src/Program.hpp create mode 100644 MAC/WinCCDBBridge/src/main.cpp create mode 100644 MAC/WinCCDBBridge/test/CMakeLists.txt create mode 100644 MAC/WinCCDBBridge/test/t_DatapointAttribute.cpp create mode 100644 MAC/WinCCDBBridge/test/t_DatapointIndex.cpp diff --git a/.gitattributes b/.gitattributes index 802cce871a5..f85aebb1417 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1769,6 +1769,7 @@ LCS/WinCCWrapper/include/CMakeLists.txt -text LCS/WinCCWrapper/include/WinCCManager.h -text LCS/WinCCWrapper/include/WinCCResources.h -text LCS/WinCCWrapper/include/WinCCWrapper.h -text +LCS/WinCCWrapper/include/exceptions.h -text LCS/WinCCWrapper/src/CMakeLists.txt -text LCS/WinCCWrapper/src/ConnectWaitForAnswer.cc -text LCS/WinCCWrapper/src/ConnectWaitForAnswer.h -text @@ -1777,8 +1778,10 @@ LCS/WinCCWrapper/src/WinCCResources.cc -text LCS/WinCCWrapper/src/WinCCWrapper.cc -text LCS/WinCCWrapper/src/WinCCWrapper_boost_python.cc -text LCS/WinCCWrapper/src/__init__.py -text +LCS/WinCCWrapper/src/exceptions.h -text LCS/WinCCWrapper/test/CMakeLists.txt -text LCS/WinCCWrapper/test/WinCCGet.cc -text +LCS/WinCCWrapper/test/WinCCQuery.cc -text LCS/WinCCWrapper/test/WinCCSet.cc -text LCS/WinCCWrapper/test/mock.py -text LCS/doc/package.dox -text @@ -4232,6 +4235,28 @@ MAC/Tools/jtagctl/rsp_infraCnn.Bin -text svneol=unset#unset MAC/Tools/jtagctl/rsp_interApl.Bin -text svneol=unset#unset MAC/Tools/jtagctl/rsp_interCnn.Bin -text svneol=unset#unset MAC/Tools/jtagctl/testdata.bin -text svneol=unset#unset +MAC/WinCCDBBridge/CMakeLists.txt -text +MAC/WinCCDBBridge/DB.ini -text +MAC/WinCCDBBridge/Docker/Dockerfile -text +MAC/WinCCDBBridge/Docker/LOFAR.tar.gz -text svneol=unset#application/x-gzip +MAC/WinCCDBBridge/Docker/run.sh -text +MAC/WinCCDBBridge/etc/CMakeLists.txt -text +MAC/WinCCDBBridge/etc/DB.ini -text +MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xml -text +MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xsd -text +MAC/WinCCDBBridge/src/CMakeLists.txt -text +MAC/WinCCDBBridge/src/DatapointAttribute.cpp -text +MAC/WinCCDBBridge/src/DatapointAttribute.hpp -text +MAC/WinCCDBBridge/src/DatapointIndex.cpp -text +MAC/WinCCDBBridge/src/DatapointIndex.hpp -text +MAC/WinCCDBBridge/src/DatapointRule.cpp -text +MAC/WinCCDBBridge/src/DatapointRule.hpp -text +MAC/WinCCDBBridge/src/Program.cpp -text +MAC/WinCCDBBridge/src/Program.hpp -text +MAC/WinCCDBBridge/src/main.cpp -text +MAC/WinCCDBBridge/test/CMakeLists.txt -text +MAC/WinCCDBBridge/test/t_DatapointAttribute.cpp -text +MAC/WinCCDBBridge/test/t_DatapointIndex.cpp -text MAC/WinCCPublisher/CMakeLists.txt -text MAC/WinCCPublisher/config/WinCCPublisher.conf -text MAC/WinCCPublisher/doc/package.dox -text diff --git a/LCS/WinCCWrapper/include/CMakeLists.txt b/LCS/WinCCWrapper/include/CMakeLists.txt index 9341a3420be..48366da002d 100644 --- a/LCS/WinCCWrapper/include/CMakeLists.txt +++ b/LCS/WinCCWrapper/include/CMakeLists.txt @@ -1,5 +1,5 @@ # Create symbolic link to include directory. -execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink +execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_BINARY_DIR}/include/${PACKAGE_NAME}) @@ -8,4 +8,5 @@ install(FILES WinCCManager.h WinCCResources.h WinCCWrapper.h + exceptions.h DESTINATION include/${PACKAGE_NAME}) diff --git a/LCS/WinCCWrapper/include/WinCCManager.h b/LCS/WinCCWrapper/include/WinCCManager.h index 69e70cbb947..c29d37cefaa 100644 --- a/LCS/WinCCWrapper/include/WinCCManager.h +++ b/LCS/WinCCWrapper/include/WinCCManager.h @@ -31,6 +31,7 @@ #include <Variable.hxx> #include <boost/python.hpp> +#include <boost/any.hpp> namespace LOFAR { namespace WINCCWRAPPER { @@ -41,7 +42,6 @@ class WinCCManager : public Manager public: //! default constructor WinCCManager(); - //! run the manager, connect to the running wincc instance. void run(); //! exit the manager, disconnect from the running wincc instance. @@ -49,8 +49,15 @@ public: //! subscribe for changes on this list of datapoints. Whenever any of these datapoints changes value, the callback function from set_connect_datapoints_callback is called with the changed datapoint name and value. void connect_datapoints(const std::vector<std::string> &data_points); + //! subscribe for changes on this list of datapoints. Whenever any of these datapoints change all the values from these datapoints gets returned in one call. + void connect_datapoints_multiple(const std::vector<std::string> &data_points); + //! unsubscribe for changes on this list of datapoints. + void disconnect_datapoints(const std::vector<std::string> &data_points); + //! provide your callback function which is called whenever any of the connected datapoints is changed. + void set_connect_datapoints_callback(std::function<void(std::string name, std::string value)> callback_functor) {callback = callback_functor;}; //! provide your callback function which is called whenever any of the connected datapoints is changed. - void set_connect_datapoints_callback(std::function<void(std::string name, std::string value)> callback) {callback = callback;}; + void set_connect_datapoints_callback(std::function<void(std::map<std::string, std::string>)> callback_functor) {callback_multi = callback_functor;}; + //! set the datapoint with given name to the given value, returns true upon success. /*! set the datapoint with given name to the given value @@ -75,7 +82,10 @@ public: bool get_datapoint(const std::string &name, boost::python::list &value); //! get the datapoint with the given name and return it's std::vector<int> value in parameter value. returns true upon success. bool get_datapoint(const std::string &name, std::vector<int> &value); - + //! get the datapoint with a any type + bool get_datapoint_any(const std::string &datapoint_name, boost::any &value); + //! set the datapoint with a any type + bool set_datapoint_any(const std::string &datapoint_name, const boost::any &value); //! mark the datapoint with given name valid. returns true upon success. bool set_datapoint_valid(const std::string &name) { return set_datapoint_validity(name, true, nullptr); } //! mark the datapoint with given name invalid. returns true upon success. @@ -83,24 +93,32 @@ public: //! set the datapoint with given name valid/invalid. returns true upon success. bool set_datapoint_validity(const std::string &name, bool validity, const Variable *value=nullptr); + bool get_query(const std::string &query, std::vector<std::vector<std::string>> & result); //! handle signals to exit nicely virtual void signalHandler(int sig); + + void wait_for_event(long sec, long microSec); + private: void init(); friend class ConnectWaitForAnswer; void handle_hotlink(const std::string &name, const std::string &value); + void handle_hotlink(const std::map<std::string, std::string> &); + void handle_get(const std::string &name, Variable *&value); bool request_datapoint(const std::string &name); + bool request_query_result(const std::string &query, PVSSulong & identifier); template <typename Tval> bool _get_datapoint(const std::string &name, Tval &value); template <typename Tval> bool _get_datapoint(const std::string &name, Tval *value); + bool get_datapoint_variable(const std::string &name, Variable *&value); bool has_received_variable(const std::string &name); @@ -109,6 +127,7 @@ private: volatile static bool doExit; std::function<void(std::string name, std::string value)> callback; + std::function<void(std::map<std::string, std::string>)> callback_multi; std::mutex mtx; std::condition_variable cv; std::map<std::string, Variable *> values; diff --git a/LCS/WinCCWrapper/include/WinCCResources.h b/LCS/WinCCWrapper/include/WinCCResources.h index 552e9fcf771..b387368eb42 100644 --- a/LCS/WinCCWrapper/include/WinCCResources.h +++ b/LCS/WinCCWrapper/include/WinCCResources.h @@ -27,9 +27,12 @@ namespace LOFAR { class WinCCResources { public: - WinCCResources(const std::string &projec_name); + WinCCResources(const std::string &project_name); + + WinCCResources(const std::string &program_name, const std::string &project_name, const int num); private: - void init(const std::string &projec_name); + void init(const std::string &project_name); + void init(const std::string &program_name, const std::string &project_name, const int num); }; } // namespace WINCCWRAPPER diff --git a/LCS/WinCCWrapper/include/WinCCWrapper.h b/LCS/WinCCWrapper/include/WinCCWrapper.h index 26fa060d606..4e57aa17166 100644 --- a/LCS/WinCCWrapper/include/WinCCWrapper.h +++ b/LCS/WinCCWrapper/include/WinCCWrapper.h @@ -21,6 +21,7 @@ #define WINCCSERVICES_WINCC_WRAPPER_H #include <string> +#include <boost/variant.hpp> #include <vector> #include <ctime> #include <functional> @@ -29,6 +30,7 @@ #include "WinCCManager.h" #include <boost/python.hpp> +#include <boost/any.hpp> namespace LOFAR { namespace WINCCWRAPPER { @@ -40,6 +42,9 @@ class WinCCWrapper public: //! default constructor WinCCWrapper(const std::string &project_name); + + //! default constructor + WinCCWrapper(const std::string &program_name, const std::string &project_name, const int num); //! disconnect from the running wincc instance. void exit(); //! connect to the running wincc instance. @@ -47,14 +52,21 @@ public: //! subscribe for changes on this list of datapoints. Whenever any of these datapoints changes value, the callback function from set_connect_datapoints_callback is called with the changed datapoint name and value. void connect_datapoints(const std::vector<std::string> &data_points); + //! subscribe for changes on this list of datapoints. Whenever any of these datapoints changes value, the callback function from set_connect_datapoints_callback is called with all the specified values. + void connect_datapoints_multi(const std::vector<std::string> &data_points); + + //! unsubscribe for changes on the given list of datapoints. + void disconnect_datapoints(const std::vector<std::string> &data_points); //! provide your callback function which is called whenever any of the connected datapoints is changed. void set_connect_datapoints_callback(std::function<void(std::string name, std::string value)> callback); + //! provide your callback function which is called whenever any of the connected datapoints is changed. It returns all the specified datapoints even if only one changes. + void set_connect_datapoints_callback(std::function<void(std::map<std::string, std::string>)> callback); //! set the datapoint with given name to the given int value, mark it valid/invalid, returns true upon success. bool set_datapoint(const std::string &name, int value, bool valid=true); //! set the datapoint with given name to the given boost::python::list value, mark it valid/invalid, returns true upon success. bool set_datapoint(const std::string &name, boost::python::list &value, bool valid=true); - //! set the datapoint with given name to the given boost::python::tuple value, mark it valid/invalid, returns true upon success. + //! set the datapoint with given name to the given boost::python::tuple value, mark it valid/invalid, returns true upon success. bool set_datapoint(const std::string &name, boost::python::tuple &value, bool valid=true); //! set the datapoint with given name to the given std::vector<int> value, mark it valid/invalid, returns true upon success. bool set_datapoint(const std::string &name, std::vector<int> &value, bool valid=true); @@ -70,11 +82,14 @@ public: //! set the datapoint with given name to the given time_t value, mark it valid/invalid, returns true upon success. bool set_datapoint_time(const std::string &name, time_t value, bool valid=true); + bool set_datapoint_any(const std::string &name, boost::any value); //! mark the datapoint with given name valid. bool set_datapoint_valid(const std::string &name); //! mark the datapoint with given name invalid. bool set_datapoint_invalid(const std::string &name); + //! provide the boost::any for the given datapoint name + boost::any get_datapoint_any (const std::string &name); //! get the datapoint with the given name and return it as an int value if possible, otherwise an exception is raised. int get_datapoint_int(const std::string &name); //! get the datapoint with the given name and return it as a long value if possible, otherwise an exception is raised. @@ -88,11 +103,17 @@ public: //! get the datapoint with the given name and return it as a time_t value if possible, otherwise an exception is raised. time_t get_datapoint_time(const std::string &name); //! get the datapoint with the given name and return it as a boost::python::list value if possible, otherwise an exception is raised. - boost::python::list get_datapoint_list(const std::string &name); + boost::python::list get_datapoint_list(const std::string &name); + //! get the datapoint last set time + time_t get_datapoint_set_time(const std::string &name); //! get the datapoint with the given name and return it as a std::vector<int> value if possible, otherwise an exception is raised. //! this method is used in the WinCCGet test std::vector<int> get_datapoint_vector(const std::string &name); + //! get the datapoint with the given name and return it as a std::vector<std::string> value. + std::vector<std::string> get_datapoint_vector_string(const std::string &name); + std::string get_formatted_datapoint(const std::string &name); + void wait_for_event(long sec, long microSec); private: // get_datapoint template <typename T> diff --git a/LCS/WinCCWrapper/include/exceptions.h b/LCS/WinCCWrapper/include/exceptions.h new file mode 100644 index 00000000000..998ab30809e --- /dev/null +++ b/LCS/WinCCWrapper/include/exceptions.h @@ -0,0 +1,23 @@ +#ifndef WINCC_EXCEPTIONS +#define WINCC_EXCEPTIONS + +namespace LOFAR{ +namespace WINCCWRAPPER{ + +class DatapointNameNotFound : public std::exception +{ +private: + std::string message; +public: + DatapointNameNotFound(const std::string & datapointName): + message{"Datapoint " + datapointName + " not found"}{} + const char * what () const throw () + { + return message.c_str(); + } +}; + +} +} + +#endif diff --git a/LCS/WinCCWrapper/src/ConnectWaitForAnswer.cc b/LCS/WinCCWrapper/src/ConnectWaitForAnswer.cc index 767ea576d76..b35c3cfe039 100644 --- a/LCS/WinCCWrapper/src/ConnectWaitForAnswer.cc +++ b/LCS/WinCCWrapper/src/ConnectWaitForAnswer.cc @@ -25,13 +25,22 @@ namespace LOFAR { using namespace std; +ConnectWaitForAnswer::ConnectWaitForAnswer(const bool handle_multi): HotLinkWaitForAnswer{}, multi{handle_multi}{ + +} + +ConnectWaitForAnswer::ConnectWaitForAnswer(): HotLinkWaitForAnswer{}{ + +} + void ConnectWaitForAnswer::hotLinkCallBack(DpMsgAnswer &answer) { + const std::string answer_id{std::to_string(answer.getAnswerId())}; for (AnswerGroup *grpPtr = answer.getFirstGroup(); grpPtr; grpPtr = answer.getNextGroup()) { if (grpPtr->getError()) { - cout << "Error!" << endl; + cout <<grpPtr->getError()->toString()<< endl; } else { @@ -42,6 +51,8 @@ void ConnectWaitForAnswer::hotLinkCallBack(DpMsgAnswer &answer) if(varPtr) { string name = get_datapoint_name(item); + if(name.compare("") == 0) name += answer_id; + Variable *value = varPtr->clone(); //WinCCManager should delete cloned pointer ((WinCCManager *) Manager::getManPtr())->handle_get(name, value); @@ -52,6 +63,14 @@ void ConnectWaitForAnswer::hotLinkCallBack(DpMsgAnswer &answer) } void ConnectWaitForAnswer::hotLinkCallBack(DpHLGroup &group) +{ + if(multi){ + handle_multi(group); + }else{ + handle_one_by_one(group); + } +} +void ConnectWaitForAnswer::handle_one_by_one(DpHLGroup &group) { for (DpVCItem *item = group.getFirstItem(); item; item = group.getNextItem()) { @@ -59,6 +78,25 @@ void ConnectWaitForAnswer::hotLinkCallBack(DpHLGroup &group) } } +void ConnectWaitForAnswer::handle_multi(DpHLGroup &group) +{ + + std::map<std::string, std::string> values; + for (DpVCItem *item = group.getFirstItem(); item; item = group.getNextItem()) + { + Variable *varPtr = item->getValuePtr(); + + if (varPtr){ + const string name = get_datapoint_name(item); + const string value = varPtr->formatValue().c_str(); + + values[name] = value; + } + } + ((WinCCManager *) Manager::getManPtr())->handle_hotlink(values); +} + + void ConnectWaitForAnswer::handle_group_item(const DpVCItem* const item) { Variable *varPtr = item->getValuePtr(); @@ -66,6 +104,7 @@ void ConnectWaitForAnswer::handle_group_item(const DpVCItem* const item) if (varPtr) { string name = get_datapoint_name(item); + string value = varPtr->formatValue().c_str(); ((WinCCManager *) Manager::getManPtr())->handle_hotlink(name, value); diff --git a/LCS/WinCCWrapper/src/ConnectWaitForAnswer.h b/LCS/WinCCWrapper/src/ConnectWaitForAnswer.h index 28dd3f0138a..dba485153ca 100644 --- a/LCS/WinCCWrapper/src/ConnectWaitForAnswer.h +++ b/LCS/WinCCWrapper/src/ConnectWaitForAnswer.h @@ -32,12 +32,18 @@ namespace LOFAR { class ConnectWaitForAnswer : public HotLinkWaitForAnswer { public: + ConnectWaitForAnswer(); + ConnectWaitForAnswer(const bool handle_multi); using HotLinkWaitForAnswer::hotLinkCallBack; virtual void hotLinkCallBack(DpMsgAnswer &answer); virtual void hotLinkCallBack(DpHLGroup &group); private: + bool multi; + void handle_one_by_one(DpHLGroup &group); + void handle_multi(DpHLGroup &group); void handle_group_item(const DpVCItem* const item); std::string get_datapoint_name(const DpVCItem* const item); + }; } // namespace WINCCWRAPPER diff --git a/LCS/WinCCWrapper/src/WinCCManager.cc b/LCS/WinCCWrapper/src/WinCCManager.cc index 6aa6c34b42e..68559dc23a6 100644 --- a/LCS/WinCCWrapper/src/WinCCManager.cc +++ b/LCS/WinCCWrapper/src/WinCCManager.cc @@ -34,9 +34,12 @@ #include <TextVar.hxx> #include <DynVar.hxx> #include <DynPtrArray.hxx> +#include <Resources.hxx> #include <cassert> #include <vector> #include <boost/python.hpp> +#include <boost/any.hpp> +#include <exceptions.h> namespace LOFAR { namespace WINCCWRAPPER { @@ -100,12 +103,77 @@ void WinCCManager::connect_datapoints(const std::vector<std::string> &dataPoints } } +void WinCCManager::connect_datapoints_multiple(const std::vector<std::string> &dataPoints) +{ + + DpIdentList dpList; + for(vector<string>::const_iterator it = dataPoints.begin(); it != dataPoints.end(); it++) + { + DpIdentifier dpIdConnect; + + if (Manager::getId(it->c_str(), dpIdConnect) == PVSS_FALSE) + { + // This name was unknown + ErrHdl::error(ErrClass::PRIO_SEVERE, + ErrClass::ERR_PARAM, + ErrClass::UNEXPECTEDSTATE, + "PublishManager", + "connect_datapoints", + CharString("Datapoint ") + CharString(it->c_str()) + + CharString(" missing")); + } + else + { + dpList.append(dpIdConnect); + + } + } + // We give the dpConnect a nice naked pointer because it will delete it when the manager stops. + HotLinkWaitForAnswer* wait = new ConnectWaitForAnswer(true); + Manager::dpConnect(dpList, wait); +} + + +void WinCCManager::disconnect_datapoints(const std::vector<std::string> &dataPoints) +{ + for(vector<string>::const_iterator it = dataPoints.begin(); it != dataPoints.end(); it++) + { + DpIdentifier dpIdConnect; + + if (Manager::getId(it->c_str(), dpIdConnect) == PVSS_FALSE) + { + // This name was unknown + ErrHdl::error(ErrClass::PRIO_SEVERE, + ErrClass::ERR_PARAM, + ErrClass::UNEXPECTEDSTATE, + "PublishManager", + "connect_datapoints", + CharString("Datapoint ") + CharString(it->c_str()) + + CharString(" missing")); + } + else + { + // We give the dpConnect a nice naked pointer because it will delete it when the manager stops. + HotLinkWaitForAnswer* wait = new ConnectWaitForAnswer(); + Manager::dpDisconnect(dpIdConnect, wait); + } + } +} + bool WinCCManager::set_datapoint(const std::string &name, const Variable &value, bool valid) { //reuse the set_datapoint_validity, and explicitly set the value to the given value return set_datapoint_validity(name, valid, &value); } + +// request the query (async). is called by _get_query which makes it blocking (synchronous) by waiting for the answer. +bool WinCCManager::request_query_result(const std::string &query, PVSSulong & identifier) +{ + HotLinkWaitForAnswer* wait = new ConnectWaitForAnswer(); + return (PVSS_TRUE == Manager::dpQuery(query.c_str(), identifier, wait)); +} + // request the datapoint (async). is called by _get_datapoint which makes it blocking (synchronous) by waiting for the answer. bool WinCCManager::request_datapoint(const std::string &name) { @@ -333,6 +401,72 @@ Variable::ConvertResult convert(Variable *var, struct tm &value, Variable *&conv return cr; } +boost::any convert_any(Variable * variable, boost::any & value){ + Variable * converted_variable{nullptr}; + switch(variable->isA()){ + case VariableType::INTEGER_VAR:{ + int casted_variable; + convert(variable, casted_variable, converted_variable); + value = casted_variable; + + break; + } + case VariableType::FLOAT_VAR: { + float casted_variable; + convert(variable, casted_variable, converted_variable); + value = casted_variable; + + break; + } + case VariableType::TEXT_VAR: { + std::string casted_variable; + convert(variable, casted_variable, converted_variable); + value = casted_variable; + + break; + } + case VariableType::LONG_VAR:{ + // Find the wincc guy who though this out + long casted_variable; + convert(variable, casted_variable, converted_variable); + value = casted_variable; + + break; + } + case VariableType::BIT_VAR: { + bool casted_variable; + convert(variable, casted_variable, converted_variable); + value = casted_variable; + + break; + } + case VariableType::TIME_VAR: { + time_t casted_variable; + convert(variable, casted_variable, converted_variable); + value = casted_variable; + + break; + } + case VariableType::ANYTYPE_VAR: { + const AnyTypeVar * converted_var{static_cast<AnyTypeVar*>(variable)}; + convert_any(converted_var->getVar(), value); + break; + } + case VariableType::DPIDENTIFIER_VAR: { + value = std::string{variable->formatValue().c_str()}; + break; + } + default: + CharString value_str = variable->formatValue(); + std::cerr<<value_str; + std::cerr<<"Datapoint type still not supported: "<<std::hex<<variable->isA()<<std::endl; + value = std::string{value_str.c_str()}; + } + + if(converted_variable != nullptr) delete converted_variable; + return true; +} + //internal generic method to get the typed (Tval) value of a datapoint //used by the public strictly typed methods template <typename Tval> @@ -355,6 +489,42 @@ bool WinCCManager::_get_datapoint(const std::string &name, Tval &value) return false; } +bool convert_to_vector2(Variable * variable, std::vector<std::vector<std::string>> & result){ + if(variable->isA(VariableType::DYN_VAR)){ + const DynVar * rows{static_cast<DynVar *>(variable)}; + for(DynPtrArrayIndex i=0; i < rows->getNumberOfItems(); i++){ + const DynVar * row{static_cast<DynVar *>(rows->getAt(i))}; + std::vector<std::string> * current_row{new std::vector<std::string>{}}; + Variable * column{static_cast<DynVar *>(row->getNext())}; + while(column){ + current_row->push_back(column->formatValue().c_str()); + column = static_cast<DynVar *>(row->getNext()); + } + result.push_back(*current_row); + row = static_cast<DynVar *>(rows->getNext()); + } + return true; + } + return false; +} + +//internal generic method to query a set of WinCC data points. Since the type is not previously know +bool WinCCManager::get_query(const std::string &query, std::vector<std::vector<std::string>> & result){ + Variable * variable_value = nullptr; + PVSSulong identifier; + if(request_query_result(query, identifier)) + { + std::string identifier_str{std::to_string(identifier)}; + if(wait_for_received_variable(identifier_str, 1000)) { + if(get_received_variable(identifier_str, variable_value)) { + convert_to_vector2(variable_value, result); + return true; + } + } + } + + return false; +} //below, a few strictly type methods for get_datapoint are defined //they just call the templated _get_datapoint, so why not just use the one and only templated method? @@ -401,10 +571,11 @@ bool WinCCManager::get_datapoint(const std::string &name, std::vector<int> &valu return _get_datapoint(name, value); } + bool WinCCManager::set_datapoint_validity(const std::string &name, bool validity, const Variable *value) { DpIdentifier dpId; - + if (Manager::getId(name.c_str(), dpId) == PVSS_FALSE) { // This name was unknown. @@ -472,6 +643,10 @@ void WinCCManager::run() } } +void WinCCManager::wait_for_event(long sec, long microSec){ + dispatch(sec, microSec); +} + void WinCCManager::exit() { Manager::exit(0); @@ -485,6 +660,14 @@ void WinCCManager::handle_hotlink(const std::string &name, const std::string &va } } +void WinCCManager::handle_hotlink(const std::map<std::string, std::string> & values) +{ + if(callback_multi) + { + callback_multi(values); + } +} + void WinCCManager::signalHandler(int sig) { if ((sig == SIGINT) || (sig == SIGTERM)) @@ -497,6 +680,81 @@ void WinCCManager::signalHandler(int sig) } } +bool WinCCManager::set_datapoint_any(const std::string &datapoint_name, const boost::any &value){ + Variable * variable{nullptr}; + + if(get_datapoint_variable(datapoint_name, variable)){ + switch(variable->isA()){ + case VariableType::INTEGER_VAR: { + IntegerVar converted_value{boost::any_cast<int>(value)}; + set_datapoint(datapoint_name, converted_value); + break; + } + case VariableType::FLOAT_VAR: { + FloatVar converted_value{boost::any_cast<float>(value)}; + set_datapoint(datapoint_name, converted_value); + break; + } + case VariableType::TEXT_VAR: { + TextVar converted_value{CharString(boost::any_cast<std::string>(value).c_str())}; + set_datapoint(datapoint_name, converted_value); + break; + } + case VariableType::LONG_VAR:{ + LongVar converted_value{boost::any_cast<long>(value)}; + set_datapoint(datapoint_name, converted_value); + + break; + } + case VariableType::BIT_VAR: { + BitVar converted_value{boost::any_cast<bool>(value)}; + set_datapoint(datapoint_name, converted_value); + + break; + } + case VariableType::TIME_VAR: { + TimeVar converted_value(0,0); + converted_value.setSeconds(boost::any_cast<time_t>(value)); + set_datapoint(datapoint_name, converted_value); + + break; + } + default: + std::cerr<<"Datapoint type still not supported: "<<std::hex<<variable->isA()<<std::endl; + return false; + } + }else{ + return false; + } + + return true; +} + +bool WinCCManager::get_datapoint_any(const std::string &datapoint_name, boost::any & value){ + // Yeah I know... WinCC API is getting a &* to allocate the memory space... + Variable * variable{nullptr}; + bool returnValue{true}; + + if(!get_datapoint_variable(datapoint_name, variable)){ + // This name was unknown. + ErrHdl::error(ErrClass::PRIO_SEVERE, + ErrClass::ERR_PARAM, + ErrClass::UNEXPECTEDSTATE, + Resources::getProgName(), + "get_datapoint", + CharString("Datapoint ") + CharString(datapoint_name.c_str()) + + CharString(" missing")); + throw DatapointNameNotFound{datapoint_name}; + } + + convert_any(variable, value); + + if(variable != nullptr) delete variable; + + return returnValue; + +} + } // namespace WINCCWRAPPER } // namespace LOFAR diff --git a/LCS/WinCCWrapper/src/WinCCResources.cc b/LCS/WinCCWrapper/src/WinCCResources.cc index 5eb7dbf8d1e..a35accb448a 100644 --- a/LCS/WinCCWrapper/src/WinCCResources.cc +++ b/LCS/WinCCWrapper/src/WinCCResources.cc @@ -31,11 +31,62 @@ WinCCResources::WinCCResources(const std::string &project_name) init(project_name); } -void WinCCResources::init(const std::string & /*project_name*/) +WinCCResources::WinCCResources(const::std::string &program_name, const std::string &project_name, const int num) { - // TODO set specific project instead of current project. - char* ownArgv[] = {(char *)"WinCCWrapper", (char *)"-currentproj", (char *)"-log", (char *)"+stderr"}; - int ownArgc = sizeof ownArgv / sizeof ownArgv[0]; + init(program_name, project_name, num); +} + + +void WinCCResources::init(const::std::string &program_name, const std::string & project_name, const int num) +{ + std::vector<std::string> args{program_name}; + + if(project_name.compare("-currentproj") == 0){ + args.push_back("-currentproj"); + }else{ + args.push_back("-proj"); + args.push_back(project_name); + } + args.push_back("-log"); + args.push_back("+stderr"); + + args.push_back("-num"); + args.push_back(std::to_string(num)); + + + int ownArgc = args.size(); + + char * ownArgv[ownArgc]; + + for(int i=0; i<ownArgc; i++){ + ownArgv[i] = const_cast<char *>(args.at(i).c_str()); + } + + Resources::init(ownArgc, ownArgv); +} + + + +void WinCCResources::init(const std::string & project_name) +{ + std::vector<std::string> args{"WinCCWrapper"}; + + if(project_name.compare("-currentproj") == 0){ + args.push_back("-currentproj"); + }else{ + args.push_back("-proj"); + args.push_back(project_name); + } + args.push_back("-log"); + args.push_back("+stderr"); + + int ownArgc = args.size(); + + char * ownArgv[ownArgc]; + + for(int i=0; i<ownArgc; i++){ + ownArgv[i] = const_cast<char *>(args.at(i).c_str()); + } Resources::init(ownArgc, ownArgv); } diff --git a/LCS/WinCCWrapper/src/WinCCWrapper.cc b/LCS/WinCCWrapper/src/WinCCWrapper.cc index ab6191dc8d6..d8e129f59b8 100644 --- a/LCS/WinCCWrapper/src/WinCCWrapper.cc +++ b/LCS/WinCCWrapper/src/WinCCWrapper.cc @@ -43,6 +43,9 @@ using namespace std; //! Each datapoint has a human readable name in the wincc database, but the actual value is stored in a sub-item. Append that to each set/get datapoint name. static const string DP_SUFFIX = ":_original.._value"; +//! This property refers to the last set time +static const string DP_SUFFIX_STIME = ":_original.._stime"; + WinCCWrapper::WinCCWrapper(const std::string &project_name) : resources(project_name) @@ -50,6 +53,12 @@ WinCCWrapper::WinCCWrapper(const std::string &project_name) : manager = new WinCCManager(); } +WinCCWrapper::WinCCWrapper(const std::string &program_name, const std::string &project_name, const int num) : + resources(program_name, project_name, num) +{ + manager = new WinCCManager(); +} + void WinCCWrapper::run() { manager->run(); @@ -65,11 +74,31 @@ void WinCCWrapper::connect_datapoints(const std::vector<std::string> &data_point manager->connect_datapoints(data_points); } +void WinCCWrapper::connect_datapoints_multi(const std::vector<std::string> &data_points) +{ + manager->connect_datapoints_multiple(data_points); +} + +void WinCCWrapper::disconnect_datapoints(const std::vector<std::string> &data_points) +{ + manager->connect_datapoints(data_points); +} + + +void WinCCWrapper::wait_for_event(long sec, long microSec){ + manager->wait_for_event(sec, microSec); +} + void WinCCWrapper::set_connect_datapoints_callback(std::function<void(std::string name, std::string value)> callback) { manager->set_connect_datapoints_callback(callback); } +void WinCCWrapper::set_connect_datapoints_callback(std::function<void(std::map<std::string, std::string>)> callback) +{ + manager->set_connect_datapoints_callback(callback); +} + // set_datapoint bool WinCCWrapper::set_datapoint(const std::string &name, int value, bool valid) { @@ -99,7 +128,7 @@ bool WinCCWrapper::set_datapoint(const std::string &name, boost::python::tuple & bool WinCCWrapper::set_datapoint(const std::string &name, std::vector<int> &value, bool valid) { DynVar variable(VariableType::INTEGER_VAR); - + for(auto iter = value.cbegin(); iter != value.cend(); iter++) { IntegerVar elem{*iter}; variable.append(elem);} @@ -142,6 +171,12 @@ bool WinCCWrapper::set_datapoint_time(const std::string &name, time_t value, boo return manager->set_datapoint(name + DP_SUFFIX, variable, valid); } +boost::any WinCCWrapper::get_datapoint_any (const std::string &name){ + boost::any datapoint_value; + manager->get_datapoint_any(name + DP_SUFFIX, datapoint_value); + return datapoint_value; +} + // get_datapoint template <typename T> bool WinCCWrapper::get_datapoint(const std::string &name, T &value) @@ -213,6 +248,21 @@ time_t WinCCWrapper::get_datapoint_time(const std::string &name) throw std::runtime_error("Could not get datapoint"); } +std::string WinCCWrapper::get_formatted_datapoint(const std::string &name){ + std::string value; + if(manager->get_datapoint(name, value)){ + return value; + } + throw std::runtime_error("Could not get datapoint"); +} + +time_t WinCCWrapper::get_datapoint_set_time(const std::string &name){ + struct tm value; + if(manager->get_datapoint(name + DP_SUFFIX_STIME, value)) + return mktime(&value); + throw std::runtime_error("Could not get datapoint"); +} + bool WinCCWrapper::set_datapoint_valid(const std::string &name) { return manager->set_datapoint_valid(name + DP_SUFFIX); @@ -223,5 +273,8 @@ bool WinCCWrapper::set_datapoint_invalid(const std::string &name) return manager->set_datapoint_invalid(name + DP_SUFFIX); } +bool WinCCWrapper::set_datapoint_any(const std::string &name, boost::any value){ + return manager->set_datapoint_any(name + DP_SUFFIX, value); +} } // namespace WINCCWRAPPER } // namespace LOFAR diff --git a/LCS/WinCCWrapper/src/exceptions.h b/LCS/WinCCWrapper/src/exceptions.h new file mode 100644 index 00000000000..cf0da769433 --- /dev/null +++ b/LCS/WinCCWrapper/src/exceptions.h @@ -0,0 +1,12 @@ +class DatapointNameNotFound : public std::exception +{ +private: + std::string message; +public: + DatapointNameNotFound(const std::string & datapointName): + message{"Datapoint " + datapointName + " not found"}{} + const char * what () const throw () + { + return message.c_str(); + } +} diff --git a/LCS/WinCCWrapper/test/CMakeLists.txt b/LCS/WinCCWrapper/test/CMakeLists.txt index 93e3e67b7f1..08c5036d1ac 100644 --- a/LCS/WinCCWrapper/test/CMakeLists.txt +++ b/LCS/WinCCWrapper/test/CMakeLists.txt @@ -8,6 +8,15 @@ IF(BUILD_TESTING) lofar_add_bin_program(WinCCSet WinCCSet.cc) lofar_add_bin_program(WinCCGet WinCCGet.cc) + lofar_add_bin_program(WinCCQuery WinCCQuery.cc) + find_package(Boost COMPONENTS program_options system REQUIRED) + + target_link_libraries(WinCCQuery + wincc_wrapper + ${Boost_LIBRARIES} + + ) + ENDIF(WINCC_FOUND) ENDIF(BUILD_TESTING) diff --git a/LCS/WinCCWrapper/test/WinCCGet.cc b/LCS/WinCCWrapper/test/WinCCGet.cc index 5958d82e915..aa10b76c422 100644 --- a/LCS/WinCCWrapper/test/WinCCGet.cc +++ b/LCS/WinCCWrapper/test/WinCCGet.cc @@ -3,28 +3,28 @@ #include <WinCCWrapper.h> #include <vector> #include <iostream> +#include <Resources.hxx> using namespace LOFAR::WINCCWRAPPER; using namespace std; void get_help(){ cout << "Usage:" << endl; - cout << "WinCCGet \"datapoint_name\" datapoint_type" << endl; + cout << "WinCCGet \"datapoint_name\" datapoint_type project_name" << endl; cout << "Accepted datapoint types:" << endl; cout << " int, float, string, list (for int lists)" << endl; } int main(int argc, char * argv[]) { - bool asking_for_help = ((argc == 2) && (string(argv[1]) == "--help" || string(argv[1]) == "--h")); - bool invalid_args = (argc != 3); + bool asking_for_help = ((argc < 4) && (string(argv[1]) == "--help" || string(argv[1]) == "--h")); + bool invalid_args = (argc != 4); if (asking_for_help || invalid_args){ get_help(); return 0; } - - WinCCWrapper wrapper{""}; + WinCCWrapper wrapper{std::string(argv[3])}; string dpname{argv[1]}; if (string(argv[2]) == "int") { diff --git a/LCS/WinCCWrapper/test/WinCCQuery.cc b/LCS/WinCCWrapper/test/WinCCQuery.cc new file mode 100644 index 00000000000..108c626d304 --- /dev/null +++ b/LCS/WinCCWrapper/test/WinCCQuery.cc @@ -0,0 +1,68 @@ + + + +#include <cstdlib> +#include <string> +#include <WinCCManager.h> +#include <WinCCResources.h> + +#include <vector> +#include <iostream> +#include <Resources.hxx> + +#include <boost/program_options.hpp> +#include <boost/any.hpp> + + + +using namespace LOFAR::WINCCWRAPPER; +using namespace std; +namespace po = boost::program_options; + +void set_options(po::options_description & description){ + description.add_options() + ("help", "produce help message") + ("sql", po::value<std::string>(), "SQL_query") + ("project", po::value<std::string>(), "WinCC project"); + +} + +int main(int argc, char * argv[]) +{ + // Declare the supported options. + po::options_description desc("Query the WinCCDatabase"); + set_options(desc); + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + + if (vm.count("help")) { + cout << desc << "\n"; + return 1; + } + + if (vm.count("sql") == 1 && vm.count("project") == 1) { + + const string sql{vm["sql"].as<string>()}; + + WinCCResources resource{"WinCCQuery", vm["project"].as<string>(), 0}; + WinCCManager manager; + cout << "The SQL is: [" << sql << "].\n"; + + std::vector<std::vector<std::string>> queryResult; + manager.get_query(sql, queryResult); + std::cout<< "RESULTS ----"<<"\n\n"; + std::cout<< "datapoint" << "\t"; + for(auto & row : queryResult){ + for(std::string & column : row){ + std::cout<<column<<"\t"; + } + std::cout<<"\n"; + } + + } else { + cout << desc; + } +} diff --git a/MAC/CMakeLists.txt b/MAC/CMakeLists.txt index 6ad267da65b..c1c9d2dcfb9 100644 --- a/MAC/CMakeLists.txt +++ b/MAC/CMakeLists.txt @@ -11,6 +11,10 @@ lofar_add_package(WinCC_Datapoints Deployment/data/PVSS) lofar_add_package(OTDB_Comps Deployment/data/OTDB) lofar_add_package(StaticMetaData Deployment/data/StaticMetaData) lofar_add_package(WinCCPublisher WinCCPublisher) +lofar_add_package(WinCCREST) +lofar_add_package(WinCCDBBridge) + + lofar_add_package(TaskManagement Services/TaskManagement) lofar_add_package(TBB) diff --git a/MAC/WinCCDBBridge/CMakeLists.txt b/MAC/WinCCDBBridge/CMakeLists.txt new file mode 100644 index 00000000000..15920ec401f --- /dev/null +++ b/MAC/WinCCDBBridge/CMakeLists.txt @@ -0,0 +1,20 @@ +lofar_package(WinCCDBBridge 1.0 DEPENDS WinCCWrapper) + +include(LofarFindPackage) +lofar_find_package(WINCC REQUIRED) + +find_package(Qt5 COMPONENTS Core Network Sql Xml REQUIRED) + +lofar_find_package(UnitTest++) + +# Auto generate moc files +set(CMAKE_AUTOMOC ON) +# As moc files are generated in the binary dir, tell CMake +# to always look for includes there: +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +include_directories(${WinCCWrapper_SOURCE_DIR}/include/) + +add_subdirectory(etc) +add_subdirectory(src) +add_subdirectory(test) diff --git a/MAC/WinCCDBBridge/DB.ini b/MAC/WinCCDBBridge/DB.ini new file mode 100644 index 00000000000..6a9648414d6 --- /dev/null +++ b/MAC/WinCCDBBridge/DB.ini @@ -0,0 +1,7 @@ +[Database] +type=QSQLITE +name=datapoints.db +address=localhost +port= +user= +pass= diff --git a/MAC/WinCCDBBridge/Docker/Dockerfile b/MAC/WinCCDBBridge/Docker/Dockerfile new file mode 100644 index 00000000000..c6aa6bf4b3f --- /dev/null +++ b/MAC/WinCCDBBridge/Docker/Dockerfile @@ -0,0 +1,55 @@ +#DOCKER FILE TO BUILD THE WinCCDBBridge service that connects to WinCC to copy the datapoint value +#into any external sql database (using the QT libraries) +# THE WINCC RPM HAVE TO BE AVAILABLE IN THE DIRECTORY IN WHICH THE DOCKER IMAGE IS BUILT + + +# THE BASE IMAGE STAGE +# it contains all the dependecies to run the application with WinCC +FROM centos:latest as base +WORKDIR /root/wincc_rpm +COPY *3.14*.rpm ./ +RUN yum -y update +RUN yum -y install libicu +RUN yum -y install *3.14*.rpm +RUN yum -y install python boost qt5-qtbase +ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/WinCC_OA/3.14/bin/:/opt/WinCC_OA/3.14/api/lib.linux/ +RUN ldconfig +ENV PATH=$PATH:/opt/WinCC_OA/3.14/bin/ + +# THE BUILDER IMAGE STAGE +# In this stage all the development tools are installed +# the source code pulled from svn and the software built +# as it is necessary +FROM base as builder +RUN yum -y groupinstall "Development Tools" +RUN yum -y install cmake python-devel boost-devel qt5-qtbase-devel +RUN yum -y install svn +WORKDIR /root +ARG BRANCH=MonitoringMaintenance-OSB-12 +RUN svn co https://svn.astron.nl/LOFAR/branches/$BRANCH +RUN cd $BRANCH && mkdir -p build/gnucxx11_debug/ +WORKDIR /root/$BRANCH/build/gnucxx11_debug/ +ARG INSTALLDIR=/opt/lofar/ +RUN cmake ../../ -DBUILD_PACKAGES=WinCCDBBridge -DUSE_LOG4CPLUS=OFF -DWINCC_ROOT_DIR=/opt/WinCC_OA/3.14 -DCMAKE_INSTALL_PREFIX=$INSTALLDIR +ARG BUILD_PROCESSES=4 +RUN make -j $BUILD_PROCESSES && make install + +# DEPLOY IMAGE STAGE +# The image to deploy is built at this step +# all the develping dependecies are not useful anymore +# hence a image pruned with unnecessary dependecies is built +FROM base as deploy +COPY --from=builder /opt/lofar /opt/lofar +WORKDIR /opt/lofar +ARG WINCCPROJECT_DIR=$INSTALLDIR/var/wincc/LOFAR/ +ENV WINCCPROJ=LOFAR +ADD LOFAR.tar.gz $WINCCPROJECT_DIR +WORKDIR $WINCCPROJECT_DIR +RUN PVSSutil.sh RegisterProject 1.0 $WINCCPROJECT_DIR /opt/WinCC_OA/3.14/ +ADD run.sh /opt/lofar/bin/run.sh +ENV PATH=$PATH:/opt/lofar/bin +ENV QT_LOGGING_RULES="*.debug=true" +ENV QT_MESSAGE_PATTERN="%{time} - %{appname} %{pid}/%{threadid}: %{message}" +ENTRYPOINT ["/opt/lofar/bin/run.sh"] +CMD ["-h"] + diff --git a/MAC/WinCCDBBridge/Docker/LOFAR.tar.gz b/MAC/WinCCDBBridge/Docker/LOFAR.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..95d3deab96e174eeb9abbd88e72603722bf14fa5 GIT binary patch literal 568 zcmV-80>}LyiwFQP1aw>g1MQVfkJB&^hI{s}u+j?`+I)9=*uyF(1l6vn*gYUrx#=`D zO6<sXN?GlXXWX<<2~~T5f?(%SB##qkW<2pbDYaV5YTO!K*};VfkvvcR7)`S9bleli z`5;QtB%S1G7)OIJO0p~;@bFRzozht+4CjNU5vr6*o?G|a^M8~r7%R2n5o4lr8y*CB zvEfW8Q32NFz!RTincUuh@~y0EwBCcSog4NhxS3RF-nI@MTJ}&BZ{D>H`u^?%OV|nj z=AI-xT4?_;BmTtxc2!;uWv~B!xKNDe^gjv1z5X+71-R1dKhF}<|DWJv1qzI)Ke2Xe z?V=Uf1Fm^6)~y>qNL3Vz+naGRiqe4LKjQP^*e`HRf43H@vVIC`@nJraR$pCZ(<opo z;e@~6lpTKW5uPw-bUmu|T9|;r7L+^xxY=Q}-B-UrX$1i*ovCMHcL!=E`+zYX7siIZ z!`x}(eNCp*DI3ngVWZi}rfWEQRZ!v_ti^t2xs{K=o#vgYWz$FpEB;CWtZ-aw!;ibe zt;JlcFT$)WUqUH53tX?c+W_Bm6>!n$4)e!bg<gu<aZI<wAQ%F@AHzD$Fq5L6&H5bX zK7(r>vO`XVZd!xpp{S3wT)tjD$~F!d2E7+`2REkN$gO`XVkg^8{dRx#`ycfGJC4#> z_8OmLXZ0Wcr2jb0@&BJ)G^+zIT>k_?5ClOG1VIo4K@bE%5ClOG1VIqQ|G_tGt-fji GC;$M+V-q9* literal 0 HcmV?d00001 diff --git a/MAC/WinCCDBBridge/Docker/run.sh b/MAC/WinCCDBBridge/Docker/run.sh new file mode 100755 index 00000000000..9d2c32fccc5 --- /dev/null +++ b/MAC/WinCCDBBridge/Docker/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e + +exec /opt/lofar/bin/winccbridge "$@" ${WINCCPROJ} diff --git a/MAC/WinCCDBBridge/etc/CMakeLists.txt b/MAC/WinCCDBBridge/etc/CMakeLists.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/MAC/WinCCDBBridge/etc/DB.ini b/MAC/WinCCDBBridge/etc/DB.ini new file mode 100644 index 00000000000..6a9648414d6 --- /dev/null +++ b/MAC/WinCCDBBridge/etc/DB.ini @@ -0,0 +1,7 @@ +[Database] +type=QSQLITE +name=datapoints.db +address=localhost +port= +user= +pass= diff --git a/MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xml b/MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xml new file mode 100644 index 00000000000..37cecbbff1b --- /dev/null +++ b/MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xml @@ -0,0 +1,34 @@ +<Table name="antenna_statuses"> + <Datapoints> + <Datapoint name="System1:T1.level" alias="WaterLevel1"> + <description>Test datapoint in the GettingStarted example of WinCCOA</description> + <CheckpointInterval value="-3" /> + <index> + <regex> + <![CDATA[(?<system>\w*)\.((?<attribute>\w*))]]> + </regex> + <attribute name="system" alias="system" type="STRING"/> + <attribute name="attribute" alias="attribute" type="STRING"/> + </index> + <attributes> + <attribute name=":_original.._value" alias="value" type="STRING" unit="m" min="-0.3" max="0.3"/> + <attribute name=":_original.._stime" alias="lastChangeDate" type="STRING" unit=""/> + </attributes> + </Datapoint> + <Datapoint name="System1:T2.level" alias="WaterLevel2"> + <description>Test datapoint in the GettingStarted example of WinCCOA</description> + <CheckpointInterval value="-3" /> + <index> + <regex> + <![CDATA[(?<system>\w*)\.((?<attribute>\w*))]]> + </regex> + <attribute name="system" alias="system" type="STRING"/> + <attribute name="attribute" alias="attribute" type="STRING"/> + </index> + <attributes> + <attribute name=":_original.._value" alias="value" type="STRING" unit=""/> + <attribute name=":_original.._stime" alias="lastChangeDate" type="STRING" unit=""/> + </attributes> + </Datapoint> + </Datapoints> +</Table> diff --git a/MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xsd b/MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xsd new file mode 100644 index 00000000000..963fec10333 --- /dev/null +++ b/MAC/WinCCDBBridge/etc/Datapoints/antennaStatuses.xsd @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> + <xs:element name="Table"> + <xs:complexType> + <xs:sequence> + <xs:element ref="Datapoints"/> + </xs:sequence> + <xs:attribute name="name" use="required" type="xs:NCName"/> + </xs:complexType> + </xs:element> + <xs:element name="Datapoints"> + <xs:complexType> + <xs:sequence> + <xs:element maxOccurs="unbounded" ref="Datapoint"/> + </xs:sequence> + </xs:complexType> + </xs:element> + <xs:element name="Datapoint"> + <xs:complexType> + <xs:sequence> + <xs:element ref="description"/> + <xs:element ref="CheckpointInterval"/> + <xs:element ref="index"/> + <xs:element ref="attributes"/> + </xs:sequence> + <xs:attribute name="name" use="required" type="xs:NMTOKEN"/> + <xs:attribute name="alias" use="required" type="xs:NCName"/> + </xs:complexType> + </xs:element> + <xs:element name="description" type="xs:string"/> + <xs:element name="CheckpointInterval"> + <xs:complexType> + <xs:attribute name="value" use="required" type="xs:integer"/> + </xs:complexType> + </xs:element> + <xs:element name="index"> + <xs:complexType> + <xs:sequence> + <xs:element ref="regex"/> + <xs:element maxOccurs="unbounded" ref="attribute"/> + </xs:sequence> + </xs:complexType> + </xs:element> + <xs:element name="regex" type="xs:string"/> + <xs:element name="attributes"> + <xs:complexType> + <xs:sequence> + <xs:element maxOccurs="unbounded" ref="attribute"/> + </xs:sequence> + </xs:complexType> + </xs:element> + <xs:element name="attribute"> + <xs:complexType> + <xs:attribute name="name" use="required" type="xs:NMTOKEN"/> + <xs:attribute name="alias" use="required" type="xs:NCName"/> + <xs:attribute name="type" use="required" type="xs:NCName"/> + <xs:attribute name="max" type="xs:decimal"/> + <xs:attribute name="min" type="xs:decimal"/> + <xs:attribute name="unit" type="xs:string"/> + <xs:attribute name="value" type="xs:string"/> + </xs:complexType> + </xs:element> +</xs:schema> diff --git a/MAC/WinCCDBBridge/src/CMakeLists.txt b/MAC/WinCCDBBridge/src/CMakeLists.txt new file mode 100644 index 00000000000..e97fdf40375 --- /dev/null +++ b/MAC/WinCCDBBridge/src/CMakeLists.txt @@ -0,0 +1,25 @@ + +include(LofarFindPackage) +cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR) +file(GLOB_RECURSE WinCCDispatcher_SRCS *.cpp *.hpp) + + +include_directories(${WinCCWrapper_SOURCE_DIR}/include/) +include_directories( + ${CMAKE_SOURCE_DIR} + ${CMAKE_BINARY_DIR} + ) +include_directories(${Qt5_INCLUDE_DIRS}) + +# Create the application +lofar_add_bin_program(winccbridge ${WinCCDispatcher_SRCS}) + +target_link_libraries(winccbridge + Qt5::Core + Qt5::Network + Qt5::Sql + Qt5::Xml + wincc_wrapper +) + +target_compile_options(winccbridge PRIVATE -std=c++11) diff --git a/MAC/WinCCDBBridge/src/DatapointAttribute.cpp b/MAC/WinCCDBBridge/src/DatapointAttribute.cpp new file mode 100644 index 00000000000..44bae647d71 --- /dev/null +++ b/MAC/WinCCDBBridge/src/DatapointAttribute.cpp @@ -0,0 +1,180 @@ +#include "DatapointAttribute.hpp" +#include "DatapointRule.hpp" + +#include <limits> + +#include <QSqlField> +#include <QVariant> +#include <QSqlDatabase> +#include <QSqlDriver> +#include <QStringList> +#include <QDomElement> + +#include <QDebug> + +DatapointAttribute::DatapointAttribute(): + m_max{std::numeric_limits<double>::max()}, + m_min{std::numeric_limits<double>::min()}, + checkBoundaries{false} +{ + +} + +DatapointAttribute::DatapointAttribute(QString name, QString alias, QString type, QString unit, QString value, double minValue, double maxValue, bool check): + m_name{name}, m_alias{alias}, m_type{type}, m_unit{unit}, m_value{value}, m_max{maxValue}, m_min{minValue}, checkBoundaries{check} +{ + +} + +QString const DatapointAttribute::formatValue() const{ + if(m_type == "STRING"){ + + QSqlField field{m_alias, QVariant::String}; + field.setValue(m_value); + + QSqlDatabase db = QSqlDatabase::database(); + + if(db.isValid() == true) { + return db.driver()->formatValue(field); + }else{ + qWarning() << "Default database it is not configured"; + return "\'" + m_value + "\'"; + } + }else{ + return m_value; + } +} + + +QString DatapointAttribute::composeDatapointURI(DatapointRule &rule){ + return rule.name() + m_name; +} + +void DatapointAttribute::setParentRule(DatapointRule& rule) +{ + m_uri = composeDatapointURI(rule); +} + +const QString& DatapointAttribute::URI() const +{ + return m_uri; +} + +void DatapointAttribute::setURI(const QString& uri) +{ + m_uri = uri; +} + +const QString& DatapointAttribute::name() const +{ + return m_name; +} + +void DatapointAttribute::setName(const QString& name) +{ + m_name = name; +} + +const QString& DatapointAttribute::alias() const +{ + return m_alias; +} + +void DatapointAttribute::setAlias(const QString& alias) +{ + m_alias = alias; +} + +const QString& DatapointAttribute::type() const +{ + return m_type; +} + +void DatapointAttribute::setType(const QString& type) +{ + m_type = type; +} + +const QString& DatapointAttribute::unit() const +{ + return m_unit; +} + +void DatapointAttribute::setUnit(const QString& unit) +{ + m_unit = unit; +} + +const QString& DatapointAttribute::value() const +{ + return m_value; +} + +void DatapointAttribute::setValue(const QString& value) +{ + m_value = value; +} + +const double& DatapointAttribute::upperLimit() const +{ + return m_max; +} + +void DatapointAttribute::setUpperLimit(const double upperLimit) +{ + m_max = upperLimit; +} + +const double& DatapointAttribute::lowerLimit() const +{ + return m_min; +} + +void DatapointAttribute::setLowerLimit(const double lowerLimit) +{ + m_min = lowerLimit; +} + + +const QString DatapointAttribute::toString() const { + return "DatapointAttribute(" + QStringList({m_name, m_alias, m_type, m_unit, m_value}).join(", ") + ")"; +} + +bool DatapointAttribute::isBoundariesCheckEnabled(){ + return checkBoundaries; +} + +void DatapointAttribute::enableBoundariesCheck(){ + checkBoundaries = true; +} + +bool DatapointAttribute::isInRange(QString value){ + if(checkBoundaries){ + const double d_value = value.toDouble(); + return d_value > m_min && d_value < m_max; + }else{ + return false; + } +} + +DatapointAttribute readAttribute(const QDomElement &attributeEntry){ + DatapointAttribute attribute; + + attribute.m_alias = attributeEntry.attribute("alias", ""); + attribute.m_name = attributeEntry.attribute("name", ""); + attribute.m_type = attributeEntry.attribute("type", ""); + attribute.m_unit = attributeEntry.attribute("unit", ""); + attribute.m_value = attributeEntry.attribute("value", ""); + + + if(attributeEntry.hasAttribute("max")){ + attribute.enableBoundariesCheck(); + attribute.m_max = attributeEntry.attribute("max").toDouble(); + } + + if(attributeEntry.hasAttribute("min")){ + attribute.enableBoundariesCheck(); + attribute.m_min = attributeEntry.attribute("min").toDouble(); + } + return attribute; +} diff --git a/MAC/WinCCDBBridge/src/DatapointAttribute.hpp b/MAC/WinCCDBBridge/src/DatapointAttribute.hpp new file mode 100644 index 00000000000..ef1b223e098 --- /dev/null +++ b/MAC/WinCCDBBridge/src/DatapointAttribute.hpp @@ -0,0 +1,76 @@ +#ifndef DATAPOINT_ATTRIBUTE_H +#define DATAPOINT_ATTRIBUTE_H + +#include <QString> +class DatapointRule; +class QDomElement; + +/*! \class DatapointAttribute + \brief Define an attribute for a given datapoint. + The DatapointAttribute describes an attribute of a datapoint that could be for example a last modification date or a value. + If it is a value it can be subject of jittering. For this reason, it can be possible to specify a range within which the value + can be without triggering an update of the database. + It has to be specified as in the following XML fragment (ex. + <attribute name=":_original.._value" alias="value" type="STRING" unit=""/> + ) +*/ + + +class DatapointAttribute +{ +public: + DatapointAttribute(); + DatapointAttribute(QString name, QString alias, QString type, QString unit, QString value, double minValue, double maxValue, bool check); + + const QString formatValue() const; + const QString toString() const; + + const QString & URI() const; + void setURI(const QString &uri); + + bool isBoundariesCheckEnabled(); + void enableBoundariesCheck(); + + bool isInRange(QString value); + + const QString & name() const; + void setName(const QString & name); + + const QString & alias() const; + void setAlias(const QString & alias); + + const QString & type() const; + void setType(const QString & type); + + const QString & unit() const; + void setUnit(const QString & unit); + + const QString & value() const; + void setValue(const QString & value); + + const double & upperLimit() const; + void setUpperLimit(const double upperLimit); + + const double & lowerLimit() const; + void setLowerLimit(const double lowerLimit); + + void setParentRule(DatapointRule & rule); + +private: + QString m_name; + QString m_alias; + QString m_type; + QString m_unit; + QString m_value; + double m_max; + double m_min; + QString m_uri; + QString composeDatapointURI(DatapointRule & rule); + bool checkBoundaries; +friend DatapointAttribute readAttribute(const QDomElement &);; +}; + + +DatapointAttribute readAttribute(const QDomElement &attributeEntry); + +#endif // DATAPOINT_ATTRIBUTE_H diff --git a/MAC/WinCCDBBridge/src/DatapointIndex.cpp b/MAC/WinCCDBBridge/src/DatapointIndex.cpp new file mode 100644 index 00000000000..fe952072768 --- /dev/null +++ b/MAC/WinCCDBBridge/src/DatapointIndex.cpp @@ -0,0 +1,52 @@ +#include "DatapointIndex.hpp" +#include "DatapointAttribute.hpp" + +#include <QRegularExpression> +#include <QDomElement> +#include <QDebug> + +DatapointIndex::DatapointIndex() +{ + +} + +const QString DatapointIndex::toString() const { + return "DatapointIndex(\"" + QStringList({pattern}).join(", ") + "\")"; +} + +void DatapointIndex::parseDPIndex(QString index) +{ + QRegularExpression regexParser{pattern}; + QRegularExpressionMatch matched = regexParser.match(index); + + + if(matched.hasMatch()){ + for(DatapointAttribute & key: m_keys){ + const QString index_id{key.name()}; + key.setValue(matched.captured(index_id)); + } + } + +} + +const std::vector<DatapointAttribute>& DatapointIndex::keys() +{ + return m_keys; +} + + + +const DatapointIndex readIndexRule(const QDomElement & element, const QString & dpName){ + DatapointIndex dpIndex; + + dpIndex.pattern = element.firstChildElement("regex").text(); + QDomNodeList attributes = element.elementsByTagName("attribute"); + for(int i=0; i < attributes.size(); i++){ + DatapointAttribute attribute{readAttribute(attributes.item(i).toElement())}; + dpIndex.m_keys.push_back(attribute); + } + + dpIndex.parseDPIndex(dpName); + + return dpIndex; +} diff --git a/MAC/WinCCDBBridge/src/DatapointIndex.hpp b/MAC/WinCCDBBridge/src/DatapointIndex.hpp new file mode 100644 index 00000000000..21cf9724234 --- /dev/null +++ b/MAC/WinCCDBBridge/src/DatapointIndex.hpp @@ -0,0 +1,40 @@ +#ifndef DATAPOINT_INDEX_H +#define DATAPOINT_INDEX_H +#include <QString> + +class DatapointAttribute; +class QDomElement; + +/*! \class DatapointIndex + \brief Define a unique index for a given datapoint. + The DatapointIndex is meant to reflect a DB table index. + Such index is derived from the datapoint name through a regex to give the widest range of possibilities in the configuration file. + In particular, the XML fragment parsed by the function readIndexRule and used to instantiate the DatapointIndex is for example: + <index> + <regex> + <![CDATA[(?<system>\w*)\.((?<attribute>\w*))]]> + </regex> + <attribute name="system" alias="system" type="STRING"/> + <attribute name="attribute" alias="attribute" type="STRING"/> + </index> + The index keys are picked up from the named regex capture group. Therefore, each capture group that has to be used as an index field + has to be named. +*/ + +class DatapointIndex +{ +public: + DatapointIndex(); + + void parseDPIndex(QString index); + const QString toString() const; + const std::vector<DatapointAttribute> & keys(); +private: + std::vector<DatapointAttribute> m_keys; + QString pattern; +friend const DatapointIndex readIndexRule(const QDomElement &, const QString &); +}; + +const DatapointIndex readIndexRule(const QDomElement & element, const QString & dpName); + +#endif // DATAPOINT_INDEX_H diff --git a/MAC/WinCCDBBridge/src/DatapointRule.cpp b/MAC/WinCCDBBridge/src/DatapointRule.cpp new file mode 100644 index 00000000000..483a54e4760 --- /dev/null +++ b/MAC/WinCCDBBridge/src/DatapointRule.cpp @@ -0,0 +1,199 @@ +#include "DatapointRule.hpp" + +#include "DatapointIndex.hpp" +#include "DatapointAttribute.hpp" + +#include <QStringList> + +#include <QSqlQuery> +#include <QTimer> + +#include <QDebug> +#include <QDomElement> +#include <QDomDocument> + +DatapointRule::DatapointRule(): statement{""} +{ + +} + +std::vector<std::string> DatapointRule::getDatapointsURI() +{ + using std::vector; + using std::string; + + vector<string> datapointURIs; + for(auto & attr: m_attributes){ + datapointURIs.push_back(attr.second.URI().toStdString()); + } + return datapointURIs; +} + +DatapointRule::DatapointRule(QString name, QString alias, QString description): + m_name{name}, m_alias{alias}, m_description{description}, statement{""}, checkBoundaries{false} +{ + +} + +void DatapointRule::addAttribute(DatapointAttribute attribute){ + attribute.setParentRule(*this); + + checkBoundaries |= attribute.isBoundariesCheckEnabled(); + + m_attributes[attribute.URI().toStdString()] = attribute; +} + +const QString DatapointRule::toString() const { + return "DatapointRule(" + QStringList({m_name, m_alias, m_description}).join(", ") + ")"; +} + +const QString& DatapointRule::name() const +{ + return m_name; +} + +const QString& DatapointRule::alias() const +{ + return m_alias; +} + +const QString& DatapointRule::description() const +{ + return m_description; +} + +const QString& DatapointRule::table() const +{ + return m_table; +} + +void DatapointRule::setName(const QString& name) +{ + m_name = name; +} + +void DatapointRule::setAlias(const QString& alias) +{ + m_alias = alias; +} + +void DatapointRule::setDescription(const QString& description) +{ + m_description = description; +} +void DatapointRule::setTable(const QString& table) +{ + m_table = table; +} + + +void DatapointRule::prepareStatement(){ + QStringList columnNames; + QStringList valueNames; + QStringList indexNames; + + for(const DatapointAttribute & att : index.keys()){ + columnNames << att.alias(); + valueNames << att.formatValue(); + } + + for(const auto & att : m_attributes){ + columnNames << att.second.alias(); + valueNames << ":" + att.second.alias(); + + } + + statement = "INSERT INTO " + m_table + " "; + statement += "(" + columnNames.join(", ") + ")"; + statement += " VALUES "; + statement += "(" + valueNames.join(", ") + ")"; +} + +const QString & DatapointRule::sqlStatement(){ + if(statement.isEmpty() == true) prepareStatement(); + return statement; +} + +void DatapointRule::bindValues(QSqlQuery & query, std::map<std::string, std::string> & values){ + qDebug() << "binding values"; + + for(auto & uriValue: values){ + const QString attribute_alias = ":" + m_attributes[uriValue.first].alias(); + const QVariant attribute_value = QString::fromStdString(uriValue.second); + qDebug() << "alias=" << attribute_alias << ", value=" << attribute_value; + query.bindValue(attribute_alias, attribute_value); + } +} + +DatapointRule * readDatapointRule(const QDomElement & element){ + + DatapointRule * dpRule{new DatapointRule}; + dpRule->m_description = element.firstChildElement("description").text(); + // READ the checkpoint interval and converts it in milliseconds + dpRule->m_checkpointInterval = std::chrono::seconds(element.firstChildElement("CheckpointInterval").attribute("value").toInt()); + dpRule->m_name = element.attribute("name"); + dpRule->m_alias = element.attribute("alias"); + + dpRule->index = readIndexRule(element.firstChildElement("index"), dpRule->m_name); + + qDebug() << dpRule->index.toString(); + + QDomNodeList attributes = element.firstChildElement("attributes").elementsByTagName("attribute"); + + for(int i=0; i < attributes.size(); i++){ + DatapointAttribute attribute{readAttribute(attributes.item(i).toElement())}; + qDebug()<<attribute.toString(); + dpRule->addAttribute(attribute); + } + return dpRule; +} + +bool DatapointRule::hasToTriggerUpdate(std::map<std::string, std::string> & values){ + if(checkBoundaries){ + qDebug() << "check values in range"; + bool inRange = true; + for(const auto uriValue: values){ + DatapointAttribute & attribute = m_attributes[uriValue.first]; + inRange = inRange && attribute.isInRange(uriValue.second.c_str()); + } + return !inRange; + }else{ + return true; + } +} + +void DatapointRule::triggerRequestCheckpoint(){ + emit requestCheckpoint(this); +} + +void DatapointRule::startCheckpointTimer(){ + triggerRequestCheckpoint(); + timer.reset(new QTimer(this)); + connect(timer.get(), &QTimer::timeout, this, &DatapointRule::triggerRequestCheckpoint); + if(m_checkpointInterval.count() > 0){ + timer->setInterval(std::chrono::milliseconds(m_checkpointInterval).count()); + timer->start(); + } +} + +std::vector<std::shared_ptr<DatapointRule>> readDatapointRules(QDomDocument & document){ + QDomElement table = document.firstChildElement("Table"); + QString tableName = table.attribute("name"); + + qDebug() << "Rule file for table " << tableName; + + QDomElement element = table.firstChildElement("Datapoints") + .firstChildElement("Datapoint"); + std::vector<std::shared_ptr<DatapointRule>> rules; + + while(element.isNull() == false){ + + DatapointRule * rule{readDatapointRule(element)}; + rule->m_table = tableName; + rule->prepareStatement(); + rules.push_back(std::shared_ptr<DatapointRule>{rule}); + + element = element.nextSiblingElement(); + } + return rules; +} diff --git a/MAC/WinCCDBBridge/src/DatapointRule.hpp b/MAC/WinCCDBBridge/src/DatapointRule.hpp new file mode 100644 index 00000000000..2e51506befe --- /dev/null +++ b/MAC/WinCCDBBridge/src/DatapointRule.hpp @@ -0,0 +1,77 @@ +#ifndef DATAPOINT_RULE_H +#define DATAPOINT_RULE_H + +#include <vector> +#include <map> +#include <memory> + +#include <QString> +#include <QObject> +#include <chrono> +class QTimer; +class QSqlQuery; +class QDomElement; +class QDomDocument; + +#include "DatapointAttribute.hpp" +#include "DatapointIndex.hpp" + +class DatapointRule: public QObject +{ + Q_OBJECT +public: + DatapointRule(); + DatapointRule(QString name, QString alias, QString description); + std::vector<std::string> getDatapointsURI(); + + std::chrono::seconds m_checkpointInterval; + + DatapointIndex index; + + const QString toString() const; + void addAttribute(DatapointAttribute attribute); + + const QString & sqlStatement(); + void bindValues(QSqlQuery & query, std::map<std::string, std::string> & values); + + bool hasToTriggerUpdate(std::map<std::string, std::string> & values); + + void prepareStatement(); + + const QString & name() const; + void setName(const QString & name); + + const QString & alias() const; + void setAlias(const QString & alias); + + const QString & description() const; + void setDescription(const QString & description); + + const QString & table() const; + void setTable(const QString & table); +private: + QString m_name; + QString m_alias; + QString m_description; + QString m_table; + + QString statement; + std::map<std::string, DatapointAttribute> m_attributes; + std::shared_ptr<QTimer> timer; + bool checkBoundaries; +signals: + void requestCheckpoint(DatapointRule *); + +public slots: + void triggerRequestCheckpoint(); + void startCheckpointTimer(); + +friend DatapointRule * readDatapointRule(const QDomElement &); +friend std::vector<std::shared_ptr<DatapointRule>> readDatapointRules(QDomDocument &); +}; + +DatapointRule * readDatapointRule(const QDomElement & element); +std::vector<std::shared_ptr<DatapointRule>> readDatapointRules(QDomDocument & document); + +#endif // DATAPOINT_RULE_H + diff --git a/MAC/WinCCDBBridge/src/Program.cpp b/MAC/WinCCDBBridge/src/Program.cpp new file mode 100644 index 00000000000..0d9cd16779a --- /dev/null +++ b/MAC/WinCCDBBridge/src/Program.cpp @@ -0,0 +1,249 @@ +#include "Program.hpp" + +#include <QDebug> +#include <QFile> +#include <QJsonDocument> +#include <QJsonArray> +#include <QJsonObject> +#include <QJsonValue> + +#include <QDomDocument> +#include <QDomElement> +#include <QDomNode> + +#include <QXmlInputSource> + +#include <QtNetwork> +#include <QUrl> +#include <QSqlQuery> +#include <QSqlError> + + +#include <exception> + +using LOFAR::WINCCWRAPPER::WinCCWrapper; + +class ConfigurationFileReadError : public std::exception { +private: + std::string message; +public: + ConfigurationFileReadError(std::string path): message{"Error reading file " + path} {} + + const char * what() const throw () { + return message.c_str(); + } +}; + +Program::Program(QObject * parent, const QString& configurationFile, const QString & projectName): QObject{parent}, + database{new QSqlDatabase} +{ + wrapper.reset(new LOFAR::WINCCWRAPPER::WinCCWrapper{"Program", projectName.toStdString(), 0}); + + try{ + readConfigurationFile(configurationFile); + + // Event connect + // -- WINCC side + wrapper->set_connect_datapoints_callback([=](std::map<std::string, std::string> values){ + emit datapointChanged(values); }); + // -- QT side + connect(this, &Program::datapointChanged, this, &Program::consolePrint); + connect(this, &Program::datapointChanged, this, &Program::messageDispatch); + } + catch (std::exception & e){ + qCritical() << "An error occurred while reading the configuration file: "<< e.what(); + qFatal("Cannot read configuration file"); + } + +} + +Program::~Program() +{ +} + +void Program::run() +{ + database->open(); + connectToDatapoints(); + emit startRulesCheckpointTriggers(); + + while(true){ + qApp->processEvents(); + wrapper->wait_for_event(0, 500); + } + wrapper->exit(); + database->close(); + qInfo()<< "Closing worker threads"; + +} + +void Program::consolePrint(std::map<std::string, std::string> values) +{ + + qDebug() << "Received: "; + for(const auto & value: values){ + qDebug()<< value.first.c_str() << " " << value.second.c_str(); + } +} + +void Program::messageDispatch(std::map<std::string, std::string> values) +{ + if(values.size() == 0) return; + + const std::string & uri = values.begin()->first; + + + DatapointRule * rule = uriToRule[uri]; + if(rule == nullptr) qWarning() << "CANNOT FIND RULE FOR"<< uri.c_str(); + + qInfo() << "update has been triggered for" <<uri.c_str() << "with name " << rule->name(); + if(rule->hasToTriggerUpdate(values)){ + + const QString queryString{rule->sqlStatement()}; + + QSqlQuery query; + query.prepare(queryString); + + QMapIterator<QString, QVariant> boundValuesIt(query.boundValues()); + + while(boundValuesIt.hasNext()){ + boundValuesIt.next(); + qDebug()<< "key" << boundValuesIt.key()<< "value" << boundValuesIt.value(); + } + + rule->bindValues(query, values); + + + if(query.exec() != true) qWarning() << query.lastError(); + + qInfo() << "Executed query " << query.executedQuery(); + qInfo() << rule->name() << " SQL:" << queryString; + }else{ + qInfo() << "Skipping update of "<< uri.c_str() << " with rule name " << rule->name() << "values all in range"; + } + +} + +const QJsonDocument readXMLFile(QFile & file){ + + QString buffer; + + file.open(QIODevice::ReadOnly | QIODevice::Text); + try{ + buffer = file.readAll(); + file.close(); + }catch(std::exception &e){ + file.close(); + qCritical()<<"An error occurred while reading configuration file"<< e.what(); + throw e; + } + return QJsonDocument::fromJson(buffer.toUtf8()); +} + +void Program::readDBConfigurationSection(QSettings & configuration){ + + configuration.beginGroup("Database"); + const QString name = configuration.value("name").toString(); + const QString type = configuration.value("type").toString(); + const QString address = configuration.value("address").toString(); + const int port = configuration.value("port").toInt(); + const QString user = configuration.value("user").toString(); + const QString pass = configuration.value("pass").toString(); + configuration.endGroup(); + + QSqlDatabase db = QSqlDatabase::addDatabase(type); + + db.setHostName(address); + db.setDatabaseName(name); + db.setPort(port); + db.setUserName(user); + db.setPassword(pass); + + qInfo()<<db; + + *database = db; +} + +void Program::readConfigurationFile(QString fileName){ + QFile pathToConfigFile{fileName}; + + if(pathToConfigFile.exists()){ + QSettings configuration{fileName, QSettings::IniFormat}; + readDBConfigurationSection(configuration); + + }else{ + throw ConfigurationFileReadError{fileName.toStdString()}; + } +} + +void Program::connectToDatapoints(){ + + std::cout<< "Datapoint rules are " << dataPointRules.size(); + + for(std::shared_ptr<DatapointRule> & rule: dataPointRules){ + std::vector<std::string> dataPoints{rule->getDatapointsURI()}; + + for(std::string name : dataPoints) qDebug()<< "Registering " << dataPoints.size() << "rule "<< name.c_str(); + wrapper->connect_datapoints_multi(dataPoints); + } +} + +void Program::checkpointRequested(DatapointRule * rule){ + + std::map<std::string, std::string> values; + qInfo() << "Processing rule " << rule->name(); + try { + + for(const std::string & uri: rule->getDatapointsURI()){ + values[uri] = wrapper->get_formatted_datapoint(uri); + qDebug() << "uri:"<<uri.c_str() << "\t" << wrapper->get_formatted_datapoint(uri).c_str(); + } + + QSqlQuery query; + query.prepare(rule->sqlStatement()); + + rule->bindValues(query, values); + if(!query.exec()) qWarning() << query.lastError(); + qInfo() << "Processed rule " << rule->name(); + + qDebug() << "Executed query " << query.executedQuery(); + }catch (...) { + qWarning() << "cannot update rule " << rule->name(); + } +} + + + +QDomDocument readFile(QString fileName){ + QFile datapointFile{fileName}; + + if(datapointFile.exists()){ + + QDomDocument parsedFile; + QXmlInputSource sourceXML{&datapointFile}; + parsedFile.setContent(&sourceXML, true); + datapointFile.close(); + return parsedFile; + + }else{ + throw ConfigurationFileReadError{fileName.toStdString()}; + } +} + +void Program::loadDatapointsConfiguration(QString configurationFile) +{ + QDomDocument parsedContent{readFile(configurationFile)}; + + dataPointRules = readDatapointRules(parsedContent); + for(std::shared_ptr<DatapointRule> & rule: dataPointRules){ + connect(rule.get(), &DatapointRule::requestCheckpoint, this, &Program::checkpointRequested); + connect(this, &Program::startRulesCheckpointTriggers, rule.get(), &DatapointRule::startCheckpointTimer); + + for(std::string uri: rule->getDatapointsURI()){ + uriToRule[uri] = rule.get(); + qDebug()<< "URI="<< uri.c_str() << "SQL: " << rule->sqlStatement(); + } + } +} + +#include "Program.moc" diff --git a/MAC/WinCCDBBridge/src/Program.hpp b/MAC/WinCCDBBridge/src/Program.hpp new file mode 100644 index 00000000000..232e6c18a5b --- /dev/null +++ b/MAC/WinCCDBBridge/src/Program.hpp @@ -0,0 +1,50 @@ +#ifndef PROGRAM_H +#define PROGRAM_H + +#include <QObject> +#include <QSqlDatabase> + +#include <WinCCWrapper.h> +#include <memory> + +#include <map> +#include <QString> + +#include "DatapointRule.hpp" + +class QSettings; + +class Program : public QObject +{ + Q_OBJECT + +public: + Program(QObject * parent, const QString& configurationFile, const QString & projectName); + void loadDatapointsConfiguration(QString configurationFile); + virtual ~Program(); + void run(); + +public slots: + void consolePrint(std::map<std::string, std::string> datapoints); //!< Prints a log message for each changed datapoint name + void messageDispatch(std::map<std::string, std::string> datapoints); //!< Send the datapoint change value into the database + void checkpointRequested(DatapointRule *); //! < React when a rule triggers a checkpoint request + +signals: + void datapointChanged(std::map<std::string, std::string> datapoints); //!< This event is triggered when a registered datapoint value changes + void startRulesCheckpointTriggers(); + +private: + std::shared_ptr<LOFAR::WINCCWRAPPER::WinCCWrapper> wrapper; //!< Pointer to an instance of the WinCCWrapper + + void readConfigurationFile(QString fileName); //!< Read the configuration file + void readDBConfigurationSection(QSettings &); //!< Read the database section of the configuration file + + void startWorkers(int num); + void connectToDatapoints(); //! Ask WinCC to get an update every time one of the specified datapoint changes + std::map<std::string, DatapointRule *> uriToRule; + std::vector<std::shared_ptr<DatapointRule>> dataPointRules; + std::shared_ptr<QSqlDatabase> database; + +}; + +#endif // PROGRAM_H diff --git a/MAC/WinCCDBBridge/src/main.cpp b/MAC/WinCCDBBridge/src/main.cpp new file mode 100644 index 00000000000..b7ed825afac --- /dev/null +++ b/MAC/WinCCDBBridge/src/main.cpp @@ -0,0 +1,69 @@ +#include <QCoreApplication> + +#include <QCommandLineParser> +#include <QDebug> + +#include "Program.hpp" + +/** +Defines the Qt application properties. +*/ +void defineApplication(){ + QCoreApplication::setApplicationName("WinCCDispatcher"); + +} + +/** +Defines the command line options +*/ +void defineCommandLineOptions(QCommandLineParser & parser){ + parser.setApplicationDescription("Stores the value directly in a chosen SQL database.\n"); + + parser.addHelpOption(); + parser.addPositionalArgument("datapoint_config", "Configuration file containing the list of datapoints to connect to"); + parser.addPositionalArgument("configuration", "Configuration file containing the database connection info"); + parser.addPositionalArgument("WinCCProjectName", "Project name to connect to"); +} + + +/** +Execute the main program +*/ +void executeProgram(const QStringList arguments){ + const QString datapointXMLConfiguration = arguments.at(0); + const QString configuration = arguments.at(1); + + const QString winccProjectName = arguments.at(2); + + Program program{nullptr, configuration, winccProjectName}; + program.loadDatapointsConfiguration(datapointXMLConfiguration); + program.run(); +} + +/** +Main function. It execute in succession: + * parsing the program options + * handle required missing parameter + * constructing the main program + * executes the main loop +*/ +int main(int argc, char * argv[]){ + QCoreApplication app{argc, argv}; + QCommandLineParser parser; + + defineApplication(); + + defineCommandLineOptions(parser); + + parser.process(app); + const QStringList arguments = parser.positionalArguments(); + + if(arguments.size() == 3) { + executeProgram(arguments); + }else{ + qCritical()<< "Missing required arguments"; + parser.showHelp(); + exit(1); + } + +} diff --git a/MAC/WinCCDBBridge/test/CMakeLists.txt b/MAC/WinCCDBBridge/test/CMakeLists.txt new file mode 100644 index 00000000000..ddbb3222fc3 --- /dev/null +++ b/MAC/WinCCDBBridge/test/CMakeLists.txt @@ -0,0 +1,27 @@ +include(LofarCTest) + +IF(HAVE_UNITTEST++) + find_package(Qt5 COMPONENTS Core Network Sql Xml REQUIRED) + + include_directories(${Qt5_INCLUDE_DIRS}) + set(TESTED_FILES + ../src/DatapointAttribute.cpp + ../src/DatapointRule.cpp + ../src/DatapointIndex.cpp) + + set(TEST_NAMES + t_DatapointAttribute + t_DatapointIndex + ) + + foreach(TEST_NAME ${TEST_NAMES}) + lofar_add_test("${TEST_NAME}" "${TEST_NAME}.cpp" ${TESTED_FILES}) + target_link_libraries("${TEST_NAME}" + Qt5::Core + Qt5::Network + Qt5::Sql + Qt5::Xml + ) + endforeach() + +ENDIF(HAVE_UNITTEST++) diff --git a/MAC/WinCCDBBridge/test/t_DatapointAttribute.cpp b/MAC/WinCCDBBridge/test/t_DatapointAttribute.cpp new file mode 100644 index 00000000000..5bd40df794b --- /dev/null +++ b/MAC/WinCCDBBridge/test/t_DatapointAttribute.cpp @@ -0,0 +1,98 @@ +#include "UnitTest++/UnitTest++.h" + +#include "../src/DatapointAttribute.hpp" +#include "../src/DatapointRule.hpp" + +#include <QDomElement> +#include <QDomDocument> + + +#include <QDebug> +TEST(test_formatValue) +{ + DatapointAttribute attr("datapointname", "alias_name", "STRING", "s", "12", 0, 15, true); + CHECK_EQUAL("\'12\'", attr.formatValue().toStdString()); + + attr.setType("INT"); + + CHECK_EQUAL("12", attr.formatValue().toStdString()); +} + +TEST(test_toString) +{ + DatapointAttribute attr("datapointname", "alias_name", "STRING", "s", "12", 0, 15, true); + + CHECK_EQUAL("DatapointAttribute(datapointname, alias_name, STRING, s, 12)", attr.toString().toStdString()); +} + +TEST(test_isInRange) +{ + DatapointAttribute attr("datapointname", "alias_name", "STRING", "s", "12", 0, 15, true); + CHECK_EQUAL(true, attr.isInRange("12")); + + CHECK_EQUAL(false, attr.isInRange("-1")); + + CHECK_EQUAL(false, attr.isInRange("18")); + + attr = DatapointAttribute{}; + attr.setUpperLimit(12); + attr.setLowerLimit(0); + + CHECK_EQUAL(false, attr.isBoundariesCheckEnabled()); + CHECK_EQUAL(false, attr.isInRange("-1")); + CHECK_EQUAL(false, attr.isInRange("15")); + CHECK_EQUAL(false, attr.isInRange("5")); + +} + +TEST(test_URI){ + DatapointAttribute attr("datapointname", "alias_name", "STRING", "s", "12", 0, 15, true); + + DatapointRule rule("rule.", "ruleAlias", "mytestrule"); + + attr.setParentRule(rule); + + CHECK_EQUAL("rule.datapointname", attr.URI().toStdString()); +} + +TEST(test_XMLParsing){ + QDomDocument doc = QDomDocument("someParentElement"); + QDomElement test = doc.createElement("attribute"); + test.setAttribute("name", "myname"); + test.setAttribute("alias", "alias"); + test.setAttribute("unit", "s"); + test.setAttribute("value", "5"); + + test.setAttribute("max", "12"); + test.setAttribute("min", "12"); + + DatapointAttribute attr = readAttribute(test); + + CHECK_EQUAL("myname", attr.name().toStdString()); + CHECK_EQUAL("5", attr.value().toStdString()); + CHECK_EQUAL("alias", attr.alias().toStdString()); + CHECK_EQUAL("s", attr.unit().toStdString()); + CHECK_EQUAL(12, attr.upperLimit()); + CHECK_EQUAL(12, attr.lowerLimit()); + CHECK_EQUAL(true, attr.isBoundariesCheckEnabled()); + + + test = doc.createElement("attribute"); + test.setAttribute("name", "myname"); + test.setAttribute("alias", "alias"); + test.setAttribute("unit", "s"); + + attr = readAttribute(test); + + CHECK_EQUAL("myname", attr.name().toStdString()); + CHECK_EQUAL("", attr.value().toStdString()); + CHECK_EQUAL("alias", attr.alias().toStdString()); + CHECK_EQUAL("s", attr.unit().toStdString()); + CHECK_EQUAL(false, attr.isBoundariesCheckEnabled()); + +} + +int main(int, const char *[]) +{ + return UnitTest::RunAllTests(); +} diff --git a/MAC/WinCCDBBridge/test/t_DatapointIndex.cpp b/MAC/WinCCDBBridge/test/t_DatapointIndex.cpp new file mode 100644 index 00000000000..47acc3b9208 --- /dev/null +++ b/MAC/WinCCDBBridge/test/t_DatapointIndex.cpp @@ -0,0 +1,11 @@ +#include "UnitTest++/UnitTest++.h" + +#include "../src/DatapointAttribute.hpp" +#include "../src/DatapointIndex.hpp" +#include "../src/DatapointRule.hpp" + + +int main(int, const char *[]) +{ + return UnitTest::RunAllTests(); +} -- GitLab