148 lines
5.0 KiB
C++
148 lines
5.0 KiB
C++
|
#include "SensorConfig.h"
|
||
|
#include <sys/stat.h>
|
||
|
#include <fstream>
|
||
|
#include <iostream>
|
||
|
#include <iomanip>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
SensorConfig m_static_sensorconfig;
|
||
|
|
||
|
SensorConfig::SensorConfig(/* args */)
|
||
|
{
|
||
|
{
|
||
|
std::lock_guard<std::mutex> m_lc(m_lock);
|
||
|
initconfigfile();
|
||
|
}
|
||
|
get_param();
|
||
|
}
|
||
|
|
||
|
SensorConfig::~SensorConfig()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
SensorConfig::SensorConfig_Param SensorConfig::get_param()
|
||
|
{
|
||
|
std::lock_guard<std::mutex> m_lc(m_lock);
|
||
|
if(access(this->Path.c_str(),0) == -1)
|
||
|
{
|
||
|
initconfigfile();
|
||
|
return m_param;
|
||
|
}
|
||
|
std::ifstream i(this->Path);
|
||
|
auto pos = i.tellg();
|
||
|
i.seekg(0, std::ios::end);
|
||
|
if (i.tellg() <= 2)
|
||
|
{
|
||
|
initconfigfile();
|
||
|
return m_param;
|
||
|
}
|
||
|
i.seekg(pos);
|
||
|
json m_json;
|
||
|
i >> m_json;
|
||
|
i.close();
|
||
|
m_param = to_param(m_json);
|
||
|
return m_param;
|
||
|
}
|
||
|
|
||
|
void SensorConfig::set_param(SensorConfig::SensorConfig_Param param)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> m_lc(m_lock);
|
||
|
m_param = param;
|
||
|
auto js = to_json(param);
|
||
|
std::ofstream o(this->Path);
|
||
|
o << std::setw(4) << js <<std::endl;
|
||
|
o.close();
|
||
|
o.flush();
|
||
|
}
|
||
|
|
||
|
SensorConfig::SensorConfig_Param SensorConfig::get_mem_param()
|
||
|
{
|
||
|
return m_param;
|
||
|
}
|
||
|
|
||
|
void SensorConfig::initconfigfile()
|
||
|
{
|
||
|
//std::lock_guard<std::mutex> m_lc(m_lock);
|
||
|
if(access(this->Path.c_str(),F_OK) == 0)
|
||
|
return;
|
||
|
SensorConfig::SensorConfig_Param param{
|
||
|
.Sensor_Scan_Pwm = 40,
|
||
|
.Sensor_ConverOpen_Pwm = 40,
|
||
|
.Sensor_Screw_1_Pwm = 40,
|
||
|
.Sensor_Screw_2_Pwm = 40,
|
||
|
.Sensor_PaperOut_Pwm = 40,
|
||
|
.Sensor_PaperDetection_Pwm = 40,
|
||
|
.Sensor_Double_double_check_cyc = 4,
|
||
|
.Sensor_Double_paper_check_cyc = 8,
|
||
|
.Sensor_Double_double_max = 0x1c,
|
||
|
.Sensor_Double_single_max = 0x70,
|
||
|
};
|
||
|
m_param = param;
|
||
|
printf("\nfile: %s not found creat ...",this->Path.c_str());
|
||
|
json js = to_json(param);
|
||
|
std::ofstream o(this->Path);
|
||
|
o << std::setw(4) << js <<std::endl;
|
||
|
o.close();
|
||
|
o.flush();
|
||
|
}
|
||
|
|
||
|
void SensorConfig::reset_json()
|
||
|
{
|
||
|
std::lock_guard<std::mutex> m_lc(m_lock);
|
||
|
remove(this->Path.c_str());
|
||
|
initconfigfile();
|
||
|
}
|
||
|
|
||
|
json SensorConfig::to_json(SensorConfig::SensorConfig_Param param)
|
||
|
{
|
||
|
json js;
|
||
|
js[Sensor_Scan_Pwm] = param.Sensor_Scan_Pwm;
|
||
|
js[Sensor_Screw_1_Pwm] = param.Sensor_Screw_1_Pwm;
|
||
|
js[Sensor_Screw_2_Pwm] = param.Sensor_Screw_2_Pwm;
|
||
|
js[Sensor_ConverOpen_Pwm] = param.Sensor_ConverOpen_Pwm;
|
||
|
js[Sensor_PaperOut_Pwm] = param.Sensor_PaperOut_Pwm;
|
||
|
js[Sensor_PaperDetection_Pwm] = param.Sensor_PaperDetection_Pwm;
|
||
|
js[Sensor_Double_double_check_cyc] = param.Sensor_Double_double_check_cyc;
|
||
|
js[Sensor_Double_paper_check_cyc] = param.Sensor_Double_paper_check_cyc;
|
||
|
js[Sensor_Double_double_max] = param.Sensor_Double_double_max;
|
||
|
js[Sensor_Double_single_max] = param.Sensor_Double_single_max;
|
||
|
return js;
|
||
|
}
|
||
|
|
||
|
SensorConfig::SensorConfig_Param SensorConfig::to_param(json js)
|
||
|
{
|
||
|
SensorConfig::SensorConfig_Param param{
|
||
|
.Sensor_Scan_Pwm = 40,
|
||
|
.Sensor_ConverOpen_Pwm = 40,
|
||
|
.Sensor_Screw_1_Pwm = 40,
|
||
|
.Sensor_Screw_2_Pwm = 40,
|
||
|
.Sensor_PaperOut_Pwm = 40,
|
||
|
.Sensor_PaperDetection_Pwm = 40,
|
||
|
.Sensor_Double_double_check_cyc = 4,
|
||
|
.Sensor_Double_paper_check_cyc = 8,
|
||
|
.Sensor_Double_double_max = 0x1c,
|
||
|
.Sensor_Double_single_max = 0x70,
|
||
|
};
|
||
|
if(js[Sensor_Scan_Pwm].is_object() && js[Sensor_Scan_Pwm].is_number_integer())
|
||
|
param.Sensor_Scan_Pwm = js[Sensor_Scan_Pwm];
|
||
|
if(js[Sensor_Screw_1_Pwm].is_object() && js[Sensor_Screw_1_Pwm].is_number_integer())
|
||
|
param.Sensor_Screw_1_Pwm = js[Sensor_Screw_1_Pwm];
|
||
|
if(js[Sensor_Screw_2_Pwm].is_object() && js[Sensor_Screw_2_Pwm].is_number_integer())
|
||
|
param.Sensor_Screw_2_Pwm = js[Sensor_Screw_2_Pwm];
|
||
|
if(js[Sensor_ConverOpen_Pwm].is_object() && js[Sensor_ConverOpen_Pwm].is_number_integer())
|
||
|
param.Sensor_ConverOpen_Pwm = js[Sensor_ConverOpen_Pwm];
|
||
|
if(js[Sensor_PaperOut_Pwm].is_object() && js[Sensor_PaperOut_Pwm].is_number_integer())
|
||
|
param.Sensor_PaperOut_Pwm = js[Sensor_PaperOut_Pwm];
|
||
|
if(js[Sensor_PaperDetection_Pwm].is_object() && js[Sensor_PaperDetection_Pwm].is_number_integer())
|
||
|
param.Sensor_PaperDetection_Pwm = js[Sensor_PaperDetection_Pwm];
|
||
|
if(js[Sensor_Double_double_check_cyc].is_object() && js[Sensor_Double_double_check_cyc].is_number_integer())
|
||
|
param.Sensor_Double_double_check_cyc = js[Sensor_Double_double_check_cyc];
|
||
|
if(js[Sensor_Double_paper_check_cyc].is_object() && js[Sensor_Double_paper_check_cyc].is_number_integer())
|
||
|
param.Sensor_Double_paper_check_cyc = js[Sensor_Double_paper_check_cyc];
|
||
|
if(js[Sensor_Double_double_max].is_object() && js[Sensor_Double_double_max].is_number_integer())
|
||
|
param.Sensor_Double_double_max = js[Sensor_Double_double_max];
|
||
|
if(js[Sensor_Double_single_max].is_object() && js[Sensor_Double_single_max].is_number_integer())
|
||
|
param.Sensor_Double_single_max = js[Sensor_Double_single_max];
|
||
|
return param;
|
||
|
}
|