Ver Fonte

first commit

ZenZ há 1 ano atrás
commit
bd4cf83f69

+ 11 - 0
.gitignore

@@ -0,0 +1,11 @@
+build-*
+*.pro.user*
+*.TMP
+*.log
+*.ilk
+*.exp
+*.dmp
+**/log.txt
+tmp/
+build/
+bin/

+ 21 - 0
DataConsumer/TDengine/TDengine.cpp

@@ -0,0 +1,21 @@
+#include "TDengine.h"
+#include <QDebug>
+TDengine::TDengine() {}
+
+void TDengine::Run()
+{
+    start();
+}
+
+void TDengine::OnData(QString user, QString key, QVariant val)
+{
+    qDebug() << user << key << val;
+}
+
+void TDengine::run()
+{
+    while (!isInterruptionRequested()) {
+        //check connection
+        sleep(1);
+    }
+}

+ 17 - 0
DataConsumer/TDengine/TDengine.h

@@ -0,0 +1,17 @@
+#ifndef TDENGINE_H
+#define TDENGINE_H
+
+#include "TDengine_global.h"
+#include "Client.h"
+
+class TDENGINE_EXPORT TDengine:public Client, public QThread
+{
+public:
+    TDengine();
+    virtual void Run();
+    virtual void OnData(QString user, QString key, QVariant val);
+private:
+    virtual void run();
+};
+
+#endif // TDENGINE_H

+ 28 - 0
DataConsumer/TDengine/TDengine.pro

@@ -0,0 +1,28 @@
+QT -= gui
+
+TEMPLATE = lib
+DEFINES += TDENGINE_LIBRARY
+
+CONFIG += c++17
+
+# You can make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+INCLUDEPATH += ../../include
+
+SOURCES += \
+    TDengine.cpp
+
+HEADERS += \
+    TDengine_global.h \
+    TDengine.h
+
+# Default rules for deployment.
+unix {
+    target.path = /usr/lib
+}else{
+    DESTDIR = $$PWD/../../bin/plugins
+}
+
+!isEmpty(target.path): INSTALLS += target

+ 12 - 0
DataConsumer/TDengine/TDengine_global.h

@@ -0,0 +1,12 @@
+#ifndef TDENGINE_GLOBAL_H
+#define TDENGINE_GLOBAL_H
+
+#include <QtCore/qglobal.h>
+
+#if defined(TDENGINE_LIBRARY)
+#define TDENGINE_EXPORT Q_DECL_EXPORT
+#else
+#define TDENGINE_EXPORT Q_DECL_IMPORT
+#endif
+
+#endif // TDENGINE_GLOBAL_H

+ 78 - 0
DataManagerMain/DataConsumer.cpp

@@ -0,0 +1,78 @@
+#include "DataConsumer.h"
+#include <QMutex>
+#include <QSemaphore>
+#include <QThread>
+#include <QDateTime>
+#include <QQueue>
+#include <QVariant>
+#include "LibraryLoader.h"
+#include "Client.h"
+DataConsumer::DataConsumer()
+{
+
+}
+
+DataConsumer::~DataConsumer()
+{
+
+}
+
+
+
+/*void DataConsumer::run()
+{
+    while(!isInterruptionRequested()){
+
+    }
+}*/
+
+void DataConsumer::Setup(ConsumerInfo &ci)
+{
+    std::string assemblyName = ci.AssemblyName;
+    std::string className = ci.ClassName;
+
+    BaseModule* pModule = nullptr;// = (BaseModule)Assembly.LoadFile(Path.GetFullPath(@".\" + assemblyName)).CreateInstance(className);
+    pModule = LibraryLoader::load<BaseModule>(assemblyName);
+    if( pModule == nullptr )
+    {
+        //        qCritical() << LOG_HEADER << " load " << szPlugin.c_str() << " failed.";
+        return;
+    }
+    if( pModule == nullptr )
+    {
+
+        qCritical() << __FILE__ << __LINE__ << " " << assemblyName.c_str() << " load failed.";
+        return;
+    }
+
+    {
+        //Client*
+        runable = dynamic_cast<Client*>(pModule);
+        runable->Run();
+    }
+}
+
+std::string DataConsumer::getTypeList()
+{
+    return "DataStorage::RunnableModule";
+}
+
+void DataConsumer::setLoader(QLibrary *)
+{
+
+}
+
+/*void DataConsumer::regConsumer(DataConsumer *dc)
+{
+    dataConsumer = dc;
+}*/
+
+/*void DataConsumer::Run()
+{
+    //start();
+}*/
+
+void DataConsumer::OnData(QString user, QString key, QVariant val)
+{
+    runable->OnData(user, key, val);
+}

