//
|
// Created by ps on 18-11-29.
|
//
|
|
#ifndef SYNCDBTOOL_ERLANGDBTOOL_H
|
#define SYNCDBTOOL_ERLANGDBTOOL_H
|
|
#include <iostream>
|
#include <cstring>
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
#include <zconf.h>
|
#include <thread>
|
|
#include <map>
|
#include <vector>
|
#include <mutex>
|
|
#include <stdio.h>
|
#include <stdarg.h>
|
#include <jsoncpp/json/json.h>
|
#include <arpa/inet.h>
|
|
#include "erl_interface.h"
|
#include "ei.h"
|
|
#include "SyncDB.hpp"
|
|
#define TIMEOUT (5000)
|
#define BUFSIZE 1000
|
#define PORT 13001
|
|
|
//typedef void (*CBF)(std::string);
|
|
extern void erlangCallBackFunc(std::string);
|
|
//#TODO 多线程优化
|
namespace ErlangTool {
|
|
static int m_loop = 0;
|
//map<uuid,人脸特征信息>
|
typedef std::map<std::string, SyncDB::Feature_Info> map_FaceDataCache;
|
//map<uuid,设备信息>
|
typedef std::map<std::string, SyncDB::Device_Info> map_DevDataCache;
|
//map<表名,底库表信息>
|
typedef std::map<std::string, SyncDB::Table_Info> map_TabDataCache;
|
//map<表名,黑白名单类型>
|
typedef std::map<std::string, SyncDB::Bw_Info> map_BwDataCache;
|
|
typedef std::vector<SyncDB::AddPersRet> vec_AddDataCache;
|
typedef std::vector<SyncDB::PerExistRet> vec_PerExistRet;
|
typedef std::vector<SyncDB::UpdPersRet> vec_UpdPersRet;
|
//c++11 互斥锁
|
// LockG(Parma);
|
typedef std::lock_guard<std::mutex> LockG;
|
|
/***
|
* 判断文件夹路径是否存在,不存在则创建
|
* @param dir 文件路径
|
* @param mkdir_flag 是否创建创建文件夹
|
* @return 是否成功
|
*/
|
static bool dir_file_exists(std::string dir, bool mkdir_flag = true);
|
|
/***
|
* 测试节点是否启动
|
* @param nodeName 节点名称
|
* @param cookie 节点cookie
|
* @return 启动状态
|
*/
|
static bool pingNode(const std::string &nodeName, const std::string &cookie);
|
|
/***
|
* 检查ETERM的数据类型
|
* @param elemen
|
* @return 返回数据类型
|
*/
|
static int checkETERMType(ETERM *elemen);
|
|
static void erlangFreeEterm(int count, ...);
|
|
/***
|
* Erlang交互工具
|
* #todo map保存到内存?
|
*/
|
class ErlangDbTool {
|
public:
|
explicit ErlangDbTool();
|
|
/***
|
* 初始化节点(zhizuo chaxun shiyong)
|
* @param nodeName 节点名称
|
* @param cookie 集群cookie
|
*/
|
ErlangDbTool(const std::string &nodeName, const std::string &cookie, const std::string &pName);
|
|
/***
|
* 初始化节点
|
* @param path 节点数据库保存路径
|
* @param nodeName 节点名称
|
* @param cookie 集群cookie
|
*/
|
ErlangDbTool(const std::string &path, const std::string &nodeName, const std::string &cookie,
|
const std::string &pName);
|
|
//clusterID clusterName
|
/***
|
* 初始化节点
|
* @param path 节点数据库保存路径
|
* @param nodeName 节点名称
|
* @param cookie 集群cookie
|
* @param clusterID 集群id
|
* @param clusterName 集群名称
|
*/
|
ErlangDbTool(const std::string &path, const std::string &nodeName, const std::string &cookie,
|
const std::string &clusterID, const std::string &clusterName);
|
|
//clusterID clusterName
|
/***
|
* 初始化节点
|
* @param path 节点数据库保存路径
|
* @param nodeName 节点名称
|
* @param cookie 集群cookie
|
* @param clusterID 集群id
|
* @param clusterName 集群名称
|
* @param pName c节点标识名
|
*/
|
ErlangDbTool(const std::string &path, const std::string &nodeName, const std::string &cookie,
|
const std::string &clusterID, const std::string &clusterName, const std::string &pName);
|
|
virtual ~ErlangDbTool();
|
|
private:
|
void initCNode();
|
|
public:
|
//#todo setCNodeName
|
|
bool setCNodeName();
|
|
bool sendMessage();
|
|
//#todo sendMessage
|
|
void test(pthread_cond_t *, pthread_mutex_t *);
|
|
/***
|
* 启动erlang节点
|
* @return 启动状态
|
*/
|
int initErlang();
|
|
std::string getConfigJsonString();
|
|
/***
|
* 启动节点数据库
|
* 新节点自动创建数据库
|
* @param FatherNodeName 第一个节点无需传入此参数,其他节点需要传入引导节点的名称
|
* @param DeviceName 设备名称
|
* @param DevAdd 设备地址
|
* @return
|
*/
|
bool startNodeDb(std::string FatherNodeName, std::string DeviceId = "", std::string DevAdd = "DevAddress");
|
|
|
/***
|
* 删除节点数据库,同时删除本地文件
|
* @return 返回状态
|
*/
|
bool removeNode();
|
|
|
bool modifyCluName(std::string CluId, std::string CluName);
|
|
|
/***
|
* 查找在线节点信息
|
* #TODO 有一个小bug
|
* device_info.create_by会被重置
|
* @return 节点集合信息
|
*/
|
map_DevDataCache findAllNode();
|
|
/***
|
* 创建底库表
|
* @param TableType car||person
|
* @param TableName
|
* @param SyncType true 1||false 0
|
* @param BwType "Black"1||"White"0
|
* @return 创建状态
|
*/
|
bool createDatabase(std::string TableType, std::string TableName, bool SyncType, std::string BwType,
|
std::string StartTime = "", std::string EndTime = "");
|
|
/***
|
* 更新底库表
|
* @param TableType car||person
|
* @param TableName
|
* @param SyncType true 1||false 0
|
* @param BwType "white"0||"black"1
|
* @return 创建状态
|
*/
|
bool updateDatabase(std::string UUID, std::string TableType, std::string TableName, bool SyncType,
|
std::string BwType, std::string StartTime = "", std::string EndTime = "");
|
|
/***
|
* 删除数据表
|
* @param TableType car||person
|
* @param TableName biaoming
|
* @param SyncType true 1||false 0
|
* @return
|
*/
|
bool deleteDatabase(std::string TableType, std::string TableName, bool SyncType);
|
|
/***
|
* 查询所有数据表
|
* @return
|
*/
|
map_TabDataCache findAllDatabase();
|
|
/***
|
* 查询所有表的黑白名单类型
|
* @return
|
*/
|
map_BwDataCache findAllTypeInfo();
|
|
/***
|
* 增加人员
|
* @param UUID out
|
* @param TableName
|
* @param Feature
|
* @param PersonName
|
* @param Age
|
* @param Sex
|
* @param IdCard
|
* @param PhoneNum
|
* @return
|
*/
|
bool addPerson(std::string &UUID, std::string TableName, std::string Feature, std::string ImgUrl = " ",
|
std::string IdCard = " ", std::string PersonName = " ", std::string Age = " ",
|
std::string Sex = " ", std::string PhoneNum = " ");
|
|
vec_AddDataCache addPersons(std::string TableUuid, std::vector<SyncDB::AddPersonInfo> &tmpPer);
|
|
/***
|
* 删除人员
|
* @param UUID
|
* @param TableName
|
* @return
|
*/
|
bool delPerson(std::string UUID, std::string TableName);
|
|
|
/***
|
* 根据表名加载人脸特征
|
* @param TableName
|
* @return
|
*/
|
map_FaceDataCache loadFaceFeaData(std::string TableName);
|
|
|
/***
|
* 根据表名加载人脸特征2
|
* @param TableName
|
* @return
|
*/
|
map_FaceDataCache loadFaceFeaData2(std::string TableName);
|
|
vec_PerExistRet personIsExistOnClu(std::string personUid);
|
|
vec_UpdPersRet singlePersonUpdate(std::string NewPerId, std::string OldId, std::string Idcard,
|
std::string PerPicUrl, std::string PerFea);
|
// private:
|
/***
|
* 重新连接节点
|
* @return 连接状态
|
*/
|
bool resetConn();
|
|
/***
|
* 等待节点启动,阻塞
|
* @return
|
*/
|
bool waitNode();
|
|
public:
|
|
private:
|
//存储路径
|
std::string m_path;
|
std::string m_nodeName;
|
std::string m_cNodeName;
|
std::string m_pName;
|
std::string m_cookie;
|
|
std::string m_clusterID;
|
std::string m_clusterName;
|
|
bool m_ret;
|
int m_fd;
|
|
std::mutex m_mutex;
|
};
|
|
static int my_listen(int port);
|
|
}
|
|
|
#endif //SYNCDBTOOL_ERLANGDBTOOL_H
|