Browse Source

V0.1.8
1、添加了tCamerInfo表的操作,插入表格函数和更新表格函数会闪退

Apple 7 months ago
parent
commit
e349ac1803

+ 82 - 0
SecurePlayAuxServer/GlobalInfo.h

@@ -34,6 +34,18 @@ struct AlgorithmInfo
         }
         return *this;
     }
+    /* 对比是否相等 */
+    bool operator==(const AlgorithmInfo& other) const {
+        if(ActionID != other.ActionID)
+        {
+            return false;
+        }
+        if(ActionName != other.ActionName || ActionTaskID != other.ActionTaskID)
+        {
+            return false;
+        }
+        return true;
+    }
 };
 
 
@@ -44,11 +56,81 @@ struct DeviceInfo
     int DeviceID;               /* 设备ID */
     int DevicePort;             /* 设备端口 */
     std::string DeviceName;     /* 设备名称 */
+    std::string DeviceIP;       /* 设备IP */
     std::string DeviceSerial;   /* 设备序列号 */
     std::string DeviceType;     /* 设备类型 */
     std::string UserAccount;    /* 用户账号 */
     std::string UserPassword;   /* 用户密码 */
     std::vector<AlgorithmInfo> vecAlgorithmInfo; /* 算法信息 */
+
+    DeviceInfo() = default;
+    DeviceInfo(const DeviceInfo& other)
+        : PKID(other.PKID), 
+        DeviceID(other.DeviceID), 
+        DevicePort(other.DevicePort), 
+        DeviceName(other.DeviceName), 
+        DeviceSerial(other.DeviceSerial), 
+        DeviceType(other.DeviceType), 
+        DeviceIP(other.DeviceIP),
+        UserAccount(other.UserAccount), 
+        UserPassword(other.UserPassword), 
+        vecAlgorithmInfo(other.vecAlgorithmInfo) {}
+    DeviceInfo& operator=(const DeviceInfo& other) {
+        if (this != &other) {
+            DeviceID = other.DeviceID;
+            DevicePort = other.DevicePort;
+            DeviceName = other.DeviceName;
+            DeviceSerial = other.DeviceSerial;
+            DeviceType = other.DeviceType;
+            DeviceIP = other.DeviceIP;
+            UserAccount = other.UserAccount;
+            UserPassword = other.UserPassword;
+            vecAlgorithmInfo = other.vecAlgorithmInfo;
+        }
+        return *this;
+    }
+    /* 对比是否相等 */
+    bool operator==(const DeviceInfo& other) const {
+        if(DeviceID != other.DeviceID)
+        {
+            return false;
+        }
+        if(DeviceName != other.DeviceName || DeviceIP != other.DeviceIP)
+        {
+            return false;
+        }
+        if(DeviceSerial != other.DeviceSerial || DeviceType != other.DeviceType)
+        {
+            return false;
+        }
+        if(DevicePort != other.DevicePort || UserAccount != other.UserAccount || UserPassword != other.UserPassword)
+        {
+            return false;
+        }
+        /* 对比算法列表 */
+        if(vecAlgorithmInfo.size() != other.vecAlgorithmInfo.size())
+        {
+            return false;
+        }else
+        {
+            bool isEquality = true;
+            for(size_t i = 0; i < vecAlgorithmInfo.size(); i++)
+            {
+                if(vecAlgorithmInfo[i] == other.vecAlgorithmInfo[i])
+                {
+                    continue;
+                }else {
+                    isEquality = false;
+                    break;
+                }
+            }
+            if(!isEquality)
+            {
+                return false;
+            }
+        }
+        return true;
+    }
 };
 
 

+ 134 - 21
SecurePlayAuxServer/SPAServer.cpp

@@ -49,10 +49,10 @@ void SPAServer::fromSuperBrainThread()
     /* 创建变量 */
     std::vector<AlgorithmInfo> vecAlgInfo;
     std::vector<DeviceInfo> vecDevInfo;
-    std::vector<AlgorithmInfo> vecAlgUpdate;
-    std::vector<AlgorithmInfo> vecAlgDelete;
+    
     /* 从EQM数据库中读取到数据信息到内存中,后续对比需要 */
     m_toEQMDataBase.getAlgorithmInfo(m_vecEqmAlgInfo);
+    m_toEQMDataBase.getDeviceInfo(m_vecEqmDevInfo);
     /* 获取一次token,后续失效了再获取 */
     m_fromSuperBrain.getToken();
     while (m_threadRunning)
@@ -61,35 +61,147 @@ void SPAServer::fromSuperBrainThread()
         m_fromSuperBrain.getTaskTypeList(vecAlgInfo);
         m_fromSuperBrain.getDeviceList(vecDevInfo);
 
-        /* 对比数据库表格信息,这里只有插入和删除,没有更新 */
-        compareAlgorithmInfo(vecAlgInfo, vecAlgUpdate, vecAlgDelete);
-        /* 更新数据库,先删除,再写入刷新 */
-        if(vecAlgDelete.size() > 0)
+        /* 处理算法信息 */
+        bool algIsUpdate = processAlgorithmInfo(vecAlgInfo);
+        /* 处理设备信息 */
+        bool devIsUpdate = processDeviceInfo(vecDevInfo);
+        
+        /* 根据返回值刷新本地数据缓存 */
+        if(algIsUpdate)
         {
-            SPDLOG_LOGGER_DEBUG(m_logger, "删除算法信息");
-            m_toEQMDataBase.deleteAlgorithmInfo(vecAlgDelete);
+            m_toEQMDataBase.getAlgorithmInfo(m_vecEqmAlgInfo);
         }
-        /* 更新一次表格数据缓存 */
-        m_toEQMDataBase.getAlgorithmInfo(m_vecEqmAlgInfo);
-        if(vecAlgUpdate.size() > 0)
+        if(devIsUpdate)
         {
-            SPDLOG_LOGGER_DEBUG(m_logger, "写入算法信息");
-            m_toEQMDataBase.writeAlgorithmInfo(vecAlgUpdate, m_vecEqmAlgInfo);
+            m_toEQMDataBase.getDeviceInfo(m_vecEqmDevInfo);
         }
 