+ 38 - 0
DataManagerMain/DataConsumer.h

@@ -0,0 +1,38 @@
+#ifndef DATACONSUMER_H
+#define DATACONSUMER_H
+
+
+#include <string>
+#include <QVariant>
+#include "Define.h"
+#include "../include/RunnableModule.h"
+
+
+class Client;
+class DataConsumer:public Receiver, public BaseModule
+{
+
+public:
+    DataConsumer();
+    ~DataConsumer();
+
+    virtual void Setup(ConsumerInfo& ci);
+
+    virtual std::string getTypeList();
+
+    virtual void setLoader(QLibrary*);
+
+    //virtual void Run() ;
+    virtual void OnData(QString user, QString key, QVariant val);
+private:
+    //virtual void run();
+private:
+    QLibrary* library = nullptr;
+    DataConsumer* dataConsumer = nullptr;
+    std::string dataName;
+    ConsumerInfo ci;
+    Client* runable = nullptr;
+
+};
+
+#endif // DATACONSUMER_H

+ 38 - 0
DataManagerMain/DataManager.cpp

@@ -0,0 +1,38 @@
+#include "DataManager.h"
+#include "Define.h"
+#include "DataManagerProxy.h"
+#include "DataSubscribe.h"
+#include "DataConsumer.h"
+DataManager::DataManager() {
+
+}
+
+// void DataManager::OnData(std::string key, QVariant val)
+// {
+//     //consumerMap[key]->OnData(key, val);
+// }
+
+void DataManager::Startup()
+{
+    Config config;
+    DataManagerProxy dmp;
+    auto lstModules = dmp.loadModuleInfos(config.serverId, config.appName);
+
+    for(auto itr = lstModules.consumers.begin(); itr != lstModules.consumers.end(); ++itr){
+        QScopedPointer<DataConsumer> consumer {new DataConsumer()};
+        //DataConsumer * consumer = new DataConsumer();
+        ConsumerInfo ci = *itr;
+        consumer->Setup(ci);
+        consumerMap.insert(QString(itr->name.c_str()),consumer.get());
+    }
+
+    for(auto itr = lstModules.modules.begin();itr!= lstModules.modules.end();++itr)
+    {
+        ModuleInfo mi = *itr;
+        QScopedPointer<DataSubscribe> subscriber(new DataSubscribe());
+        subscriber->regConsumer(consumerMap[itr->AssemblyName.c_str()]);
+        subscriber->Setup(mi);
+        subscriber->Run();
+    }
+
+}

+ 19 - 0
DataManagerMain/DataManager.h

@@ -0,0 +1,19 @@
+#ifndef DATAMANAGER_H
+#define DATAMANAGER_H
+#include <string>
+#include <QVariant>
+#include <QMap>
+#include "DataConsumer.h"
+#include <QSharedDataPointer>
+class DataManager
+{
+public:
+    DataManager();
+public:
+    //virtual void OnData(std::string key, QVariant val);
+    void Startup();
+private:
+    QMap<QString, DataConsumer*> consumerMap;
+};
+
+#endif // DATAMANAGER_H

+ 48 - 0
DataManagerMain/DataManagerMain.pro

@@ -0,0 +1,48 @@
+QT = core
+
+CONFIG += c++17 cmdline
+
+# You can make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+INCLUDEPATH += ../include/
+
+# HEADERS += \
+#     ../include/BaseModule.h \
+#     ../include/Define.h \
+#     ../include/JobModule.h \
+#     ../include/LibraryLoader.h \
+#     ../include/RunnableModule.h \
+#     ../include/Publisher.h
+
+
+HEADERS += \
+    DataConsumer.h \
+    DataManager.h \
+    DataManagerProxy.h \
+    DataSubscribe.h
+
+SOURCES += \
+        DataConsumer.cpp \
+        DataManager.cpp \
+        DataManagerProxy.cpp \
+        DataSubscribe.cpp \
+        main.cpp
+
+TRANSLATIONS += \
+    DataManager_zh_CN.ts
+CONFIG += lrelease
+CONFIG += embed_translations
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target
+
+unix{
+}
+else{
+    DESTDIR = $$PWD/../bin/
+}
+
+

+ 41 - 0
DataManagerMain/DataManagerProxy.cpp

