GrpcPrint/PrintS/Config/dao/CommunicationCfgDao.cpp

399 lines
15 KiB
C++
Raw Normal View History

#include "CommunicationCfgDao.h"
2024-03-19 17:45:12 +08:00
#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"远控", _(u8"远控"), CommunicationCfg::TCP);
2024-03-19 17:45:12 +08:00
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"仙塔净化器", _(u8"仙塔净化器"), CommunicationCfg::S7);
2024-03-19 17:45:12 +08:00
xtPurifier->m_IP = "192.168.2.1";
xtPurifier->m_Port = 102;
xtPurifier->m_Interval = 500;
2024-03-19 17:45:12 +08:00
xtPurifier->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[xtPurifier->m_ClientCode] = xtPurifier;
CommunicationCfg* hbdPurifier3 = new CommunicationCfg(mid, "HBD_PURIFIER_G3", u8"三代净化器", _(u8"三代净化器"), CommunicationCfg::S7);
2024-03-19 17:45:12 +08:00
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* m1Purifier = new CommunicationCfg(mid, "M1_PURIFIER", u8"M1系列净化器", _(u8"M1系列净化器"), CommunicationCfg::S7);
m1Purifier->m_IP = "192.168.2.1";
m1Purifier->m_Port = 102;
m1Purifier->m_Interval = 300;
m1Purifier->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[m1Purifier->m_ClientCode] = m1Purifier;
2024-03-19 17:45:12 +08:00
CommunicationCfg* laser1 = new CommunicationCfg(mid, "LASER1", u8"激光1", _(u8"激光1"), CommunicationCfg::TCP);
laser1->m_IP = "192.168.2.30";
2024-03-19 17:45:12 +08:00
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", _(u8"激光2"), CommunicationCfg::TCP);
laser2->m_IP = "192.168.2.31";
2024-03-19 17:45:12 +08:00
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", _(u8"激光3"), CommunicationCfg::TCP);
laser3->m_IP = "192.168.2.32";
2024-03-19 17:45:12 +08:00
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", _(u8"激光4"), CommunicationCfg::TCP);
laser4->m_IP = "192.168.2.33";
2024-03-19 17:45:12 +08:00
laser4->m_Port = 10001;
laser4->m_Interval = 1000;
laser4->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser4->m_ClientCode] = laser4;
CommunicationCfg* laser5 = new CommunicationCfg(mid, "LASER5", u8"激光5", _(u8"激光5"), CommunicationCfg::TCP);
laser5->m_IP = "192.168.2.34";
laser5->m_Port = 10001;
laser5->m_Interval = 1000;
laser5->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser5->m_ClientCode] = laser5;
CommunicationCfg* laser6 = new CommunicationCfg(mid, "LASER6", u8"激光6", _(u8"激光6"), CommunicationCfg::TCP);
laser6->m_IP = "192.168.2.35";
laser6->m_Port = 10001;
laser6->m_Interval = 1000;
laser6->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser6->m_ClientCode] = laser6;
CommunicationCfg* laser7 = new CommunicationCfg(mid, "LASER7", u8"激光7", _(u8"激光7"), CommunicationCfg::TCP);
laser7->m_IP = "192.168.2.36";
laser7->m_Port = 10001;
laser7->m_Interval = 1000;
laser7->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser7->m_ClientCode] = laser7;
CommunicationCfg* laser8 = new CommunicationCfg(mid, "LASER8", u8"激光8", _(u8"激光8"), CommunicationCfg::TCP);
laser8->m_IP = "192.168.2.37";
laser8->m_Port = 10001;
laser8->m_Interval = 1000;
laser8->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laser8->m_ClientCode] = laser8;
CommunicationCfg* plateTemp = new CommunicationCfg(mid, "PLATE_TEMP", u8"基板温控", _(u8"基板温控"), CommunicationCfg::TCP);
2024-03-19 17:45:12 +08:00
plateTemp->m_Addr = 1;
plateTemp->m_IP = "192.168.2.200";
plateTemp->m_Port = 4004;
plateTemp->m_Interval = 1000;
2024-03-19 17:45:12 +08:00
plateTemp->m_AlarmTimeoutTimes = 5;
m_CommunicationCfgMap[plateTemp->m_ClientCode] = plateTemp;
CommunicationCfg* oxygen = new CommunicationCfg(mid, "OXYGEN", u8"测氧", _(u8"测氧"), CommunicationCfg::TCP);
2024-03-19 17:45:12 +08:00
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", _("PLC"), CommunicationCfg::S7);
2024-03-19 17:45:12 +08:00
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* laserChiller = new CommunicationCfg(mid, "BUS_CHILLER", u8"总冷水机", _(u8"总冷水机"), CommunicationCfg::TCP);
laserChiller->m_Addr = 0x01;
laserChiller->m_IP = "192.168.2.206";
2024-03-19 17:45:12 +08:00
laserChiller->m_Port = 4001;
laserChiller->m_Interval = 1000;
laserChiller->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[laserChiller->m_ClientCode] = laserChiller;
CommunicationCfg* ups = new CommunicationCfg(mid, "UPS", "UPS", _("UPS"), CommunicationCfg::TCP);
2024-03-19 17:45:12 +08:00
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"单供粉", _(u8"单供粉"), CommunicationCfg::S7);
2024-03-19 17:45:12 +08:00
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"称重小车", _(u8"称重小车"), CommunicationCfg::TCP);
2024-03-19 17:45:12 +08:00
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;
2024-03-19 17:45:12 +08:00
CommunicationCfg* scanpower = new CommunicationCfg(mid, "SCANNER_POWER", u8"振镜电源", _(u8"振镜电源"), CommunicationCfg::TCP);
2024-03-19 17:45:12 +08:00
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"电能质量", _(u8"电能质量"), CommunicationCfg::TCP);
2024-03-19 17:45:12 +08:00
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;
CommunicationCfg* temphum = new CommunicationCfg(mid, "TEMP_HUMIDITY_UNIT", u8"温湿单元", _(u8"温湿单元"), CommunicationCfg::TCP);
temphum->m_IP = "192.168.2.200";
temphum->m_Port = 4008;
temphum->m_Interval = 1000;
temphum->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[temphum->m_ClientCode] = temphum;
CommunicationCfg* pipeWater = new CommunicationCfg(mid, "PIPE_WATER_TEMP", u8"管道水温监控", _(u8"管道水温监控"), CommunicationCfg::TCP);
pipeWater->m_Addr = 0x1;
pipeWater->m_IP = "192.168.2.200";
pipeWater->m_Port = 4009;
pipeWater->m_COM = "COM1";
pipeWater->m_BaudRate = 9600;
pipeWater->m_Parity = 0;
pipeWater->m_DataBits = 8;
pipeWater->m_StopBits = 1;
pipeWater->m_Interval = 1000;
pipeWater->m_AlarmTimeoutTimes = 3;
m_CommunicationCfgMap[pipeWater->m_ClientCode] = pipeWater;
2024-03-19 17:45:12 +08:00
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)
2024-03-19 17:45:12 +08:00
{
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";
}
}
}