You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.4 KiB

1 year ago
  1. #pragma once
  2. #include <sqlite3.h>
  3. #include <stdio.h>
  4. #include <fstream>
  5. #include <functional>
  6. #include <iostream>
  7. #include <list>
  8. #include <map>
  9. #include <memory>
  10. #include <mutex>
  11. #include <set>
  12. #include <sstream>
  13. #include <string>
  14. #include <vector>
  15. #include "iflytop/core/spdlogfactory/logger.hpp"
  16. #include "iflytop/core/thread/thread.hpp"
  17. #include "user_behavior_des.hpp"
  18. #define USER_DB "user.db"
  19. #define SETTING_DB "setting.db"
  20. #define DISINFECTION_RECORD_DB "disinfection_record.db"
  21. #define FORMULA_DB "formula.db"
  22. #define USER_BEHAVIOR_RECORD_DB "user_behavior_record.db"
  23. #define USER_BEHAVIOR_RECORD_DB_MAX_RECORDS 30000
  24. /**
  25. * @brief
  26. *
  27. * USER_DB
  28. * table: user
  29. * id uid passwd permission_level
  30. *
  31. *
  32. * SETTING_DB
  33. * table: setting
  34. * id setting_name setting_name_ch val_upper_limit val_lower_limit permission_level val
  35. *
  36. * DISINFECTION_RECORD_DB
  37. * table: disinfection_record
  38. * id uuid uid date loglevel duration
  39. *
  40. *
  41. * table: sensor_record
  42. * id disinfection_id date heating_strip air_compressor sprinkler_pump disinfectant_volume h2o2_1 temp_1 humid_1 saturation_1 h2o2_2 temp_2 humid_2
  43. * saturation_2 h2o2_3 temp_3 humid_3 saturation_3
  44. *
  45. */
  46. namespace iflytop {
  47. using namespace std;
  48. using namespace std;
  49. using namespace core;
  50. using namespace nlohmann;
  51. namespace db {
  52. struct User {
  53. public:
  54. int id;
  55. string uid;
  56. string passwd;
  57. int permission_level;
  58. int visible;
  59. };
  60. struct Setting {
  61. public:
  62. int id;
  63. string name;
  64. string name_ch;
  65. int val_lower_limit;
  66. int val_upper_limit;
  67. int permission_level;
  68. int val;
  69. };
  70. struct Formula {
  71. public:
  72. int id;
  73. string loglevel;
  74. string formula_id;
  75. string stoped_gs;
  76. string continued_gs;
  77. string stoped_satur;
  78. string continued_satur;
  79. string stoped_humi;
  80. string continued_humi;
  81. string injection_pump_speed;
  82. };
  83. struct UserBehaviorRecord {
  84. int id;
  85. string uid;
  86. string date;
  87. int behavior;
  88. string behaviorinfo;
  89. };
  90. } // namespace db
  91. using namespace db;
  92. class DBService : public enable_shared_from_this<DBService> {
  93. ENABLE_LOGGER(DBService);
  94. recursive_mutex lock_;
  95. public:
  96. DBService();
  97. void initialize();
  98. public:
  99. /*******************************************************************************
  100. * USER_DB *
  101. *******************************************************************************/
  102. list<shared_ptr<db::User>> getAllUser();
  103. json getAllUserJson();
  104. shared_ptr<db::User> getUser(string uid);
  105. vector<string> getUserNames();
  106. bool isUserExist(string uid);
  107. bool ispasswdCorrect(string uid, string passwd);
  108. /**
  109. * @brief
  110. *
  111. * @param uid
  112. * @param passwd
  113. * @param permission_level ,0 3
  114. */
  115. void addUser(string uid, string passwd, int permission_level);
  116. shared_ptr<db::User> delUser(int id);
  117. shared_ptr<db::User> updateUserPermissionLevel(int id, int permission_level);
  118. shared_ptr<db::User> changePasswd(string uid, string passwd);
  119. shared_ptr<db::User> updateUserUid(int id, string uid, string& olduid);
  120. public:
  121. /*******************************************************************************
  122. * SETTING_DB *
  123. *******************************************************************************/
  124. list<shared_ptr<db::Setting>> getAllSetting();
  125. json getAllSettingJson();
  126. bool setSettingVal(int id, int val);
  127. bool setSettingVal(string name, int val);
  128. int getSettingVal(string name);
  129. public:
  130. /*******************************************************************************
  131. * Formula *
  132. *******************************************************************************/
  133. list<shared_ptr<db::Formula>> getAllFormula();
  134. json getAllFormulaJson();
  135. void addFormula(string formula_id, string loglevel, string stoped_gs, string continued_gs, string stoped_satur, string continued_satur, string stoped_humi,
  136. string continued_humi, string injection_pump_speed);
  137. void addFormula(string formula_id, int loglevel, int stoped_gs, int continued_gs, int stoped_satur, int continued_satur, int stoped_humi, int continued_humi,
  138. int injection_pump_speed);
  139. shared_ptr<db::Formula> delFormula(int id);
  140. shared_ptr<db::Formula> updateFormula(int id, string column, string val);
  141. shared_ptr<db::Formula> updateFormula(shared_ptr<db::Formula> formula);
  142. shared_ptr<db::Formula> getFormula(int id);
  143. /*******************************************************************************
  144. * UserBehaviorRecord *
  145. *******************************************************************************/
  146. list<shared_ptr<db::UserBehaviorRecord>> getAllUserBehaviorRecord();
  147. int getUserBehaviorRecordCount();
  148. int getUserBehaviorRecordTheFirstId();
  149. json getUserBehaviorRecordDescJson(int page, int page_size);
  150. void addUserBehaviorRecord(string uid, int behavior, string behaviorinfo);
  151. void cleanUserBehaviorRecord();
  152. private:
  153. void init_usr_db();
  154. void init_setting_db();
  155. void init_formula_db();
  156. };
  157. }; // namespace iflytop