@@ -0,0 +1,41 @@
+#include "DataManagerProxy.h"
+
+DataManagerProxy::DataManagerProxy() {
+
+}
+
+DataManagerInfo DataManagerProxy::loadModuleInfos(int id, std::string app)
+{
+    DataItem objDataItem;
+    objDataItem.Id = 1;
+    objDataItem.Code = "CEJINGYI-001.TEMP";
+    objDataItem.DataName = "1号测径仪温度";
+    objDataItem.DataType = "Integer";
+    objDataItem.Description = "1号测径仪温度值";
+    objDataItem.Displayed = true;
+
+    ModuleInfo objModule;
+    objModule.Id = 1;
+    objModule.Name = "Modbus000";
+    objModule.Model = "Modbus Com A0001";
+    objModule.Code = "Modbus000-1";
+    objModule.Type = ModuleType::Sensor;
+    objModule.AssemblyName = "plugins/SampleTemperature.dll";
+    objModule.ClassName = "Lanpeng.Module.Sensors.Modbus001";
+    objModule.Version = "1.0";
+
+    DeviceInfo objDevice;
+    objDevice.Id = 1;
+    objDevice.Name = "测径仪温度传感器";
+    objDevice.Type = "传感器";
+    objDevice.Model = "Temperatur Sensor A001";
+    objDevice.Code = "ASCDSC001221S";
+    objDevice.Description = "aaaaa";
+    objDevice.ModuleInfo = objModule;
+    objDevice.Properties.push_back(objDataItem);
+
+    std::list<DeviceInfo> lstDevices;
+    lstDevices.push_back(objDevice);
+    DataManagerInfo listDataManageInfo;
+    return listDataManageInfo;
+}

+ 14 - 0
DataManagerMain/DataManagerProxy.h

@@ -0,0 +1,14 @@
+#ifndef DATAMANAGERPROXY_H
+#define DATAMANAGERPROXY_H
+#include "Define.h"
+#include <QMap>
+class DataManagerProxy
+{
+public:
+    DataManagerProxy();
+
+public:
+    DataManagerInfo loadModuleInfos(int id,std::string app);
+};
+
+#endif // DATAMANAGERPROXY_H

+ 3 - 0
DataManagerMain/DataManager_zh_CN.ts

@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="zh_CN"></TS>

+ 74 - 0
DataManagerMain/DataSubscribe.cpp

@@ -0,0 +1,74 @@
+#include "DataSubscribe.h"
+#include <QVariant>
+#include "LibraryLoader.h"
+#include "Publisher.h"
+
+DataSubscribe::DataSubscribe()
+{
+
+}
+
+
+
+void DataSubscribe::run()
+{
+    while(!isInterruptionRequested()){
+        if(shares.sem->tryAcquire(1, 500)){
+            shares.mutex->lock();
+            shares.commandList.dequeue();
+            shares.mutex->unlock();
+            dataConsumer->OnData(QString(dataName.c_str()), "time", QVariant());
+        }
+    }
+}
+
+void DataSubscribe::Setup(ModuleInfo &mi)
+{
+    std::string assemblyName = mi.AssemblyName;
+    std::string className = mi.ClassName;
+
+    BaseModule* pModule = nullptr;// = (BaseModule)Assembly.LoadFile(Path.GetFullPath(@".\" + assemblyName)).CreateInstance(className);
+    pModule = LibraryLoader::load<BaseModule>(assemblyName);
+    if( pModule == nullptr )
+    {
+        //        qCritical() << LOG_HEADER << " load " << szPlugin.c_str() << " failed.";
+        return;
+    }
+    if( pModule == nullptr )
+    {
+
+        qCritical() << __FILE__ << __LINE__ << " " << assemblyName.c_str() << " load failed.";
+        return;
+    }
+
+    {
+        Publisher* runable = dynamic_cast<Publisher*>(pModule);
+        runable->shares(shares);
+        runable->Run();
+    }
+}
+
+
+
+std::string DataSubscribe::getTypeList()
+{
+    return "DataStorage::RunnableModule";
+}
+
+void DataSubscribe::setLoader(QLibrary *)
+{
+
+}
+
+void DataSubscribe::regConsumer(DataConsumer *dc)
+{
+    dataConsumer = dc;
+}
+
+void DataSubscribe::Run()
+{
+    start();
+}
+
+
+

+ 34 - 0
DataManagerMain/DataSubscribe.h

