GrpcPrint/PrintS/Config/dao/AxisCfgDao.cpp

160 lines
5.9 KiB
C++

#include "AxisCfgDao.h"
AxisCfgDao::AxisCfgDao(SQLite::Database* pdb) :BaseDao(pdb)
{
}
AxisCfgDao::~AxisCfgDao()
{
}
void AxisCfgDao::CreateTable()
{
if (m_pDB->tableExists(AxisCfg::TABLE_NAME))return;
char buffer[1024];
sprintf_s(buffer, sizeof(buffer), "CREATE TABLE %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s INTEGER,%s VARCHAR(50),%s VARCHAR(50),UNIQUE(%s,%s))"
, AxisCfg::TABLE_NAME.c_str(), AxisCfg::FIELD_ID.c_str(), AxisCfg::FIELD_AXIS_ID.c_str(), AxisCfg::FIELD_CODE.c_str(), AxisCfg::FIELD_VALUE.c_str(),
AxisCfg::FIELD_AXIS_ID.c_str(), AxisCfg::FIELD_CODE.c_str());
m_pDB->exec(buffer);
}
void AxisCfgDao::Find(map<short, AxisCfg*>& cfgMap)
{
char buffer[2048];
vector<string> needins;
for (map<short, AxisCfg*>::iterator it = cfgMap.begin(); it != cfgMap.end(); it++) {
AxisCfg* cfg = it->second;
sprintf_s(buffer, sizeof(buffer), "SELECT * FROM %s WHERE %s=%d", AxisCfg::TABLE_NAME.c_str(), AxisCfg::FIELD_AXIS_ID.c_str(), cfg->m_axis_id);
SQLite::Statement query(*m_pDB, buffer);
map<string, string> valueMap;
while (query.executeStep()) {
valueMap[query.getColumn(AxisCfg::FIELD_CODE.c_str()).getString()] = query.getColumn(AxisCfg::FIELD_VALUE.c_str()).getString();
}
sprintf_s(buffer, sizeof(buffer), "INSERT INTO %s(%s,%s,%s) VALUES(%d,", AxisCfg::TABLE_NAME.c_str(), AxisCfg::FIELD_AXIS_ID.c_str(),
AxisCfg::FIELD_CODE.c_str(), AxisCfg::FIELD_VALUE.c_str(), cfg->m_axis_id);
string strhead = string(buffer);
if (valueMap.find(AxisCfg::CODE_NAME) != valueMap.end()) {
cfg->m_name = valueMap[AxisCfg::CODE_NAME];
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_NAME.c_str(), cfg->m_name.c_str());
needins.push_back(buffer);
}
if (valueMap.find(AxisCfg::CODE_ACTIVE_LIMIT) != valueMap.end()) {
cfg->m_active_limit->SetValue(stod(valueMap[AxisCfg::CODE_ACTIVE_LIMIT]));
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_ACTIVE_LIMIT.c_str(), to_string(cfg->m_active_limit->GetValue()).c_str());
needins.push_back(buffer);
}
if (valueMap.find(AxisCfg::CODE_NEGACTIVE_LIMIT) != valueMap.end()) {
cfg->m_negactive_limit->SetValue(stoi(valueMap[AxisCfg::CODE_NEGACTIVE_LIMIT]));
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_NEGACTIVE_LIMIT.c_str(), to_string(cfg->m_negactive_limit->GetValue()).c_str());
needins.push_back(buffer);
}
if (valueMap.find(AxisCfg::CODE_ACTIVE_DIRECT) != valueMap.end()) {
cfg->m_active_direct->SetValue(stoi(valueMap[AxisCfg::CODE_ACTIVE_DIRECT]));
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_ACTIVE_DIRECT.c_str(), to_string(cfg->m_active_direct->GetValue()).c_str());
needins.push_back(buffer);
}
if (valueMap.find(AxisCfg::CODE_OVER_LOAD_LIMIT) != valueMap.end()) {
cfg->m_OverLoadLimit->SetValue(stof(valueMap[AxisCfg::CODE_OVER_LOAD_LIMIT]));
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_OVER_LOAD_LIMIT.c_str(), to_string(cfg->m_OverLoadLimit->GetValue()).c_str());
needins.push_back(buffer);
}
if (valueMap.find(AxisCfg::CODE_SHOW_POS_INV) != valueMap.end()) {
cfg->m_ShowPosInv->SetValue((stoi(valueMap[AxisCfg::CODE_SHOW_POS_INV]) > 0 ? true : false));
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_SHOW_POS_INV.c_str(), cfg->m_ShowPosInv->GetValue() ? "1" : "0");
needins.push_back(buffer);
}
if (valueMap.find(AxisCfg::CODE_SHOW_REF_ZERO) != valueMap.end()) {
cfg->m_ShowRefZero->SetValue(stod(valueMap[AxisCfg::CODE_SHOW_REF_ZERO]));
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_SHOW_REF_ZERO.c_str(), to_string(cfg->m_ShowRefZero->GetValue()).c_str());
needins.push_back(buffer);
}
if (valueMap.find(AxisCfg::CODE_MAX_LOAD_VALUE) != valueMap.end()) {
cfg->m_MaxLoadValue->SetValue(stof(valueMap[AxisCfg::CODE_MAX_LOAD_VALUE]));
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_MAX_LOAD_VALUE.c_str(), to_string(cfg->m_MaxLoadValue->GetValue()).c_str());
needins.push_back(buffer);
}
if (valueMap.find(AxisCfg::CODE_SAFE_LIMIT) != valueMap.end()) {
cfg->m_SafeLimit->SetValue(stoi(valueMap[AxisCfg::CODE_SAFE_LIMIT]) > 0 ? true : false);
}
else {
sprintf_s(buffer, sizeof(buffer), "%s'%s','%s')", strhead.c_str(), AxisCfg::CODE_SAFE_LIMIT.c_str(), cfg->m_SafeLimit->GetValue() ? "1" : "0");
needins.push_back(buffer);
}
}
if (!needins.empty()) {
SQLite::Transaction transaction(*m_pDB);
for (size_t i = 0; i < needins.size(); ++i) {
m_pDB->exec(needins[i]);
}
transaction.commit();
}
}
void AxisCfgDao::Save(map<short, AxisCfg*>& axismap)
{
vector<string> ups;
axismap[GTS_AXIS_ID_MOLD]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_LOAD]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_ARM]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_SUPPLY]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_CLEAN]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_ELE]->GetUpdateSql(ups);
for (size_t i = 0; i < ups.size(); i++) {
m_pDB->exec(ups[i]);
}
}
void AxisCfgDao::Export(map<short, AxisCfg*>& axismap, stringstream& sql)
{
vector<string> ups;
axismap[GTS_AXIS_ID_MOLD]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_LOAD]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_ARM]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_SUPPLY]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_CLEAN]->GetUpdateSql(ups);
axismap[GTS_AXIS_ID_ELE]->GetUpdateSql(ups);
for (size_t i = 0; i < ups.size(); i++) {
sql << ups[i] << "\n";
}
}
void AxisCfgDao::UpdateZeroOffset(int id, long value)
{
string strsql = "UPDATE %s SET %s=%d WHERE %s=%d AND %s='%s'";
char buffer[1024];
sprintf_s(buffer, sizeof(buffer), strsql.c_str(),
AxisCfg::TABLE_NAME.c_str(),
AxisCfg::FIELD_VALUE.c_str(), value,
AxisCfg::FIELD_AXIS_ID.c_str(), id,
AxisCfg::FIELD_CODE.c_str(), AxisCfg::CODE_SHOW_REF_ZERO.c_str());
m_pDB->exec(buffer);
}