#include "dlginputaddr.h" #include "ui_dlginputaddr.h" #include #include #include #include #include #include #include #include "qnamespace.h" #include #include #include #include #include #include #include // 修改超级密码,一周一变,增加校验码,且顺序改为 123C4DD #define MAC_FILE_PATH "/home/plg/conf/mac.bin" DlgInputAddr::DlgInputAddr(QWidget *parent) : QDialog(parent), ui(new Ui::DlgInputAddr) { ui->setupUi(this); initTimer(); connect(this,SIGNAL(changePowerCnt(QString )),ui->labPowerMinuteCnt,SLOT(setText(QString))); connect(this,SIGNAL(changePrintCnt(QString )),ui->labPrintDotCnt,SLOT(setText(QString))); curDate =QDate::currentDate(); QDate curDate1; curDate1.setDate(2017,11,13); if(curDatelineEdit->setValidator(new QIntValidator(100000, 999999, this)); ui->superNodeIp3->setValidator(new QIntValidator(1, 255, this)); ui->superNodeIp2->setValidator(new QIntValidator(0, 255, this)); ui->superNodeIp1->setValidator(new QIntValidator(0, 255, this)); ui->superNodeIp0->setValidator(new QIntValidator(1, 255, this)); ui->edgeIp3->setValidator(new QIntValidator(1, 255, this)); ui->edgeIp2->setValidator(new QIntValidator(0, 255, this)); ui->edgeIp1->setValidator(new QIntValidator(0, 255, this)); ui->edgeIp0->setValidator(new QIntValidator(1, 255, this)); repairTimeLess = 0; adjustSize(); } DlgInputAddr::~DlgInputAddr() { delete ui; } void DlgInputAddr::SetAddr() { jd.addr = ui->lineEdit->text().toInt(0,10); if(jd.addr < 100001 || jd.addr > 999999) jd.addr = 100001; //QStringList superNodeIpList = ui->superNodeIpEdit->text().split("."); quint8 ip3 = ui->superNodeIp3->text().toInt(0,10); quint8 ip2 = ui->superNodeIp2->text().toInt(0,10); quint8 ip1 = ui->superNodeIp1->text().toInt(0,10); quint8 ip0 = ui->superNodeIp0->text().toInt(0,10); jd.superNodeIp = (ip3<<24) + (ip2<<16) + (ip1<<8) + ip0; ip3 = ui->edgeIp3->text().toInt(0,10); ip2 = ui->edgeIp2->text().toInt(0,10); ip1 = ui->edgeIp1->text().toInt(0,10); ip0 = ui->edgeIp0->text().toInt(0,10); jd.edgeIp = (ip3<<24) + (ip2<<16) + (ip1<<8) + ip0; SaveJm(); SaveAddrBackup(); } void DlgInputAddr::vpnConnect() { QStringList edgeArgList; //QProcess edge //edge -d n2n0 -c vernonbach -k encryptvb -a 10.0.0.11 -l xxx.xxx.xxx.xxx:1000 >/dev/null & edgeArgList<<"-d"<<"n2n0"; edgeArgList<<"-c"<<"vernonbach"; edgeArgList<<"-k"<<"encryptvb"; edgeArgList<<"-a"<>24)&0xFF).arg((jd.edgeIp>>16)&0xFF).arg((jd.edgeIp>>8)&0xFF).arg((jd.edgeIp)&0xFF); edgeArgList<<"-l"<>24)&0xFF).arg((jd.superNodeIp>>16)&0xFF).arg((jd.superNodeIp>>8)&0xFF).arg((jd.superNodeIp)&0xFF); edgeArgList<<">/dev/null &"; QProcess::startDetached("edge",edgeArgList); } QString DlgInputAddr::GetAddr() { ReadJm(); return ui->lineEdit->text(); } void DlgInputAddr::SaveJm() { QFile file("/lib/syst"); if (file.isOpen()) { file.close(); } jdDataRwLock.lockForWrite(); if(!file.open(QIODevice::WriteOnly)){//怀疑是QIODevice::Truncate 引起参数错误,去掉试试看 QMessageBox::warning(0,"警告",QString("无法打开文件 1") ); } else { file.seek(0); file.write((char *)&jd,(qint64)sizeof(jd)); fsync(file.handle()); file.close(); } jdDataRwLock.unlock(); } void DlgInputAddr::ReadJm() { QFile file("/lib/syst"); QFile Macfile(MAC_FILE_PATH); unsigned int addr; if(!Macfile.exists()) { if(!Macfile.open(QIODevice::WriteOnly)){//怀疑是QIODevice::Truncate 引起参数错误,去掉试试看 QMessageBox::warning(0,"警告",QString("无法打开文件:mac.bin") ); } } if(!file.exists()) { if(!file.open(QIODevice::WriteOnly)){//怀疑是QIODevice::Truncate 引起参数错误,去掉试试看 QMessageBox::warning(0,"警告",QString("无法打开文件") ); } else { file.close(); ResetJm(); show(); } } else { if (file.isOpen()) { file.close(); } jdDataRwLock.lockForRead(); if(!file.open(QIODevice::ReadOnly)){//怀疑是QIODevice::Truncate 引起参数错误,去掉试试看 QMessageBox::warning(0,"警告",QString("无法打开文件") ); } else { file.seek(0); //file.read((char *)&jd.addr,(long long int)sizeof(jd)); file.read((char *)&jd,(qint64)sizeof(jd)); file.close(); } //////////////////// if (Macfile.isOpen()) { Macfile.close(); } //jdDataRwLock.lockForRead(); if(!Macfile.open(QIODevice::ReadOnly)){//怀疑是QIODevice::Truncate 引起参数错误,去掉试试看 //QMessageBox::warning(0,"警告",QString("无法打开文件") ); } else { Macfile.seek(0); //file.read((char *)&jd.addr,(long long int)sizeof(jd)); Macfile.read((char *)&addr,(qint64)sizeof(addr)); Macfile.close(); } jdDataRwLock.unlock(); } if((jd.addr != addr) && (addr >= 100001 && addr <= 999999)){ jd.addr = addr; SaveJm(); } ui->lineEdit->setText(QString().sprintf("%06d",jd.addr) ); ui->labPowerMax->setText(QString("%1").arg(jd.powerMinuteMax)); ui->labPrintMax->setText(QString("%1").arg(jd.printMax)); ui->labPowerMinuteCnt->setText(QString("%1").arg(jd.powerMinuteCnt)); ui->labPrintDotCnt->setText(QString("%1").arg(jd.printCnt)); ui->EditStartTime->setText(QString("%1").arg(jd.start)); ui->EditEndTime->setText(QString("%1").arg(jd.end)); //qDebug()<<"jd.superNodeIp"<superNodeIp3->setText(QString::number((jd.superNodeIp>>24)&0xFF)); ui->superNodeIp2->setText(QString::number((jd.superNodeIp>>16)&0xFF)); ui->superNodeIp1->setText(QString::number((jd.superNodeIp>>8)&0xFF)); ui->superNodeIp0->setText(QString::number((jd.superNodeIp)&0xFF)); ui->edgeIp3->setText(QString::number((jd.edgeIp>>24)&0xFF)); ui->edgeIp2->setText(QString::number((jd.edgeIp>>16)&0xFF)); ui->edgeIp1->setText(QString::number((jd.edgeIp>>8)&0xFF)); ui->edgeIp0->setText(QString::number((jd.edgeIp)&0xFF)); //ui->lineEditLanIP->setInputMask("119.23.201.56"); } void DlgInputAddr::setNodeIpAndPrivateIp(bool bNodeIpSetDone) { ui->label->setVisible(bNodeIpSetDone); ui->lineEdit->setEnabled(bNodeIpSetDone); ui->lineEdit->setVisible(bNodeIpSetDone); ui->label_2->setVisible(bNodeIpSetDone); ui->EditStartTime->setEnabled(bNodeIpSetDone); ui->EditStartTime->setVisible(bNodeIpSetDone); ui->label_3->setVisible(bNodeIpSetDone); ui->EditEndTime->setEnabled(bNodeIpSetDone); ui->EditEndTime->setVisible(bNodeIpSetDone); ui->horizontalLayout->setEnabled(bNodeIpSetDone); ui->labPowerMinuteCnt->setVisible(bNodeIpSetDone); ui->labPowerMax->setVisible(bNodeIpSetDone); ui->labPrintDotCnt->setVisible(bNodeIpSetDone); ui->labPrintMax->setVisible(bNodeIpSetDone); ui->line->setVisible(bNodeIpSetDone); ui->line_2->setVisible(bNodeIpSetDone); ui->line_3->setVisible(bNodeIpSetDone); //adjustSize(); } void DlgInputAddr::on_buttonBox_clicked(QAbstractButton *button) { //QMessageBox::warning(0,"警告",QString("无法打开文件 3") ); if(ui->buttonBox->button(QDialogButtonBox::Ok) == button) //判断按下的是否为"确定”按钮 { if(!ui->lineEdit->text().isEmpty()) //判断lineEdit是否为空,不为空返回0 { SetAddr(); vpnConnect(); emit changeMac(QString(" Mac:%1").arg(jd.addr)); } } setNodeIpAndPrivateIp(true); //else if(button == ui->buttonBox->button((QDialogButtonBox::Cancel))) // ui->lineEdit->setText("001002"); } #define XXXX char DlgInputAddr::isEnabled()// check where is enable?10秒钟1次 { char res=0; static int cnt=0; static char changTimeCnt=0; curDate =QDate::currentDate(); unsigned int ndate = curDate.toString("yyyyMMdd").toInt(0,10); if(cnt<5) { cnt++; if((jd.workout && jd.nDate<=jd.start) && ndate>=jd.start && ndate< jd.end ) { jd.nDate = ndate; jd.workout =0; SaveJm(); // qDebug()<ndate ) { res |=(1<<2); if( jd.workout==0) { jd.nDate = ndate; jd.workout =1; SaveJm(); // qDebug()<jd.end ) { // QString jdEnd = QString::number(jd.end); // QDateTime currentTime = QDateTime::currentDateTime(); // QDateTime endTime = QDateTime::fromString(jdEnd, "yyyyMMdd"); //// qDebug() << "currentTime.toTime_t()" << currentTime.toTime_t(); //// qDebug() << "endTime.toTime_t(" << endTime.toTime_t(); // repairTimeLess = (endTime.toTime_t() - currentTime.toTime_t()) / (60*60); // emit printErrorStatus(QString(tr("Mac:%1维护时间剩余")+"%2").arg(jd.addr).arg(repairTimeLess)+tr("小时,喷码机将停止工作.请与供应商联系!"),2); } //qDebug()<3) { changTimeCnt=0; QTime time1= QTime::currentTime(); //qDebug()<=jd.end); else { // jd.nDate = ndate; // SaveJm(); //qDebug()< void adjustdate1(int year, int mon, int mday, int hour, int min, int sec) { time_t t; struct tm nowtime; nowtime.tm_sec = sec; // Seconds.[0-60] (1 leap second) nowtime.tm_min = min; // Minutes.[0-59] nowtime.tm_hour = hour; // Hours. [0-23] nowtime.tm_mday = mday; // Day.[1-31] nowtime.tm_mon = mon - 1; // Month. [0-11] nowtime.tm_year = year - 1900; // Year- 1900. nowtime.tm_isdst = -1; // DST.[-1/0/1] t = mktime(&nowtime); // qDebug()<start(10 * 1000); } unsigned char DlgInputAddr::checkSupperJmOLD(QString str) { QString s; QDate curDate; curDate =QDate::currentDate(); s.sprintf("%04d%02d", curDate.year(),curDate.month()); unsigned long m,n; m= str.toLong(0,10); n= mb_crc2(s,0xa001); //qDebug()< #include /*用到time函数和gmtime函数。*/ int getWeekNumber() { int nweek = 0; int wd = 0; time_t t; /*声明time_t结构体 t 。*/ struct tm *ptr; /*声明tm结构体指针。*/ time(&t); /*初始化time_t结构体t 。*/ ptr = gmtime(&t); /*获得已初始化的tm结构体。*/ nweek = ptr->tm_yday / 7 + 1; /*以1月1日那周为第1周。*/ wd = ptr->tm_wday; /*算出今天是星期几。*/ if (wd - ptr->tm_yday % 7 < 0)/*表示是新的一周。*/ { nweek++; } //printf("从1月1日算起今周是第 %d 周。\n", nweek); return nweek; } // 一时一变 unsigned char DlgInputAddr::checkSupperInit(QString str) { QString s; QDate curDate; curDate =QDate::currentDate(); QTime curTime; curTime = QTime::currentTime(); s.sprintf("%04d%02d", curDate.year(), getWeekNumber()+curDate.day()+curTime.hour()); unsigned long m,n; m= str.toLong(0,10); n= mb_crc2(s,0xa061); s.sprintf("%06ld",n*100+ (m%100)); QString s2 =mb_crc8(s); if(mb_crc8(s) == str) { SetNewJm(enValidity(m,0xa061) ); return 1; } return 0; } // 一周一变 unsigned char DlgInputAddr::checkSupperJm(QString str) { QString s; QDate curDate; curDate =QDate::currentDate(); // check crc //s.sprintf("%04d%02d", curDate.year(), curDate.month()); s.sprintf("%04d%02d", curDate.year(), getWeekNumber()); unsigned long m,n; m= str.toLong(0,10); n= mb_crc2(s,0xa001); s.sprintf("%06ld",n*100+ (m%100)); //qDebug()<>=1; crc^=m; // } else crc>>=1; } } return(crc ); } void DlgInputAddr::enCodeMsh(QString s) { } void DlgInputAddr::SaveAddrBackup() { QFile Macfile(MAC_FILE_PATH); if (Macfile.isOpen()) { Macfile.close(); } jdDataRwLock.lockForWrite(); if(!Macfile.open(QIODevice::WriteOnly)){//怀疑是QIODevice::Truncate 引起参数错误,去掉试试看 //QMessageBox::warning(0,"警告",QString("无法打开文件:mac.bin") ); } else { Macfile.seek(0); Macfile.write((char *)&jd,(qint64)sizeof(jd)); fsync(Macfile.handle()); Macfile.close(); } jdDataRwLock.unlock(); }