+        vecAlgInfo.clear();
+        vecDevInfo.clear();
+        std::this_thread::sleep_for(std::chrono::seconds(10));
+    }
+}
+
+/* 处理算法信息,返回值为true,说明有改变,需要重新读取 */
+bool SPAServer::processAlgorithmInfo(std::vector<AlgorithmInfo> vecNewAlgInfo)
+{
+    std::vector<AlgorithmInfo> vecAlgUpdate;
+    std::vector<AlgorithmInfo> vecAlgDelete;
+
+    /* 对比数据库表格信息,这里只有插入和删除,没有更新 */
+    compareAlgorithmInfo(vecNewAlgInfo, vecAlgUpdate, vecAlgDelete);
         
-        /* 再读取数据库,刷新本地存储 */
-        if(vecAlgUpdate.size() > 0 || vecAlgDelete.size() > 0)
+    /* 更新数据库,先删除,再写入刷新 */
+    bool isUpdate = false;
+    if(vecAlgDelete.size() > 0)
+    {
+        SPDLOG_LOGGER_DEBUG(m_logger, "删除算法信息");
+        m_toEQMDataBase.deleteAlgorithmInfo(vecAlgDelete);
+        isUpdate = true;
+    }
+    if(vecAlgUpdate.size() > 0)
+    {
+        SPDLOG_LOGGER_DEBUG(m_logger, "写入算法信息");
+        m_toEQMDataBase.writeAlgorithmInfo(vecAlgUpdate);
+        isUpdate = true;
+    }
+
+    return isUpdate;
+}
+
+
+/**
+ * @brief 处理设备信息
+ * 
+ * @param vecNewDevInfo 传入新获取到的值
+ * @return true 需要重新读取数据库,获取新的数据
+ * @return false 无需读取数据库
+ */
+bool SPAServer::processDeviceInfo(std::vector<DeviceInfo> vecNewDevInfo)
+{
+    std::vector<DeviceInfo> vecDevInsert;
+    std::vector<DeviceInfo> vecDevUpdate;
+    std::vector<DeviceInfo> vecDevDelete;
+
+    /* 如果本地缓存没有数据,那么就全部插入 */
+    if(m_vecEqmDevInfo.size() > 0)
+    {
+        for(const auto& it : vecNewDevInfo)
         {
-            m_toEQMDataBase.getAlgorithmInfo(m_vecEqmAlgInfo);
+            bool isExist = false;
+            for(const auto& it0 : m_vecEqmDevInfo)
+            {
+                if(it.DeviceID == it0.DeviceID)
+                {
+                    isExist = true;
+                    /* 对比其他项是否相等,不相等就更新 */
+                    if(it == it0)
+                    {
+                        continue;
+                    }else {
+                        vecDevUpdate.push_back(it);
+                    }
+                    break;
+                }
+            }
+            if(!isExist)
+            {
+                vecDevInsert.push_back(it);
+            }
         }
+    }else {
+        vecDevInsert = vecNewDevInfo;
+    }
 
-        vecAlgInfo.clear();
-        vecDevInfo.clear();
-        vecAlgUpdate.clear();
-        vecAlgDelete.clear();
-        std::this_thread::sleep_for(std::chrono::seconds(10));
+    /* 获取删除列表 */
+    if(vecNewDevInfo.size() > 0)
+    {
+        bool isExist = false;
+        for(const auto& it : m_vecEqmDevInfo)
+        {
+            isExist = false;
+            for(const auto& it0 : vecNewDevInfo)
+            {
+                if(it.DeviceID == it0.DeviceID)
+                {
+                    isExist = true;
+                    break;
+                }
+            }
+            if(!isExist)
+            {
+                vecDevDelete.push_back(it);
+            }
+        }
+    }else {
+        vecDevDelete = m_vecEqmDevInfo;
     }
+
+    bool isUpdate = false;
+    /* 先删除多余的数据 */
+    if(vecDevDelete.size() > 0)
+    {
+        SPDLOG_LOGGER_DEBUG(m_logger, "删除设备信息");
+        m_toEQMDataBase.deleteDeviceInfo(vecDevDelete);
+        isUpdate = true;
+    }
+    /* 更新数据 */
+    if(vecDevUpdate.size() > 0)
+    {
+        SPDLOG_LOGGER_DEBUG(m_logger, "更新设备信息");
+        m_toEQMDataBase.updateDeviceInfo(vecDevUpdate);
+        isUpdate = true;
+    }
+    /* 插入数据 */
+    if(vecDevInsert.size() > 0)
+    {
+        SPDLOG_LOGGER_DEBUG(m_logger, "插入设备信息");
+        m_toEQMDataBase.insertDeviceInfo(vecDevInsert);
+        isUpdate = true;
+    }
+
+
+    return isUpdate;
 }
 
 /* 对比现有的数据和新获取到的数据,取出要删除和添加的数据 */
