GrpcPrint/PrintS/Config/dao/CommunicationCfgDao.cpp

399 lines
17 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"远控", _(u8"远控"), CommunicationCfg::TCP);
remote->m_IP->SetValue("192.168.3.88");
remote->m_Port ->SetValue( 12019);
remote->m_Interval->SetValue(500);
remote->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[remote->m_ClientCode->GetValueStr()] = remote;
CommunicationCfg* xtPurifier = new CommunicationCfg(mid, "XT_PURIFIER", u8"仙塔净化器", _(u8"仙塔净化器"), CommunicationCfg::S7);
xtPurifier->m_IP->SetValue("192.168.2.1");
xtPurifier->m_Port->SetValue(102);
xtPurifier->m_Interval->SetValue(500);
xtPurifier->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[xtPurifier->m_ClientCode->GetValueStr()] = xtPurifier;
CommunicationCfg* hbdPurifier3 = new CommunicationCfg(mid, "HBD_PURIFIER_G3", u8"三代净化器", _(u8"三代净化器"), CommunicationCfg::S7);
hbdPurifier3->m_IP->SetValue("192.168.2.1");
hbdPurifier3->m_Port->SetValue(102);
hbdPurifier3->m_Interval->SetValue(300);
hbdPurifier3->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[hbdPurifier3->m_ClientCode->GetValueStr()] = hbdPurifier3;
CommunicationCfg* m1Purifier = new CommunicationCfg(mid, "M1_PURIFIER", u8"M1系列净化器", _(u8"M1系列净化器"), CommunicationCfg::S7);
m1Purifier->m_IP->SetValue("192.168.2.1");
m1Purifier->m_Port->SetValue(102);
m1Purifier->m_Interval->SetValue(300);
m1Purifier->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[m1Purifier->m_ClientCode->GetValueStr()] = m1Purifier;
CommunicationCfg* laser1 = new CommunicationCfg(mid, "LASER1", u8"激光1", _(u8"激光1"), CommunicationCfg::TCP);
laser1->m_IP->SetValue("192.168.2.30");
laser1->m_Port->SetValue(10001);
laser1->m_Interval->SetValue(1000);
laser1->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laser1->m_ClientCode->GetValueStr()] = laser1;
CommunicationCfg* laser2 = new CommunicationCfg(mid, "LASER2", u8"激光2", _(u8"激光2"), CommunicationCfg::TCP);
laser2->m_IP->SetValue("192.168.2.31");
laser2->m_Port->SetValue(10001);
laser2->m_Interval->SetValue(1000);
laser2->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laser2->m_ClientCode->GetValueStr()] = laser2;
CommunicationCfg* laser3 = new CommunicationCfg(mid, "LASER3", u8"激光3", _(u8"激光3"), CommunicationCfg::TCP);
laser3->m_IP->SetValue("192.168.2.32");
laser3->m_Port->SetValue(10001);
laser3->m_Interval->SetValue(1000);
laser3->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laser3->m_ClientCode->GetValueStr()] = laser3;
CommunicationCfg* laser4 = new CommunicationCfg(mid, "LASER4", u8"激光4", _(u8"激光4"), CommunicationCfg::TCP);
laser4->m_IP->SetValue("192.168.2.33");
laser4->m_Port->SetValue(10001);
laser4->m_Interval->SetValue(1000);
laser4->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laser4->m_ClientCode->GetValueStr()] = laser4;
CommunicationCfg* laser5 = new CommunicationCfg(mid, "LASER5", u8"激光5", _(u8"激光5"), CommunicationCfg::TCP);
laser5->m_IP->SetValue("192.168.2.34");
laser5->m_Port->SetValue(10001);
laser5->m_Interval->SetValue(1000);
laser5->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laser5->m_ClientCode->GetValueStr()] = laser5;
CommunicationCfg* laser6 = new CommunicationCfg(mid, "LASER6", u8"激光6", _(u8"激光6"), CommunicationCfg::TCP);
laser6->m_IP->SetValue("192.168.2.35");
laser6->m_Port->SetValue(10001);
laser6->m_Interval->SetValue(1000);
laser6->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laser6->m_ClientCode->GetValueStr()] = laser6;
CommunicationCfg* laser7 = new CommunicationCfg(mid, "LASER7", u8"激光7", _(u8"激光7"), CommunicationCfg::TCP);
laser7->m_IP->SetValue("192.168.2.36");
laser7->m_Port->SetValue(10001);
laser7->m_Interval->SetValue(1000);
laser7->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laser7->m_ClientCode->GetValueStr()] = laser7;
CommunicationCfg* laser8 = new CommunicationCfg(mid, "LASER8", u8"激光8", _(u8"激光8"), CommunicationCfg::TCP);
laser8->m_IP->SetValue("192.168.2.37");
laser8->m_Port->SetValue(10001);
laser8->m_Interval->SetValue(1000);
laser8->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laser8->m_ClientCode->GetValueStr()] = laser8;
CommunicationCfg* plateTemp = new CommunicationCfg(mid, "PLATE_TEMP", u8"基板温控", _(u8"基板温控"), CommunicationCfg::TCP);
plateTemp->m_Addr->SetValue(1);
plateTemp->m_IP->SetValue("192.168.2.200");
plateTemp->m_Port->SetValue(4004);
plateTemp->m_Interval->SetValue(1000);
plateTemp->m_AlarmTimeoutTimes->SetValue(5);
m_CommunicationCfgMap[plateTemp->m_ClientCode->GetValueStr()] = plateTemp;
CommunicationCfg* oxygen = new CommunicationCfg(mid, "OXYGEN", u8"测氧", _(u8"测氧"), CommunicationCfg::TCP);
oxygen->m_Addr->SetValue(1);
oxygen->m_IP->SetValue("192.168.2.200");
oxygen->m_Port->SetValue(4000);
oxygen->m_Interval->SetValue(300);
oxygen->m_AlarmTimeoutTimes->SetValue(5);
oxygen->m_Enable->SetValue(true);
m_CommunicationCfgMap[oxygen->m_ClientCode->GetValueStr()] = oxygen;
CommunicationCfg* plc = new CommunicationCfg(mid, "PLC", "PLC", _("PLC"), CommunicationCfg::S7);
plc->m_IP->SetValue("192.168.2.202");
plc->m_Port->SetValue(102);
plc->m_Interval->SetValue(30);
plc->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[plc->m_ClientCode->GetValueStr()] = plc;
CommunicationCfg* laserChiller = new CommunicationCfg(mid, "BUS_CHILLER", u8"总冷水机", _(u8"总冷水机"), CommunicationCfg::TCP);
laserChiller->m_Addr->SetValue(0x01);
laserChiller->m_IP->SetValue("192.168.2.206");
laserChiller->m_Port->SetValue(4001);
laserChiller->m_Interval->SetValue(1000);
laserChiller->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[laserChiller->m_ClientCode->GetValueStr()] = laserChiller;
CommunicationCfg* ups = new CommunicationCfg(mid, "UPS", "UPS", _("UPS"), CommunicationCfg::TCP);
ups->m_IP->SetValue("192.168.2.200");
ups->m_Port->SetValue(4003);
ups->m_Interval->SetValue(1000);
ups->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[ups->m_ClientCode->GetValueStr()] = ups;
CommunicationCfg* simplesupply = new CommunicationCfg(mid, "SIMPLE_SUPPLY", u8"单供粉", _(u8"单供粉"), CommunicationCfg::S7);
simplesupply->m_IP->SetValue("192.168.2.2");
simplesupply->m_Port->SetValue(102);
simplesupply->m_Interval->SetValue(500);
simplesupply->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[simplesupply->m_ClientCode->GetValueStr()] = simplesupply;
CommunicationCfg* weightcar = new CommunicationCfg(mid, "WEIGHT_CAR", u8"称重小车", _(u8"称重小车"), CommunicationCfg::TCP);
weightcar->m_IP->SetValue("192.168.2.200");
weightcar->m_Port->SetValue(4007);
weightcar->m_Interval->SetValue(2000);
weightcar->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[weightcar->m_ClientCode->GetValueStr()] = weightcar;
CommunicationCfg* scanpower = new CommunicationCfg(mid, "SCANNER_POWER", u8"振镜电源", _(u8"振镜电源"), CommunicationCfg::TCP);
scanpower->m_IP->SetValue("192.168.2.200");
scanpower->m_Port->SetValue(4006);
scanpower->m_Interval->SetValue(1000);
scanpower->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[scanpower->m_ClientCode->GetValueStr()] = scanpower;
CommunicationCfg* powerMeter = new CommunicationCfg(mid, "POWER_METER", u8"电能质量", _(u8"电能质量"), CommunicationCfg::TCP);
powerMeter->m_IP->SetValue("192.168.2.200");
powerMeter->m_Port->SetValue(4005);
powerMeter->m_Interval->SetValue(1000);
powerMeter->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[powerMeter->m_ClientCode->GetValueStr()] = powerMeter;
CommunicationCfg* temphum = new CommunicationCfg(mid, "TEMP_HUMIDITY_UNIT", u8"温湿单元", _(u8"温湿单元"), CommunicationCfg::TCP);
temphum->m_IP->SetValue("192.168.2.200");
temphum->m_Port->SetValue(4008);
temphum->m_Interval->SetValue(1000);
temphum->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[temphum->m_ClientCode->GetValueStr()] = temphum;
CommunicationCfg* pipeWater = new CommunicationCfg(mid, "PIPE_WATER_TEMP", u8"管道水温监控", _(u8"管道水温监控"), CommunicationCfg::TCP);
pipeWater->m_Addr->SetValue(0x1);
pipeWater->m_IP->SetValue("192.168.2.200");
pipeWater->m_Port->SetValue(4009);
pipeWater->m_COM->SetValue("COM1");
pipeWater->m_BaudRate->SetValue(9600);
pipeWater->m_Parity->SetValue(0);
pipeWater->m_DataBits->SetValue(8);
pipeWater->m_StopBits->SetValue(1);
pipeWater->m_Interval->SetValue(1000);
pipeWater->m_AlarmTimeoutTimes->SetValue(3);
m_CommunicationCfgMap[pipeWater->m_ClientCode->GetValueStr()] = pipeWater;
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->GetValueStr().c_str());
ins.push_back(buffer);
}
else {
it->second->m_Name->SetValue((*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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Type->SetValue(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->GetValueStr().c_str());
ins.push_back(buffer);
}
else {
it->second->m_IP->SetValue((*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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Port->SetValue(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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Addr->SetValue(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->GetValueStr().c_str());
ins.push_back(buffer);
}
else {
it->second->m_COM->SetValue((*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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_BaudRate->SetValue(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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Parity->SetValue(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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_DataBits->SetValue(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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_StopBits->SetValue(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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_Interval->SetValue(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->GetValue()).c_str());
ins.push_back(buffer);
}
else {
it->second->m_AlarmTimeoutTimes->SetValue(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->GetValue() ? "1" : "0");
ins.push_back(buffer);
}
else {
it->second->m_Enable->SetValue(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";
}
}
}