@@ -0,0 +1,34 @@
+#ifndef DATASUBSCRIBE_H
+#define DATASUBSCRIBE_H
+#include "../include/RunnableModule.h"
+
+#include <QtCore/QThread>
+#include "DataConsumer.h"
+#include "Define.h"
+
+#include <QDateTime>
+
+class DataSubscribe : public RunnableModule, public QThread
+{
+public:
+    DataSubscribe();
+    virtual void Setup(ModuleInfo& mi);
+
+    virtual std::string getTypeList();
+
+    virtual void regConsumer(DataConsumer* dc) ;
+    virtual void Run() ;
+
+private:
+    virtual void run();
+    virtual void setLoader(QLibrary*);
+private:
+    QLibrary* library = nullptr;
+    DataConsumer* dataConsumer = nullptr;
+    std::string dataName;
+    ModuleInfo mi;
+    SharedData shares;
+
+};
+
+#endif // DATASUBSCRIBE_H

+ 23 - 0
DataManagerMain/main.cpp

@@ -0,0 +1,23 @@
+#include <QCoreApplication>
+#include <QLocale>
+#include <QTranslator>
+#include "DataManager.h"
+int main(int argc, char *argv[])
+{
+    QCoreApplication a(argc, argv);
+
+    QTranslator translator;
+    const QStringList uiLanguages = QLocale::system().uiLanguages();
+    for (const QString &locale : uiLanguages) {
+        const QString baseName = "DataManager_" + QLocale(locale).name();
+        if (translator.load(":/i18n/" + baseName)) {
+            a.installTranslator(&translator);
+            break;
+        }
+    }
+
+    DataManager dataMgr;
+    dataMgr.Startup();
+
+    return a.exec();
+}

+ 19 - 0
DataManater.pro

@@ -0,0 +1,19 @@
+TEMPLATE = subdirs
+CONFIG += ordered
+
+SUBDIRS += \
+    #DataConsumer \
+    #DataSubscribe \
+    DataConsumer/TDengine \
+    DataManagerMain/DataManagerMain.pro
+    
+
+
+HEADERS += \
+    include/BaseModule.h \
+    include/Define.h \
+    include/JobModule.h \
+    include/LibraryLoader.h \
+    include/RunnableModule.h \
+    include/Publisher.h \
+    include/Client.h

+ 22 - 0
include/BaseModule.h

@@ -0,0 +1,22 @@
+#pragma once
+#include "Define.h"
+#include <QtCore/QLibrary>
+#include <string>
+#include <QString>
+#include <QVariant>
+
+class Receiver
+{
+public:
+    virtual void OnData(QString user, QString key, QVariant val) = 0;
+};
+
+
+class BaseModule
+{
+public:
+    BaseModule(){}
+    virtual ~BaseModule(){};
+    virtual void setLoader(QLibrary*) = 0;
+    virtual std::string getTypeList() = 0;
+};

+ 14 - 0
include/Client.h

@@ -0,0 +1,14 @@
+#pragma once
+#include "RunnableModule.h"
+#include <QtCore/QThread>
+#include "Define.h"
+#include <QString>
+
+class Client : public RunnableModule{
+public:
+    virtual void OnData(QString user, QString key, QVariant val) = 0;
+
+};
+
+
+

+ 92 - 0
include/Define.h

@@ -0,0 +1,92 @@
+#pragma once
+
+#include <string>
+#include <list>
+
+enum ModuleType
+{
+    Sensor=1,
+    Actor=2,
+    Purifier=3,
+    Calculator=4,
+    Storage=5,
+    Transfer=6,
+};
+
+struct DataItem
+{
+    int Id;
+    std::string DataName;
+    std::string Code;
+    std::string DataType;
+    bool Displayed;
+    std::string Description;
+    std::string ToString()
+    {
+        return Code + DataName;
+    }
+};
+
+struct ModuleInfo
+{
+    int Id;
+    ModuleType Type;                // 类型
+    std::string Name;               // 名称
+    std::string Code;               // 标识码
+    std::string Model;              // 型号
+    std::string AssemblyName;       // 组件程序(dll、jar)名字,含路径
+    std::string ClassName;          // 组件的 类名
+    std::string Description;        // 描述
+    std::string Version;            // 版本
+};
+
+struct DeviceInfo
+{
+    int Id;
+    std::string Name;                // 名称
+    std::string Type;                // 类型
+    std::string Code;                // 标识码
+    std::string Model;               // 型号,通信组件的Code属性
+    int ServerId;                    //
+    ModuleInfo ModuleInfo;           //
+    std::string Description;         // 描述
+    std::list<DataItem> Properties;
+};
+
+struct ConsumerInfo{
+    std::string name;
+    std::string AssemblyName;       // 组件程序(dll、jar)名字,含路径
+    std::string ClassName;          // 组件的 类名
+};
+
+struct DataManagerInfo{
+    int Id;
+    std::string Name;                // 名称
+    std::string Type;                // 类型
+    std::string Code;                // 标识码
+    std::string Model;               // 型号,通信组件的Code属性
+    int ServerId;                    //
+    std::string Description;         // 描述
+    std::list<ModuleInfo> modules;
+    std::list<ConsumerInfo>consumers;
+};
+
+
+struct Config
+{
+    int serverId;
+    std::string appName;
+};
+#include <QMutex>
+#include <QSemaphore>
+#include <QQueue>
+
+struct PublishData{
+
+};
+
+struct SharedData{
+    QMutex* mutex;
+    QSemaphore* sem;
+    QQueue<PublishData>commandList;
+};

+ 9 - 0
include/JobModule.h

@@ -0,0 +1,9 @@
+#pragma once
+#include "BaseModule.h"
+#include <QtCore/QObject>
+
+class JobModule : public BaseModule
+{
+public:
+    virtual QObject Process(QObject data) = 0;
+};

+ 91 - 0
include/LibraryLoader.h

@@ -0,0 +1,91 @@
+#pragma once
+
+#include <QtCore/QLibrary>
+#include <QtCore/QFile>
+#include <QtCore/QDebug>
+
+class LibraryLoader
+{  
+private:
+   LibraryLoader()
+   {
+   }  
+  
+   ~LibraryLoader()
+   {
+   }  
+
+public:
+    template <typename T,typename D>
+    static D* to(T* t)
+    {
+        return dynamic_cast<D*>(t);
+    }
+
+
+    template <typename T>
+    static T* load(std::string szPlugin)
+    {
+       QString szAddin = szPlugin.c_str();
+       return load<T>(szAddin);
+    }
+
+    template <typename T>
+    static T* load(QString szPlugin)
+    {
+#ifndef _Andriod
+       QFile file;
+       if( file.exists(szPlugin.toStdString().c_str()) == false )
+       //if( FileManager::ifFileExist(szPlugin.toStdString()) == false )
+       {
+//         qCritical() << LOG_HEADER << szPlugin << " not exist.";
+          return NULL;
+       }
+#endif
+        T* pPlugin = NULL;
+
+        QLibrary* pLoader = new QLibrary(szPlugin);
+        if( pLoader->load() )
+        {
+           typedef T* (*funInterface)();
+           funInterface fun = (funInterface)pLoader->resolve("instance");
+           if( fun != nullptr )
+           {
+               pPlugin = fun();
+           }
+        }
+
+        if( pPlugin != nullptr)
+        {
+            pPlugin->setLoader(pLoader);
+        }
+        else
+        {
+			QString szError = pLoader->errorString();
+//			qCritical() << LOG_HEADER << " load " << szPlugin.toStdString().c_str() << " : " << szError.toStdString().c_str();
+            pLoader->unload();
+            pLoader->deleteLater();
+        }
+
+        return pPlugin;
+    }
+
+    // 释放对象
+    // 卸载插件
+    template <typename T>
+    static bool unload(T* pPlugin )
+    {
+        if( pPlugin == nullptr )
+        {
+            return false;
+        }
+
+        QLibrary* pLoader = pPlugin->getLoader();
+        if( pLoader != nullptr )
+        {
+            pLoader->unload();
+        }
+
+        return true;
+    }
+};  

+ 15 - 0
include/Publisher.h

@@ -0,0 +1,15 @@
+#pragma once
+#include "RunnableModule.h"
+#include <QtCore/QThread>
+#include "DataConsumer.h"
+#include "Define.h"
+
+class Publisher : public RunnableModule, public QThread{
+public:
+    virtual void shares(SharedData & share) = 0;
+
+
+};
+
+
+

+ 12 - 0
include/RunnableModule.h

@@ -0,0 +1,12 @@
+#pragma once
+#include "BaseModule.h"
+#include <QtCore/QObject>
+
+class RunnableModule: public BaseModule
+{
+public:
+ //   virtual void onData(QObject data) = 0;
+    virtual void Run() = 0;
+};
+
+