#include "userinfo.h"
/************** boost ******************/
#define BOOST_CONTAINER_NO_LIB
#define BOOST_JSON_NO_LIB
#include <boost/json.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
using namespace boost::property_tree;
namespace json = boost::json;
#include <boost/filesystem.hpp>
// json toString
template <typename T> struct strtranslator
{
typedef T internal_type;
typedef T external_type;
boost::optional<T> get_value(const T &v) {
return v.substr(1, v.size() - 2);
}
boost::optional<T> put_value(const T &v) {
return '"' + v + '"';
}
};
/************** boost ******************/
UserInfo::UserInfo()
{
}
bool UserInfo::initUserInfo()
{
appDefaultData = {
"data1"
};
appData = {
"data1"
};
// 配置文件目录
std::string workPath = AppWorkPath;//boost::filesystem::current_path().string(); //当前程序路径
userDirName = workPath + "appdata/";
if(!boost::filesystem::is_directory(userDirName)) {
if (!boost::filesystem::create_directories(userDirName)) {
std::cout<<TAG<<" ERROR: " <<"Failed to create dir: " + userDirName<<std::endl;
return false;
}
}
// enable debug log
setDebug(true);
// 初始化 log
logT = new LogT(userDirName + LogDirName);
LogD(TAG, userDirName);
// auth
if(!checkLicence(userDirName + LicenceName)){
return false;
}
// init local data
initLocalData();
//LogD(TAG, "cpuRate = " + getCPURate() + ", memRate = " + getMemoryUsed());
LogD(TAG, "initUserInfo done");
return true;
}
UserInfo::~UserInfo()
{
appDataFile.flush();
appDataFile.close();
delete logT;
}
bool UserInfo::checkLicence(std::string licencePath)
{
// std::ifstream licenceIfs(licencePath);
// if (!licenceIfs.good()) {
// LogE(TAG, "Failed to find licence file = " + licencePath);
// return false;
// }
// std::string cpu_id, disk_id;
// bool ret = GetBaseHardwareInfo(&cpu_id, &disk_id);
// disk_id = "j(*sdlkj*#?@#20e";
// if (!ret) {
// LogE(TAG, "Failed to read hardware fingerprint !" );
// return false;
// }
// std::string licenceInfo = ReadStringFormFile(licencePath);
// std::string publicKey1 = "-----BEGIN RSA PUBLIC KEY-----";
// std::string publicKey2 = "MIGJAoGBAML82CBtrKg1pVRLvc1pY4VTIncVttuTfLOQxmckhjhgTNk/50AC0k0t";
// std::string publicKey3 = "IH96rp2dVyzekSQ/vkaGRmjJXedPvlSBFooH/VPGoeXMLnTrDIXgpoW6F+3DoaKk";
// std::string publicKey4 = "DJeCHsrlsWK1oVK/ytaIazn0ScggZLX5FAuskbPw1hcvHdgJ+kZBAgMBAAE=";
// std::string publicKey5 = "-----END RSA PUBLIC KEY-----";
// std::string publicKey = publicKey1 + "\n" + publicKey2 + "\n" + publicKey3 + "\n" + publicKey4 + "\n" + publicKey5 + "\n";
// //验证 licence信息
// bool isVerified = Verifylicence(publicKey, licenceInfo, cpu_id, disk_id);
// //std::cout << "isVerified:" << isVerified << std::endl; //check验证非法,即在同一台设备运行,允许运行
// if (!isVerified) {
// LogE(TAG, "Failed to authorization !" );
// return false;
// }
return true;
}
std::string UserInfo::getAppDefaultData(int type)
{
return appDefaultData[type];
}
void UserInfo::setAppDefaultData(int type)
{
appData[type] = appDefaultData[type];
}
std::string UserInfo::getAppData(int type)
{
return appData[type];
}
void UserInfo::setAppData(std::string data, int type)
{
if(type > -1) {
appData[type] = data;
saveQuickAppData();
}
}
std::string UserInfo::getUserDir()
{
return userDirName;
}
std::string UserInfo::getLogDir()
{
return userDirName + LogDirName;
}
std::string UserInfo::getEdgeAppVersion()
{
return edgeAppVersion;
}
std::string UserInfo::getModelAIVersion()
{
return modelAIVersion;
}
void UserInfo::initLocalData()
{
#if 0
// read current version
std::ifstream releaseNoteFile(userDirName + releaseNoteFileName);
if(releaseNoteFile.is_open()) {
std::string versionStr = "";
std::getline(releaseNoteFile, versionStr);
std::vector<std::string> versionStrList;
strSplit(versionStr, ' ', versionStrList);
if(versionStr.empty() || versionStrList.size() != 3){
LogE(TAG, "versionStr is wrong: "+ versionStr);
} else {
edgeAppVersion = versionStrList.at(1);
modelAIVersion = versionStrList.at(2);
}
releaseNoteFile.close();
} else {
LogE(TAG, "Failed to find open file: " + userDirName + releaseNoteFileName);
}
std::string versionInfo = "EdgeAppVersion = " + edgeAppVersion + ", modelAIVersion = " + modelAIVersion;
LogD(TAG, versionInfo);
#endif
// App data
appDataFile.open(userDirName + appDataFileName, std::ios::in);
if(appDataFile.is_open()){
std::string appDataStr = "";
std::getline(appDataFile, appDataStr);
std::vector<std::string> appDataFileData;;
strSplit(appDataStr, ',', appDataFileData);
if(checkAppData(appDataFileData)) {
appData.clear();
appData = appDataFileData;
} else {
appDataFile.close();
appDataFile.open(userDirName + appDataFileName, std::ios::out | std::ios::trunc);
appDataFile.close();
if(appData.size() != Index_AppDataSize || appDefaultData.size() != Index_AppDataSize) {
LogE(TAG, "appData size is wrong, appDataSize = " + std::to_string(appData.size()) + " appDefaultData = " + std::to_string(appDefaultData.size()) + " Index_AppDataSize = " + std::to_string(Index_AppDataSize));
}
}
} else {
LogE(TAG, "Failed to open appDataFile !!");
}
saveQuickAppData();
}
void UserInfo::saveQuickAppData()
{
appDataFile.close();
appDataFile.open(userDirName + appDataFileName, std::ios::out);
std::string appDataStr = strJoin(appData, ",");
//LogD(TAG, "appDataStr: " + appDataStr);
appDataFile<<appDataStr;
appDataFile.flush();
appDataFile.sync();
appDataFile.close();
//LogD(TAG, "save App Data: " + appDataStr);
}
// 检查数据合法性
bool UserInfo::checkAppData(std::vector<std::string> &readData)
{
bool retBool = true;
if(Index_AppDataSize != readData.size()){
retBool = false;
}
std::string workPath = readData[Index_data1];
// if(!boost::filesystem::exists(workPath) || !boost::filesystem::is_directory(workPath)) {
// LogE(TAG, "checkLocalData Index_WorkPath = " + readData[Index_data1]);
// readData[Index_data1] = appDefaultData[Index_data1];
// }
return retBool;
}
/****************** Debug *********************/
void UserInfo::setDebug(bool flag)
{
isDebug = flag;
}
void UserInfo::LogD(std::string tag, std::string qStr)
{
D(tag, qStr);
}
void UserInfo::LogE(std::string tag, std::string qStr)
{
E(tag, qStr);
}
void UserInfo::D(std::string tag, std::string qStr)
{
if(!isDebug){
return;
}
logT->D(tag, qStr);
}
void UserInfo::E(std::string tag, std::string qStr)
{
logT->E(tag, qStr);
}
void UserInfo::msSleep(long msec)
{
#if 1
std::this_thread::sleep_for(std::chrono::milliseconds(msec));
#else
if(msec >= 1000){
sleep(msec/1000);
usleep((msec%1000)*1000);
} else {
usleep(msec*1000);// 最大1000000
}
#endif
}
int UserInfo::getMSDuration(std::chrono::steady_clock::time_point &startTime, std::chrono::steady_clock::time_point &endTime)
{
// std::chrono::steady_clock::time_point currHeartTime = std::chrono: