diff --git a/bin/Release/assets/config/app.json b/bin/Release/assets/config/app.json index 9732221..67688de 100644 --- a/bin/Release/assets/config/app.json +++ b/bin/Release/assets/config/app.json @@ -5,5 +5,6 @@ "user": "root", "passwd": "123456", "dbname": "ees" - } + }, + "token":"" } \ No newline at end of file diff --git a/doc/光储充站控系统-HTTP管理接口.docx b/doc/光储充站控系统-HTTP管理接口.docx new file mode 100644 index 0000000..3874895 Binary files /dev/null and b/doc/光储充站控系统-HTTP管理接口.docx differ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c3bb643..4b25f1c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -66,6 +66,7 @@ include_directories( ${THIRDPARTY_PATH} ${THIRDPARTY_PATH}/mysql/include ${THIRDPARTY_PATH}/nlohmann_json-3.11.2 + ${THIRDPARTY_PATH}/cpp-httplib-0.25.0 ${PVLIBS_PATH}/include/pvserver ${PVLIBS_PATH}/include/rllib ) diff --git a/src/app/AppData.cpp b/src/app/AppData.cpp index e782537..e826751 100644 --- a/src/app/AppData.cpp +++ b/src/app/AppData.cpp @@ -5,8 +5,8 @@ #include "app/Policy.h" #include "database/Dao.h" #include "common/JsonN.h" +#include "common/Snowflake.h" -#include "common/Utils.h" void ElectPeriod::parse(std::string jsonstr) { @@ -216,7 +216,6 @@ void AppData::initFromDB() void AppData::init() { this->initFromDB(); - this->initUser(); } std::shared_ptr AppData::getStation(int stationId) @@ -276,12 +275,35 @@ void AppData::loadStatData() { } - -void AppData::initUser() +static std::map g_mapUser; +std::string AppData::userLogin(std::string userId, std::string account) { - auto dao = DaoEntity::create(""); - std::vector result; + for (auto iter = g_mapUser.begin(); iter!=g_mapUser.end(); ++iter) + { + if (iter->second.userId == userId) // 重复登录 + { + g_mapUser.erase(iter); + break; + } + } + User user; + user.userId = userId; + user.account = account; + user.loginTime = Utils::time(); + user.token = Snowflake::instance().getIdStr(); + g_mapUser[user.token] = user; + return user.token; } +User AppData::getUser(std::string token) +{ + auto iter = g_mapUser.find(token); + if (iter != g_mapUser.end()) + { + return iter->second; + } + return User(); +} + int AppData::getWorkModeIdByName(std::string name) { diff --git a/src/app/AppData.h b/src/app/AppData.h index 23066a1..3a3569f 100644 --- a/src/app/AppData.h +++ b/src/app/AppData.h @@ -6,6 +6,7 @@ #include #include #include "common/Fields.h" +#include "app/Config.h" class Station; class Device; @@ -13,6 +14,14 @@ class MyPolicy; using VecPairSS = std::vector>; +struct User +{ + std::string userId; + std::string account; + std::string token; + int64_t loginTime {}; +}; + struct DeviceType { int typeId {}; @@ -50,13 +59,11 @@ public: void init(); void initFromDB(); - - // 读取统计数据: 今日统计数据,累计统计数据 void loadStatData(); - void initUser(); - + std::string userLogin(std::string userId, std::string account); + User getUser(std::string token); std::shared_ptr getStation(int stationId); diff --git a/src/app/Application.cpp b/src/app/Application.cpp index d8677c3..ee953e3 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -8,6 +8,8 @@ #include "app/Station.h" #include "app/Device.h" + + void Application::init() { // 初始化系统配置,读取配置文件 diff --git a/src/app/Config.cpp b/src/app/Config.cpp index b7d02ab..590c0bf 100644 --- a/src/app/Config.cpp +++ b/src/app/Config.cpp @@ -4,6 +4,7 @@ #include "common/JsonN.h" #include "Logger.h" +#include "AppData.h" AppOption Config::option; @@ -34,5 +35,11 @@ bool Config::init(std::string filename) XLOGI() << "[APP] load database config error: not found. host=" << option.database.host; } + if (jsonroot.contains("token")) + { + std::string token = jsonroot["token"]; + option.useToken = !token.empty(); + } + return true; } \ No newline at end of file diff --git a/src/app/Config.h b/src/app/Config.h index 39d5a99..c2e35aa 100644 --- a/src/app/Config.h +++ b/src/app/Config.h @@ -14,6 +14,7 @@ struct DatabaseOption struct AppOption { DatabaseOption database; + bool useToken {true}; }; class Config diff --git a/src/app/Constants.h b/src/app/Constants.h new file mode 100644 index 0000000..544f0b6 --- /dev/null +++ b/src/app/Constants.h @@ -0,0 +1,11 @@ +#ifndef _CONSTANTS_H_ +#define _CONSTANTS_H_ + +#include + +namespace CONST +{ + extern const std::string VAR; +} + +#endif // !_CONSTANTS_H_ diff --git a/src/app/Dao1.cpp b/src/app/Dao1.cpp index b7b6bb7..14dd8e0 100644 --- a/src/app/Dao1.cpp +++ b/src/app/Dao1.cpp @@ -15,8 +15,9 @@ std::shared_ptr DAO1::get(std::string tableName) return std::make_shared(tableName); } -Errcode DAO1::login(std::shared_ptr dao, std::string account, std::string passwd, std::string& err) +Errcode DAO1::login(std::shared_ptr dao, std::string account, std::string passwd, Fields& fields) { + std::string err; std::string t = Utils::timeStr(); if (!dao) { @@ -29,23 +30,24 @@ Errcode DAO1::login(std::shared_ptr dao, std::string account, std::st return Errcode::ERR_DB_CONN; } - std::string sql = "SELECT * FROM user WHERE account='" + account + "';"; + std::string sql = "SELECT u.*, ur.role_id FROM `user` u" + " LEFT JOIN user_role ur ON u.user_id = ur.user_id WHERE u.account=" + account + "';"; - std::vector res; - bool ret = dao->exec(sql, res); + std::vector result; + bool ret = dao->exec(sql, result); if (!ret) { err = "数据库操作错误"; DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err); return Errcode::ERR_DB_CONN; } - if (res.size() <=0) + if (result.size() <=0) { err = "用户不存在"; DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err); return Errcode::ERR_LOGIN_USER_NOTEXIST; } - Fields& fields = res[0]; + fields = result[0]; std::string userId = fields.value("user_id"); int loginCount = fields.get("login_count"); @@ -57,6 +59,15 @@ Errcode DAO1::login(std::shared_ptr dao, std::string account, std::st return Errcode::ERR_LOGIN_PASSWD; } + // 读取用户权限 + { + result.clear(); + std::string sql = "SELECT rp.role_id, rp.permission_id, p.name FROM role_permission rp " + "LEFT JOIN permission p ON p.permission_id = rp.permission_id" + "WHERE rp.role_id = 1;"; + + } + err = "登录成功"; // 数据库更新用户登录信息 diff --git a/src/app/Dao1.h b/src/app/Dao1.h index 9e9f0cd..5a924c6 100644 --- a/src/app/Dao1.h +++ b/src/app/Dao1.h @@ -7,7 +7,7 @@ class DAO1 public: static std::shared_ptr get(std::string tableName=""); - static Errcode login(std::shared_ptr dao, std::string account, std::string passwd, std::string& err); + static Errcode login(std::shared_ptr dao, std::string account, std::string passwd, Fields& res); static bool writeSystemLog(std::shared_ptr dao, int type, std::string userId, std::string account, std::string text); diff --git a/src/app/Device.cpp b/src/app/Device.cpp index 8496dbf..fec753a 100644 --- a/src/app/Device.cpp +++ b/src/app/Device.cpp @@ -37,7 +37,7 @@ int Device::startComm() { if (!isOpen) { - if (commEntity && commEntity->isAlive()) + if (commEntity && commEntity->alive) { commEntity->close(); } diff --git a/src/app/errcode.cpp b/src/app/errcode.cpp new file mode 100644 index 0000000..8878a00 --- /dev/null +++ b/src/app/errcode.cpp @@ -0,0 +1,24 @@ +#include "errcode.h" + +static std::unordered_map mapErr = +{ + {Errcode::OK, "操作成功"}, + {Errcode::ERR, "系统错误"}, + {Errcode::ERR_TOKEN, "TOKEN错误"}, + {Errcode::ERR_PARAM, "参数错误"}, + {Errcode::ERR_PARAM_NUL, "缺少参数"}, + + {Errcode::ERR_LOGIN_USER_NOTEXIST, "用户不存在"}, + {Errcode::ERR_LOGIN_PASSWD, "密码错误"}, + + {Errcode::ERR_DB_CONN, "数据库连接错误"}, + {Errcode::ERR_DB_DUPLICATE, "数据库数据重复"}, + {Errcode::ERR_DB_SQL, "数据库执行错误"}, + {Errcode::ERR_DB_VAL, "参数值错误"}, +}; + +std::string ErrcodeStr(Errcode code) +{ + auto iter = mapErr.find(code); + return (iter != mapErr.end() ? iter->second : ""); +} \ No newline at end of file diff --git a/src/app/errcode.h b/src/app/errcode.h index f308f47..003e7d4 100644 --- a/src/app/errcode.h +++ b/src/app/errcode.h @@ -1,16 +1,28 @@ #pragma once +#include +#include enum class Errcode { OK = 0, + ERR = 1, + ERR_TOKEN, // TOKEN错误 + ERR_PARAM, // 参数错误 + ERR_PARAM_NUL, // 缺少参数 - ERR = 100, - - ERR_DB_CONN = 101, // 数据库连接错误 - ERR_DB_SQL = 102, // 数据库查询SQL错误 - ERR_DB_DUPLICATE, // 数据重复 - + ERR_USER = 100, ERR_LOGIN_USER_NOTEXIST, // 登入错误,用户不存在 ERR_LOGIN_PASSWD, // 登入错误,密码不正确 -}; \ No newline at end of file + ERR_DEVICE = 200, + + + ERR_DB_CONN = 1001, // 数据库连接错误 + + ERR_DB_DUPLICATE = 1062, // Duplicate entry for key + ERR_DB_SQL = 1064, // 数据库查询SQL错误 + ERR_DB_VAL = 1366, // 1366,Incorrect decimal value,通常为参数值错误,例如空值、值类型错误 + +}; + +extern std::string ErrcodeStr(Errcode code); \ No newline at end of file diff --git a/src/common/Fields.cpp b/src/common/Fields.cpp index 23ff4b2..5c34f35 100644 --- a/src/common/Fields.cpp +++ b/src/common/Fields.cpp @@ -96,9 +96,10 @@ string Fields::toStr() string s; for (auto it = mapFields.begin(); it != mapFields.end(); it++) { - s += ("{" + it->first + ":" + it->second + "} "); + if (!s.empty()) s += ","; + s += ("\"" + it->first + "\":\"" + it->second + "\""); } - return s; + return "{" + s + "}"; } string Fields::toSqlInsert(string tableName) diff --git a/src/common/JsonN.h b/src/common/JsonN.h index ef66330..cc17059 100644 --- a/src/common/JsonN.h +++ b/src/common/JsonN.h @@ -1,4 +1,6 @@ -#include +#pragma once + +#include #include #include #include @@ -68,7 +70,7 @@ public: } catch (nlohmann::json::parse_error& e) { - std::cout << "JSON parse error: " << e.what() << std::endl; + std::cout << "JSON parse error: " << e.what() << "\n" << jsonstr << std::endl; return false; } return true; diff --git a/src/database/Dao.cpp b/src/database/Dao.cpp index 98b9830..e597aab 100644 --- a/src/database/Dao.cpp +++ b/src/database/Dao.cpp @@ -1,5 +1,9 @@ #include "Dao.h" #include "common/Utils.h" +#include "common/Snowflake.h" +#include "common/JsonN.h" +#include "app/Application.h" +#include "app/AppData.h" std::string DAO::sqlPageLimit(int index, int size) { @@ -8,19 +12,19 @@ std::string DAO::sqlPageLimit(int index, int size) return " LIMIT " + std::to_string(startIndex) + "," + std::to_string(size); } -bool DAO::count(DaoEntity& dao, std::string tableName, std::string condition, int& count) +Errcode DAO::count(DaoEntity& dao, std::string tableName, std::string condition, int& count) { std::string sql = "SELECT COUNT(*) count FROM " + tableName; if (!condition.empty()) { sql += " WHERE " + condition; }; sql += ";"; std::vector result; - bool ret = dao.exec(sql, result); - if (ret) + int ret = dao.exec(sql, result); + if (ret == 0) { count = (result.size() > 0) ? result[0].get("count") : 0; } - return ret; + return Errcode(ret); } Errcode DAO::exec(std::shared_ptr dao, std::string sql) @@ -31,7 +35,7 @@ Errcode DAO::exec(std::shared_ptr dao, std::string sql) return Errcode::ERR_DB_CONN; } auto ret = dao->exec(sql); - return ret ? Errcode::OK : Errcode::ERR_DB_SQL; + return Errcode(ret); } Errcode DAO::exec(std::shared_ptr dao, std::string sql, vector& result) @@ -42,68 +46,109 @@ Errcode DAO::exec(std::shared_ptr dao, std::string sql, vectorexec(sql, result); - return ret ? Errcode::OK : Errcode::ERR_DB_SQL; + return Errcode(ret); } - -static bool QueryCount(DaoEntity& dao, std::string sqlFrom, int& count) +Errcode DAO::query(std::shared_ptr dao, std::string tableName, std::vector keys, std::string condition) +{ + std::string sql; + return DAO::exec(dao, sql); +} +Errcode DAO::queryPagination(std::shared_ptr dao, std::string tableName, std::vector keys, std::string condition) +{ + std::string sql; + return DAO::exec(dao, sql); +} +Errcode DAO::insert(std::shared_ptr dao, std::string tableName, Fields params) +{ + return DAO::exec(dao, params.toSqlInsert(tableName)); +} +Errcode DAO::update(std::shared_ptr dao, std::string tableName, Fields params, std::string primaryKey) +{ + std::string primaryVal = params.remove(primaryKey); + if (primaryVal.empty()) + { + XLOGE() << "DAO update [" + tableName + "] failed, " << primaryKey << "=NULL."; + return Errcode::ERR_PARAM; + } + if (params.size() == 0) + { + XLOGE() << "DAO update [" + tableName + "] failed, params size=0."; + return Errcode::ERR_PARAM_NUL; + } + std::string condition = "WHERE " + primaryKey + "='" + primaryVal + "'"; + string sql = params.toSqlUpdate(tableName, condition); + return DAO::exec(dao, sql); +} +Errcode DAO::remove(std::shared_ptr dao, std::string tableName, std::string primaryKey, std::string val) +{ + std::string sql = "DELETE from `" + tableName + "` WHERE " + primaryKey + "='" + val + "';"; + return DAO::exec(dao, sql); +} +static Errcode QueryCount(DaoEntity& dao, std::string sqlFrom, int& count) { std::vector result; - bool ret = dao.exec("SELECT COUNT(*) count " + sqlFrom, result); - if (ret) + int ret = dao.exec("SELECT COUNT(*) count " + sqlFrom, result); + if (ret != 0) { count = (result.size() > 0) ? result[0].get("count") : 0; } - return ret; + return Errcode(ret); } -static bool QueryPagination(std::string sqlFields, std::string sqlCondition, PageInfo& page, vector& result) +static Errcode QueryPagination(std::string sqlFields, std::string sqlCondition, PageInfo& page, vector& result) { DaoEntity dao(""); - - int count {0}; - if (!QueryCount(dao, sqlCondition, count)) + if (!dao.isConnected()) { - return false; + return Errcode::ERR_DB_CONN; + } + int count {0}; + Errcode err = QueryCount(dao, sqlCondition, count); + if (err != Errcode::OK) + { + return err; } page.total = count; std::string sql = "SELECT " + sqlFields + " " + sqlCondition + DAO::sqlPageLimit(page.index, page.size); - bool ret = dao.exec(sql, result); - if (!ret) - { - XLOGE() << "DAO database error: sql=" << sql; - } - return ret; + int ret = dao.exec(sql, result); + return Errcode(ret); } // 新增用户信息 Errcode DAO::insertUser(Fields& params) { - std::string createTime = Utils::timeStr(); - auto dao = DaoEntity::create(DMUser::TABLENAME); std::string account = params.value(DMUser::ACCOUNT); - - std::string userRoleId = params.remove(DMRole::ROLE_ID); - - - // step1: 查询 - std::vector result; - bool ret = dao->exec("SELECT * from user WHERE account='" + account + "';", result); - if (!ret) + if (account.empty()) { - return Errcode::ERR_DB_CONN; + return Errcode::ERR_PARAM; + } + + // step1: 查询检查登录名是否已经存在 + std::vector result; + auto dao = DaoEntity::create(DMUser::TABLENAME); + int ret = dao->exec("SELECT * from user WHERE account='" + account + "';", result); + if (ret != 0) + { + return Errcode(ret); } if (result.size() > 0) { return Errcode::ERR_DB_DUPLICATE; } + + std::string createTime = Utils::timeStr(); + params.set(DMUser::USER_ID, Snowflake::instance().getIdStr()); params.set(DMUser::CREATETIME, createTime); + params.set(DMUser::PASSWD, "123456"); ret = dao->insertFields(params); - if (!ret) + if (ret != 0) { - return Errcode::ERR_DB_SQL; + return Errcode(ret); } + + std::string userRoleId = params.remove(DMRole::ROLE_ID); if (!userRoleId.empty()) { Fields paramsUserRole; @@ -115,20 +160,79 @@ Errcode DAO::insertUser(Fields& params) return Errcode::OK; } +Errcode DAO::login(std::shared_ptr dao, std::string account, std::string passwd, Fields& fields) +{ + if (!dao) { dao = std::make_shared(""); } + if (!dao->isConnected()) + { + + //DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err); + return Errcode::ERR_DB_CONN; + } + std::string t = Utils::timeStr(); + + std::string sql = "SELECT u.*, ur.role_id FROM `user` u" + " LEFT JOIN user_role ur ON u.user_id = ur.user_id WHERE u.account='" + account + "';"; + std::vector result; + int ret = dao->exec(sql, result); + if (ret != 0) + { + //DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err); + return Errcode(ret); + } + if (result.size() <=0) + { + //DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err); + return Errcode::ERR_LOGIN_USER_NOTEXIST; + } + fields = result[0]; + std::string userId = fields.value("user_id"); + int loginCount = fields.get("login_count"); + + // 判断密码 + if (passwd != fields.remove("passwd")) + { + //DAO1::writeSystemLog(dao, 2, userId, account, "用户登录失败:" + err); + return Errcode::ERR_LOGIN_PASSWD; + } + + // 数据库更新用户登录信息 + //sql = "UPDATE user SET login_time='" + t + "', login_count='" + std::to_string(loginCount + 1) + "' WHERE user_id = '" + userId + "'; "; + //ret = dao->exec(sql); + //if (ret != 0) + //{ + // XLOGE() << "更新用户登录信息失败:sql=" << sql; + //} + + //DAO1::writeSystemLog(dao, 2, userId, account, "用户登录成功"); + return Errcode::OK; +} + +Errcode DAO::queryRolePermission(std::shared_ptr dao, int roleId, vector& result) +{ + std::string sql = "SELECT rp.role_id, rp.permission_id, p.name FROM role_permission rp" + " LEFT JOIN permission p ON p.permission_id = rp.permission_id" + " WHERE rp.is_open='1' AND rp.role_id ='" + std::to_string(roleId) + "';"; + return DAO::exec(dao, sql, result); +} + +Errcode DAO::queryRolePermission(std::shared_ptr dao, vector& result) +{ + // 查询 role 的 permission + std::string sql = "SELECT rp.role_id, rp.permission_id, rp.is_open, r.name role_name, p.name permission_name FROM role_permission rp" + " LEFT JOIN `role` r ON r.role_id = rp.role_id" + " LEFT JOIN permission p ON p.permission_id = rp.permission_id" + " WHERE rp.is_open='1';"; + return DAO::exec(dao, sql, result); +} // 分页查询用户信息列表 -bool DAO::queryUserList(PageInfo& pageInfo, vector& result) +Errcode DAO::queryUserList(PageInfo& pageInfo, vector& result) { - std::string sqlFields = "u.*, r.role_id , r.name role_name"; + std::string sqlFields = "u.user_id, u.account, u.name, u.phone, u.age, u.email, u.gender, u.is_open, u.create_time, u.update_time, r.role_id , r.name role_name"; std::string sqlCondition = "FROM USER u LEFT JOIN user_role ur ON ur.user_id = u.user_id LEFT JOIN ROLE r ON r.role_id =ur.role_id"; - DaoEntity dao(""); - bool ret = QueryPagination(sqlFields, sqlCondition, pageInfo, result); - if (!ret) - { - XLOGE() << "DAO database error: queryUserList failed."; - } - return ret; + return QueryPagination(sqlFields, sqlCondition, pageInfo, result); } Errcode DAO::updateUserById(Fields& params) @@ -141,10 +245,10 @@ Errcode DAO::updateUserById(Fields& params) if (params.size() > 0) { - bool ret = dao->updateFields(params, "WHERE " + DMUser::USER_ID + "='" + userId + "'"); - if (!ret) + int ret = dao->updateFields(params, "WHERE " + DMUser::USER_ID + "='" + userId + "'"); + if (ret != 0) { - return Errcode::ERR_DB_SQL; + return Errcode(ret); } } if (!roleId.empty()) @@ -154,43 +258,137 @@ Errcode DAO::updateUserById(Fields& params) paramsUserRole.set(DMUserRole::USER_ID, userId); paramsUserRole.set(DMUserRole::ROLE_ID, roleId); paramsUserRole.set(DMUserRole::UPDATETIME, createTime); - bool ret = dao->duplicateUpdate(paramsUserRole, {DMUserRole::ROLE_ID}); - if (!ret) + int ret = dao->duplicateUpdate(paramsUserRole, {DMUserRole::ROLE_ID}); + if (ret != 0) { - return Errcode::ERR_DB_SQL; + return Errcode(ret); } } return Errcode::OK; } +Errcode DAO::deleteUserById(std::string userId) +{ + std::string sql = "DELETE from user WHERE user_id='" + userId + "';"; + return DAO::exec(NULL, sql); +} + +Errcode DAO::queryPermissionList(PageInfo& pageInfo, vector& result) +{ + std::string sqlFrom = "FROM " + DMPermission::TABLENAME; + return QueryPagination("*", sqlFrom, pageInfo, result); +} + +Errcode DAO::insertPermission(Fields& params) +{ + return DAO::exec(NULL, params.toSqlInsert(DMPermission::TABLENAME)); +} + +Errcode DAO::updatePermissionById(Fields& params) +{ + std::string primaryKey = DMPermission::PERMISSION_ID; + std::string primaryVal = params.remove(primaryKey); + if (primaryVal.empty()) + { + return Errcode::ERR_DB_SQL; + } + std::string condition = "WHERE " + primaryKey + "='" + primaryVal + "'"; + string sql = params.toSqlUpdate(DMPermission::TABLENAME, condition); + return DAO::exec(NULL, sql); +} + +Errcode DAO::deletePermissionById(std::string permissionId) +{ + std::string sql = "DELETE from permission WHERE permission_id='" + permissionId + "';"; + return DAO::exec(NULL, sql); +} + Errcode DAO::queryRoleList(std::shared_ptr dao, vector& result) { std::string sql = "SELECT * FROM " + DMRole::TABLENAME + ";"; return DAO::exec(dao, sql, result); } -bool DAO::queryRoleList(PageInfo& pageInfo, vector& result) +Errcode DAO::queryRoleList(PageInfo& pageInfo, vector& result) { - DaoEntity dao(""); - std::string sqlFrom = "FROM " + DMRole::TABLENAME; - bool ret = QueryPagination("*", sqlFrom, pageInfo, result); - if (!ret) - { - XLOGE() << "DAO database error: queryRoleList failed."; - } - return ret; + std::string sqlFields = ""; + std::string sqlCondition = "FROM " + DMRole::TABLENAME; + return QueryPagination("*", sqlCondition, pageInfo, result); } -bool DAO::queryPermissionList(PageInfo& pageInfo, vector& result) +Errcode DAO::updateRolePermission(std::shared_ptr dao, std::string roleId, std::string permission) { - DaoEntity dao(""); - std::string sqlFrom = "FROM " + DMPermission::TABLENAME; - bool ret = QueryPagination("*", sqlFrom, pageInfo, result); - if (!ret) + NJsonNode jnode; + NJson::parse(permission, jnode); + std::vector vec; + for (auto& item: jnode) { - XLOGE() << "DAO database error: queryPermissionList failed."; + std::string permissionId; + if (item.is_number()) { permissionId = std::to_string(item.get()); } + if (item.is_string()) { permissionId = item.get(); } + if (!permissionId.empty()) + { + Fields field; + field.set("role_id", roleId); + field.set("permission_id", permissionId); + vec.emplace_back(field); + } } - return ret; + dao->setTableName(DMRolePermission::TABLENAME); + std::string sqlDel = "DELETE from " + DMRolePermission::TABLENAME + " WHERE role_id='" + roleId + "';"; + int ret = dao->exec(sqlDel); + if (ret != 0 ){ return Errcode(ret); }; + ret = dao->insertFields(vec); + if (ret != 0) { return Errcode(ret); }; + return Errcode::OK; +} + +// 新增角色 +Errcode DAO::insertRole(Fields& params) +{ + std::string permission = params.remove("permission"); + std::string name = params.value("name"); + auto dao = DaoEntity::create(DMRole::TABLENAME); + auto err = DAO::insert(dao, DMRole::TABLENAME, params); + + if (err == Errcode::OK && !permission.empty()) + { + // 查询获取 roleId + std::vector res; + std::string sql = "SELECT * FROM " + DMRole::TABLENAME + " WHERE name='" + name + "';"; + err = DAO::exec(dao, sql, res); + if (err == Errcode::OK && res.size() > 0) + { + std::string roleId = res[0].value("role_id"); + err = DAO::updateRolePermission(dao, roleId, permission); + } + } + return err; +} + +// 更新角色 +Errcode DAO::updateRoleById(Fields& params) +{ + auto roleId = params.value(DMRole::ROLE_ID); + if (roleId.empty()) + { + return Errcode::ERR_DB_SQL; + } + auto permission = params.remove("permission"); + + auto dao = DaoEntity::create(DMRole::TABLENAME); + auto err = DAO::update(dao, DMRole::TABLENAME, params, DMRole::ROLE_ID); + if (err == Errcode::OK && !permission.empty()) + { + err = DAO::updateRolePermission(dao, roleId, permission); + } + return err; +} + +// 删除角色 +Errcode DAO::deleteRoleById(std::string userId) +{ + return DAO::remove(NULL, DMRole::TABLENAME, DMRole::ROLE_ID, userId); } Errcode DAO::insertStation(Fields& params) @@ -201,7 +399,7 @@ Errcode DAO::insertStation(Fields& params) params.check(DMStation::LONGITUDE, "", "NULL"); std::string sql = params.toSqlInsert(DMStation::TABLENAME); - return DAO::exec(dao, sql); + return DAO::insert(NULL, DMStation::TABLENAME, params); } // 查询场站信息列表 @@ -212,33 +410,16 @@ Errcode DAO::queryStationList(std::shared_ptr dao, vector& re } // 分页查询场站信息列表 -bool DAO::queryStationList(PageInfo& pageInfo, vector& result) +Errcode DAO::queryStationList(PageInfo& pageInfo, vector& result) { - DaoEntity dao(""); std::string sqlFrom = "FROM " + DMStation::TABLENAME; - bool ret = QueryPagination("*", sqlFrom, pageInfo, result); - if (!ret) - { - XLOGE() << "DAO database error: queryStationList failed."; - } - return ret; + return QueryPagination("*", sqlFrom, pageInfo, result); } Errcode DAO::updateStationById(Fields& params) { - std::string stationId = params.value(DMStation::STATION_ID); - if (stationId.empty()) - { - return Errcode::ERR_DB_SQL; - } - params.remove(DMStation::STATION_ID); - auto dao = DaoEntity::create(DMStation::TABLENAME); - bool ret = dao->updateFields(params, "WHERE " + DMStation::STATION_ID + "='" + stationId + "'"); - if (!ret) - { - return Errcode::ERR_DB_SQL; - } - return Errcode::OK; + std::string primaryKey = DMStation::STATION_ID; + return DAO::update(NULL, DMStation::TABLENAME, params, primaryKey); } // 查询设备信息列表 @@ -249,16 +430,10 @@ Errcode DAO::queryDeviceList(std::shared_ptr dao, vector& res } // 分页查询设备信息列表 -bool DAO::queryDeviceList(PageInfo& pageInfo, vector& result) +Errcode DAO::queryDeviceList(PageInfo& pageInfo, vector& result) { - DaoEntity dao(""); std::string sqlFrom = "FROM " + DMDevice::TABLENAME; - bool ret = QueryPagination("*", sqlFrom, pageInfo, result); - if (!ret) - { - XLOGE() << "DAO database error: queryDeviceList failed."; - } - return ret; + return QueryPagination("*", sqlFrom, pageInfo, result); } // 查询设备类型定义 @@ -275,26 +450,14 @@ Errcode DAO::insertDevice(Fields& params) Errcode DAO::updateDeviceById(Fields& params) { - std::string deviceId = params.value(DMDevice::DEVICE_ID); - if (deviceId.empty()) - { - return Errcode::ERR_DB_SQL; - } - std::string sql = params.toSqlUpdate(DMDevice::TABLENAME, "WHERE " + DMDevice::DEVICE_ID + "='" + deviceId + "'"); - return DAO::exec(NULL, sql); + return DAO::update(NULL, DMDevice::TABLENAME, params, DMDevice::DEVICE_ID); } // 策略管理 -bool DAO::queryPolicyList(PageInfo& pageInfo, vector& result) +Errcode DAO::queryPolicyList(PageInfo& pageInfo, vector& result) { - auto dao = DaoEntity::create(""); std::string sqlFrom = "FROM " + DMPolicy::TABLENAME; - bool ret = QueryPagination("*", sqlFrom, pageInfo, result); - if (!ret) - { - XLOGE() << "DAO database error: queryPolicyList failed."; - } - return ret; + return QueryPagination("*", sqlFrom, pageInfo, result); } Errcode DAO::queryPolicyList(std::shared_ptr dao, vector& result) @@ -304,31 +467,77 @@ Errcode DAO::queryPolicyList(std::shared_ptr dao, vector& res } Errcode DAO::insertPolicy(Fields& params) { - return DAO::exec(NULL, params.toSqlInsert(DMPolicy::TABLENAME)); + return DAO::insert(NULL, DMPolicy::TABLENAME, params); } Errcode DAO::updatePolicyById(Fields& params) { - std::string policyId = params.value(DMPolicy::POLICY_ID); - if (policyId.empty()) - { - return Errcode::ERR_DB_SQL; - } - std::string sql = params.toSqlUpdate(DMPolicy::TABLENAME, "WHERE " + DMPolicy::POLICY_ID + "='" + policyId + "'"); - return DAO::exec(NULL, sql); + return DAO::update(NULL, DMPolicy::TABLENAME, params, DMPolicy::POLICY_ID); +} +Errcode DAO::deletePolicyById(std::string policyId) +{ + return DAO::remove(NULL, DMPolicy::TABLENAME, DMPolicy::POLICY_ID, policyId); } // 系统日志管理 -bool DAO::querySystemLogList(PageInfo& pageInfo, vector& result) +Errcode DAO::querySystemLogList(PageInfo& pageInfo, vector& result) { - DaoEntity dao(""); - std::string sqlFrom = "FROM " + DMSystemLog::TABLENAME; - bool ret = QueryPagination("*", sqlFrom, pageInfo, result); - if (!ret) - { - XLOGE() << "DAO database error: querySystemLogList failed."; - } - return ret; + std::string sqlFrom = "FROM " + DMLogSystem::TABLENAME; + return QueryPagination("*", sqlFrom, pageInfo, result); } +Errcode DAO::insertSystemLog(Fields& params) +{ + params.set(DMLogAlert::LOG_ID, Snowflake::instance().getIdStr()); + return DAO::insert(NULL, DMLogSystem::TABLENAME, params); +} +Errcode DAO::updateSystemLogById(Fields& params) +{ + return DAO::update(NULL, DMLogSystem::TABLENAME, params, DMLogSystem::LOG_ID); +} + +Errcode DAO::insertSystemLogSys(std::string content, int status) +{ + Fields fields; + fields.set("type", 1); + fields.set("content", content); + fields.set("status", status); + return DAO::insertSystemLog(fields); +} +Errcode DAO::insertSystemLogUser(std::string token, std::string content, int status) +{ + User user = Application::data().getUser(token); + Fields fields; + fields.set("type", 2); + fields.set("content", content); + fields.set("status", status); + fields.set("user_id", user.userId); + fields.set("user_account", user.account); + return DAO::insertSystemLog(fields); +} +Errcode DAO::insertSystemLogDevice(std::string deviceId, std::string content, int status) +{ + Fields fields; + fields.set("type", 3); + fields.set("content", content); + fields.set("status", status); + fields.set("device_id", deviceId); + return DAO::insertSystemLog(fields); +} + +Errcode DAO::queryAlertLogList(PageInfo& pageInfo, vector& result) +{ + std::string sqlFrom = "FROM " + DMLogAlert::TABLENAME; + return QueryPagination("*", sqlFrom, pageInfo, result); +} +Errcode DAO::insertAlertLog(Fields& params) +{ + params.set(DMLogAlert::LOG_ID, Snowflake::instance().getIdStr()); + return DAO::insert(NULL, DMLogAlert::TABLENAME, params); +} +Errcode DAO::updateAlertLogById(Fields& params) +{ + return DAO::update(NULL, DMLogAlert::TABLENAME, params, DMLogAlert::LOG_ID); +} + Errcode DAO::queryStatDataList(std::shared_ptr dao, std::string startDate, std::string endDate, vector& result) { @@ -342,7 +551,6 @@ Errcode DAO::queryWorkModeDef(std::shared_ptr dao, vector& re return DAO::exec(dao, sql, result); } - Errcode DAO::queryPolicyTypeDef(std::shared_ptr dao, vector& result) { std::string sql = "SELECT * FROM " + DMDefPolicyType::TABLENAME + ";"; diff --git a/src/database/Dao.h b/src/database/Dao.h index 7388981..78930ad 100644 --- a/src/database/Dao.h +++ b/src/database/Dao.h @@ -9,38 +9,60 @@ class DAO public: static std::string sqlPageLimit(int index, int size); - static bool count(DaoEntity& dao, std::string tableName, std::string condition, int& count); + static Errcode count(DaoEntity& dao, std::string tableName, std::string condition, int& count); static Errcode exec(std::shared_ptr dao, std::string sql); static Errcode exec(std::shared_ptr dao, std::string sql, vector& result); + static Errcode query(std::shared_ptr dao, std::string tableName, std::vector keys, std::string condition); + static Errcode queryPagination(std::shared_ptr dao, std::string tableName, std::vector keys, std::string condition); + static Errcode insert(std::shared_ptr dao, std::string tableName, Fields params); + static Errcode update(std::shared_ptr dao, std::string tableName, Fields params, std::string primaryKey); + static Errcode remove(std::shared_ptr dao, std::string tableName, std::string primaryKey, std::string val); + static Errcode login(std::shared_ptr dao, std::string account, std::string passwd, Fields& fields); - // 查询用户信息列表(分页) - static bool queryUserList(PageInfo& pageInfo, vector& result); + static Errcode queryRolePermission(std::shared_ptr dao, int roleId, vector& result); + static Errcode queryRolePermission(std::shared_ptr dao, vector& result); - // 新增用户信息 + // 查询用户列表(分页) + static Errcode queryUserList(PageInfo& pageInfo, vector& result); + + // 新增用户 static Errcode insertUser(Fields& params); - // 更新用户信息 + // 更新用户 static Errcode updateUserById(Fields& params); - - - - // 查询角色信息列表(分页) - static bool queryRoleList(PageInfo& pageInfo, vector& result); - - // 查询角色信息列表 - static Errcode queryRoleList(std::shared_ptr dao, vector& result); - + // 删除用户 + static Errcode deleteUserById(std::string userId); // 查询权限信息列表(分页) - static bool queryPermissionList(PageInfo& pageInfo, vector& result); + static Errcode queryPermissionList(PageInfo& pageInfo, vector& result); + // 新增权限 + static Errcode insertPermission(Fields& params); + // 更新权限 + static Errcode updatePermissionById(Fields& params); + // 删除权限 + static Errcode deletePermissionById(std::string userId); + static Errcode updateRolePermission(std::shared_ptr dao, std::string roleId, std::string permission); + + // 查询角色列表(分页) + static Errcode queryRoleList(PageInfo& pageInfo, vector& result); + + // 查询角色列表 + static Errcode queryRoleList(std::shared_ptr dao, vector& result); + // 新增角色 + static Errcode insertRole(Fields& params); + // 更新角色 + static Errcode updateRoleById(Fields& params); + // 删除角色 + static Errcode deleteRoleById(std::string userId); + // 查询场站信息列表(分页) - static bool queryStationList(PageInfo& pageInfo, vector& result); + static Errcode queryStationList(PageInfo& pageInfo, vector& result); // 查询场站信息列表 static Errcode queryStationList(std::shared_ptr dao, vector& result); @@ -52,7 +74,7 @@ public: // 查询设备信息列表(分页) - static bool queryDeviceList(PageInfo& pageInfo, vector& result); + static Errcode queryDeviceList(PageInfo& pageInfo, vector& result); // 查询设备信息列表 static Errcode queryDeviceList(std::shared_ptr dao, vector& result); // 查询设备类型定义 @@ -65,17 +87,28 @@ public: /////////////////////////////////////////////////////////////////////////////////////////////// // === 策略管理 === // 分页查询策略信息列表 - static bool queryPolicyList(PageInfo& pageInfo, vector& result); + static Errcode queryPolicyList(PageInfo& pageInfo, vector& result); static Errcode queryPolicyList(std::shared_ptr dao, vector& result); static Errcode insertPolicy(Fields& params); static Errcode updatePolicyById(Fields& params); + static Errcode deletePolicyById(std::string policyId); /////////////////////////////////////////////////////////////////////////////////////////////// // === 系统日志管理 === // 分页查询系统日志列表 - static bool querySystemLogList(PageInfo& pageInfo, vector& result); + static Errcode querySystemLogList(PageInfo& pageInfo, vector& result); + static Errcode insertSystemLog(Fields& params); + static Errcode updateSystemLogById(Fields& params); + + static Errcode insertSystemLogSys(std::string content, int status); + static Errcode insertSystemLogUser(std::string token, std::string content, int status); + static Errcode insertSystemLogDevice(std::string deviceId, std::string content, int status); + + static Errcode queryAlertLogList(PageInfo& pageInfo, vector& result); + static Errcode insertAlertLog(Fields& params); + static Errcode updateAlertLogById(Fields& params); /////////////////////////////////////////////////////////////////////////////////////////////// // === 统计数据管理 === diff --git a/src/database/DaoEntity.cpp b/src/database/DaoEntity.cpp index 9be63fe..5fdcd27 100644 --- a/src/database/DaoEntity.cpp +++ b/src/database/DaoEntity.cpp @@ -66,23 +66,23 @@ bool DaoEntity::isConnected() return db_->isConnected(); } -bool DaoEntity::exec(string sql) +int DaoEntity::exec(string sql) { return db_->exec(sql); } -bool DaoEntity::exec(string sql, vector& result) +int DaoEntity::exec(string sql, vector& result) { return db_->exec(sql, result); } -bool DaoEntity::insertFields(Fields& fields) +int DaoEntity::insertFields(Fields& fields) { string sql = fields.toSqlInsert(tableName_); return this->db_->exec(sql); } -bool DaoEntity::insertFields(vector& vec_fields) +int DaoEntity::insertFields(vector& vec_fields) { //"insert into TABLE () values ()"; string sql = "insert into " + tableName_; @@ -127,7 +127,7 @@ bool DaoEntity::insertFields(vector& vec_fields) return this->db_->exec(sql); } -bool DaoEntity::duplicateUpdate(Fields& fields, const vector& keys) +int DaoEntity::duplicateUpdate(Fields& fields, const vector& keys) { //insert into device_attr(device_id, attr_id, attr_val) values('26', 'model', '型号1') on duplicate key update attr_val='型号1'; string key; @@ -161,33 +161,34 @@ bool DaoEntity::duplicateUpdate(Fields& fields, const vector& keys) // }); //} -bool DaoEntity::queryFields(string keys, const string& condition, vector& result) +int DaoEntity::queryFields(string keys, const string& condition, vector& result) { ostringstream oss; oss << "SELECT " + keys + " FROM " << tableName_ << (" " + condition) << "; "; return this->db_->exec(oss.str(), result); } -bool DaoEntity::queryFields(string keys, const string& condition, PageInfo& page, vector& result) +int DaoEntity::queryFields(string keys, const string& condition, PageInfo& page, vector& result) { + int err = 0; ostringstream oss; oss << "SELECT count(1) total FROM `" << tableName_ << "` " << condition << ";"; vector res_total; - if (!this->db_->exec(oss.str().c_str(), res_total)) + if (err = this->db_->exec(oss.str().c_str(), res_total)) { - return false; + return err; } if (res_total.size() <= 0) { page.total = 0; - return true; + return err; } page.total = res_total[0].get("total"); if (page.total <= 0) { - return true; + return err; } oss.str(""); @@ -200,13 +201,13 @@ bool DaoEntity::queryFields(string keys, const string& condition, PageInfo& page return this->db_->exec(oss.str().c_str(), result); } -bool DaoEntity::updateFields(Fields& fields, const string& condition) +int DaoEntity::updateFields(Fields& fields, const string& condition) { string sql = fields.toSqlUpdate(tableName_, condition); return this->db_->exec(sql); } -bool DaoEntity::updateFields(Fields& fields, vector vecKeys, const string& condition) +int DaoEntity::updateFields(Fields& fields, vector vecKeys, const string& condition) { string sql = fields.toSqlUpdate(tableName_, vecKeys, condition); return this->db_->exec(sql); diff --git a/src/database/DaoEntity.h b/src/database/DaoEntity.h index 0dab9a5..9bc208a 100644 --- a/src/database/DaoEntity.h +++ b/src/database/DaoEntity.h @@ -42,39 +42,39 @@ public: /** * 执行sql语句 */ - bool exec(string sql); + int exec(string sql); /** * 执行sql语句并返回执行(查询)的结果集 */ - bool exec(string sql, vector& result); + int exec(string sql, vector& result); /** * 数据库插入一条数据, 需要先指定数据表名称 * @param: fields 写入的数据字段和值 */ - bool insertFields(Fields& vecFields); + int insertFields(Fields& vecFields); /** * 数据库插入多条数据, 需要先指定数据表名称 * @param: vecFields 写入的数据字段和值的集合 */ - bool insertFields(vector& vecFields); + int insertFields(vector& vecFields); /** * 数据库插入多条数据,UNIQUE索引或PRIMARY KEY重复时执行更新数据, 需要先指定数据表名称 * @param: vecFields 写入的数据字段和值的集合 * @param: keys 数据重复时需要更新的字段 */ - bool duplicateUpdate(Fields& vecFields, const vector& keys); + int duplicateUpdate(Fields& vecFields, const vector& keys); /** * 数据库查询,需要先指定数据表名称 * @param: sql_c 查询条件,例:"where id='1'" * @param: result 查询的数据结果集 */ - bool queryFields(string keys, const string& sql_c, vector& result); + int queryFields(string keys, const string& sql_c, vector& result); /** * 数据库查询,需要先指定数据表名称 @@ -82,14 +82,14 @@ public: * @param: pageinfo 分页信息 * @param: result 查询的数据结果集 */ - bool queryFields(string keys, const string& sql_c, PageInfo& pageinfo, vector& result); + int queryFields(string keys, const string& sql_c, PageInfo& pageinfo, vector& result); /** * 数据库更新,需要先指定数据表名称 * @param: fields 要更新的数据字段和值 * @param: sql_c 更新条件 */ - bool updateFields(Fields& fields, const string& sql_c); + int updateFields(Fields& fields, const string& sql_c); /** * 数据库更新,需要先指定数据表名称 @@ -97,7 +97,7 @@ public: * @param: vecKeys 要更新的字段名称 * @param: cond 更新条件 */ - bool updateFields(Fields& fields, vector vecKeys, const string& cond); + int updateFields(Fields& fields, vector vecKeys, const string& cond); protected: static MysqlOption option; diff --git a/src/database/DataModelDef.h b/src/database/DataModelDef.h index 6cfb9ec..f28dbd0 100644 --- a/src/database/DataModelDef.h +++ b/src/database/DataModelDef.h @@ -140,9 +140,9 @@ namespace DMPolicy const string UPDATE_TIME = "update_time"; } -namespace DMSystemLog +namespace DMLogSystem { - const string TABLENAME = "system_log"; + const string TABLENAME = "log_system"; const string LOG_ID = "log_id"; const string TYPE = "type"; const string USER_ID = "user_id"; @@ -153,9 +153,9 @@ namespace DMSystemLog const string UPDATE_TIME = "update_time"; } -namespace DMAlertLog +namespace DMLogAlert { - const string TABLENAME = "alert_log"; + const string TABLENAME = "log_alert"; const string LOG_ID = "log_id"; const string TYPE = "type"; const string DEVICE_ID = "device_id"; diff --git a/src/database/MysqlClient.cpp b/src/database/MysqlClient.cpp index d73fbf4..3fac260 100644 --- a/src/database/MysqlClient.cpp +++ b/src/database/MysqlClient.cpp @@ -2,6 +2,13 @@ #include "common/Utils.h" //#include "Spdlogger.h" #include "Logger.h" +#include +using namespace std; + +static int64_t GetTimestamp() +{ + return chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); +} MysqlClient::MysqlClient(MysqlOption option) : option(option) { @@ -13,22 +20,35 @@ MysqlClient::~MysqlClient() this->close(); } +static int64_t g_tickErr {0}; + int MysqlClient::conn() { if (mysql_) { return 0; } + if (GetTimestamp() - g_tickErr <= 5) + { + return 1; + } mysql_ = mysql_init(nullptr); - MYSQL* ret = mysql_real_connect(mysql_, option.host.c_str(), option.user.c_str(), option.password.c_str(), option.dbname.c_str(), option.port, NULL, 0); + MYSQL* ret = mysql_real_connect(mysql_, + option.host.c_str(), + option.user.c_str(), + option.password.c_str(), + option.dbname.c_str(), + option.port, NULL, 0); if (ret == NULL) { std::string err = mysql_error(mysql_); //Spdlogger::info("[mysql] connect failed: {}", mysql_error(mysql_)); mysql_ = nullptr; + g_tickErr = GetTimestamp(); } else { + g_tickErr = 0; mysql_query(mysql_, "set names 'utf8';"); } return 0; @@ -48,70 +68,80 @@ void MysqlClient::close() } } -bool MysqlClient::exec(std::string sql) +static int MysqlQuery(MYSQL* mysql, const std::string& sql) { - if (!mysql_) + int err = 0; + if (!mysql) { XLOGE() << "Mysql exec error, database is not connected."; - return false; + return err; } if (sql.empty()) { XLOGE() << "Mysql exec error, sql is empty."; - return false; + return err; } - int ret = mysql_query(mysql_, sql.c_str()); - if (0 != ret) + err = mysql_query(mysql, sql.c_str()); + if (0 != err) { - XLOGE() << "Mysql exec error: " << mysql_error(mysql_) << ", sql=" << sql; - return false; + err = mysql_errno(mysql); + XLOGE() << "Mysql exec error: " << err << "," << mysql_error(mysql) << ", sql=" << sql; + return err; } - return true; + return err; } -bool MysqlClient::exec(std::string sql, vector& result) +int MysqlClient::exec(std::string sql) +{ + int err = MysqlQuery(mysql_, sql); + // 确保读取并释放结果集,否则会产生 [2014,Commands out of sync;] 错误 + MYSQL_RES* res = mysql_store_result(mysql_); + if (res) { mysql_free_result(res); } + return err; +} + +int MysqlClient::exec(std::string sql, vector& result) { result.clear(); - bool ret = MysqlClient::exec(sql); - if (!ret) + int err = MysqlQuery(mysql_, sql); + if (err != 0) { - return false; + return err; } MYSQL_RES* res = mysql_store_result(mysql_); - if (!res) + if (res) { - return false; - } - vector fieldNames; - while (true) - { - MYSQL_FIELD* field = mysql_fetch_field(res); - if (!field) + vector fieldNames; + while (true) { - break; - } - fieldNames.push_back(field->name); - } - - while (true) - { - MYSQL_ROW row = mysql_fetch_row(res); - if (!row) - { - break; + MYSQL_FIELD* field = mysql_fetch_field(res); + if (!field) + { + break; + } + fieldNames.push_back(field->name); } - Fields rowData; - for (size_t i = 0; i < fieldNames.size(); ++i) + while (true) { - string field_text = (row[i] == NULL) ? "" : row[i]; - rowData.set(fieldNames[i], field_text); - } - result.push_back(rowData); - } + MYSQL_ROW row = mysql_fetch_row(res); + if (!row) + { + break; + } - // 释放结果集 - mysql_free_result(res); - return true; + Fields rowData; + for (size_t i = 0; i < fieldNames.size(); ++i) + { + string field_text = (row[i] == NULL) ? "" : row[i]; + rowData.set(fieldNames[i], field_text); + } + result.push_back(rowData); + } + + // 释放结果集 + mysql_free_result(res); + } + return 0; } \ No newline at end of file diff --git a/src/database/MysqlClient.h b/src/database/MysqlClient.h index 3d5e9e0..5c63f38 100644 --- a/src/database/MysqlClient.h +++ b/src/database/MysqlClient.h @@ -50,12 +50,12 @@ public: /** * @brief: 执行sql语句 */ - bool exec(std::string sql); + int exec(std::string sql); /** * @brief: 执行sql语句, 获取查询结果集 */ - bool exec(std::string, vector& result); + int exec(std::string, vector& result); private: // mysql数据库连接对象 diff --git a/src/main.cpp b/src/main.cpp index d655e19..02a9acc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,10 +19,29 @@ #include "pv/PvUser.h" #include "rlsocket.h" +#include "protocol/HttpEntity.h" + +enum EAA +{ + A = 1, + B = 2 +}; #define wsa rlwsa int main(int argc, char** argv) { + EAA aa = EAA(100); + std::cout << aa; + + std::thread([]() { + while (1) { + HttpEntity http; + http.listen("0.0.0.0", 19800); + } + }).detach(); + + + // 设置控制台输出为 UTF-8 编码 SetConsoleOutputCP(CP_UTF8); // 设置控制台输入为 UTF-8 编码(如果需要输入中文) @@ -57,8 +76,8 @@ int main(int argc, char** argv) std::map mapT; bool ff = mapT[1]; - //rlwsa(); - //rlSocket socket("127.0.0.1", 19801, 1); + rlwsa(); + rlSocket socket("127.0.0.1", 19801, 1); //int ret = socket.connect(); //std::string s1 = "helloworld"; //socket.write(s1.c_str(), s1.size()); diff --git a/src/protocol/CommEntity.cpp b/src/protocol/CommEntity.cpp index 79457a5..4f04a89 100644 --- a/src/protocol/CommEntity.cpp +++ b/src/protocol/CommEntity.cpp @@ -11,7 +11,7 @@ std::shared_ptr CommEntity::create(Fields& data) if (commType == "TCP") { auto entity = std::make_shared(); - entity->setHost(ip, port, isclient); + entity->setAddr(ip, port, isclient); return entity; } else if (commType == "MODBUS") diff --git a/src/protocol/CommEntity.h b/src/protocol/CommEntity.h index 6dd03e9..a6f4608 100644 --- a/src/protocol/CommEntity.h +++ b/src/protocol/CommEntity.h @@ -18,17 +18,16 @@ public: // 启动通讯连接 virtual int start() { return 0; }; // 关闭通讯连接 - virtual void close() { isCloseRequest_ = true; }; + virtual void close() { isCloseRequest = true; }; std::string id() { return id_; } - bool isAlive() { return isAlive_; } - bool isConnected() { return isConnected_; } public: std::string id_; - bool isAlive_ = false; - bool isConnected_ = false; - bool isCloseRequest_ = false; std::string type; + int commtype = 0; + bool alive = false; + bool isConnected = false; + bool isCloseRequest = false; }; diff --git a/src/protocol/HttpEntity.cpp b/src/protocol/HttpEntity.cpp new file mode 100644 index 0000000..4d6ec88 --- /dev/null +++ b/src/protocol/HttpEntity.cpp @@ -0,0 +1,502 @@ +#include "HttpEntity.h" +#include "database/Dao.h" +#include +#include "common/Utils.h" +#include "common/Snowflake.h" +#include "app/Application.h" +#include "app/AppData.h" + +static NJsonNode FieldsToJsonArray(std::vector vecFields) +{ + NJsonNode jsonnode = NJsonNode::array(); + for (auto& fields : vecFields) + { + NJsonNode jnode; + for (auto& item : fields.map()) + { + jnode[item.first] = item.second; + } + jsonnode.push_back(jnode); + } + return jsonnode; +} + +static void GetRequestParam(const httplib::Request& req, const std::vector& vecKeys, Fields& fields) +{ + for (auto& key : vecKeys) + { + if (req.has_param(key)) + { + fields.set(key, req.get_param_value(key)); + } + } +} + +class HttpHelper +{ +public: + static bool CheckRequestParam(const httplib::Request& req, httplib::Response& resp, const std::vector& vecKeys, std::string& errmsg) + { + errmsg = ""; + for (auto& key : vecKeys) + { + if (!req.has_param(key)) + { + if (!errmsg.empty()) { errmsg += ","; } + errmsg += "缺少参数[" + key + "]"; + } + } + if (!errmsg.empty()) + { + return false; + } + return true; + } + static void setPagination(PageInfo& pageinfo, std::vector result, NJsonNode& json) + { + json["count"] = pageinfo.total; + json["page"] = pageinfo.index; + json["page_size"] = pageinfo.size; + json["data"] = FieldsToJsonArray(result); + } +}; + +using HandlerFunc = Errcode(HttpEntity::*)(const httplib::Request& req, httplib::Response& resp, NJsonNode& jnode); + +struct HandlerOptions +{ + HandlerFunc func; + std::vector requiredKeys; + std::vector keys; + + + HandlerOptions(HandlerFunc func, const std::vector& requiredKeys) + : func(func), requiredKeys(requiredKeys) + { + } +}; + +static std::map g_mapHttpHandler = +{ + {"/login", HandlerOptions(&HttpEntity::login, {DMUser::ACCOUNT, DMUser::PASSWD})}, + {"/queryUserList", HandlerOptions(&HttpEntity::queryUserList, {"token"})}, + {"/insertUser", HandlerOptions(&HttpEntity::insertUser, {"token", DMUser::ACCOUNT})}, + {"/updateUser", HandlerOptions(&HttpEntity::updateUser, {"token", DMUser::USER_ID})}, + {"/deleteUser", HandlerOptions(&HttpEntity::deleteUser, {"token", DMUser::USER_ID})}, + + {"/queryPermissionList", HandlerOptions(&HttpEntity::queryPermissionList, {"token"})}, + {"/insertPermission", HandlerOptions(&HttpEntity::insertPermission, {"token", DMPermission::NAME})}, + {"/updatePermission", HandlerOptions(&HttpEntity::updatePermission, {"token", DMPermission::PERMISSION_ID})}, + {"/deletePermission", HandlerOptions(&HttpEntity::deletePermission, {"token", DMPermission::PERMISSION_ID})}, + + {"/queryRoleList", HandlerOptions(&HttpEntity::queryRoleList, {"token"})}, + {"/insertRole", HandlerOptions(&HttpEntity::insertRole, {"token", DMRole::NAME})}, + {"/updateRole", HandlerOptions(&HttpEntity::updateRole, {"token", DMRole::ROLE_ID})}, + {"/deleteRole", HandlerOptions(&HttpEntity::deleteRole, {"token", DMRole::ROLE_ID})}, + + {"/queryStationList", HandlerOptions(&HttpEntity::queryStationList, {"token"})}, + {"/insertStation", HandlerOptions(&HttpEntity::insertStation, {"token", DMStation::NAME})}, + {"/updateStation", HandlerOptions(&HttpEntity::updateStation, {"token", DMStation::STATION_ID})}, + {"/deleteStation", HandlerOptions(&HttpEntity::deleteStation, {"token", DMStation::STATION_ID})}, + + {"/queryDeviceList", HandlerOptions(&HttpEntity::queryDeviceList, {"token"})}, + {"/insertDevice", HandlerOptions(&HttpEntity::insertDevice, {"token", DMDevice::NAME})}, + {"/updateDevice", HandlerOptions(&HttpEntity::updateDevice, {"token", DMDevice::DEVICE_ID})}, + {"/deleteDevice", HandlerOptions(&HttpEntity::deleteDevice, {"token", DMDevice::DEVICE_ID})}, + {"/queryDevicTypeDef", HandlerOptions(&HttpEntity::queryDevicTypeDef, {"token"})}, + + {"/queryPolicyList", HandlerOptions(&HttpEntity::queryPolicyList, {"token"})}, + {"/insertPolicy", HandlerOptions(&HttpEntity::insertPolicy, {"token", DMPolicy::NAME})}, + {"/updatePolicy", HandlerOptions(&HttpEntity::updatePolicy, {"token", DMPolicy::POLICY_ID})}, + {"/deletePolicy", HandlerOptions(&HttpEntity::deletePolicy, {"token", DMPolicy::POLICY_ID})}, + + {"/querySystemLogList", HandlerOptions(&HttpEntity::querySystemLogList, {"token"})}, + + {"/queryAlertLogList", HandlerOptions(&HttpEntity::queryAlertLogList, {"token"})}, + + {"/queryPredictionDetail", HandlerOptions(&HttpEntity::queryPredictionDetail, {"token"})}, + + //{"/insert", HandlerOptions(&HttpEntity::insert, {})}, + //{"/update", HandlerOptions(&HttpEntity::update, {})}, + //{"/delete", HandlerOptions(&HttpEntity::delete, {})}, + + +}; + + +void HttpEntity::listen(std::string addr, int port) +{ + for (auto& item : g_mapHttpHandler) + { + std::string name = item.first; + HandlerOptions& handler = item.second; + this->httpsvr.Get(name, [=, &handler](const httplib::Request& req, httplib::Response& resp) + { + NJsonNode json; + Errcode errcode = Errcode::OK; + + if (name != "/login" && Config::option.useToken) + { + // 验证token + std::string token = req.get_param_value("token"); + if (token.empty()) + { + errcode = Errcode::ERR_TOKEN; + } + else + { + User user = Application::data().getUser(token); + if (user.userId.empty()) + { + errcode = Errcode::ERR_TOKEN; + } + } + } + + std::string errmsg; + if (errcode == Errcode::OK) + { + if (!HttpHelper::CheckRequestParam(req, resp, handler.requiredKeys, errmsg)) + { + errcode = Errcode::ERR_PARAM; + } + else + { + errcode = (this->*(handler.func))(req, resp, json); + } + } + + json["errcode"] = errcode; + json["errmsg"] = ErrcodeStr(errcode) + (errmsg.empty() ? "" : (":"+errmsg)); + resp.set_content(json.dump(), "text/plain; charset=utf-8"); + resp.status = 200; + }); + } + + if (addr.empty()) addr = "0.0.0.0"; + httpsvr.listen(addr, port); +} + +void HttpEntity::registGet(std::string name, void (HttpEntity::* func)(const httplib::Request& req, httplib::Response& resp)) +{ + this->httpsvr.Get(name, std::bind(func, this, std::placeholders::_1, std::placeholders::_2)); +} + +Errcode HttpEntity::login(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string userId; + std::string token; + std::string account = req.get_param_value("account"); + std::string passwd = req.get_param_value("passwd"); + + Fields fields; + auto dao = DaoEntity::create(""); + Errcode err = DAO::login(dao, account, passwd, fields); + userId = fields.value(DMUser::USER_ID); + token = Application::data().userLogin(userId, account); + if (err == Errcode::OK) + { + json["token"] = token; + + std::vector vecPermission; + int roleId = fields.get(DMRole::ROLE_ID); + DAO::queryRolePermission(dao, roleId, vecPermission); + + NJsonNode jnode = NJsonNode::array(); + for (auto& item : vecPermission) { jnode.push_back(item.value("name")); } + json["permission"] = jnode; + } + + DAO::insertSystemLogUser(token, "用户登录:" + ErrcodeStr(err), (err==Errcode::OK) ? 0: 1); + return err; +} + +Errcode HttpEntity::queryUserList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string token = req.get_param_value("token"); + PageInfo pageinfo; + pageinfo.index = Utils::toInt(req.get_param_value("page")); + pageinfo.size = Utils::toInt(req.get_param_value("page_size")); + + std::vector result; + auto err = DAO::queryUserList(pageinfo, result); + if (err == Errcode::OK) + { + HttpHelper::setPagination(pageinfo, result, json); + } + DAO::insertSystemLogUser(token, "查询用户列表:" + ErrcodeStr(err), (err==Errcode::OK) ? 0 : 1); + return err; +} + +Errcode HttpEntity::insertUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"account", "name", "gender", "age", "phone", "email", "role_id"}, params); + return DAO::insertUser(params); +} + +Errcode HttpEntity::updateUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"user_id", "name", "gender", "age", "phone", "email", "role_id"}, params); + return DAO::updateUserById(params); +} + +Errcode HttpEntity::deleteUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string userId = req.get_param_value("user_id"); + return DAO::deleteUserById(userId); +} + +Errcode HttpEntity::queryPermissionList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + PageInfo pageinfo; + pageinfo.index = Utils::toInt(req.get_param_value("page")); + pageinfo.size = Utils::toInt(req.get_param_value("page_size")); + + std::vector result; + auto err = DAO::queryPermissionList(pageinfo, result); + HttpHelper::setPagination(pageinfo, result, json); + return err; +} + +Errcode HttpEntity::insertPermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"name", "describe", "is_open"}, params); + return DAO::insertPermission(params); +} + +Errcode HttpEntity::updatePermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"permission_id", "name", "describe", "is_open"}, params); + return DAO::updatePermissionById(params); +} + +Errcode HttpEntity::deletePermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string permissionId = req.get_param_value("permission_id"); + return DAO::deletePermissionById(permissionId); +} + + +Errcode HttpEntity::queryRoleList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string token = req.get_param_value("page"); + PageInfo pageinfo; + pageinfo.index = Utils::toInt(req.get_param_value("page")); + pageinfo.size = Utils::toInt(req.get_param_value("page_size")); + + std::vector result; + auto err = DAO::queryRoleList(pageinfo, result); + // 查询所有的角色权限关联 + if (err == Errcode::OK) + { + std::vector vecPermission; + err = DAO::queryRolePermission(NULL, vecPermission); + if (err != Errcode::OK) + { + return err; + } + + std::map> mapPermission; + for (auto& item: vecPermission) + { + std::string roleId = item.value("role_id"); + auto& v = mapPermission[roleId]; + NJsonNode jnode; + jnode["id"] = item.value("permission_id"); + jnode["name"] = item.value("permission_name"); + v.push_back(jnode); + } + + HttpHelper::setPagination(pageinfo, result, json); + if (json.contains("data")) + { + for (auto& item : json["data"]) + { + std::string roleId = item["role_id"]; + item["permission"] = mapPermission[roleId]; + } + } + } + return err; +} + +Errcode HttpEntity::insertRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"name", "describe", "is_open", "permission"}, params); + return DAO::insertRole(params); +}; +Errcode HttpEntity::updateRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"role_id", "name", "describe", "is_open", "permission"}, params); + return DAO::updateRoleById(params); +}; +Errcode HttpEntity::deleteRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string roleId = req.get_param_value(DMRole::ROLE_ID); + return DAO::remove(NULL, DMRole::TABLENAME, DMRole::ROLE_ID, roleId); +}; + +Errcode HttpEntity::queryStationList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + PageInfo pageinfo; + pageinfo.index = Utils::toInt(req.get_param_value("page")); + pageinfo.size = Utils::toInt(req.get_param_value("page_size")); + + std::vector result; + auto err = DAO::queryStationList(pageinfo, result); + HttpHelper::setPagination(pageinfo, result, json); + return err; +}; + +Errcode HttpEntity::insertStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + + Fields params; + GetRequestParam(req, {"name", "address", "lon", "lat", "tel", "capacity", "status"}, params); + return DAO::insertStation(params); +}; + +Errcode HttpEntity::updateStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"station_id", "name", "address", "lon", "lat", "tel", "capacity", "status"}, params); + return DAO::updateStationById(params); +}; + +Errcode HttpEntity::deleteStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string primaryKey = DMStation::STATION_ID; + return DAO::remove(NULL, DMStation::TABLENAME, primaryKey, req.get_param_value(primaryKey)); +}; + + +Errcode HttpEntity::queryDeviceList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + PageInfo pageinfo; + pageinfo.index = Utils::toInt(req.get_param_value("page")); + pageinfo.size = Utils::toInt(req.get_param_value("page_size")); + + std::vector result; + auto err = DAO::queryDeviceList(pageinfo, result); + HttpHelper::setPagination(pageinfo, result, json); + return err; +}; + +Errcode HttpEntity::insertDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params); + return DAO::insertDevice(params); +}; +Errcode HttpEntity::updateDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"device_id", "station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params); + return DAO::updateDeviceById(params); +}; +Errcode HttpEntity::deleteDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string primaryKey = DMDevice::DEVICE_ID; + return DAO::remove(NULL, DMDevice::TABLENAME, primaryKey, req.get_param_value(primaryKey)); +}; +Errcode HttpEntity::queryDevicTypeDef(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + std::string sql = "SELECT device_type_id, name FROM def_device_type;"; + std::vector result; + auto err = DAO::exec(NULL, sql, result); + json["data"] = FieldsToJsonArray(result); + return err; +} + +Errcode HttpEntity::queryPolicyList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + PageInfo pageinfo; + pageinfo.index = Utils::toInt(req.get_param_value("page")); + pageinfo.size = Utils::toInt(req.get_param_value("page_size")); + + std::vector result; + auto err = DAO::queryPolicyList(pageinfo, result); + HttpHelper::setPagination(pageinfo, result, json); + return err; +}; +Errcode HttpEntity::insertPolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"type", "name", "describe", "value", "is_open"}, params); + return DAO::insertPolicy(params); +}; +Errcode HttpEntity::updatePolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"policy_id", "type", "describe", "value", "is_open"}, params); + return DAO::updatePolicyById(params); +}; +Errcode HttpEntity::deletePolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + return DAO::deletePolicyById(req.get_param_value("prolicy_id")); +}; + +Errcode HttpEntity::querySystemLogList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + PageInfo pageinfo; + pageinfo.index = Utils::toInt(req.get_param_value("page")); + pageinfo.size = Utils::toInt(req.get_param_value("page_size")); + + std::vector result; + auto err = DAO::querySystemLogList(pageinfo, result); + HttpHelper::setPagination(pageinfo, result, json); + return err; +} + +//Errcode insertSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); +Errcode HttpEntity::updateSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"log_id", "status"}, params); + return DAO::updateSystemLogById(params); +} + +Errcode HttpEntity::queryAlertLogList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + PageInfo pageinfo; + pageinfo.index = Utils::toInt(req.get_param_value("page")); + pageinfo.size = Utils::toInt(req.get_param_value("page_size")); + + std::vector result; + auto err = DAO::queryAlertLogList(pageinfo, result); + HttpHelper::setPagination(pageinfo, result, json); + return err; +} + +//Errcode insertAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); +Errcode HttpEntity::updateAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + Fields params; + GetRequestParam(req, {"log_id", "status"}, params); + return DAO::updateAlertLogById(params); +} + +Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, httplib::Response& resp, NJsonNode& json) +{ + NJsonNode jsonData = NJsonNode::array(); + for (int i = 1; i<=5; i++) + { + NJsonNode jnode; + jnode["datatype"] = i; + NJsonNode jsonValues = NJsonNode::array(); + for (int i = 0; i<1440; ++i) + { + jsonValues.push_back(float(Utils::random(50, 100))); + } + jnode["values"] = jsonValues; + jsonData.push_back(jnode); + } + json["data"] = jsonData; + return Errcode::OK; +} \ No newline at end of file diff --git a/src/protocol/HttpEntity.h b/src/protocol/HttpEntity.h new file mode 100644 index 0000000..10aa165 --- /dev/null +++ b/src/protocol/HttpEntity.h @@ -0,0 +1,58 @@ +#include "httplib.h" +#include "common/JsonN.h" +#include "errcode.h" + +class HttpEntity +{ +public: + httplib::Server httpsvr; + void listen(std::string addr, int port); + void registGet(std::string name, void (HttpEntity::* func)(const httplib::Request& req, httplib::Response& resp)); + + //void onGet(const httplib::Request& req, httplib::Response& resp); + + Errcode login(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + Errcode queryUserList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode insertUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode updateUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode deleteUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + Errcode queryPermissionList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode insertPermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode updatePermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode deletePermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + + Errcode queryRoleList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode insertRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode updateRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode deleteRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + Errcode queryStationList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode insertStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode updateStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode deleteStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + Errcode queryDeviceList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode insertDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode updateDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode deleteDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode queryDevicTypeDef(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + Errcode queryPolicyList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode insertPolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode updatePolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode deletePolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + Errcode querySystemLogList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + //Errcode insertSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode updateSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + Errcode queryAlertLogList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + //Errcode insertAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + Errcode updateAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + + Errcode queryPredictionDetail(const httplib::Request& req, httplib::Response& resp, NJsonNode& json); + +}; \ No newline at end of file diff --git a/src/protocol/TcpEntity.cpp b/src/protocol/TcpEntity.cpp index f6bd2c1..8f51e5c 100644 --- a/src/protocol/TcpEntity.cpp +++ b/src/protocol/TcpEntity.cpp @@ -15,25 +15,18 @@ static std::string ToHexText(std::string s) return ss.str(); } -TcpEntity::TcpEntity(TcpHandler* handler) - : handler_(handler), isClient_(true) +TcpEntity::TcpEntity() { } - TcpEntity::~TcpEntity() { } -void TcpEntity::setHandler(TcpHandler* handler) +void TcpEntity::setAddr(string addr, int port, int commtype) { - handler_ = handler; -} - -void TcpEntity::setHost(string host, int port, bool isClient) -{ - host_ = host; - port_ = port; - isClient_ = isClient; + this->addr = addr; + this->port = port; + this->commtype = commtype; } @@ -44,11 +37,8 @@ void TcpEntity::setReconnect(int ms) int TcpEntity::start() { - if (isAlive_) - { - return 1; - } - isAlive_ = true; + if (alive) { return 1; } + alive = true; WSADATA wsaData; if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) @@ -56,97 +46,88 @@ int TcpEntity::start() return -1; } - sockaddr_.sin_family = AF_INET; - sockaddr_.sin_port = htons(port_); - sockaddr_.sin_addr.S_un.S_addr = (isClient_ ? inet_addr(host_.c_str()) : htonl(INADDR_ANY)); + sockaddr.sin_family = AF_INET; + sockaddr.sin_port = htons(port); + sockaddr.sin_addr.S_un.S_addr = ((commtype != 0) ? inet_addr(addr.c_str()) : htonl(INADDR_ANY)); - std::thread([=]() { this->runThreadTcp(); }).detach(); + std::thread([=]() { + //if (isRequestClose_) { break; } + //std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + std::cout << "TCP thread start ..." << std::endl; + if ((commtype != 0)) + { + this->runClientLoop(); + } + else + { + this->runServerLoop(); + } + alive = false; + }).detach(); return 0; } -void TcpEntity::runThreadTcp() -{ - //if (isRequestClose_) { break; } - //std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - - std::cout << "TCP thread start ..." << std::endl; - if (isClient_) - { - this->runClientLoop(); - } - else - { - this->runServerLoop(); - } - isAlive_ = false; -} - -void TcpEntity::close() -{ - isCloseRequest_ = true; -} void TcpEntity::runServerLoop() { - sock_ = ::socket(AF_INET, SOCK_STREAM, 0); + sock = ::socket(AF_INET, SOCK_STREAM, 0); // 绑定套接字 【注意】functional中定义了bind与winsock2的定义发生重载导致异常,这里需要使用::bind(加::) - if (::bind(sock_, (SOCKADDR*)&sockaddr_, sizeof(SOCKADDR)) == SOCKET_ERROR) + if (::bind(sock, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR)) == SOCKET_ERROR) { - std::cout << "TCP server bind [" << hostport() << "] failed." << std::endl; + std::cout << "TCP server bind [" << getAddrPort() << "] failed." << std::endl; return; } // 启动监听,准备接收客户请求 - if (::listen(sock_, 5) == SOCKET_ERROR) + if (::listen(sock, 5) == SOCKET_ERROR) { - std::cout << "TCP server listen [" << hostport() << "] failed." << std::endl; + std::cout << "TCP server listen [" << getAddrPort() << "] failed." << std::endl; return; } int addrlen = sizeof(SOCKADDR); while (1) { - if (isCloseRequest_) { break; } + if (isCloseRequest) { break; } + // 等待client连接请求 Client client; - - // 等待客户请求到来 - client.sock = ::accept(sock_, (SOCKADDR*)&client.sock_addr, &addrlen); + client.sock = ::accept(sock, (SOCKADDR*)&client.sockaddr, &addrlen); if (client.sock == INVALID_SOCKET) { break; } - client.host = inet_ntoa(client.sock_addr.sin_addr); - - // 存储客户端的连接信息 - vecClient_.push_back(client); + // client连接成功,存储信息 + client.host = inet_ntoa(client.sockaddr.sin_addr); + vecClient.push_back(client); - // 创建线程处理 + // 创建client处理线程 std::thread th([=]() { this->runServerRecvLoop(client, client.host); }); th.detach(); } - ::closesocket(sock_); - // 连接关闭 - for (auto iter = vecClient_.begin(); iter != vecClient_.end(); ++iter) + // 客户端的连接关闭 + for (auto iter = vecClient.begin(); iter != vecClient.end(); ++iter) { ::closesocket(iter->sock); - vecClient_.erase(iter); + vecClient.erase(iter); } - isCloseRequest_ = false; + + // 关闭socket + ::closesocket(sock); + isCloseRequest = false; } void TcpEntity::runServerRecvLoop(Client client, std::string client_name) { - std::vector buf(1024000, 0); + std::vector buf(10240, 0); while (1) { - if (isCloseRequest_ || !isAlive_) - { - break; - } - memset(buf.data(), 0, buf.size()); + if (isCloseRequest || !alive) { break; } + // 接收数据 + memset(buf.data(), 0, buf.size()); int n = ::recv(client.sock, &buf[0], buf.size(), 0); // 需要判断 errno是否等于 EINTR 。如果errno == EINTR 则说明recv函数是由于程序接收到信号后返回的,socket连接还是正常的 if (n <= 0 && GetLastError() != EINTR) @@ -159,11 +140,11 @@ void TcpEntity::runServerRecvLoop(Client client, std::string client_name) } } // 连接关闭 - for (auto iter = vecClient_.begin(); iter != vecClient_.end(); ++iter) + for (auto iter = vecClient.begin(); iter != vecClient.end(); ++iter) { if (iter->sock == client.sock) { - vecClient_.erase(iter); + vecClient.erase(iter); break; } } @@ -172,31 +153,31 @@ void TcpEntity::runServerRecvLoop(Client client, std::string client_name) void TcpEntity::runClientLoop() { // 数据缓存 - std::vector buf(1024000, 0); + std::vector buf(10240, 0); while (1) { - if (isCloseRequest_) { break; } + if (isCloseRequest) { break; } //创建套接字,向服务器发出连接请求 - sock_ = ::socket(AF_INET, SOCK_STREAM, 0); - if (::connect(sock_, (SOCKADDR*)&sockaddr_, sizeof(SOCKADDR)) != SOCKET_ERROR) + sock = ::socket(AF_INET, SOCK_STREAM, 0); + if (::connect(sock, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR)) != SOCKET_ERROR) { - isConnected_ = true; - std::cout << "TCP client connect to [" << hostport() << "] success." << std::endl; + isConnected = true; + std::cout << "TCP client connect to [" << getAddrPort() << "] success." << std::endl; // 连接服务器成功,循环等待接受消息 while (1) { - if (isCloseRequest_) { break; } + if (isCloseRequest) { break; } memset(buf.data(), 0, buf.size()); - int n = ::recv(sock_, buf.data(), buf.size(), 0); + int n = ::recv(sock, buf.data(), buf.size(), 0); if (n <= 0 && GetLastError() != EINTR) { // TCP通讯异常, 关闭连接 - ::closesocket(sock_); - isConnected_ = false; + ::closesocket(sock); + isConnected = false; break; } else @@ -207,75 +188,55 @@ void TcpEntity::runClientLoop() } else { - isConnected_ = false; - std::cout << "TCP client connect to [" << hostport() << "] failed." << std::endl; + isConnected = false; + std::cout << "TCP client connect to [" << getAddrPort() << "] failed." << std::endl; } // 连接异常 if (tReconnect_ > 0) { // 重新连接 - std::cout << "TCP client [" << hostport() << "] reconnect (" << tReconnect_ << ")." << std::endl; + std::cout << "TCP client [" << getAddrPort() << "] reconnect (" << tReconnect_ << ")." << std::endl; //std::this_thread::sleep_for(std::chrono::microseconds(tReconnect_)); Sleep(tReconnect_); } else { // 关闭线程 - std::cout << "TCP client [" << hostport() << "] close." << std::endl; + std::cout << "TCP client [" << getAddrPort() << "] close." << std::endl; break; } } - if (sock_ != INVALID_SOCKET) + if (sock != INVALID_SOCKET) { - ::closesocket(sock_); - sock_ = INVALID_SOCKET; - isConnected_ = false; + ::closesocket(sock); + sock = INVALID_SOCKET; + isConnected = false; } - if (isCloseRequest_) + if (isCloseRequest) { } - isCloseRequest_ = false; + isCloseRequest = false; } -bool TcpEntity::sendData(std::string data, std::string clientId) +bool TcpEntity::write(std::string data) { - if (isClient_) + if (commtype == 0) { - // #客户端 - if (sock_ == INVALID_SOCKET) + // #服务端 + if (vecClient.size() <= 0) { return false; } + for (auto& client : vecClient) { - //Spdlogger::error("TCP client send data failed, connect error, invalid socket, device: {}:{}.", this->type_, client_code); - return false; + std::string clientAddr = inet_ntoa(client.sockaddr.sin_addr); + ::send(client.sock, data.c_str(), data.size(), 0); } - int len = ::send(sock_, data.c_str(), data.size(), 0); - //Spdlogger::info("TCP client send data success, data length={}, device: {}:{}.", len, this->type_, client_code); - return (len > 0); + return true; } else { - if (vecClient_.size() <= 0) - { - return false; - } - for (auto& client : vecClient_) - { - std::string client_addr = inet_ntoa(client.sock_addr.sin_addr); - ::send(client.sock, data.c_str(), data.size(), 0); - - } - - return true; + // #客户端 + if (sock == INVALID_SOCKET) { return false; } + int len = ::send(sock, data.c_str(), data.size(), 0); + return (len > 0); } } - - -bool TcpEntity::isAlive() -{ - return isAlive_; -} - -bool TcpEntity::isConnected() -{ - return isConnected_; -} diff --git a/src/protocol/TcpEntity.h b/src/protocol/TcpEntity.h index 0d53bbf..512efc5 100644 --- a/src/protocol/TcpEntity.h +++ b/src/protocol/TcpEntity.h @@ -11,6 +11,12 @@ using namespace std; +enum class ETcpType +{ + SERVER = 0, + CLIENT = 1, +}; + enum class ETcpEvent { NUL = 0, // @@ -32,80 +38,54 @@ class TcpEntity : public CommEntity, public std::enable_shared_from_this parser = nullptr; }; public: - // 初始化服务端 - TcpEntity(TcpHandler* handler = nullptr); + TcpEntity(); ~TcpEntity(); int start() override; - void close() override; - void runThreadTcp(); - - void setHost(string host, int port, bool is_client); - std::string host() { return host_; } - int port() { return port_; } - std::string hostport() { return host_ + ":" + std::to_string(port_); } + void setAddr(string host, int port, int commtype); + std::string getAddr() { return addr; } + std::string getAddrPort() { return addr + ":" + std::to_string(port); } + int getPort() { return port; } void setReconnect(int ms); - - bool isClient() { return isClient_; } - - void setHandler(TcpHandler* handler); - - bool sendData(std::string data, std::string clientId=""); - - bool isAlive(); - bool isConnected(); - - - std::shared_ptr parser = nullptr; + bool write(std::string data); private: void runServerLoop(); void runServerRecvLoop(Client client, std::string client_name); - void runClientLoop(); private: // 本机的SOCKET对象 - SOCKET sock_ = INVALID_SOCKET; + SOCKET sock = INVALID_SOCKET; // socket addr信息 - SOCKADDR_IN sockaddr_; - - // TCP类型是否是客户端: true: 客户端, false: 服务端 - bool isClient_ = true; + SOCKADDR_IN sockaddr {}; // 通讯地址,作为客户端时有效 - std::string host_; + std::string addr; // 通讯端口 - int port_ = 0; + int port = 0; // 重连间隔时间,单位秒 int tReconnect_ = 0; // 作为服务端时连接的客户端SOCKET - std::vector vecClient_; - - // 回调处理对象 - TcpHandler* handler_ = nullptr; - - bool isAlive_ = false; - bool isCloseRequest_ = false; - bool isConnected_ = false; + std::vector vecClient; // 状态更新时间戳 - int64_t ts_; + int64_t ts_ = 0; - int64_t tsHeartbeat_=0; + int64_t tsHeartbeat_ = 0; }; class TcpHandler diff --git a/src/pv/MaskMain.cpp b/src/pv/MaskMain.cpp index 8e13a81..c3e46a1 100644 --- a/src/pv/MaskMain.cpp +++ b/src/pv/MaskMain.cpp @@ -27,10 +27,10 @@ int MaskMain::initUI() PvApp::label(p, PV_ID_MAIN, 0, 0, 1920, 1080, "", "background-color: rgb(1, 32, 54)"); PvApp::image(p, 0, 0, 0, 1920, 90, "bkgHead.png"); - ui.datetime = PvApp::label(p, PV_ID_MAIN, 10, 30, 420, 30, GetDateTimeWeekday(), qss::label(20)); + ui.datetime = PvApp::label(p, PV_ID_MAIN, 10, 30, 420, 30, GetDateTimeWeekday(), QSS::label(20)); pvSetAlignment(p, ui.datetime, AlignCenter); - int idStationTitle = PvApp::label(p, 0, 620, 0, 660, 90, "能源站监控与运行管理系统", qss::label(48)); + int idStationTitle = PvApp::label(p, 0, 620, 0, 660, 90, "能源站监控与运行管理系统", QSS::label(48)); pvSetAlignment(p, idStationTitle, AlignCenter); // 初始化子页面 @@ -81,7 +81,7 @@ int MaskMain::initUI() std::string& title = vecMenuItems[i]; EPvCode statusTmp = PvApp::getPvCode(title); int x = x0 + (w+margin)*i; - int id = PvApp::button(p, 0, x, y, w, h, title, (statusTmp == pvcode_) ? qss::BTN_ACTIVE : qss::BTN); + int id = PvApp::button(p, 0, x, y, w, h, title, (statusTmp == pvcode_) ? QSS::BTN_ACTIVE : QSS::BTN); mapMenuInfo_[id] = std::make_pair(title, statusTmp); } } diff --git a/src/pv/PvApp.cpp b/src/pv/PvApp.cpp index 87cfcfe..cb24e4f 100644 --- a/src/pv/PvApp.cpp +++ b/src/pv/PvApp.cpp @@ -160,38 +160,38 @@ int PvApp::widget(PARAM* p, int parent, int x, int y, int w, int h) return id; } -int PvApp::label(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string qss) +int PvApp::label(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS) { static const std::string style = "QLabel { border: none; background-color: transparent; } QLabel:disabled { color: gray;}"; int id = PvApp::pvid(p); pvQLabel(p, id, parent); pvSetGeometry(p, id, x, y, w, h); if (!text.empty()) { pvSetText(p, id, text.c_str()); } - pvSetStyleSheet(p, id, qss.empty() ? style.c_str() : qss.c_str()); + pvSetStyleSheet(p, id, QSS.empty() ? style.c_str() : QSS.c_str()); return id; } -int PvApp::labelCenter(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string qss) +int PvApp::labelCenter(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS) { - int id = PvApp::label(p, parent, x, y, w, h, text, qss); + int id = PvApp::label(p, parent, x, y, w, h, text, QSS); pvSetAlignment(p, id, AlignCenter); return id; } -int PvApp::labelAlignCenter(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string qss) +int PvApp::labelAlignCenter(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS) { - int id = PvApp::label(p, parent, x, y, w, h, text, qss); + int id = PvApp::label(p, parent, x, y, w, h, text, QSS); pvSetAlignment(p, id, AlignCenter); return id; } -int PvApp::button(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string qss) +int PvApp::button(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS) { int id = PvApp::pvid(p); pvQPushButton(p, id, parent); pvSetGeometry(p, id, x, y, w, h); if (!text.empty()) { pvSetText(p, id, text.c_str()); } - pvSetStyleSheet(p, id, qss.empty() ? qss::button().c_str() : qss.c_str()); + pvSetStyleSheet(p, id, QSS.empty() ? QSS::button().c_str() : QSS.c_str()); return id; } @@ -208,7 +208,7 @@ int PvApp::combox(PARAM* p, int parent, int x, int y, int w, int h, const std::v int id = PvApp::pvid(p); pvQComboBox(p, id, parent, 0, 0); pvSetGeometry(p, id, x, y, w, h); - pvSetStyleSheet(p, id, qss::COMBOX_14.c_str()); + pvSetStyleSheet(p, id, QSS::COMBOX_14.c_str()); for (int i=0; i& vecItems, int index=0); - static int textedit(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string qss = ""); + static int textedit(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS = ""); - static int multiTextedit(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string qss = ""); + static int multiTextedit(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS = ""); static int radioButton(PARAM* p, int parent, int x, int y, int w, int h, std::string text); diff --git a/src/pv/PvChart.cpp b/src/pv/PvChart.cpp index ffdad68..de6ce6f 100644 --- a/src/pv/PvChart.cpp +++ b/src/pv/PvChart.cpp @@ -249,8 +249,8 @@ PvChartCurve::PvChartCurve(PARAM* p, int parent, int x, int y, int w, int h) : P void PvChartCurve::setBackground(PARAM* p, int r, int g, int b) { qpwSetCanvasBackground(p, plotId_, r, g, b); - std::string qss = "border: none; border-radius: 0px; background-color: " + PvColor(r, g, b).rgb() + ";"; - pvSetStyleSheet(p, pvid_, qss.c_str()); + std::string QSS = "border: none; border-radius: 0px; background-color: " + PvColor(r, g, b).rgb() + ";"; + pvSetStyleSheet(p, pvid_, QSS.c_str()); } void PvChartCurve::setLabelYLeft(std::string title, float min, float max, float step) diff --git a/src/pv/PvPopWidget.cpp b/src/pv/PvPopWidget.cpp index 99af3f3..bd8b44b 100644 --- a/src/pv/PvPopWidget.cpp +++ b/src/pv/PvPopWidget.cpp @@ -17,24 +17,24 @@ PvPopWidget::PvPopWidget(PARAM* p, int width, int height, std::string name) ui.bkgL = PvApp::label(p, ui.widget, 0, 0, 60, height, "", "background-color: transparent; border: 0 solid rgb(42, 149, 245); border-width: 5px 0 5px 5px;"); ui.bkgR = PvApp::label(p, ui.widget, width-60, 0, 60, height, "", "background-color: transparent; border: 0 solid rgb(42, 149, 245); border-width: 5px 5px 5px 0;"); - ui.title = PvApp::label(p, ui.widget, 20, 10, width-20, 30, name, qss::label(20)); - PvApp::label(p, ui.widget, 20, 40, width*0.5-20, 3, "", qss::QSS_UNDERLINE); + ui.title = PvApp::label(p, ui.widget, 20, 10, width-20, 30, name, QSS::label(20)); + PvApp::label(p, ui.widget, 20, 40, width*0.5-20, 3, "", QSS::QSS_UNDERLINE); { int w = 100, h = 40, offset = 50; int x = (width- w*2 - offset) *0.5; int y = height - h - 40; - ui.btnOK = PvApp::button(p, ui.widget, x, y, w, h, "确定", qss::BTN_CONFIRM); + ui.btnOK = PvApp::button(p, ui.widget, x, y, w, h, "确定", QSS::BTN_CONFIRM); PvApp::bind(p, PvEvent::BUTTON_EVENT, ui.btnOK, [=](std::string) { if (callbackConfirm) { callbackConfirm(); } }); - ui.btnCancel = PvApp::button(p, ui.widget, x+w+offset, y, w, h, "取消", qss::BTN_CANCEL); + ui.btnCancel = PvApp::button(p, ui.widget, x+w+offset, y, w, h, "取消", QSS::BTN_CANCEL); PvApp::bind(p, PvEvent::BUTTON_EVENT, ui.btnCancel, [=](std::string) { this->show(false); }); } - ui.msg = PvApp::label(p, ui.widget, 50, height-110, width-100, 24, "", qss::label(14, "red")); + ui.msg = PvApp::label(p, ui.widget, 50, height-110, width-100, 24, "", QSS::label(14, "red")); } std::shared_ptr PvPopWidget::addParamLine(std::string type, std::string key, std::string title, int x, int y, bool editable/* = true*/) @@ -42,7 +42,7 @@ std::shared_ptr PvPopWidget::addParamLine(std::string ty auto line = std::make_shared(type, key); mapLines[key] = line; - PvApp::label(p, ui.widget, x, y, lineKeyWidth, lineHeight, title, qss::label(15)); + PvApp::label(p, ui.widget, x, y, lineKeyWidth, lineHeight, title, QSS::label(15)); if (type == "textedit") { line->widget = PvApp::textedit(p, ui.widget, x+lineKeyWidth, y, lineValWidth, lineHeight, ""); diff --git a/src/pv/PvStyle.cpp b/src/pv/PvStyle.cpp index a9a6e0f..a9f553c 100644 --- a/src/pv/PvStyle.cpp +++ b/src/pv/PvStyle.cpp @@ -6,7 +6,7 @@ std::string BUTTON() return ""; } -namespace qss +namespace QSS { std::string label(int fontSize, std::string color, std::string bkgcolor, std::string border) { @@ -44,11 +44,11 @@ namespace qss return ss.str(); } - const std::string LABEL_BKG_1 = qss::label(14, "", "rgb(5, 47, 77)", "none; border-radius:5px"); + const std::string LABEL_BKG_1 = QSS::label(14, "", "rgb(5, 47, 77)", "none; border-radius:5px"); - const std::string LABEL_BKG_2 = qss::label(14, "", "rgb(8, 54, 91)", "none; border-radius:5px"); + const std::string LABEL_BKG_2 = QSS::label(14, "", "rgb(8, 54, 91)", "none; border-radius:5px"); - const std::string LABEL_BOX = qss::label(16, "", "rgba(200,200,200,20)", "none; border-radius:2px") + const std::string LABEL_BOX = QSS::label(16, "", "rgba(200,200,200,20)", "none; border-radius:2px") + "QLabel:hover {border: 1px solid rgb(1, 183, 209);}"; const std::string QSS_BOX_ACTIVE = @@ -56,10 +56,10 @@ namespace qss "QLabel:hover {border: 1px solid rgb(1, 183, 209);}" "QLabel:disabled { color:rgb(150,150,150);}"; - const std::string LABEL_TITLE = qss::label(16, "rgb(99, 196, 216)", "", "none; padding-top: 0px;"); + const std::string LABEL_TITLE = QSS::label(16, "rgb(99, 196, 216)", "", "none; padding-top: 0px;"); - const std::string LABEL_KEY = qss::label(13, "rgb(180,180,180)", "", ""); - const std::string LABEL_VAL = qss::label(14, "", "", ""); + const std::string LABEL_KEY = QSS::label(13, "rgb(180,180,180)", "", ""); + const std::string LABEL_VAL = QSS::label(14, "", "", ""); const std::string BTN = "QPushButton { background-color:rgb(4, 96, 142);border-radius:10px;border:0px solid rgb(10,120,215);color:white;font:bold 18px;}" @@ -133,16 +133,16 @@ namespace qss /////////////////////////////////////////////////////////////////////////////////////////////////// /// === 表格 const std::string QSS_TABLE = - qss::label(15, "", "rgb(7, 46, 74)", "1px solid rgb(28, 121, 122)"); + QSS::label(15, "", "rgb(7, 46, 74)", "1px solid rgb(28, 121, 122)"); // 表头标签 const std::string QSS_TABLE_HEAD = - qss::label(14, "", "rgb(18, 93, 113)", "1px solid rgb(120, 120, 120); border-style:inset solid"); + QSS::label(14, "", "rgb(18, 93, 113)", "1px solid rgb(120, 120, 120); border-style:inset solid"); //"background-color: rgb(18, 93, 113); color:rgb(255, 255, 255); font:bold 16px;" //"border-width:1 1 1 1px; border-style:inset solid; border-color:rgb(120, 120, 120);"; // 单元格 - const std::string QSS_TABLE_CELL = qss::label(14, "", "", "none; padding-left: 10px"); + const std::string QSS_TABLE_CELL = QSS::label(14, "", "", "none; padding-left: 10px"); const std::string QSS_TABLE_BTN_VIEW = "QPushButton { background-color: rgb(7, 46, 74); color:white; border-radius:2px; border:none; font:bold 14px;}" diff --git a/src/pv/PvStyle.h b/src/pv/PvStyle.h index fe82bee..b8b2e2a 100644 --- a/src/pv/PvStyle.h +++ b/src/pv/PvStyle.h @@ -1,7 +1,7 @@ #pragma once #include -namespace qss +namespace QSS { std::string label(int fontSize = 14, std::string color = "", std::string bkgcolor = "", std::string border = ""); @@ -23,8 +23,6 @@ namespace qss extern const std::string LINEEDIT; - - extern const std::string QSS_BOX_ACTIVE; extern const std::string LABEL_TITLE; diff --git a/src/pv/PvTable.cpp b/src/pv/PvTable.cpp index 6fb837b..9639240 100644 --- a/src/pv/PvTable.cpp +++ b/src/pv/PvTable.cpp @@ -20,7 +20,7 @@ PvTable::PvTable(PARAM* p, int parent, int x, int y, int w, int irow, Options& o pvid = PvApp::widget(p, parent, x, y, w, h+1); // 表格的背景色和边框样式 - PvApp::label(p, pvid, 0, 0, w, h+1, "", qss::QSS_TABLE); + PvApp::label(p, pvid, 0, 0, w, h+1, "", QSS::QSS_TABLE); vecHeads.resize(0); vecRows.resize(nrow); @@ -30,8 +30,8 @@ PvTable::PvTable(PARAM* p, int parent, int x, int y, int w, int irow, Options& o for (int row = 0; row < nrow; row++) { int y = row * option.row_height + (option.show_header ? option.head_height : 0); - string qss = (row % 2 != 0) ? qss::QSS_TABLE_ROW_0 : qss::QSS_TABLE_ROW_1; - int widgetRow = PvApp::label(p, pvid, 1, y, rect.w-2, option.row_height, "", qss); + string QSS = (row % 2 != 0) ? QSS::QSS_TABLE_ROW_0 : QSS::QSS_TABLE_ROW_1; + int widgetRow = PvApp::label(p, pvid, 1, y, rect.w-2, option.row_height, "", QSS); pvHide(p, widgetRow); vecRows[row].widget = widgetRow; } @@ -48,13 +48,13 @@ void PvTable::addHead(string id, string text, int width, vector 0 && irow <= vecRows.size()) { - if (v) { qss = "background-color:rgba(14,45,60,200);border:1px solid rgba(255,0,0,100);"; } - pvSetStyleSheet(p, vecRows[irow].widget, qss.c_str()); + if (v) { QSS = "background-color:rgba(14,45,60,200);border:1px solid rgba(255,0,0,100);"; } + pvSetStyleSheet(p, vecRows[irow].widget, QSS.c_str()); } } @@ -110,11 +110,11 @@ void PvTable::addOperate(vector vecOpt) // 创建表头的标签 if (option.show_header) { - PvApp::label(p, pvid, posCol, 0, rect.w - posCol, option.head_height, "操作", qss::QSS_TABLE_HEAD); + PvApp::label(p, pvid, posCol, 0, rect.w - posCol, option.head_height, "操作", QSS::QSS_TABLE_HEAD); } for (int row = 0; row < nrow; ++row) { - int cellWidget = PvApp::label(p, vecRows[row].widget, posCol, 0, rect.w - posCol, option.row_height, "", qss::QSS_TABLE_CELL); + int cellWidget = PvApp::label(p, vecRows[row].widget, posCol, 0, rect.w - posCol, option.row_height, "", QSS::QSS_TABLE_CELL); vecOper.push_back({ cellWidget, vector() }); auto& vec_opt_btn_ = vecOper.back().second; int x = 5, w = 60; @@ -122,7 +122,7 @@ void PvTable::addOperate(vector vecOpt) { auto& title = vecOpt[i]; w = 20 + 15 * title.size() / 3; - int btn = PvApp::button(p, cellWidget, x, 4, w, 24, title, qss::button(14, "", "", "none; border-radius: 0px")); + int btn = PvApp::button(p, cellWidget, x, 4, w, 24, title, QSS::button(14, "", "", "none; border-radius: 0px")); PvApp::bind(p, PvEvent::BUTTON_EVENT, btn, [=](std::string) { if (callbackOper) { callbackOper(row, 0, title); } }); @@ -239,7 +239,7 @@ PvPagination::PvPagination(PARAM* p, int parent, int x, int y, int n) PvApp::bind(p, PvEvent::BUTTON_EVENT, btn, [=](string) { this->activePage(i, true); }); } btnNext = PvApp::button(p, pvid, 32, 0, 30, 30, ">", STYLE_NORMAL); - labelInfo = PvApp::label(p, pvid, 2*32, 0, 80, 30, " 共0页", qss::label(14, "rgb(27, 220, 224)")); + labelInfo = PvApp::label(p, pvid, 2*32, 0, 80, 30, " 共0页", QSS::label(14, "rgb(27, 220, 224)")); pvSetEnabled(p, btnPrev, 0); pvSetEnabled(p, btnNext, 0); diff --git a/src/pv/pages/MaskPageForecast.cpp b/src/pv/pages/MaskPageForecast.cpp index f13949d..e74fe4a 100644 --- a/src/pv/pages/MaskPageForecast.cpp +++ b/src/pv/pages/MaskPageForecast.cpp @@ -4,9 +4,9 @@ static int CreatePanel(PARAM* p, int parentId, int x, int y, int w, int h, std::string title) { - int id = PvApp::label(p, parentId, x, y, w, h, "", qss::LABEL_BKG_2); - PvApp::label(p, id, 10, 10, w, 20, title, qss::STYLE_TITLE_ICON); - PvApp::label(p, id, 20, 30, w, 2, "", qss::QSS_UNDERLINE); + int id = PvApp::label(p, parentId, x, y, w, h, "", QSS::LABEL_BKG_2); + PvApp::label(p, id, 10, 10, w, 20, title, QSS::STYLE_TITLE_ICON); + PvApp::label(p, id, 20, 30, w, 2, "", QSS::QSS_UNDERLINE); return id; } diff --git a/src/pv/pages/MaskPageHome.cpp b/src/pv/pages/MaskPageHome.cpp index 486fa77..42f74a2 100644 --- a/src/pv/pages/MaskPageHome.cpp +++ b/src/pv/pages/MaskPageHome.cpp @@ -19,9 +19,9 @@ void TestPage(PARAM* p) static int CreatePanel(PARAM* p, int parent, int x, int y, int w, int h, std::string title) { - int panelId = PvApp::label(p, parent, x, y, w, h, "", qss::LABEL_BKG_1); - int titleId = PvApp::label(p, panelId, 10, 8, w, 22, title, qss::STYLE_TITLE_ICON); - PvApp::label(p, panelId, 20, 28, w, 2, "", qss::QSS_UNDERLINE); + int panelId = PvApp::label(p, parent, x, y, w, h, "", QSS::LABEL_BKG_1); + int titleId = PvApp::label(p, panelId, 10, 8, w, 22, title, QSS::STYLE_TITLE_ICON); + PvApp::label(p, panelId, 20, 28, w, 2, "", QSS::QSS_UNDERLINE); return panelId; } @@ -35,7 +35,7 @@ static int CreatePanel1(PARAM* p, int parent, int x, int y, int w, int h, std::s static int CreateCard1(PARAM* p, int parentId, int x, int y, int w, int h, std::string title, std::string val) { - int id = PvApp::label(p, parentId, x, y, w, h, "", qss::LABEL_BKG_1); + int id = PvApp::label(p, parentId, x, y, w, h, "", QSS::LABEL_BKG_1); int idTitle = PvApp::label(p, id, 0, h*0.5, w, h*0.5, title, "background:transparent; font: bold 28px;"); int idVal = PvApp::label(p, id, 0, 0, w, h*0.5, val, "background:transparent; font: bold 28px; color:rgb(77,215,240);"); pvSetAlignment(p, idTitle, AlignCenter); @@ -45,7 +45,7 @@ static int CreateCard1(PARAM* p, int parentId, int x, int y, int w, int h, std:: static int CreateCard2(PARAM* p, int parent, int x, int y, int w, int h, std::string title, std::string val) { - int id = PvApp::label(p, parent, x, y, w, h, "", qss::LABEL_BKG_1); + int id = PvApp::label(p, parent, x, y, w, h, "", QSS::LABEL_BKG_1); int idTitle = PvApp::label(p, id, 0, 0, w, h*0.5, title); int idVal = PvApp::label(p, id, 0, h*0.5, w, h*0.5, val, "background:transparent; font: bold 16px; color:rgb(77,215,240);"); @@ -59,15 +59,15 @@ static int CreateBox(PARAM* p, int parent, int x, int y, int w, int h, std::stri int id = PvApp::label(p, parent, x, y, w, h, "", "border-radius:0px; background-color: rgb(7, 45, 66); border: 1px solid rgb(27, 88, 105);"); { int len = 10; - std::string qss = "background-color: transparent; border: 1px solid rgb(0, 218, 216);"; - PvApp::label(p, id, 0, 0, len, len, "", qss + "border-width: 2px 0 0 2px"); - PvApp::label(p, id, w-len, 0, len, len, "", qss + "border-width: 2px 2px 0 0"); - PvApp::label(p, id, w-len, h-len, len, len, "", qss + "border-width: 0 2px 2px 0"); - PvApp::label(p, id, 0, h-len, len, len, "", qss + "border-width: 0 0 2px 2px"); + std::string QSS = "background-color: transparent; border: 1px solid rgb(0, 218, 216);"; + PvApp::label(p, id, 0, 0, len, len, "", QSS + "border-width: 2px 0 0 2px"); + PvApp::label(p, id, w-len, 0, len, len, "", QSS + "border-width: 2px 2px 0 0"); + PvApp::label(p, id, w-len, h-len, len, len, "", QSS + "border-width: 0 2px 2px 0"); + PvApp::label(p, id, 0, h-len, len, len, "", QSS + "border-width: 0 0 2px 2px"); } // "border:none; background-color: transparent; font: bold 14px; padding-bottom: 0px;" int titleId = PvApp::label(p, id, 0, 0, w, h*0.5-2, k); - int valId = PvApp::label(p, id, 0, h*0.5+2, w, h*0.5-2, val, qss::label(16, "rgb(77, 215, 240)")); + int valId = PvApp::label(p, id, 0, h*0.5+2, w, h*0.5-2, val, QSS::label(16, "rgb(77, 215, 240)")); pvSetAlignment(p, titleId, AlignHCenter | AlignBottom); pvSetAlignment(p, valId, AlignHCenter | AlignTop); return valId; @@ -276,7 +276,7 @@ int MaskPageHome::initUI(EPvCode pvcode) // 中间区域 { - int panel = PvApp::label(p, 0, x = 10+500+10, y, w = 880, h1+h2+h3+20, "", qss::LABEL_BKG_1); + int panel = PvApp::label(p, 0, x = 10+500+10, y, w = 880, h1+h2+h3+20, "", QSS::LABEL_BKG_1); ////// 饼图 //int left = PvApp::widget(p, panel, 100, 100, 100, 100); @@ -298,7 +298,7 @@ int MaskPageHome::initUI(EPvCode pvcode) auto popStation = new PopStation(p); popStation->show(0); - int btn = PvApp::button(p, panel, 10, 20, 100, 30, "场站一", qss::BTN); + int btn = PvApp::button(p, panel, 10, 20, 100, 30, "场站一", QSS::BTN); PvApp::bind(p, PvEvent::BUTTON_EVENT, btn, [=](std::string) { popStation->setStatus("场站一"); pvShow(p, popStation->widget); diff --git a/src/pv/pages/MaskPageRunning.cpp b/src/pv/pages/MaskPageRunning.cpp index 397fee3..a01e323 100644 --- a/src/pv/pages/MaskPageRunning.cpp +++ b/src/pv/pages/MaskPageRunning.cpp @@ -8,8 +8,8 @@ static int CreateParamLabel(PARAM* p, int parent, int x, int y, std::string k, std::string v) { - PvApp::label(p, parent, x, y, 70, 30, k, qss::LABEL_KEY); - return PvApp::label(p, parent, x += 70, y, 120, 30, v, qss::LABEL_VAL); + PvApp::label(p, parent, x, y, 70, 30, k, QSS::LABEL_KEY); + return PvApp::label(p, parent, x += 70, y, 120, 30, v, QSS::LABEL_VAL); } class CardDevice : PvObject @@ -22,24 +22,24 @@ public: CardDevice(PARAM* p, int parent, int x, int y) : PvObject(p) { - card_ = PvApp::label(p, parent, x, y, 400, 250, "", qss::QSS_CARD_DEVICE); + card_ = PvApp::label(p, parent, x, y, 400, 250, "", QSS::QSS_CARD_DEVICE); PvApp::label(p, card_, 10, 10, 60, 60, "", "border:none; background-color: rgb(39, 158, 145);"); ui.name = PvApp::label(p, card_, 80, 10, 100, 20, ""); ui.code = PvApp::label(p, card_, 80, 30, 100, 20, ""); - ui.type = PvApp::label(p, card_, 80, 50, 100, 20, "", qss::label(14, "rgb(8, 161, 249)")); + ui.type = PvApp::label(p, card_, 80, 50, 100, 20, "", QSS::label(14, "rgb(8, 161, 249)")); int x1 = 190; - ui.online = PvApp::labelAlignCenter(p, card_, x1, 10, 70, 30, "在线", qss::LABEL_VAL); - ui.running = PvApp::labelAlignCenter(p, card_, x1 += 70, 10, 70, 30, "空闲", qss::LABEL_VAL); - ui.err = PvApp::labelAlignCenter(p, card_, x1 += 70, 10, 70, 30, "正常", qss::LABEL_VAL); + ui.online = PvApp::labelAlignCenter(p, card_, x1, 10, 70, 30, "在线", QSS::LABEL_VAL); + ui.running = PvApp::labelAlignCenter(p, card_, x1 += 70, 10, 70, 30, "空闲", QSS::LABEL_VAL); + ui.err = PvApp::labelAlignCenter(p, card_, x1 += 70, 10, 70, 30, "正常", QSS::LABEL_VAL); - PvApp::labelAlignCenter(p, card_, x1 = 190, 40, 70, 30, "在线状态", qss::LABEL_KEY); - PvApp::labelAlignCenter(p, card_, x1 += 70, 40, 70, 30, "工作状态", qss::LABEL_KEY); - PvApp::labelAlignCenter(p, card_, x1 += 70, 40, 70, 30, "故障状态", qss::LABEL_KEY); + PvApp::labelAlignCenter(p, card_, x1 = 190, 40, 70, 30, "在线状态", QSS::LABEL_KEY); + PvApp::labelAlignCenter(p, card_, x1 += 70, 40, 70, 30, "工作状态", QSS::LABEL_KEY); + PvApp::labelAlignCenter(p, card_, x1 += 70, 40, 70, 30, "故障状态", QSS::LABEL_KEY); - PvApp::label(p, card_, 10, 80, 80, 30, "运行分析:", qss::LABEL_KEY); + PvApp::label(p, card_, 10, 80, 80, 30, "运行分析:", QSS::LABEL_KEY); PvApp::button(p, card_, 80, 83, 60, 24, "查看"); // 默认创建 10 个参数标签: @@ -50,8 +50,8 @@ public: int row = i/2; int col = i%2; int h = 25; - vecParamLabel[i].first = PvApp::label(p, card_, 10 + 200*col, 115 + h*row, 70, h, "参数"+std::to_string(i) + ":", qss::LABEL_KEY); - vecParamLabel[i].second = PvApp::label(p, card_, 10 + 200*col + 70, 115 + h*row, 120, h, "---", qss::LABEL_VAL); + vecParamLabel[i].first = PvApp::label(p, card_, 10 + 200*col, 115 + h*row, 70, h, "参数"+std::to_string(i) + ":", QSS::LABEL_KEY); + vecParamLabel[i].second = PvApp::label(p, card_, 10 + 200*col + 70, 115 + h*row, 120, h, "---", QSS::LABEL_VAL); } } @@ -166,7 +166,7 @@ int MaskPageRunning::initUI(EPvCode pvcode) pvHide(p, security.workspace); { - std::string style = qss::label(20, "white; padding: 0px 0px 0px 10px;", "rgb(8, 54, 91)", "none; border-radius: 5px;"); + std::string style = QSS::label(20, "white; padding: 0px 0px 0px 10px;", "rgb(8, 54, 91)", "none; border-radius: 5px;"); for (int i = 0; i<12; ++i) { int w = 320, h = 240; @@ -174,20 +174,20 @@ int MaskPageRunning::initUI(EPvCode pvcode) int cardId = PvApp::label(p, security.workspace, x, y, w, h, "监控点 " + std::to_string(i+1), style); pvSetAlignment(p, cardId, AlignLeft | AlignTop); - PvApp::label(p, cardId, 10, 40, w-20, h-50, "", qss::label(14, "", "", "8px solid black; border-radius: 0px;")); + PvApp::label(p, cardId, 10, 40, w-20, h-50, "", QSS::label(14, "", "", "8px solid black; border-radius: 0px;")); PvApp::image(p, cardId, (w-77)*0.5, 40+(h-40-77)*0.5, 77, 77, "play1.png"); } } { int w = 320, h = 50; - int pid = PvApp::label(p, security.workspace, 1320, 10, w, 200, "环境温度信息", qss::label(20, "", "", "none;")); + int pid = PvApp::label(p, security.workspace, 1320, 10, w, 200, "环境温度信息", QSS::label(20, "", "", "none;")); pvSetAlignment(p, pid, AlignLeft | AlignTop); - PvApp::label(p, pid, 0, 30, w, 5, "", qss::QSS_UNDERLINE); + PvApp::label(p, pid, 0, 30, w, 5, "", QSS::QSS_UNDERLINE); int x = 0, y = 50; - PvApp::label(p, pid, x, y, w, h, "", qss::label(14, "", "rgb(16, 105, 125)", "none; border-radius: 5px 5px 0px 0px;")); - PvApp::label(p, pid, x, y, w, h*2, "", qss::label(14, "", "", "1px solid rgb(12, 255, 251); border-radius: 5px;")); + PvApp::label(p, pid, x, y, w, h, "", QSS::label(14, "", "rgb(16, 105, 125)", "none; border-radius: 5px 5px 0px 0px;")); + PvApp::label(p, pid, x, y, w, h*2, "", QSS::label(14, "", "", "1px solid rgb(12, 255, 251); border-radius: 5px;")); { w = w/3; PvApp::labelCenter(p, pid, x, y, w, h, "点位"); @@ -200,13 +200,13 @@ int MaskPageRunning::initUI(EPvCode pvcode) } { int w = 320, h = 50; - int pid = PvApp::label(p, security.workspace, 1320, 200, w, 500, "消防信息", qss::label(20, "", "", "none;")); + int pid = PvApp::label(p, security.workspace, 1320, 200, w, 500, "消防信息", QSS::label(20, "", "", "none;")); pvSetAlignment(p, pid, AlignLeft | AlignTop); - PvApp::label(p, pid, 0, 30, w, 5, "", qss::QSS_UNDERLINE); + PvApp::label(p, pid, 0, 30, w, 5, "", QSS::QSS_UNDERLINE); int x = 0, y = 50; - PvApp::label(p, pid, x, y, w, h, "", qss::label(14, "", "rgb(16, 105, 125)", "none; border-radius: 5px 5px 0px 0px;")); - PvApp::label(p, pid, x, y, w, h*9, "", qss::label(14, "", "", "1px solid rgb(12, 255, 251); border-radius: 5px;")); + PvApp::label(p, pid, x, y, w, h, "", QSS::label(14, "", "rgb(16, 105, 125)", "none; border-radius: 5px 5px 0px 0px;")); + PvApp::label(p, pid, x, y, w, h*9, "", QSS::label(14, "", "", "1px solid rgb(12, 255, 251); border-radius: 5px;")); { w = w/2; PvApp::labelCenter(p, pid, x, y, w, h, "点位"); @@ -246,8 +246,8 @@ void MaskPageRunning::initModule(Module& module, std::string name, int x, int y, module.name = name; int deviceNum = activeStation->getDeviceNumByGroup(name); - int pid = module.widget = PvApp::label(p, 0, x, y, w, h, "", qss::LABEL_BOX); - PvApp::label(p, module.widget, 10, 20, w, 30, module.name, qss::LABEL_TITLE); + int pid = module.widget = PvApp::label(p, 0, x, y, w, h, "", QSS::LABEL_BOX); + PvApp::label(p, module.widget, 10, 20, w, 30, module.name, QSS::LABEL_TITLE); pvSetAlignment(p, module.widget, AlignTop | AlignLeft); module.labelPower = PvApp::lineLabel(p, pid, PvRect(10, 80, 180, 30), 70, "总功率:", "60 W"); module.labelNum = PvApp::lineLabel(p, pid, PvRect(10, 110, 180, 30), 70, "设备数量:", std::to_string(deviceNum).c_str()); @@ -268,13 +268,13 @@ void MaskPageRunning::activeBoxPanel(Module* module) { if (activeBox) { - pvSetStyleSheet(p, activeBox->widget, qss::LABEL_BOX.c_str()); + pvSetStyleSheet(p, activeBox->widget, QSS::LABEL_BOX.c_str()); pvHide(p, activeBox->workspace); } activeBox = module; if (activeBox) { - pvSetStyleSheet(p, activeBox->widget, qss::QSS_BOX_ACTIVE.c_str()); + pvSetStyleSheet(p, activeBox->widget, QSS::QSS_BOX_ACTIVE.c_str()); pvShow(p, activeBox->workspace); } diff --git a/src/pv/pages/MaskPageStat.cpp b/src/pv/pages/MaskPageStat.cpp index ff40722..b8e8096 100644 --- a/src/pv/pages/MaskPageStat.cpp +++ b/src/pv/pages/MaskPageStat.cpp @@ -4,9 +4,9 @@ static int CreatePanel(PARAM* p, int parentId, int x, int y, int w, int h, std::string title) { - int id = PvApp::label(p, parentId, x, y, w, h, "", qss::LABEL_BKG_2); - PvApp::label(p, id, 10, 10, w, 20, title, qss::STYLE_TITLE_ICON); - PvApp::label(p, id, 20, 30, w, 2, "", qss::QSS_UNDERLINE); + int id = PvApp::label(p, parentId, x, y, w, h, "", QSS::LABEL_BKG_2); + PvApp::label(p, id, 10, 10, w, 20, title, QSS::STYLE_TITLE_ICON); + PvApp::label(p, id, 20, 30, w, 2, "", QSS::QSS_UNDERLINE); return id; } @@ -52,7 +52,7 @@ static VecStatDef statDef = { int MaskPageStat::initUI(EPvCode pvcode) { - PvApp::label(p, PV_ID_MAIN, 10, 100, 1900, 850, "", qss::LABEL_BKG_1); + PvApp::label(p, PV_ID_MAIN, 10, 100, 1900, 850, "", QSS::LABEL_BKG_1); if (pvcode == EPvCode::MASK_STAT) { pvcode = EPvCode::MASK_STAT_STORAGE; } std::string curModuleName; @@ -63,7 +63,7 @@ int MaskPageStat::initUI(EPvCode pvcode) std::string moduleName = statDef[i].first; // 创建按钮 bool isActive = (PvApp::getPvCode(moduleName) == pvcode); - int pageBtn = PvApp::button(p, PV_ID_MAIN, 10+(i*190), 110, 180, 40, moduleName, isActive ? qss::QSS_BTN_MGR_ACTIVE : qss::QSS_BTN_MGR); + int pageBtn = PvApp::button(p, PV_ID_MAIN, 10+(i*190), 110, 180, 40, moduleName, isActive ? QSS::QSS_BTN_MGR_ACTIVE : QSS::QSS_BTN_MGR); mapSubpage_[pageBtn] = moduleName; if (isActive) { diff --git a/src/pv/pages/MaskPageSysmgr.cpp b/src/pv/pages/MaskPageSysmgr.cpp index 0d65a6a..9811cd3 100644 --- a/src/pv/pages/MaskPageSysmgr.cpp +++ b/src/pv/pages/MaskPageSysmgr.cpp @@ -79,7 +79,7 @@ MaskPageSysmgr::MaskPageSysmgr(PARAM* p) : PvMask(p) int MaskPageSysmgr::initUI(EPvCode pvcode) { - PvApp::label(p, PV_ID_MAIN, 10, 150, 1900, 790, "", qss::LABEL_BKG_1); + PvApp::label(p, PV_ID_MAIN, 10, 150, 1900, 790, "", QSS::LABEL_BKG_1); if (pvcode == EPvCode::MASK_SYSMGR) { pvcode = EPvCode::MASK_MGR_USER; } @@ -88,7 +88,7 @@ int MaskPageSysmgr::initUI(EPvCode pvcode) { std::string& title = vecPageNames[i]; bool isActive = (PvApp::getPvCode(title) == pvcode); - int idPageBtn = PvApp::button(p, PV_ID_MAIN, 10+(i*110), 100, 100, 40, title, isActive ? qss::QSS_BTN_MGR_ACTIVE : qss::QSS_BTN_MGR); + int idPageBtn = PvApp::button(p, PV_ID_MAIN, 10+(i*110), 100, 100, 40, title, isActive ? QSS::QSS_BTN_MGR_ACTIVE : QSS::QSS_BTN_MGR); mapSubpage_[idPageBtn] = title; } diff --git a/src/pv/pages/PageSysmgr.cpp b/src/pv/pages/PageSysmgr.cpp index 19de603..6d7ae5a 100644 --- a/src/pv/pages/PageSysmgr.cpp +++ b/src/pv/pages/PageSysmgr.cpp @@ -438,12 +438,12 @@ std::string PagePolicy::onPopConfirm(std::shared_ptr pop, Fields& f // === PageSyslog === PageSyslog::PageSyslog(PARAM* p, EPvCode pvcode) : PageTable(p) { - table->addHead(DMSystemLog::LOG_ID, "日志编号", 160, {}); - table->addHead(DMSystemLog::TYPE, "日志类型", 160, {}); - table->addHead(DMSystemLog::USER_ACCOUNT, "用户", 160, {}); - table->addHead(DMSystemLog::CONTENT, "日志详情", 800, {}); - table->addHead(DMSystemLog::STATUS, "状态", 160, {}); - table->addHead(DMSystemLog::CREATE_TIME, "记录时间", 200, {}); + table->addHead(DMLogSystem::LOG_ID, "日志编号", 160, {}); + table->addHead(DMLogSystem::TYPE, "日志类型", 160, {}); + table->addHead(DMLogSystem::USER_ACCOUNT, "用户", 160, {}); + table->addHead(DMLogSystem::CONTENT, "日志详情", 800, {}); + table->addHead(DMLogSystem::STATUS, "状态", 160, {}); + table->addHead(DMLogSystem::CREATE_TIME, "记录时间", 200, {}); table->addOperate({"查看"}); } void PageSyslog::onQueryTable(PageInfo& pageInfo, std::vector& result) @@ -477,12 +477,12 @@ std::string PageSyslog::onPopConfirm(std::shared_ptr pop, Fields& f // === PageAlertlog === PageAlertlog::PageAlertlog(PARAM* p, EPvCode pvcode) : PageTable(p) { - table->addHead(DMAlertLog::LOG_ID, "日志编号", 160, {}); - table->addHead(DMAlertLog::TYPE, "日志类型", 160, {}); - table->addHead(DMAlertLog::DEVICE_ID, "设备ID", 160, {}); - table->addHead(DMAlertLog::CONTENT, "日志详情", 800, {}); - table->addHead(DMAlertLog::STATUS, "状态", 160, {}); - table->addHead(DMAlertLog::CREATE_TIME, "记录时间", 200, {}); + table->addHead(DMLogAlert::LOG_ID, "日志编号", 160, {}); + table->addHead(DMLogAlert::TYPE, "日志类型", 160, {}); + table->addHead(DMLogAlert::DEVICE_ID, "设备ID", 160, {}); + table->addHead(DMLogAlert::CONTENT, "日志详情", 800, {}); + table->addHead(DMLogAlert::STATUS, "状态", 160, {}); + table->addHead(DMLogAlert::CREATE_TIME, "记录时间", 200, {}); table->addOperate({"查看"}); } void PageAlertlog::onQueryTable(PageInfo& pageInfo, std::vector& result) diff --git a/src/pv/pages/PanelPolicy.cpp b/src/pv/pages/PanelPolicy.cpp index eba66db..488cec4 100644 --- a/src/pv/pages/PanelPolicy.cpp +++ b/src/pv/pages/PanelPolicy.cpp @@ -103,7 +103,7 @@ PanelPolicyPeak::PanelPolicyPeak(PARAM* p, int parent, int ix, int iy, int iw, i } if (row == 0 || col == 0) { - std::string qssLabel = (row==0) ? qss::label(14, "", "rgb(19, 93, 114)") : ""; + std::string qssLabel = (row==0) ? QSS::label(14, "", "rgb(19, 93, 114)") : ""; int label = PvApp::label(p, pvid, x+w*col, y+h*row, w, h, text, qssLabel); pvSetAlignment(p, label, AlignCenter); } @@ -127,7 +127,7 @@ PanelPolicyPeak::PanelPolicyPeak(PARAM* p, int parent, int ix, int iy, int iw, i ui.radioPolicy1 = PvApp::radioButton(p, labelPolicy, 80, 0, 80, H, "一充一放"); ui.radioPolicy2 = PvApp::radioButton(p, labelPolicy, 170, 0, 80, H, "两充两放"); - ui.label1 = PvApp::label(p, pvid, 10, y+=30, 610, 90, "第一次充放电过程", qss::label(14, "", "", "1px solid rgb(49, 130, 141)")); + ui.label1 = PvApp::label(p, pvid, 10, y+=30, 610, 90, "第一次充放电过程", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)")); pvSetAlignment(p, ui.label1, AlignLeft | AlignTop); { int y0 = 70; @@ -152,7 +152,7 @@ PanelPolicyPeak::PanelPolicyPeak(PARAM* p, int parent, int ix, int iy, int iw, i ui.arrt1LabelPowerOut = PvApp::textedit(p, labelPowerOut, y0 += 70, 0, 80, H, ""); } - ui.label2 = PvApp::label(p, pvid, 630, y, 610, 90, "第二次充放电过程", qss::label(14, "", "", "1px solid rgb(49, 130, 141)")); + ui.label2 = PvApp::label(p, pvid, 630, y, 610, 90, "第二次充放电过程", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)")); pvSetAlignment(p, ui.label2, AlignLeft | AlignTop); { int y0 = 70; @@ -394,14 +394,14 @@ PanelPolicyRequire::PanelPolicyRequire(PARAM* p, int parent, int ix, int iy, int int y = 10; const int H = 24; - ui.label1 = PvApp::label(p, pvid, 10, y, 600, 80, "响应削峰指令", qss::label(14, "", "", "1px solid rgb(49, 130, 141)")); + ui.label1 = PvApp::label(p, pvid, 10, y, 600, 80, "响应削峰指令", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)")); pvSetAlignment(p, ui.label1, AlignLeft | AlignTop); { ui.dischargeSoc = PvApp::lineTextedit(p, ui.label1, PvRect(10, 40, 270, H), 100, "电池SOC阈值", ""); ui.dischargePower = PvApp::lineTextedit(p, ui.label1, PvRect(300, 40, 270, H), 70, "放电功率", ""); } - ui.label2 = PvApp::label(p, pvid, 10, y += 90, 600, 90, "响应填谷指令", qss::label(14, "", "", "1px solid rgb(49, 130, 141)")); + ui.label2 = PvApp::label(p, pvid, 10, y += 90, 600, 90, "响应填谷指令", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)")); pvSetAlignment(p, ui.label2, AlignLeft | AlignTop); { ui.chargeSoc = PvApp::lineTextedit(p, ui.label2, PvRect(10, 40, 270, H), 100, "电池SOC阈值", ""); @@ -458,7 +458,7 @@ PanelPolicySelf::PanelPolicySelf(PARAM* p, int parent, int ix, int iy, int iw, i int y = 10; const int H = 24; - ui.label1 = PvApp::label(p, pvid, 10, y, 600, 120, "响应削峰指令", qss::label(14, "", "", "1px solid rgb(49, 130, 141)")); + ui.label1 = PvApp::label(p, pvid, 10, y, 600, 120, "响应削峰指令", QSS::label(14, "", "", "1px solid rgb(49, 130, 141)")); pvSetAlignment(p, ui.label1, AlignLeft | AlignTop); { ui.usedSoc = PvApp::lineTextedit(p, ui.label1, PvRect(10, 40, 270, H), 100, "自用电量比值", ""); diff --git a/src/pv/pvmain.cpp b/src/pv/pvmain.cpp index 838fd2a..87fffb8 100644 --- a/src/pv/pvmain.cpp +++ b/src/pv/pvmain.cpp @@ -30,7 +30,7 @@ int pvMain(PARAM* p) // 管理客户端的连接信息 PvUser pvuser; PvApp::setPvUser(p, &pvuser); - XLOGD() << "Browser client connect: s= " << int(p->s) << ", user=" << int(&pvuser); + XLOGD() << "Browser client connect: s= " << int(p->s) << ", user=" << (&pvuser); // 客户端断开时回调 pvSetCleanup(p, onPvThreadCleanup, p); diff --git a/web/.eslintrc.js b/web/.eslintrc.js index 1ade541..ca360bb 100644 --- a/web/.eslintrc.js +++ b/web/.eslintrc.js @@ -15,6 +15,7 @@ module.exports = { }, plugins: ['react', '@typescript-eslint', 'prettier'], // 添加 prettier 插件 rules: { + 'vue/require-explicit-emits': 'off', // 关闭 emits 声明检查 'vue/v-on-event-hyphenation': 'off', 'prettier/prettier': 'off', 'react/react-in-jsx-scope': 'off', // React 17+ 可关闭 JSX 运行时检查 @@ -24,9 +25,8 @@ module.exports = { eqeqeq: 0, // 警告使用全等 quotes: [0, 'single'], // 单引号 singleQuote: 0, - 'no-console': ['off'], // 允许所有 console 语句 - // 'no-console': 2, // 不禁用console - 'no-debugger': 2, // 警告debugger + 'no-console': 0, // 不禁用console + 'no-debugger': 0, // 警告debugger 'no-var': 2, // 对var禁止 'no-eval': 0, semi: 0, // 强制使用分号 diff --git a/web/package.json b/web/package.json index b3848bb..227bf61 100644 --- a/web/package.json +++ b/web/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "private": true, "scripts": { - "serve": "vue-cli-service serve && webpack-dev-server", + "serve": "vue-cli-service serve ", "build": "vue-cli-service build", "lint": "vue-cli-service lint", "show-webpack-version": "webpack --version" diff --git a/web/public/index.html b/web/public/index.html index b7d64a0..e376c05 100644 --- a/web/public/index.html +++ b/web/public/index.html @@ -6,8 +6,7 @@ <%= htmlWebpackPlugin.options.title %> - - +