Commit 45648845 by xiaojing.zhang

add return msg and load xml

parent 303b1ab7
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <QMap> #include <QMap>
#include <QStringList> #include <QStringList>
//char XML_FREEMUD_CLIENT[256] = {0};
control::control(QObject *parent) : QObject(parent) control::control(QObject *parent) : QObject(parent)
{ {
_isrollbacking = false; _isrollbacking = false;
...@@ -488,6 +490,48 @@ control::~control() ...@@ -488,6 +490,48 @@ control::~control()
} }
} }
bool control::addmessage(string &data)
{
try
{
string src = data;
int statuscode = 0;
rapidjson::Document s;
s.Parse<0>(src.data());
if(s.HasMember(JSON_STATUSCODE_POS))
{
Value &val = s[JSON_STATUSCODE_POS];
if(val.IsInt())
statuscode = val.GetInt();
else if(val.IsString())
statuscode = atoi(val.GetString());
if(statuscode == 100)
{
if(s.HasMember(JSON_MSG_POS))
{
rapidjson::Value &val = s[JSON_MSG_POS];
val.SetString(DEFAULT_JSON_MSG, s.GetAllocator());
} else {
s.AddMember(JSON_MSG_POS, DEFAULT_JSON_MSG, s.GetAllocator());
}
rapidjson::StringBuffer buffers;
rapidjson::Writer<rapidjson::StringBuffer> writers(buffers);
s.Accept(writers);
data = std::string(buffers.GetString());
}
}
return true;
}catch(...)
{
return false;
}
}
void control::swap_and_send_data(std::string compledata, int reqtype) void control::swap_and_send_data(std::string compledata, int reqtype)
{ {
std::string error; std::string error;
...@@ -525,7 +569,7 @@ void control::swap_and_send_data(std::string compledata, int reqtype) ...@@ -525,7 +569,7 @@ void control::swap_and_send_data(std::string compledata, int reqtype)
return ; return ;
} }
QLOG_INFO() << "POS json to server json : " << QString::fromUtf8(respons_pos.data()); QLOG_INFO() << "Server json to pos json : " << QString::fromUtf8(respons_pos.data());
if(respons_pos.empty() || respons_pos.compare("{}") == 0) if(respons_pos.empty() || respons_pos.compare("{}") == 0)
{ {
...@@ -533,6 +577,15 @@ void control::swap_and_send_data(std::string compledata, int reqtype) ...@@ -533,6 +577,15 @@ void control::swap_and_send_data(std::string compledata, int reqtype)
QLOG_INFO() << "chang error msg to pos : " << QString::fromUtf8(respons_pos.data()); QLOG_INFO() << "chang error msg to pos : " << QString::fromUtf8(respons_pos.data());
} }
if(!addmessage(respons_pos))
{
respons_pos = std::string(EMPTY_JSON_FMCONPON);
QLOG_INFO() << "addmessage msg to pos : " << QString::fromUtf8(respons_pos.data());
}
QLOG_INFO() << "Add message json : " << QString::fromUtf8(respons_pos.data());
if(!_network->senddata(QByteArray(respons_pos.data()))) if(!_network->senddata(QByteArray(respons_pos.data())))
{ {
QLOG_ERROR() << "send data to Pos failed"; QLOG_ERROR() << "send data to Pos failed";
......
...@@ -44,6 +44,8 @@ public: ...@@ -44,6 +44,8 @@ public:
bool compressjson(std::string &data); bool compressjson(std::string &data);
bool checkneedhead(QByteArray &data); bool checkneedhead(QByteArray &data);
static bool addmessage(std::string &data);
signals: signals:
public slots: public slots:
......
...@@ -19,7 +19,12 @@ ...@@ -19,7 +19,12 @@
#include "Calc.hpp" #include "Calc.hpp"
#include "tool.h"
#include "global.h"
#include "QsLog.h"
#define XML_FREEMUD_CLIENT "Client.xml" #define XML_FREEMUD_CLIENT "Client.xml"
//extern char XML_FREEMUD_CLIENT[256];
#define XML_NODE_ROOT "Freemud" #define XML_NODE_ROOT "Freemud"
...@@ -88,12 +93,8 @@ typedef vector<string> stringlist; ...@@ -88,12 +93,8 @@ typedef vector<string> stringlist;
class fm_xmltojson class fm_xmltojson
{ {
#ifdef FM_TEST
public:
#else
private:
#endif
public:
static bool GetXMLDocumentHandle(file<> &fdoc, xml_document<> &doc, stringstream &lasterror) static bool GetXMLDocumentHandle(file<> &fdoc, xml_document<> &doc, stringstream &lasterror)
{ {
try try
...@@ -1369,7 +1370,11 @@ public: ...@@ -1369,7 +1370,11 @@ public:
desdoc.Parse<0>("{}"); desdoc.Parse<0>("{}");
GetProcPath(posxml); GetProcPath(posxml);
posxml.append(XML_FREEMUD_CLIENT); QString filename = Tool::ReadCfg(CFG_SECTION_XMLFILENAME, XML_FREEMUD_CLIENT).toString();
posxml.append(filename.toStdString());
QLOG_DEBUG() << "JsonConvertFront xml name :" << QString::fromStdString(posxml);
#ifdef WIN32 #ifdef WIN32
if(_access(posxml.data(), 0) == -1) if(_access(posxml.data(), 0) == -1)
#else #else
...@@ -1430,8 +1435,10 @@ public: ...@@ -1430,8 +1435,10 @@ public:
} }
GetProcPath(posxml); GetProcPath(posxml);
posxml.append(XML_FREEMUD_CLIENT);
QString filename = Tool::ReadCfg(CFG_SECTION_XMLFILENAME, XML_FREEMUD_CLIENT).toString();
posxml.append(filename.toStdString());
QLOG_DEBUG() << "JsonConvertPost xml name :" << QString::fromStdString(posxml);
#ifdef WIN32 #ifdef WIN32
if(_access(posxml.data(), 0) == -1) if(_access(posxml.data(), 0) == -1)
#else #else
......
...@@ -20,7 +20,8 @@ INCLUDEPATH += $$/PWD/include ...@@ -20,7 +20,8 @@ INCLUDEPATH += $$/PWD/include
SOURCES += main.cpp \ SOURCES += main.cpp \
network.cpp \ network.cpp \
control.cpp control.cpp \
refreshxmlwork.cpp
HEADERS += \ HEADERS += \
config.h \ config.h \
...@@ -29,6 +30,7 @@ HEADERS += \ ...@@ -29,6 +30,7 @@ HEADERS += \
tool.h \ tool.h \
control.h \ control.h \
Calc.hpp \ Calc.hpp \
fm_xmltojson.hpp fm_xmltojson.hpp \
refreshxmlwork.h
OTHER_FILES += OTHER_FILES +=
...@@ -39,12 +39,20 @@ ...@@ -39,12 +39,20 @@
#define CFG_SECTION_URLPATHLIST "all/urlpathlist" #define CFG_SECTION_URLPATHLIST "all/urlpathlist"
#define CFG_SECTION_ROLLBACKLIST "all/rolllist" #define CFG_SECTION_ROLLBACKLIST "all/rolllist"
#define CFG_SECTION_XMLHOST "xml/host"
#define CFG_SECTION_XMLFILENAME "all/xmlfilename"
//FMCONPON return error //FMCONPON return error
#define ERRRO_JSON_FMCONPON "{\"status_code\":210, \"msg\":\"%1\"}" #define ERRRO_JSON_FMCONPON "{\"status_code\":210, \"msg\":\"%1\"}"
#define SUCCESS_JSON_FMCONPON "{\"status_code\":100, \"msg\":\"营业日设置成功\"}" #define SUCCESS_JSON_FMCONPON "{\"status_code\":100, \"msg\":\"营业日设置成功\"}"
#define EMPTY_JSON_FMCONPON "{\"status_code\":211, \"msg\":\"服务端返回数据异常\"}" #define EMPTY_JSON_FMCONPON "{\"status_code\":211, \"msg\":\"服务端返回数据异常\"}"
#define ERROR_REQ_HEADER "{\"status_code\":212, \"msg\":\"POS请求数据消息头异常或缺失\"}" #define ERROR_REQ_HEADER "{\"status_code\":212, \"msg\":\"POS请求数据消息头异常或缺失\"}"
//request json
#define DEFAULT_XML_REQUEST_JSON "{\"reqtype\":0}"
#define DEFAULT_JSON_MSG "码已核销"
#define ROLLBACK_FILL "rollback.txt" #define ROLLBACK_FILL "rollback.txt"
...@@ -54,6 +62,15 @@ ...@@ -54,6 +62,15 @@
#define JSON_POSID_POS "pos_id" #define JSON_POSID_POS "pos_id"
#define JSON_STOREID_POS "store_id" #define JSON_STOREID_POS "store_id"
#define JSON_PARTNERID_POS "partner_id" #define JSON_PARTNERID_POS "partner_id"
#define JSON_STATUSCODE_POS "status_code"
#define JSON_MSG_POS "msg"
#define XML_FILENAME "Client_"
#define XML_END ".xml"
#define JSON_PATH_XML "path"
#define JSON_MD5_XML "md5"
#define JSON_STATUSCODE_XML "status_code"
#define JSON_SIGNPARAM_XJ "signParam" #define JSON_SIGNPARAM_XJ "signParam"
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "QsLog.h" #include "QsLog.h"
#include "config.h" #include "config.h"
#include "control.h" #include "control.h"
#include "refreshxmlwork.h"
#ifdef FM_TEST #ifdef FM_TEST
...@@ -23,6 +24,9 @@ int main(int argc, char *argv[]) ...@@ -23,6 +24,9 @@ int main(int argc, char *argv[])
QsLogging::DestinationPtr consleDest(QsLogging::DestinationFactory::MakeDebugOutputDestination()); QsLogging::DestinationPtr consleDest(QsLogging::DestinationFactory::MakeDebugOutputDestination());
logger.addDestination(consleDest); logger.addDestination(consleDest);
RefreshXmlWork work;
work.start();
control con; control con;
con.start(); con.start();
...@@ -54,4 +58,51 @@ int main(int argc, char *argv[]) ...@@ -54,4 +58,51 @@ int main(int argc, char *argv[])
#else #else
#include<QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString logDir = QString("%1/log").arg(QCoreApplication::applicationDirPath());
QDir().mkdir(logDir);
QsLogging::Logger &logger = QsLogging::Logger::instance();
logger.setLoggingLevel(QsLogging::TraceLevel);
QString logPath = QString("%1/%2").arg(logDir, "fmconpon.log");
QsLogging::DestinationPtr fileDst(QsLogging::DestinationFactory::MakeFileDestination(
logPath, QsLogging::EnableLogRotation, QsLogging::MaxSizeBytes(2*1024*1024), QsLogging::MaxOldLogCount(10)));
logger.addDestination(fileDst);
QsLogging::DestinationPtr consleDest(QsLogging::DestinationFactory::MakeDebugOutputDestination());
logger.addDestination(consleDest);
{
std::string msg("{\"status_code\":100}");
bool flag = control::addmessage(msg);
QLOG_INFO() << "flag" << flag << QString::fromUtf8(msg.data());
}
{
std::string msg("{\"status_code\":100,\"msg\":\"test\"}");
bool flag = control::addmessage(msg);
QLOG_INFO() << "flag" << flag << QString::fromUtf8(msg.data());
}
{
std::string msg("{\"status_code\":\"100\"}");
bool flag = control::addmessage(msg);
QLOG_INFO() << "flag" << flag << QString::fromUtf8(msg.data());
}
RefreshXmlWork work;
work.start();
}
#endif #endif
#include "refreshxmlwork.h"
#include "global.h"
#include "tool.h"
#include "QsLog.h"
#include "fm_xmltojson.hpp"
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QEventLoop>
#include <QTimer>
#include <QDir>
#include <QFile>
RefreshXmlWork::RefreshXmlWork(QObject *parent) :
QObject(parent)
{
reply = 0;
}
void RefreshXmlWork::start()
{
QString host = Tool::ReadCfg(QString(CFG_SECTION_XMLHOST), "").toString();
QString path, md5;
if(!getrefreshxml(host, path, md5))
{
QLOG_WARN() << "getrefreshxml failed";
return ;
}
QString localpath;
if(!downloadxml(path, md5, localpath))
{
QLOG_WARN() << "downloadxml failed";
return ;
}
appliedcxml(localpath);
}
bool RefreshXmlWork::getrefreshxml(QString &host, QString &downloadxmlpath, QString &md5)
{
QByteArray recv;
if(!request(QByteArray(DEFAULT_XML_REQUEST_JSON), recv, host))
return false;
QLOG_INFO() << "Request data" << recv;
return getpathandmd5(recv, downloadxmlpath, md5);
}
bool RefreshXmlWork::downloadxml(const QString &path, const QString &md5, QString &localfilename)
{
localfilename = QString(XML_FILENAME) + md5 + XML_END;
if(!downloadfile(path, localfilename))
{
QLOG_WARN() << "downloadfile error";
return false;
}
QString localmd5 = Tool::FileMd5(localfilename);
QLOG_DEBUG() << localmd5;
QLOG_DEBUG() << md5;
if(localmd5.compare(md5, Qt::CaseInsensitive) != 0)
{
QLOG_ERROR() << "md5 check error; local : " << localmd5 << "; md5 : " << md5;
QFile::remove(localfilename);
return false;
}
return true;
}
void RefreshXmlWork::appliedcxml(QString &localfilename)
{
Tool::SetCfg(CFG_SECTION_XMLFILENAME, localfilename);
}
bool RefreshXmlWork::request(const QByteArray &data, QByteArray &recv, const QString &url)
{
QNetworkAccessManager manager;
if(QNetworkAccessManager::Accessible != manager.networkAccessible())
manager.setNetworkAccessible(QNetworkAccessManager::Accessible);
QNetworkRequest request;
request.setUrl(url);
QEventLoop loop;
QNetworkReply *reply = manager.post(request, data);
connect(&manager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), &loop, SLOT(quit()));
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
QTimer::singleShot(10000, &loop, SLOT(quit()));
loop.exec();
if(reply->error() != QNetworkReply::NoError)
{
QLOG_ERROR() << "RefreshXmlWork::request "<< url << " error :" << reply->errorString();
return false;
}
recv = reply->readAll();
if(recv.size() == 0)
{
QLOG_ERROR() << "recv size 0";
return false;
}
return true;
}
bool RefreshXmlWork::getpathandmd5(const QByteArray &data, QString &downloadxmlpath, QString &md5)
{
std::string src(data.data());
try
{
rapidjson::Document s;
s.Parse<0>(src.data());
if(!(s.HasMember(JSON_STATUSCODE_XML) && s[JSON_STATUSCODE_XML].GetInt() == 100))
{
QLOG_WARN() << "status_code error";
return false;
}
if(s.HasMember(JSON_PATH_XML) && s.HasMember(JSON_MD5_XML))
{
std::string tmppath = s[JSON_PATH_XML].GetString();
std::string tmpmd5 = s[JSON_MD5_XML].GetString();
downloadxmlpath = QString::fromStdString(tmppath);
md5 = QString::fromStdString(tmpmd5);
QLOG_INFO() << "downloadxmlpath : " << downloadxmlpath;
QLOG_INFO() << "md5 : " << md5;
return true;
}
return false;
}catch(...)
{
return false;
}
}
bool RefreshXmlWork::downloadfile(const QString &path, QString filename)
{
std::string dirpath;
fm_xmltojson::GetProcPath(dirpath);
QString relpath = QString::fromStdString(dirpath).append(filename);
QLOG_INFO() << "XML download path : " << relpath;
file.setFileName(relpath);
if(!file.open(QIODevice::WriteOnly))
{
QLOG_ERROR() << "file open filed : " << filename;
return false;
}
QNetworkAccessManager manager;
QNetworkRequest request;
QEventLoop loop;
request.setUrl(path);
QLOG_INFO() << "Request URL: " << path;
reply = manager.get(request);
connect(&manager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), &loop, SLOT(quit()));
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
connect(reply, SIGNAL(readyRead()), this, SLOT(onreadyread()));
QTimer::singleShot(10000, &loop, SLOT(quit()));
loop.exec();
file.close();
reply->deleteLater();
if(reply->error() != QNetworkReply::NoError)
{
QLOG_ERROR() << "RefreshXmlWork::request "<< path << " error :" << reply->errorString();
return false;
}
return true;
}
void RefreshXmlWork::onreadyread()
{
file.write(reply->readAll());
}
#ifndef REFRESHXMLWORK_H
#define REFRESHXMLWORK_H
#include <QObject>
#include <QFile>
#include <QNetworkReply>
class RefreshXmlWork : public QObject
{
Q_OBJECT
public:
explicit RefreshXmlWork(QObject *parent = 0);
public:
void start();
bool getrefreshxml(QString &host, QString &downloadxmlpath, QString &md5);
bool downloadxml(const QString &path, const QString &md5, QString &localfilename);
void appliedcxml(QString &localfilename);
bool request(const QByteArray &data, QByteArray &recv, const QString &url);
bool getpathandmd5(const QByteArray &data, QString &downloadxmlpath, QString &md5);
bool downloadfile(const QString &path, QString filename);
public slots:
void onreadyread();
private:
QFile file;
QNetworkReply *reply;
};
#endif // REFRESHXMLWORK_H
...@@ -5,10 +5,13 @@ ...@@ -5,10 +5,13 @@
#include <QString> #include <QString>
#include <QSettings> #include <QSettings>
#include <QVariant> #include <QVariant>
#include <QFile>
#include <QCryptographicHash>
#include "config.h" #include "config.h"
class Tool class Tool
{ {
public: public:
...@@ -18,12 +21,47 @@ public: ...@@ -18,12 +21,47 @@ public:
return QSettings(cfgfilepath, QSettings::IniFormat).value(section, defaultvalue); return QSettings(cfgfilepath, QSettings::IniFormat).value(section, defaultvalue);
} }
static void SetCfg(QString section, QString value)
{
QString cfgfilepath = qApp->applicationDirPath() + QString("/") + QString(CFG_FILEMAE);
QSettings(cfgfilepath, QSettings::IniFormat).setValue(section, QVariant(value));
}
static void GetMD5(std::string src, std::string &des) static void GetMD5(std::string src, std::string &des)
{ {
QByteArray data = QCryptographicHash::hash(QByteArray(src.data()), QCryptographicHash::Md5); QByteArray data = QCryptographicHash::hash(QByteArray(src.data()), QCryptographicHash::Md5);
des = std::string(data.toHex().toUpper().data()); des = std::string(data.toHex().toUpper().data());
} }
static QString FileMd5(const QString &filepath)
{
QFile file(filepath);
qint64 filesize = file.size();
const qint64 size = 4096;
if(file.open(QFile::ReadOnly))
{
char buffer[4096];
int byteRead;
qint64 readsize = qMin(filesize, size);
QCryptographicHash hash(QCryptographicHash::Md5);
while(readsize > 0 && (byteRead = file.read(buffer, readsize)) > 0)
{
filesize -= byteRead;
hash.addData(buffer, byteRead);
readsize = qMin(filesize, size);
}
file.close();
return QString(hash.result().toHex());
}
return QString();
}
}; };
#endif // TOOL_H #endif // TOOL_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment