GrpcPrint/PrintS/Config/dao/CommunicationCfgDao.cpp
2024-03-19 17:45:12 +08:00

364 lines
14 KiB
C++

#include "CommunicationCfgDao.h"
#include "../ConfigManager.h"
#include "../../LanguageManager.h"
CommunicationCfgDao::CommunicationCfgDao(SQLite::Database* pdb) :BaseDao(pdb)
{
}
CommunicationCfgDao::~CommunicationCfgDao()
{
for (map<string, CommunicationCfg*>::iterator it = m_CommunicationCfgMap.begin(); it != m_CommunicationCfgMap.end(); it++) {
delete it->second;
}
m_CommunicationCfgMap.clear();
}
void CommunicationCfgDao::CreateTable()
{
if (m_pDB->tableExists(CommunicationCfg::TABLE_NAME))return;
char buffer[1024];
string createsql = "CREATE TABLE %s(%s INTEGER,%s VARCHAR(30),%s VARCHAR(30),%s VARCHAR(30),UNIQUE(%s,%s,%s))";
sprintf_s(buffer, sizeof(buffer), createsql.c_str(),
CommunicationCfg::TABLE_NAME.c_str(),
CommunicationCfg::FIELD_MACHINE_ID.c_str(),
CommunicationCfg::FIELD_CLIENT_CODE.c_str(),
CommunicationCfg::FIELD_PARAM_CODE.c_str(),
CommunicationCfg::FIELD_PARAM_VALUE.c_str(),
CommunicationCfg::FIELD_MACHINE_ID.c_str(),
CommunicationCfg::FIELD_CLIENT_CODE.c_str(),
CommunicationCfg::FIELD_PARAM_CODE.c_str()
);
m_pDB->exec(buffer);
}
void CommunicationCfgDao::InitBeforeFind(int mid)
{
CommunicationCfg* remote = new CommunicationCfg(mid, "REMOTE", u8"远控", CommunicationCfg::TCP);
remote->m_IP = "192.168.3.88";
remote->m_Port = 12019;
remote->m_Interval = 500;
remote->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[remote->m_ClientCode] = remote;
CommunicationCfg* xtPurifier = new CommunicationCfg(mid, "XT_PURIFIER", u8"仙塔净化器", CommunicationCfg::S7);
xtPurifier->m_IP = "192.168.2.1";
xtPurifier->m_Port = 102;
xtPurifier->m_Interval = 300;
xtPurifier->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[xtPurifier->m_ClientCode] = xtPurifier;
CommunicationCfg* hbdPurifier3 = new CommunicationCfg(mid, "HBD_PURIFIER_G3", u8"三代净化器", CommunicationCfg::S7);
hbdPurifier3->m_IP = "192.168.2.1";
hbdPurifier3->m_Port = 102;
hbdPurifier3->m_Interval = 300;
hbdPurifier3->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[hbdPurifier3->m_ClientCode] = hbdPurifier3;
CommunicationCfg* hbdPurifier4 = new CommunicationCfg(mid, "PURIFIER_G4", u8"G4净化器", CommunicationCfg::S7);
hbdPurifier4->m_IP = "192.168.2.1";
hbdPurifier4->m_Port = 102;
hbdPurifier4->m_Interval = 300;
hbdPurifier4->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[hbdPurifier4->m_ClientCode] = hbdPurifier4;
CommunicationCfg* laser1 = new CommunicationCfg(mid, "LASER1", u8"激光1", CommunicationCfg::TCP);
laser1->m_IP = "192.168.2.13";
laser1->m_Port = 10001;
laser1->m_Interval = 1000;
laser1->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser1->m_ClientCode] = laser1;
CommunicationCfg* laser2 = new CommunicationCfg(mid, "LASER2", u8"激光2", CommunicationCfg::TCP);
laser2->m_IP = "192.168.2.14";
laser2->m_Port = 10001;
laser2->m_Interval = 1000;
laser2->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser2->m_ClientCode] = laser2;
CommunicationCfg* laser3 = new CommunicationCfg(mid, "LASER3", u8"激光3", CommunicationCfg::TCP);
laser3->m_IP = "192.168.2.15";
laser3->m_Port = 10001;
laser3->m_Interval = 1000;
laser3->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser3->m_ClientCode] = laser3;
CommunicationCfg* laser4 = new CommunicationCfg(mid, "LASER4", u8"激光4", CommunicationCfg::TCP);
laser4->m_IP = "192.168.2.16";
laser4->m_Port = 10001;
laser4->m_Interval = 1000;
laser4->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser4->m_ClientCode] = laser4;
CommunicationCfg* plateTemp = new CommunicationCfg(mid, "PLATE_TEMP", u8"基板温控", CommunicationCfg::TCP);
plateTemp->m_Addr = 1;
plateTemp->m_IP = "192.168.2.200";
plateTemp->m_Port = 4004;
plateTemp->m_Interval = 800;
plateTemp->m_AlarmTimeoutTimes = 5;
m_CommunicationCfgMap[plateTemp->m_ClientCode] = plateTemp;
CommunicationCfg* oxygen = new CommunicationCfg(mid, "OXYGEN", u8"测氧", CommunicationCfg::TCP);
oxygen->m_Addr = 1;
oxygen->m_IP = "192.168.2.200";
oxygen->m_Port = 4000;
oxygen->m_Interval = 300;
oxygen->m_AlarmTimeoutTimes = 5;
oxygen->m_Enable = true;
m_CommunicationCfgMap[oxygen->m_ClientCode] = oxygen;
CommunicationCfg* plc = new CommunicationCfg(mid, "PLC", "PLC", CommunicationCfg::S7);
plc->m_IP = "192.168.2.202";
plc->m_Port = 102;
plc->m_Interval = 30;
plc->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[plc->m_ClientCode] = plc;
/*CommunicationCfg* supply = new CommunicationCfg(mid, "SUPPLY", u8"供粉", CommunicationCfg::S7);
supply->m_IP = "192.168.2.2";
supply->m_Port = 102;
supply->m_Interval = 500;
supply->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[supply->m_ClientCode] = supply;*/
CommunicationCfg* laserChiller = new CommunicationCfg(mid, "LASER_CHILLER", u8"激光器冷水机", CommunicationCfg::TCP);
laserChiller->m_Addr = 254;
laserChiller->m_IP = "192.168.2.200";
laserChiller->m_Port = 4001;
laserChiller->m_Interval = 1000;
laserChiller->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laserChiller->m_ClientCode] = laserChiller;
/*CommunicationCfg* scannerChiller = new CommunicationCfg(mid, "PURIFIER_CHILLER", u8"净化冷水机", CommunicationCfg::TCP);
scannerChiller->m_Addr = 0x01;
scannerChiller->m_IP = "192.168.2.200";
scannerChiller->m_Port = 4002;
scannerChiller->m_Interval = 1000;
scannerChiller->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[scannerChiller->m_ClientCode] = scannerChiller;*/
CommunicationCfg* ups = new CommunicationCfg(mid, "UPS", "UPS", CommunicationCfg::TCP);
ups->m_IP = "192.168.2.200";
ups->m_Port = 4003;
ups->m_Interval = 1000;
ups->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[ups->m_ClientCode] = ups;
CommunicationCfg* simplesupply = new CommunicationCfg(mid, "SIMPLE_SUPPLY", u8"单供粉", CommunicationCfg::S7);
simplesupply->m_IP = "192.168.2.2";
simplesupply->m_Port = 102;
simplesupply->m_Interval = 500;
simplesupply->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[simplesupply->m_ClientCode] = simplesupply;
/*CommunicationCfg* weightcar = new CommunicationCfg(mid, "WEIGHT_CAR", u8"称重小车", CommunicationCfg::TCP);
weightcar->m_IP = "192.168.2.200";
weightcar->m_Port = 4007;
weightcar->m_Interval = 2000;
weightcar->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[weightcar->m_ClientCode] = weightcar;*/
CommunicationCfg* scanpower = new CommunicationCfg(mid, "SCANNER_POWER", u8"振镜电源", CommunicationCfg::TCP);
scanpower->m_IP = "192.168.2.200";
scanpower->m_Port = 4006;
scanpower->m_Interval = 1000;
scanpower->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[scanpower->m_ClientCode] = scanpower;
CommunicationCfg* powerMeter = new CommunicationCfg(mid, "POWER_METER", u8"电能质量", CommunicationCfg::TCP);
powerMeter->m_IP = "192.168.2.200";
powerMeter->m_Port = 4005;
powerMeter->m_Interval = 1000;
powerMeter->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[powerMeter->m_ClientCode] = powerMeter;
g_LngManager->VarComTrans();
}
void CommunicationCfgDao::Find(int mid)
{
InitBeforeFind(mid);
char buffer[1024];
string selsql = "SELECT %s,%s,%s,%s FROM %s WHERE %s=%d";
sprintf_s(buffer, sizeof(buffer), selsql.c_str(),
CommunicationCfg::FIELD_MACHINE_ID.c_str(),
CommunicationCfg::FIELD_CLIENT_CODE.c_str(),
CommunicationCfg::FIELD_PARAM_CODE.c_str(),
CommunicationCfg::FIELD_PARAM_VALUE.c_str(),
CommunicationCfg::TABLE_NAME.c_str(),
CommunicationCfg::FIELD_MACHINE_ID.c_str(), mid);
SQLite::Statement query(*m_pDB, buffer);
map<string, map<string, string>*> cfgs;
while (query.executeStep())
{
map<string, string>* params = NULL;
string clientCode = query.getColumn(CommunicationCfg::FIELD_CLIENT_CODE.c_str()).getString();
if (cfgs.find(clientCode) == cfgs.end()) {
params = new map<string, string>();
cfgs[clientCode] = params;
}
else {
params = cfgs[clientCode];
}
string paramCode = query.getColumn(CommunicationCfg::FIELD_PARAM_CODE.c_str()).getString();
string paramValue = query.getColumn(CommunicationCfg::FIELD_PARAM_VALUE.c_str()).getString();
params->insert(pair<string, string>(paramCode, paramValue));
}
//ConfigManager::GetInstance()->GetMachine()->CheckInitCommunicationCfg(m_CommunicationCfgMap);
string strins = "INSERT INTO " + CommunicationCfg::TABLE_NAME + "(" + CommunicationCfg::FIELD_MACHINE_ID + "," + CommunicationCfg::FIELD_CLIENT_CODE + "," +
CommunicationCfg::FIELD_PARAM_CODE + "," + CommunicationCfg::FIELD_PARAM_VALUE + ") VALUES(" + to_string(mid) + ",'%s','%s','%s');";
vector<string> ins;
for (map<string, CommunicationCfg*>::iterator it = m_CommunicationCfgMap.begin(); it != m_CommunicationCfgMap.end(); it++) {
map<string, string>* relmap = cfgs[it->first];
if (!relmap || relmap->find(CommunicationCfg::PARAM_NAME) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_NAME.c_str(), it->second->m_Name.c_str());
ins.push_back(buffer);
}
else {
it->second->m_Name = (*relmap)[CommunicationCfg::PARAM_NAME];
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_TYPE) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_TYPE.c_str(), to_string(it->second->m_Type).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Type = stoi((*relmap)[CommunicationCfg::PARAM_TYPE]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_IP) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_IP.c_str(), it->second->m_IP.c_str());
ins.push_back(buffer);
}
else {
it->second->m_IP = (*relmap)[CommunicationCfg::PARAM_IP];
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_PORT) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_PORT.c_str(), to_string(it->second->m_Port).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Port = stoi((*relmap)[CommunicationCfg::PARAM_PORT]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_ADDR) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_ADDR.c_str(), to_string(it->second->m_Addr).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Addr = stoi((*relmap)[CommunicationCfg::PARAM_ADDR]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_COM) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_COM.c_str(), it->second->m_COM.c_str());
ins.push_back(buffer);
}
else {
it->second->m_COM = (*relmap)[CommunicationCfg::PARAM_COM];
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_BAUD_RATE) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_BAUD_RATE.c_str(), to_string(it->second->m_BaudRate).c_str());
ins.push_back(buffer);
}
else {
it->second->m_BaudRate = stoi((*relmap)[CommunicationCfg::PARAM_BAUD_RATE]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_PARITY) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_PARITY.c_str(), to_string(it->second->m_Parity).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Parity = stoi((*relmap)[CommunicationCfg::PARAM_PARITY]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_DATABITS) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_DATABITS.c_str(), to_string(it->second->m_DataBits).c_str());
ins.push_back(buffer);
}
else {
it->second->m_DataBits = stoi((*relmap)[CommunicationCfg::PARAM_DATABITS]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_STOPBITS) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_STOPBITS.c_str(), to_string(it->second->m_StopBits).c_str());
ins.push_back(buffer);
}
else {
it->second->m_StopBits = stoi((*relmap)[CommunicationCfg::PARAM_STOPBITS]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_INTERVAL) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_INTERVAL.c_str(), to_string(it->second->m_Interval).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Interval = stoul((*relmap)[CommunicationCfg::PARAM_INTERVAL]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_ALARM_TIMEOUT_TIMES) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_ALARM_TIMEOUT_TIMES.c_str(), to_string(it->second->m_AlarmTimeoutTimes).c_str());
ins.push_back(buffer);
}
else {
it->second->m_AlarmTimeoutTimes = stoul((*relmap)[CommunicationCfg::PARAM_ALARM_TIMEOUT_TIMES]);
}
if (!relmap || relmap->find(CommunicationCfg::PARAM_ENABLE) == relmap->end()) {
sprintf_s(buffer, sizeof(buffer), strins.c_str(), it->first.c_str(), CommunicationCfg::PARAM_ENABLE.c_str(), it->second->m_Enable ? "1" : "0");
ins.push_back(buffer);
}
else {
it->second->m_Enable = (stoi((*relmap)[CommunicationCfg::PARAM_ENABLE]) > 0 ? true : false);
}
}
if (!ins.empty()) {
SQLite::Transaction transaction(*m_pDB);
for (size_t i = 0; i < ins.size(); ++i) {
m_pDB->exec(ins[i]);
}
transaction.commit();
}
//FindTcp();
//FindCom();
}
void CommunicationCfgDao::Save()
{
for (map<string, CommunicationCfg*>::iterator it = m_CommunicationCfgMap.begin(); it != m_CommunicationCfgMap.end(); it++) {
CommunicationCfg* cfg = it->second;
vector<string> ups;
cfg->GetUpdateSql(ups);
for (size_t i = 0; i < ups.size(); i++) {
m_pDB->exec(ups[i]);
}
}
}
void CommunicationCfgDao::Export(stringstream &sql)
{
for (map<string, CommunicationCfg*>::iterator it = m_CommunicationCfgMap.begin(); it != m_CommunicationCfgMap.end(); it++) {
CommunicationCfg* cfg = it->second;
vector<string> ups;
cfg->GetUpdateSql(ups);
for (size_t i = 0; i < ups.size(); i++) {
sql << ups[i] << "\n";
}
}
}