123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include "readmodulefromredis.h"
- #include <QJsonArray>
- #include <QJsonObject>
- #include <QJsonDocument>
- #include <QtCore/QTextCodec>
- #include <fstream>
- readmodulefromredis::readmodulefromredis(RedisAgent* redis)
- {
- m_pRedis = redis;
- }
- bool readmodulefromredis::parse(QString cfg,ModuleInfo& moduleInfo)
- {
- QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
- QTextCodec::setCodecForLocale(utf8);
- QJsonParseError err;
- QJsonDocument jsonDoc(QJsonDocument::fromJson(cfg.toLocal8Bit(),&err));
- QJsonObject jsonObject = jsonDoc.object();
- QStringList keys = jsonObject.keys();
- foreach (QString key, keys)
- {
- if( key.compare("name",Qt::CaseInsensitive) == 0)
- {
- moduleInfo.Name = jsonObject["name"].toString().toLocal8Bit().toStdString();
- }
- else if( key.compare("code",Qt::CaseInsensitive) == 0)
- {
- moduleInfo.Code = jsonObject["code"].toString().toLocal8Bit().toStdString();
- }
- else if( key.compare("type",Qt::CaseInsensitive) == 0)
- {
- QString type = jsonObject["type"].toString();
- moduleInfo.Type = control;
- }
- else if( key.compare("model",Qt::CaseInsensitive) == 0)
- {
- moduleInfo.AssemblyName = jsonObject["model"].toString().toLocal8Bit().toStdString();
- }
- else if( key.compare("serverId",Qt::CaseInsensitive) == 0)
- {
- moduleInfo.serverId = jsonObject["serverId"].toInt();
- }
- else if( key.compare("settings",Qt::CaseInsensitive) == 0)
- {
- QJsonObject jsonSettings = jsonObject["settings"].toObject();
- QStringList lst = jsonSettings.keys();
- foreach (QString item, lst)
- {
- Setting oSetting;
- oSetting.Name = item.toLocal8Bit().toStdString();
- QJsonValue::Type t = jsonSettings[item].type();
- switch( t )
- {
- case QJsonValue::Bool:
- oSetting.qValue = jsonSettings[item].toBool();
- break;
- case QJsonValue::Double:
- oSetting.qValue = jsonSettings[item].toDouble();
- break;
- case QJsonValue::String:
- oSetting.qValue = jsonSettings[item].toString();
- break;
- }
- moduleInfo.vSettings.push_back(oSetting);
- }
- }
- else if( key.compare("inputs",Qt::CaseInsensitive) == 0)
- {
- if( jsonObject["inputs"].isArray() )
- {
- QJsonArray arr = jsonObject["inputs"].toArray();
- for(auto i : arr)
- {
- if( i.type() == QJsonValue::String )
- {
- moduleInfo.lstInputs.push_back(i.toString().toLocal8Bit().toStdString());
- }
- }
- }
- }
- }
- return true;
- }
- std::list<ModuleInfo> readmodulefromredis::loadModules(QString id)
- {
- std::list<ModuleInfo> lstModules;
- if( m_pRedis != nullptr)
- {
- QStringList lst = m_pRedis->hvals("intelligentcontrol");
- foreach (QString str, lst) // 我就不取hkey了,那个就是用编号区分一下
- {
- if( str.isEmpty() )
- continue;
- ModuleInfo moduleInfo;
- if( parse(str,moduleInfo) )
- {
- // 判断一下是否这个服务器管理的实例,如果是的话加入
- if(moduleInfo.serverId == id.toInt())
- lstModules.push_back(moduleInfo);
- }
- }
- }
- return lstModules;
- }
|