1. 添加 SystemSV 数据
2. 更新指定 ID 的 SystemSV 数据
3. 更新所有 SystemSV 数据
4. 删除指定 ID 的 SystemSV 数据
5.删除所有 SystemSV 数据
6. 添加 EqpSV 数据
7. 添加辅助函数判断VID是否重复和判断名称是否重复
已修改2个文件
445 ■■■■■ 文件已修改
SourceCode/Bond/Servo/SECSRuntimeManager.cpp 286 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/SECSRuntimeManager.h 159 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/Servo/SECSRuntimeManager.cpp
@@ -39,6 +39,64 @@
    return 0;
}
// 判断VID是否重复
bool SECSRuntimeManager::isIDDuplicate(int nID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return false;
    }
    // 定义要检查的表
    std::vector<std::string> tables = { "SystemSV", "EqpSV", "SystemDV", "EqpDV", "SystemEC", "EqpEC" };
    // 遍历表,检查是否有重复的 ID
    for (const auto& table : tables) {
        // 创建 SQL 查询
        std::string checkSQL = "SELECT COUNT(*) FROM " + table + " WHERE ID = " + std::to_string(nID) + ";";
        // 执行查询
        auto results = m_pDB->fetchResults(checkSQL);
        int count = (!results.empty() && !results[0].empty()) ? std::stoi(results[0][0]) : 0;
        // 如果找到了重复的 ID,则返回 true
        if (count > 0) {
            return true;
        }
    }
    // 如果没有重复,返回 false
    return false;
}
// 判断名称是否重复
bool SECSRuntimeManager::isNameDuplicate(const std::string& sName) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return false;
    }
    // 定义要检查的表
    std::vector<std::string> tables = { "SystemSV", "EqpSV", "SystemDV", "EqpDV", "SystemEC", "EqpEC" };
    // 遍历表,检查是否有重复的 Name
    for (const auto& table : tables) {
        // 创建 SQL 查询
        std::string checkSQL = "SELECT COUNT(*) FROM " + table + " WHERE Name = '" + sName + "';";
        // 执行查询
        auto results = m_pDB->fetchResults(checkSQL);
        int count = (!results.empty() && !results[0].empty()) ? std::stoi(results[0][0]) : 0;
        // 如果找到了重复的 Name,则返回 true
        if (count > 0) {
            return true;
        }
    }
    // 如果没有重复,返回 false
    return false;
}
// 设置数据库连接
void SECSRuntimeManager::setDatabase(BL::Database* db) {
    std::lock_guard<std::mutex> lock(m_mutex);
@@ -159,6 +217,201 @@
    }
}
// 添加 SystemSV 数据
int SECSRuntimeManager::addSystemSV(int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    if (isIDDuplicate(nID)) {
        return 2;
    }
    if (isNameDuplicate(sName)) {
        return 3;
    }
    // 如果 Unit 是 "NULL" 字符串或者为空,则插入 NULL 值
    std::string insertSQL = "INSERT INTO SystemSV (ID, Name, DataType, Length, Unit, Remark, SystemID) VALUES ("
        + std::to_string(nID) + ", '"
        + sName + "', '"
        + sDataType + "', "
        + (nLength > 0 ? std::to_string(nLength) : "NULL") + ", "
        + ((sUnit == "NULL" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'") + ", '"
        + sRemark + "', "
        + std::to_string(nSystemID) + ");";
    if (!m_pDB->executeQuery(insertSQL)) {
        return 4;
    }
    return 0;
}
// 更新指定 ID 的 SystemSV 数据
int SECSRuntimeManager::updateIDSystemSV(int nID, int sNewID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 检查是否存在该 ID
    if (!isIDDuplicate(nID)) {
        return 2;
    }
    if (isIDDuplicate(sNewID)) {
        return 3;
    }
    // 构建更新的 SQL 语句
    std::string updateSQL = "UPDATE SystemSV SET ID = " + std::to_string(sNewID) + " WHERE ID = " + std::to_string(nID) + ";";
    if (!m_pDB->executeQuery(updateSQL)) {
        return 4;
    }
    return 0;
}
// 更新所有 SystemSV 数据
int SECSRuntimeManager::updateAllSystemSV(int nID, int sNewID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 检查是否存在该 ID
    if (!isIDDuplicate(nID)) {
        return 2;
    }
    // 检查新的 ID 是否已存在,如果已存在,则返回错误代码 3。
    if (isIDDuplicate(sNewID)) {
        return 3;
    }
    // 构建更新的 SQL 语句
    std::string updateSQL = "UPDATE SystemSV SET ";
    bool firstField = true;
    // 如果新的 ID 被提供,更新 ID
    if (sNewID > 0) {
        if (!firstField) {
            updateSQL += ", ";
        }
        updateSQL += "ID = " + std::to_string(sNewID);
        firstField = false;
    }
    // 更新 Name
    if (!sName.empty()) {
        if (!firstField) {
            updateSQL += ", ";
        }
        updateSQL += "Name = '" + sName + "'";
        firstField = false;
    }
    // 更新 DataType
    if (!sDataType.empty()) {
        if (!firstField) {
            updateSQL += ", ";
        }
        updateSQL += "DataType = '" + sDataType + "'";
        firstField = false;
    }
    // 更新 Length
    if (nLength > 0) {
        if (!firstField) {
            updateSQL += ", ";
        }
        updateSQL += "Length = " + std::to_string(nLength);
        firstField = false;
    }
    // 更新 Unit
    if (sUnit != "NULL" && !sUnit.empty()) {
        if (!firstField) {
            updateSQL += ", ";
        }
        updateSQL += "Unit = '" + sUnit + "'";
        firstField = false;
    }
    else if (sUnit == "NULL") {
        if (!firstField) {
            updateSQL += ", ";
        }
        updateSQL += "Unit = NULL";
        firstField = false;
    }
    // 更新 Remark
    if (!sRemark.empty()) {
        if (!firstField) {
            updateSQL += ", ";
        }
        updateSQL += "Remark = '" + sRemark + "'";
        firstField = false;
    }
    // 更新 SystemID
    if (nSystemID > 0) {
        if (!firstField) {
            updateSQL += ", ";
        }
        updateSQL += "SystemID = " + std::to_string(nSystemID);
    }
    // 添加 WHERE 子句来指定更新哪个记录
    updateSQL += " WHERE ID = " + std::to_string(nID) + ";";
    // 执行更新操作
    if (!m_pDB->executeQuery(updateSQL)) {
        return 4;
    }
    return 0;
}
// 删除指定 ID 的 SystemSV 数据
int SECSRuntimeManager::deleteSystemSVByID(int nID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 检查是否存在该 ID
    if (!isIDDuplicate(nID)) {
        return 2;
    }
    // 构建删除的 SQL 语句
    std::string deleteSQL = "DELETE FROM SystemSV WHERE ID = " + std::to_string(nID) + ";";
    if (!m_pDB->executeQuery(deleteSQL)) {
        return 3;
    }
    return 0;
}
int SECSRuntimeManager::deleteAllSystemSV() {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 构建删除所有数据的 SQL 语句
    std::string deleteSQL = "DELETE FROM SystemSV;";
    if (!m_pDB->executeQuery(deleteSQL)) {
        return 2;
    }
    return 0; // 删除成功,返回 0 表示操作成功完成。
}
// 初始化 EqpSV 表
void SECSRuntimeManager::initEqpSVTable() {
    std::lock_guard<std::mutex> lock(m_mutex);
@@ -182,6 +435,39 @@
    }
}
// 添加 EqpSV 数据
int SECSRuntimeManager::addEqpSV(int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSeqNo) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    if (isIDDuplicate(nID)) {
        return 2;
    }
    if (isNameDuplicate(sName)) {
        return 3;
    }
    // 构建 SQL 插入语句,插入数据到 EqpSV 表中。
    std::string insertSQL = "INSERT INTO EqpSV (ID, Name, DataType, Length, Unit, Remark, SeqNo) VALUES ("
        + std::to_string(nID) + ", '"
        + sName + "', '"
        + sDataType + "', "
        + ((nLength <= 0) ? "NULL" : std::to_string(nLength))+", "
        + ((sUnit == "NULL" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'")+", '"
        + sRemark + "', "
        + std::to_string(nSeqNo) + ");";
    // 执行插入操作,若失败则抛出异常。
    if (!m_pDB->executeQuery(insertSQL)) {
        return 4;
    }
    return 0; // 插入成功,返回 0 表示操作成功完成。
}
// 初始化 SystemDV 表
void SECSRuntimeManager::initSystemDVTable() {
    std::lock_guard<std::mutex> lock(m_mutex);
SourceCode/Bond/Servo/SECSRuntimeManager.h
@@ -1,4 +1,4 @@
#ifndef SECS_RUNTIME_MANAGER_H
#ifndef SECS_RUNTIME_MANAGER_H
#define SECS_RUNTIME_MANAGER_H
#include <string>
@@ -9,80 +9,191 @@
class SECSRuntimeManager {
public:
    /**
     * 获取单例实例
     * @return SECSRuntimeManager实例的引用
     * 获取单例实例
     * @return SECSRuntimeManager实例的引用
     */
    static SECSRuntimeManager& getInstance();
    /**
     * 设置数据库连接
     * @param db 数据库连接的指针
     * 设置数据库连接
     * @param db 数据库连接的指针
     */
    void setDatabase(BL::Database* db);
    /**
     * 初始化SECS运行设置管理库
     * @return 成功返回true,失败返回false
     * 初始化SECS运行设置管理库
     * @return 成功返回true,失败返回false
     */
    bool initRuntimeSetting();
    /**
    * 销毁SECS运行设置管理库
    * 销毁SECS运行设置管理库
    */
    void termRuntimeSetting();
    /**
    * 初始化SystemSV表
    * 初始化SystemSV表
    */
    void initSystemSVTable();
    /**
    * 初始化Eqp表
     * 添加 SystemSV 数据
     * @param nID: 需要添加的 SystemSV 的 ID,必须是唯一的。
     * @param sName: 需要添加的 SystemSV 的名称,必须是唯一的。
     * @param sDataType: 数据类型,表示该系统值的类型,例如 "ASCII"、"UINT_1" 等。
     * @param nLength: 系统值的数据长度,通常为一个正整数,用于表示该数据的长度。
     * @param sUnit: 系统值的单位。如果为空或者为 "NULL",则插入数据库中的 NULL 值。
     * @param sRemark: 备注信息,描述该系统值的其他信息,可用于说明该字段的用途或特性。
     * @param nSystemID: 该数据所属的系统 ID,用于与其他表进行关联。
     * @return 1: 数据库未连接。
     * @return 2: ID 重复,无法插入数据。
     * @return 3: Name 重复,无法插入数据。
     * @return 4: 插入数据失败。
     * @return 0: 插入成功,数据已添加到 SystemSV 表中。
     *
     * 此函数用于将一条新的数据插入到 SystemSV 表中。它首先会检查传入的 `ID` 和 `Name` 是否已存在于表中,
     * 如果存在则返回相应的错误代码。如果 `Unit` 参数为 "NULL" 或者为空,函数会将其转换为数据库中的 NULL 值。
     * 然后,构造一个 SQL 插入语句并执行插入操作。如果插入失败,则抛出异常。
     * 如果一切顺利,返回 0 表示数据成功插入。
     */
    int addSystemSV(int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID);
    /**
     * 更新指定 ID 的 SystemSV 数据
     * @param nID: 需要更新的 SystemSV 的当前 ID。
     * @param sNewID: 要更新为的新 ID。
     * @return 1: 数据库未连接。
     * @return 2: 未找到指定的 ID。
     * @return 3: 新的 ID 已经存在,无法更新。
     * @return 4: 更新操作失败。
     * @return 0: 更新成功。
     *
     * 此函数用于更新 `SystemSV` 表中指定 `nID` 的记录,将其 `ID` 字段更新为 `sNewID`。
     * 在执行更新前,函数会检查:
     * 1. 当前的 `nID` 是否存在于表中。
     * 2. 新的 `sNewID` 是否已经存在于表中,如果存在,则无法进行更新。
     *
     * 如果 `nID` 不存在,则返回错误代码 2。如果 `sNewID` 已经存在,则返回错误代码 3。
     * 如果数据库更新失败,则返回错误代码 4。成功时,返回 0 表示操作成功。
     */
    int updateIDSystemSV(int nID, int sNewID);
    /**
     * 更新所有 SystemSV 数据
     * @param nID: 需要更新的 SystemSV 的当前 ID。
     * @param sNewID: 要更新为的新 ID,如果为空或为 -1,则不更新 ID。
     * @param sName: 新的名称,如果为空,则不更新。
     * @param sDataType: 新的数据类型,如果为空,则不更新。
     * @param nLength: 新的数据长度,如果为负值或零,则不更新。
     * @param sUnit: 新的单位,如果为空或 "NULL",则不更新。
     * @param sRemark: 新的备注,如果为空,则不更新。
     * @param nSystemID: 新的系统 ID,如果为负值,则不更新。
     * @return 1: 数据库未连接。
     * @return 2: 没有找到该 ID 对应的记录。
     * @return 3: 新的 ID 已经存在,无法更新。
     * @return 4: 更新操作失败。
     * @return 0: 更新成功。
     *
     * 此函数用于更新指定 `ID` 的 `SystemSV` 数据。如果某个字段为空,则跳过该字段的更新。
     * 如果给定的 `ID` 不存在,则返回错误代码 2。如果新的 `ID` 已经存在,则返回错误代码 3。
     *
     * 如果字段为空,跳过该字段的更新。
     */
    int updateAllSystemSV(int nID, int sNewID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID);
    /**
     * 删除指定 ID 的 SystemSV 数据
     * @param nID: 需要删除的 SystemSV 的 ID。
     * @return 1: 数据库未连接。
     * @return 2: 未找到指定的 ID 对应的记录。
     * @return 3: 删除操作失败。
     * @return 0: 删除成功。
     *
     * 此函数用于删除 `SystemSV` 表中指定 `nID` 的记录。如果给定的 `nID` 不存在,则返回错误代码 2。
     * 删除操作成功后,返回 0 表示删除成功。
     */
    int deleteSystemSVByID(int nID);
    /**
     * 删除所有 SystemSV 数据
     * @return 1: 数据库未连接。
     * @return 2: 删除操作失败。
     * @return 0: 删除成功。
     *
     * 此函数用于删除 `SystemSV` 表中的所有记录。如果数据库未连接,则返回错误代码 1。
     * 如果删除操作失败,则返回错误代码 2。删除成功后,返回 0 表示删除成功。
     */
    int deleteAllSystemSV();
    /**
    * 初始化Eqp表
    */
    void initEqpSVTable();
    /**
    * 初始化SystemDV表
     * 添加 EqpSV 数据
     * @param nID: 需要添加的 EqpSV 的 ID,必须是唯一的。
     * @param sName: 需要添加的 EqpSV 的名称,必须是唯一的。
     * @param sDataType: 数据类型,表示该设备值的类型,例如 "ASCII"、"UINT_1" 等。
     * @param nLength: 设备值的数据长度,通常为一个正整数,用于表示该数据的长度。
     * @param sUnit: 设备值的单位。如果为空或者为 "NULL",则插入数据库中的 NULL 值。
     * @param sRemark: 备注信息,描述该设备值的其他信息,可用于说明该字段的用途或特性。
     * @param nSeqNo: 该数据的序号,用于排序。
     * @return 1: 数据库未连接。
     * @return 2: ID 重复,无法插入数据。
     * @return 3: Name 重复,无法插入数据。
     * @return 4: 插入数据失败。
     * @return 0: 插入成功,数据已添加到 EqpSV 表中。
     *
     * 此函数用于将一条新的数据插入到 EqpSV 表中。它首先会检查传入的 `ID` 和 `Name` 是否已存在于表中,
     * 如果存在则返回相应的错误代码。如果 `Unit` 参数为 "NULL" 或者为空,函数会将其转换为数据库中的 NULL 值。
     * 然后,构造一个 SQL 插入语句并执行插入操作。如果插入失败,则抛出异常。
     * 如果一切顺利,返回 0 表示数据成功插入。
     */
    int addEqpSV(int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSeqNo);
    /**
    * 初始化SystemDV表
    */ 
    void initSystemDVTable();
    /**
    * 初始化EqpDV表
    * 初始化EqpDV表
    */ 
    void initEqpDVTable();
    /**
    * 初始化SystemEC表
    * 初始化SystemEC表
    */ 
    void initSystemECTable();
    /**
    * 初始化EqpEC表
    * 初始化EqpEC表
    */
    void initEqpECTable();
    /**
    * 初始化SystemEvent表
    * 初始化SystemEvent表
    */
    void initSystemEventTable();
    /**
    * 初始化EqpEvent表
    * 初始化EqpEvent表
    */
    void initEqpEventTable();
    /**
    * 初始化EventLink表
    * 初始化EventLink表
    */
    void initEventLinkTable();
    /**
     * 初始化PPID表
     * 初始化PPID表
     */
    void initPPIDTable();
    /**
    * 初始化RPTID表
    * 初始化RPTID表
    */ 
    void initRPTIDTable();
@@ -90,13 +201,19 @@
    SECSRuntimeManager();
    ~SECSRuntimeManager();
    // 禁止拷贝和赋值
    // 禁止拷贝和赋值
    SECSRuntimeManager(const SECSRuntimeManager&) = delete;
    SECSRuntimeManager& operator=(const SECSRuntimeManager&) = delete;
    // 从数据库中获取整数
    // 从数据库中获取整数
    int getIntFromDB(const std::string& query);
    // 判断VID是否重复
    bool isIDDuplicate(int nID);
    // 判断名称是否重复
    bool isNameDuplicate(const std::string& sName);
    BL::Database* m_pDB;
    static std::mutex m_mutex;
};