@@ -145,3 +257,4 @@ void SPAServer::compareAlgorithmInfo(const std::vector<AlgorithmInfo>& vecNewInf
 
 
 
+

+ 6 - 1
SecurePlayAuxServer/SPAServer.h

@@ -21,8 +21,13 @@ public:
 private:
     /* 从基础平台获取算法信息和设备信息的线程函数 */
     void fromSuperBrainThread();
-    /* 对比现有的数据和新获取到的数据,取出要删除和添加的数据 */
+    /* 处理算法信息,返回值为true,说明有改变,需要重新读取 */
+    bool processAlgorithmInfo(std::vector<AlgorithmInfo> vecNewAlgInfo);
+    /* 处理设备信息,传入新获取到的值,返回true需要更新本地数据缓存 */
+    bool processDeviceInfo(std::vector<DeviceInfo> vecNewDevInfo);
+    /* 对比算法信息现有的数据和新获取到的数据,取出要删除和添加的数据 */
     void compareAlgorithmInfo(const std::vector<AlgorithmInfo>& vecNewInfo, std::vector<AlgorithmInfo>& vecAlgUpdate, std::vector<AlgorithmInfo>& vecAlgDelete);
+    
 
 
 private:

+ 3 - 3
SecurePlayAuxServer/communication/FromSuperBrain.cpp

@@ -127,9 +127,9 @@ bool FromSuperBrain::getDeviceList(std::vector<DeviceInfo>& vecInfo)
     for(const auto& it : json1)
     {
         info.DeviceID = it["deviceId"].get<int>();
-        info.DeviceName = it["deviceName"].get<std::string>();
-        info.DeviceSerial = it["deviceSerial"].get<std::string>();
-        info.DeviceType = it["deviceType"].get<std::string>();
+        info.DeviceName = it["deviceName"].is_null() ? "" : it["deviceName"].get<std::string>();
+        info.DeviceSerial = it["deviceSerial"].is_null() ? "" : it["deviceSerial"].get<std::string>();
+        info.DeviceType = it["deviceType"].is_null() ? "" : it["deviceType"].get<std::string>();
         /* 这三个可能是null */
         info.DevicePort = it["port"].is_null() ? 0 : it["port"].get<int>();
         info.UserAccount = it["userAccount"].is_null() ? "" : it["userAccount"].get<std::string>();

+ 201 - 8
SecurePlayAuxServer/communication/ToEQMDataBase.cpp

@@ -67,11 +67,11 @@ bool ToEQMDataBase::initWebApi(const QString& url, const QString& serverIP, cons
  * @brief 写入算法信息,写入tAction表
  * 
  * @param vecInfo 要写入的表格数据
- * @param vecNowInfo 现有的标配个数据,主要是为了重复利用已经删除的自增主键而传入的
+ * @param vecNowInfo 现有的表格数据,主要是为了重复利用已经删除的自增主键而传入的
  * @return true 
  * @return false 
  */
-bool ToEQMDataBase::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo, std::vector<AlgorithmInfo>& vecNowInfo)
+bool ToEQMDataBase::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
 {
     if(m_httpApi == nullptr)
     {
@@ -79,12 +79,21 @@ bool ToEQMDataBase::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo, std:
         return false;
     }
     /* 取出可用的自增主键,从0开始计算 */
-    std::list<int> listPKID;
-    for(const auto& it : vecNowInfo)
-    {
-        listPKID.push_back(it.PKID);
-    }
-
+    // std::list<int> listPKID;
+    // int nPKID = 0;
+    // for(const auto& it : vecNowInfo)
+    // {
+    //     while(it.ActionTaskID == nPKID)
+    //     {
+    //         if(it.PKID != nPKID)
+    //         {
+    //             listPKID.push_back(nPKID);
+    //             break;
+    //         }
+    //         nPKID ++;
+    //     }
+    // }
+    /* 循环写入数据 */
     for(const auto& it : vecInfo)
     {
         QString retStr;
@@ -182,3 +191,187 @@ bool ToEQMDataBase::getAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
     return true;
 }
 
+
+/* 插入设备信息 */
+bool ToEQMDataBase::insertDeviceInfo(std::vector<DeviceInfo>& vecInfo)
+{
+    if(m_httpApi == nullptr)
+    {
+        SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
+        return false;
+    }
+    if(vecInfo.empty())
+    {
+        return false;
+    }
+    bool isSuccess = true;
+    for(const auto& it : vecInfo)
+    {
+        nJson json0;
+        json0["opName"] = "SPSS_InsertToCamerInfo";
+        nJson json1;
+        json1["camerID"] = it.DeviceID;
+        json1["camerName"] = it.DeviceName.empty() ? nullptr : it.DeviceName;
+        json1["camerIP"] = it.DeviceIP.empty() ? nullptr : it.DeviceIP;
+        json1["camerPort"] = it.DevicePort;
+        json1["camerUser"] = it.UserAccount.empty() ? nullptr : it.UserAccount;
+        json1["camerPwd"] = it.UserPassword.empty() ? nullptr : it.UserPassword;
+        json1["camerType"] = it.DeviceType.empty() ? nullptr : it.DeviceType;
+        json1["camerSerial"] = it.DeviceSerial.empty() ? nullptr : it.DeviceSerial;
+        json1["camerChannel"] = nullptr;
+        json1["camerUrl"] = nullptr;
+
+        json0["paramList"] = json1;
+        QString strCmd = QString::fromStdString(json0.dump());
+        QString strRet;
+        auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
+        if(ret < 0)
+        {
+            SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
+            isSuccess = false;
+        }
+        SPDLOG_LOGGER_DEBUG(m_logger,"插入数据 {} 到 CamerInfo 成功!", it.DeviceID);
+    }
+
+    return isSuccess;
+}
+
+
+/* 更新设备信息 */
+bool ToEQMDataBase::updateDeviceInfo(std::vector<DeviceInfo>& vecUpdateInfo)
+{
+    if(m_httpApi == nullptr)
+    {
+        SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
+        return false;
+    }
+    if(vecUpdateInfo.empty())
+    {
+        return false;
+    }
+    bool isSuccess = true;
+
+    for(const auto& it : vecUpdateInfo)
+    {
+        nJson json0;
+        json0["opName"] = "SPSS_UpdateToCamerInfo";
+        nJson json1;
+        json1["camerID"] = it.DeviceID;
+        json1["camerName"] = it.DeviceName.empty() ? nullptr : it.DeviceName;
+        json1["camerIP"] = it.DeviceIP.empty() ? nullptr : it.DeviceIP;
+        json1["camerPort"] = it.DevicePort;
+        json1["camerUser"] = it.UserAccount.empty() ? nullptr : it.UserAccount;
+        json1["camerPwd"] = it.UserPassword.empty() ? nullptr : it.UserPassword;
+        json1["camerType"] = it.DeviceType.empty() ? nullptr : it.DeviceType;
+        json1["camerSerial"] = it.DeviceSerial.empty() ? nullptr : it.DeviceSerial;
+        json1["camerChannel"] = nullptr;
+        json1["camerUrl"] = nullptr;
+
+        json0["paramList"] = json1;
+        QString strCmd = QString::fromStdString(json0.dump());
+        QString strRet;
+        auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
+        if(ret < 0)
+        {
+            SPDLOG_LOGGER_DEBUG(m_logger,"更新设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
+            isSuccess = false;
+        }
+        SPDLOG_LOGGER_DEBUG(m_logger,"更新数据 {} 到 CamerInfo 成功!", it.DeviceID);
+    }
+
+
+
+    return isSuccess;
+}
+
+
+/* 删除设备信息 */
+bool ToEQMDataBase::deleteDeviceInfo(std::vector<DeviceInfo>& vecDeleteInfo)
+{
+    if(m_httpApi == nullptr)
+    {
+        SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
+        return false;
+    }
+    if(vecDeleteInfo.empty())
+    {
+        return false;
+    }
+    bool isSuccess = true;
+
+    for(const auto& it : vecDeleteInfo)
+    {
+        nJson json0;
+        json0["opName"] = "SPSS_DeleteFromCamerInfo";
+        nJson json1;
+        json1["camerID"] = it.DeviceID;
+        json0["paramList"] = json1;
+        QString strCmd = QString::fromStdString(json0.dump());
+        QString strRet;
+        auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
+        if(ret < 0)
+        {
+            SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
+            isSuccess = false;
+        }
+        SPDLOG_LOGGER_DEBUG(m_logger,"删除数据 {} 到 CamerInfo 成功!", it.DeviceID);
+    }
+
+    return isSuccess;
+}
+
+/* 从EQM获取CamerInfo信息 */
+bool ToEQMDataBase::getDeviceInfo(std::vector<DeviceInfo>& vecInfo)
+{
+    if(m_httpApi == nullptr)
+    {
+        SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
+        return false;
+    }
+
+    vecInfo.clear();
+    nJson json0;
+    json0["opName"] = "SPSS_SelectFromCamerInfo";
+    QString strCmd = QString::fromStdString(json0.dump());
+    QString strRet;
+    auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
+    if(ret < 0)
+    {
+        SPDLOG_LOGGER_DEBUG(m_logger,"获取CamerInfo失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
+        return false;
+    }
+
+    /* 解析信息 */
+    nJson json1 = nJson::parse(strRet.toStdString());
+    int retCode = json1["code"].get<int>();
+    if(retCode != 0)
+    {
+        SPDLOG_LOGGER_ERROR(m_logger,"获取CamerInfo失败");
+        return false;
+    }
+    nJson result = json1["result"];
+    if(result.empty())
+    {
+        return false;
+    }
+    for(const auto& it : result)
+    {
+        // SPDLOG_LOGGER_DEBUG(m_logger,"camerID:{}",it["camerId"].get<int>());
+        DeviceInfo info;
+        info.DeviceID = it["camerId"].get<int>();
+        info.DeviceName = it["camerName"].is_null() ? "" : it["camerName"].get<std::string>();
+        info.DeviceIP = it["camerIp"].is_null() ? "" : it["camerIp"].get<std::string>();
+        info.DevicePort = it["camerPort"].is_null() ? 0 : it["camerPort"].get<int>();
+        info.UserAccount = it["camerUsr"].is_null() ? "" : it["camerUsr"].get<std::string>();
+        info.UserPassword = it["camerPwd"].is_null() ? "" : it["camerPwd"].get<std::string>();
+        info.DeviceType = it["camerType"].is_null() ? "" : it["camerType"].get<std::string>();
+        info.DeviceSerial = it["camerSerial"].is_null() ? "" : it["camerSerial"].get<std::string>();
+
+        vecInfo.push_back(info);
+    }
+
+
+    return true;
+}
+
+

+ 9 - 1
SecurePlayAuxServer/communication/ToEQMDataBase.h

@@ -14,11 +14,19 @@ public:
     /* 初始化WebAPI */
     bool initWebApi(const QString& url,const QString& serverIP,const QString& serID);
     /* 写入算法信息,写入tAction表 */
-    bool writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo, std::vector<AlgorithmInfo>& vecNowInfo);
+    bool writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo);
     /* 删除算法信息 */
     bool deleteAlgorithmInfo(std::vector<AlgorithmInfo>& vecDeleteInfo);
     /* 获取tAction数据 */
     bool getAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo);
+    /* 插入设备信息 */
+    bool insertDeviceInfo(std::vector<DeviceInfo>& vecInfo);
+    /* 更新设备信息 */
+    bool updateDeviceInfo(std::vector<DeviceInfo>& vecUpdateInfo);
+    /* 删除设备信息 */
+    bool deleteDeviceInfo(std::vector<DeviceInfo>& vecDeleteInfo);
+    /* 从EQM获取CamerInfo信息 */
+    bool getDeviceInfo(std::vector<DeviceInfo>& vecInfo);
 
 private:
     std::shared_ptr<spdlog::logger> m_logger = nullptr;

+ 0 - 1
SecurePlayAuxServer/main.cpp

@@ -1,5 +1,4 @@
 
-#include <iostream>
 
 #include "spdlog/spdlog.h"
 #include "SPAServer.h"

+ 2 - 1
json.json

@@ -357,4 +357,5 @@
             "actionName": "盯屏"
         }
     ]
-}
+}
+