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
181 lines
5.4 KiB
#pragma once
|
|
|
|
#include <sqlite3.h>
|
|
#include <stdio.h>
|
|
|
|
#include <fstream>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <list>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <set>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "iflytop/core/spdlogfactory/logger.hpp"
|
|
#include "iflytop/core/thread/thread.hpp"
|
|
#include "user_behavior_des.hpp"
|
|
|
|
#define USER_DB "user.db"
|
|
#define SETTING_DB "setting.db"
|
|
#define DISINFECTION_RECORD_DB "disinfection_record.db"
|
|
#define FORMULA_DB "formula.db"
|
|
#define USER_BEHAVIOR_RECORD_DB "user_behavior_record.db"
|
|
|
|
#define USER_BEHAVIOR_RECORD_DB_MAX_RECORDS 30000
|
|
/**
|
|
* @brief
|
|
*
|
|
* USER_DB
|
|
* table: user
|
|
* id uid passwd permission_level
|
|
*
|
|
*
|
|
* SETTING_DB
|
|
* table: setting
|
|
* id setting_name setting_name_ch val_upper_limit val_lower_limit permission_level val
|
|
*
|
|
* DISINFECTION_RECORD_DB
|
|
* table: disinfection_record
|
|
* id uuid uid date loglevel duration
|
|
*
|
|
*
|
|
* table: sensor_record
|
|
* 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
|
|
* saturation_2 h2o2_3 temp_3 humid_3 saturation_3
|
|
*
|
|
*/
|
|
|
|
namespace iflytop {
|
|
using namespace std;
|
|
using namespace std;
|
|
using namespace core;
|
|
using namespace nlohmann;
|
|
namespace db {
|
|
|
|
struct User {
|
|
public:
|
|
int id;
|
|
string uid;
|
|
string passwd;
|
|
int permission_level;
|
|
int visible;
|
|
};
|
|
|
|
struct Setting {
|
|
public:
|
|
int id;
|
|
string name;
|
|
string name_ch;
|
|
int val_lower_limit;
|
|
int val_upper_limit;
|
|
int permission_level;
|
|
int val;
|
|
};
|
|
|
|
struct Formula {
|
|
public:
|
|
int id;
|
|
string loglevel;
|
|
string formula_id;
|
|
string stoped_gs;
|
|
string continued_gs;
|
|
string stoped_satur;
|
|
string continued_satur;
|
|
string stoped_humi;
|
|
string continued_humi;
|
|
string injection_pump_speed;
|
|
};
|
|
|
|
struct UserBehaviorRecord {
|
|
int id;
|
|
string uid;
|
|
string date;
|
|
int behavior;
|
|
string behaviorinfo;
|
|
};
|
|
|
|
} // namespace db
|
|
using namespace db;
|
|
class DBService : public enable_shared_from_this<DBService> {
|
|
ENABLE_LOGGER(DBService);
|
|
recursive_mutex lock_;
|
|
|
|
public:
|
|
DBService();
|
|
void initialize();
|
|
|
|
public:
|
|
/*******************************************************************************
|
|
* USER_DB *
|
|
*******************************************************************************/
|
|
list<shared_ptr<db::User>> getAllUser();
|
|
json getAllUserJson();
|
|
shared_ptr<db::User> getUser(string uid);
|
|
vector<string> getUserNames();
|
|
bool isUserExist(string uid);
|
|
bool ispasswdCorrect(string uid, string passwd);
|
|
|
|
/**
|
|
* @brief 添加用户
|
|
*
|
|
* @param uid 用户名
|
|
* @param passwd 密码
|
|
* @param permission_level 许可等级,0超级管理员 3普通用户
|
|
*/
|
|
void addUser(string uid, string passwd, int permission_level);
|
|
shared_ptr<db::User> delUser(int id);
|
|
shared_ptr<db::User> updateUserPermissionLevel(int id, int permission_level);
|
|
shared_ptr<db::User> changePasswd(string uid, string passwd);
|
|
shared_ptr<db::User> updateUserUid(int id, string uid, string& olduid);
|
|
|
|
public:
|
|
/*******************************************************************************
|
|
* SETTING_DB *
|
|
*******************************************************************************/
|
|
list<shared_ptr<db::Setting>> getAllSetting();
|
|
json getAllSettingJson();
|
|
bool setSettingVal(int id, int val);
|
|
bool setSettingVal(string name, int val);
|
|
int getSettingVal(string name);
|
|
|
|
public:
|
|
/*******************************************************************************
|
|
* Formula *
|
|
*******************************************************************************/
|
|
|
|
list<shared_ptr<db::Formula>> getAllFormula();
|
|
json getAllFormulaJson();
|
|
void addFormula(string formula_id, string loglevel, string stoped_gs, string continued_gs, string stoped_satur, string continued_satur, string stoped_humi,
|
|
string continued_humi, string injection_pump_speed);
|
|
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,
|
|
int injection_pump_speed);
|
|
shared_ptr<db::Formula> delFormula(int id);
|
|
shared_ptr<db::Formula> updateFormula(int id, string column, string val);
|
|
shared_ptr<db::Formula> updateFormula(shared_ptr<db::Formula> formula);
|
|
|
|
shared_ptr<db::Formula> getFormula(int id);
|
|
|
|
/*******************************************************************************
|
|
* UserBehaviorRecord *
|
|
*******************************************************************************/
|
|
|
|
list<shared_ptr<db::UserBehaviorRecord>> getAllUserBehaviorRecord();
|
|
|
|
int getUserBehaviorRecordCount();
|
|
int getUserBehaviorRecordTheFirstId();
|
|
|
|
json getUserBehaviorRecordDescJson(int page, int page_size);
|
|
void addUserBehaviorRecord(string uid, int behavior, string behaviorinfo);
|
|
void cleanUserBehaviorRecord();
|
|
|
|
private:
|
|
void init_usr_db();
|
|
void init_setting_db();
|
|
void init_formula_db();
|
|
};
|
|
|
|
}; // namespace iflytop
|