196 lines
6.7 KiB
C++
196 lines
6.7 KiB
C++
#include "NTPServerConfig.h"
|
|
#include "ui_NTPServerConfig.h"
|
|
#include "NetMgr.h"
|
|
#include <QTime>
|
|
#include <QRegExpValidator>
|
|
#include "ftpclient.h"
|
|
|
|
CNTPServerConfig::CNTPServerConfig(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::CNTPServerConfig)
|
|
{
|
|
ui->setupUi(this);
|
|
ui->widget_3->setProperty("flag", "Title");
|
|
ui->widget->setProperty("flag", "normal");
|
|
QString exp = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.)"
|
|
"{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
|
|
QRegExp rege(exp);
|
|
QValidator *Validator = new QRegExpValidator(rege);
|
|
ui->lineEdit_NTP_IP->setValidator(Validator);
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
CNTPServerConfig::~CNTPServerConfig()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void CNTPServerConfig::on_pushButton_2_clicked()
|
|
{
|
|
|
|
}
|
|
void CNTPServerConfig::slotNetMgr(QString sAddr, const QVariant &msg)
|
|
{
|
|
QJsonObject objec = msg.value<QJsonObject>();
|
|
if(objec.contains("cmd"))
|
|
{
|
|
QJsonValue arrays_value = objec.take("cmd");
|
|
if(arrays_value.toString() == "02")
|
|
{
|
|
QJsonObject cmdBodyObj = objec["cmdBody"].toObject();
|
|
QJsonValue arrays_value = cmdBodyObj.take("type");
|
|
if(arrays_value == "SET"){
|
|
bool Status = objec.take("success").toBool();
|
|
QString strMessage = objec.take("message").toString();
|
|
if(Status){
|
|
QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("保存成功!"));
|
|
}else{
|
|
QMessageBox::information(this, QStringLiteral("提示"), strMessage);
|
|
}
|
|
}else if(arrays_value == "GET"){
|
|
int timestamp_value = cmdBodyObj.take("timeStamp").toInt();
|
|
QDateTime dateTime;
|
|
|
|
dateTime.setSecsSinceEpoch(timestamp_value);
|
|
|
|
QString strTime = dateTime.toString("yyyy-MM-dd hh:mm:ss");
|
|
ui->label_time->setText(strTime);
|
|
}
|
|
}
|
|
}
|
|
disconnect(g_NetMgr,SIGNAL(sigNetMgr(QString, const QVariant&)), this, SLOT(slotNetMgr(QString,const QVariant&)));
|
|
}
|
|
|
|
void CNTPServerConfig::on_pushButton_manual_clicked()
|
|
{
|
|
QJsonObject allObj,cmdBody;
|
|
allObj.insert("cmd", "02");
|
|
cmdBody.insert("type","SET");
|
|
long timeStamp = QDateTime::currentDateTime().toTime_t();
|
|
QString str = QString("%1").arg(timeStamp);
|
|
cmdBody["timeStamp"] = str.toInt();
|
|
allObj["cmdBody"] = cmdBody;
|
|
QNetworkRequest req;
|
|
QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP);
|
|
req.setUrl(sUrl);
|
|
g_NetMgr->PostJson(req,allObj);
|
|
connect(g_NetMgr,SIGNAL(sigNetMgr(QString, const QVariant&)), this, SLOT(slotNetMgr(QString,const QVariant&)));
|
|
}
|
|
|
|
void CNTPServerConfig::Init()
|
|
{
|
|
|
|
QString fileName = QCoreApplication::applicationDirPath() + "\\config\\ServerConfig.json";
|
|
if(!fileName.isEmpty())
|
|
{
|
|
qDebug() << "打开" << fileName ;
|
|
QFile file(fileName);
|
|
file.open(QIODevice::ReadOnly | QIODevice::Text);
|
|
QString value = file.readAll();
|
|
file.close();
|
|
|
|
QJsonParseError parseJsonErr;
|
|
QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr);
|
|
if (!(parseJsonErr.error == QJsonParseError::NoError)) {
|
|
QMessageBox::about(NULL, "提示", "读取文件错误!");
|
|
return;
|
|
}
|
|
m_ServerObject = document.object();
|
|
if(m_ServerObject.contains(QStringLiteral("NTP_Server"))){
|
|
QJsonObject tempObject = m_ServerObject.value(QStringLiteral("NTP_Server")).toObject();
|
|
QJsonObject OptionObject = tempObject["option"].toObject();
|
|
ui->lineEdit_NTP_IP->setText(OptionObject["ntpNetworkStation"].toString());
|
|
if(OptionObject["ntpSwitch"].toString() == "0"){
|
|
ui->radioButton_switch->setChecked(false);
|
|
ui->radioButton_switch->setText("关闭");
|
|
}else if(OptionObject["ntpSwitch"].toString() == "1"){
|
|
ui->radioButton_switch->setChecked(true);
|
|
ui->radioButton_switch->setText("启动");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void CNTPServerConfig::slotReplyStatus(int result)
|
|
{
|
|
qDebug()<< "Result" << result << endl;
|
|
if(!result){
|
|
QJsonObject NTPServerObj,itemObj;
|
|
itemObj["ntpNetworkStation"] = ui->lineEdit_NTP_IP->text();
|
|
if(ui->radioButton_switch->isChecked()){
|
|
ui->radioButton_switch->setChecked(false);
|
|
itemObj["ntpSwitch"] = "0";
|
|
ui->radioButton_switch->setText("关闭");
|
|
}
|
|
else{
|
|
ui->radioButton_switch->setChecked(true);
|
|
itemObj["ntpSwitch"] = "1";
|
|
ui->radioButton_switch->setText("启动");
|
|
}
|
|
|
|
NTPServerObj["option"] = itemObj;
|
|
m_ServerObject["NTP_Server"] = NTPServerObj;
|
|
QJsonDocument jsonDoc;
|
|
jsonDoc.setObject(m_ServerObject);
|
|
|
|
QString fileName = QCoreApplication::applicationDirPath() + "\\config\\ServerConfig.json";
|
|
QFile file(fileName);
|
|
file.open(QIODevice::WriteOnly);
|
|
file.write(jsonDoc.toJson());
|
|
file.close();
|
|
}
|
|
}
|
|
|
|
void CNTPServerConfig::on_radioButton_switch_clicked()
|
|
{
|
|
connect(g_FtpClient, SIGNAL(sigReplyStatus(int)), this, SLOT(slotReplyStatus(int)));
|
|
|
|
QJsonObject NTPServerObj,itemObj;
|
|
itemObj["ntpNetworkStation"] = ui->lineEdit_NTP_IP->text();
|
|
if(ui->radioButton_switch->isChecked()){
|
|
itemObj["ntpSwitch"] = "1";
|
|
ui->radioButton_switch->setText("启动");
|
|
}
|
|
else{
|
|
itemObj["ntpSwitch"] = "0";
|
|
ui->radioButton_switch->setText("关闭");
|
|
}
|
|
|
|
NTPServerObj["option"] = itemObj;
|
|
m_ServerObject["NTP_Server"] = NTPServerObj;
|
|
QJsonDocument jsonDoc;
|
|
jsonDoc.setObject(m_ServerObject);
|
|
|
|
QString fileName = QCoreApplication::applicationDirPath() + "\\config\\ServerConfig.json";
|
|
QFile file(fileName);
|
|
file.open(QIODevice::WriteOnly);
|
|
file.write(jsonDoc.toJson());
|
|
file.close();
|
|
|
|
QString str = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("ServerConfig.json");
|
|
g_FtpClient->SetServerInfo(str);
|
|
g_FtpClient->SetUserInfo("root","@#cidw!@123456");
|
|
g_FtpClient->UpLoadFile(fileName,"ServerConfig.json",4);
|
|
}
|
|
|
|
|
|
void CNTPServerConfig::on_pushButton_get_clicked()
|
|
{
|
|
QJsonObject allObj,cmdBody;
|
|
allObj.insert("cmd", "02");
|
|
cmdBody.insert("type","GET");
|
|
long timeStamp = QDateTime::currentDateTime().toTime_t();
|
|
QString str = QString("%1").arg(timeStamp);
|
|
cmdBody["timeStamp"] = str.toInt();
|
|
allObj["cmdBody"] = cmdBody;
|
|
QNetworkRequest req;
|
|
QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP);
|
|
req.setUrl(sUrl);
|
|
g_NetMgr->PostJson(req,allObj);
|
|
connect(g_NetMgr,SIGNAL(sigNetMgr(QString, const QVariant&)), this, SLOT(slotNetMgr(QString,const QVariant&)));
|
|
}
|
|
|