#include "MotorboardParam.h" #include #include #include #include "commondef.h" #include MotorboardParam::MotorboardParam(/* args */) { } MotorboardParam::~MotorboardParam() { } int MotorboardParam::GetOrSetEnableAutomaticControlFeedMode(bool isget,int value){ std::lock_guard lc(m_setlock); if(isget) { auto js = getjson(); if(!js[automaticcontrol_feedmode].is_null() && js[automaticcontrol_feedmode].is_number()) return js[automaticcontrol_feedmode]; else{ printf("\nread motorboardparam error : feedmode!!! "); return 1; } } else{ auto js = getjson(); if(!js[automaticcontrol_feedmode].is_null() && js[automaticcontrol_feedmode].is_number()) { js[automaticcontrol_feedmode] = value; savejson(js); } return 0; } } int MotorboardParam::GetOrSetFeedMode(bool isget,int value){ std::lock_guard lc(m_setlock); if(isget) { auto js = getjson(); if(!js[feed_mode].is_null() && js[feed_mode].is_number()) return js[feed_mode]; else{ printf("\nread motorboardparam error : feedmode!!! "); return 1; } } else{ if(value < 0 || value > 2) return 0; auto js = getjson(); if(!js[feed_mode].is_null() && js[feed_mode].is_number()) { js[feed_mode] = value; savejson(js); } return 0; } } int MotorboardParam::GetOrSetTrayPosition(bool isget,int value){ std::lock_guard lc(m_setlock); if(isget) { auto js = getjson(); if(!js[trayposition].is_null() && js[trayposition].is_number()) return js[trayposition]; else{ printf("\nread motorboardparam error : feedmode!!! "); return 0; } } else{ if(value < 0 || value > 2) return 0; auto js = getjson(); if(!js[trayposition].is_null() && js[trayposition].is_number()) { js[trayposition] = value; savejson(js); } return 0; } } void MotorboardParam::savejson(json js){ remove(ParamPath.c_str()); std::ofstream o(ParamPath); o << std::setw(4) << js < lc(m_setlock); json tmp = getjson(); return json2struct(tmp); } void MotorboardParam::SaveParam(MotorboardParam::MBParam param){ std::lock_guard lc(m_setlock); auto tmp = struct2json(param); savejson(tmp); } json MotorboardParam::getdefaultjson(){ json m_json; m_json[feed_mode] = 1; //0 low 1 mid 2 high m_json[automaticcontrol_feedmode] = 0; m_json[automaticcontrolfeedmode_threshold] = 0.5; m_json[trayposition] = 0; return m_json; } json MotorboardParam::struct2json(MotorboardParam::MBParam param){ json js; js[feed_mode] = param.feedmode; //0 low 1 mid 2 high js[automaticcontrol_feedmode] = param.automaticcontrolfeedmode; js[automaticcontrolfeedmode_threshold] = param.automaticcontrolfeedmode_threshold; js[trayposition] = param.trayposition; return js; } MotorboardParam::MBParam MotorboardParam::json2struct(json js){ MotorboardParam::MBParam param{0}; if(js[feed_mode].is_number()) param.feedmode = js[feed_mode]; else param.feedmode = 1; if(js[automaticcontrol_feedmode].is_number()) param.automaticcontrolfeedmode = js[automaticcontrol_feedmode]; else param.automaticcontrolfeedmode = 0; if(js[automaticcontrolfeedmode_threshold].is_number_float()) param.automaticcontrolfeedmode_threshold = js[automaticcontrolfeedmode_threshold]; else param.automaticcontrolfeedmode_threshold = 0.5 ; if(js[trayposition].is_number()) param.trayposition = js[trayposition]; else param.trayposition = 0; return param; } json MotorboardParam::getjson(){ if(access(JSON_CORRECTDIR_PATH,0) == -1) { auto ret = mkdir(JSON_CORRECTDIR_PATH, 0777); if (ret != 0) { printf("make dir failed .path=%s \n", JSON_CORRECTDIR_PATH); } } if (access(ParamPath.c_str(), F_OK) != 0) { auto cfs = getdefaultjson(); savejson(cfs); return cfs; } std::ifstream i(ParamPath); auto pos = i.tellg(); i.seekg(0, std::ios::end); if (i.tellg() <= 2) { auto cfs = getdefaultjson(); savejson(cfs); return cfs; } i.seekg(pos); json m_json; i >> m_json; i.close(); return m_json; }