// DIOS.Dev.GEN.DEMO.cpp : 定义 DLL 应用程序的导出函数。 // #include "stdafx.h" #include "FileVersion.hpp" #include "DIOS.Dev.SyncBox.FullUCB.h" #include "Helper.JSON.hpp" #include "common_api.h" using namespace DIOS::Dev::Detail::SYNBOX; namespace nsSYN = DIOS::Dev::Detail::SYNBOX; #pragma warning (disable:4244) // warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据 #pragma warning (disable:4305) // warning C4305: “参数”: 从“double”到“float”截断 #pragma warning (disable:4267) // warning C4267 : “初始化”: 从“size_t”转换到“int”,可能丢失数据 using namespace DIOS::Dev::Detail::SYNBOX; namespace nsSYN = DIOS::Dev::Detail::SYNBOX; #define COM_Deliver 0 nsSYN::SyncBoxDevice* nsSYN::SyncBoxDevice::m_SingleExample = nullptr; //----------------------------------------------------------------------------- // 串口处理类 //----------------------------------------------------------------------------- Log4CPP::Logger* mLog::gLogger = nullptr; #if COM_Deliver static nsSerialGPM::CDeliverModule m_tDelivermodule; static int m_nCMDType_Directly{ 0 }; static int m_nCMDType_WaitTime{ 0 }; static int m_nCMDType_WaitSelf{ 0 }; void nsSYN::ProcessClientData(const char* pData, unsigned long nDataLength, void* lparam) { SyncBoxDevice* pCurSyn = (SyncBoxDevice*)lparam; pCurSyn->HWSend(pData, nDataLength); } void nsSYN::WriteLog(const char* pData, nsSerialGPM::LOG_V2_LEVEL level) { switch (level) { case nsSerialGPM::LOG_V2_FATAL: case nsSerialGPM::LOG_V2_ERROR: mLog::FERROR(pData); break; case nsSerialGPM::LOG_V2_WARNING: mLog::FWARN(pData); break; case nsSerialGPM::LOG_V2_DEBUG: mLog::FDEBUG(pData); break; case nsSerialGPM::LOG_V2_INFO: mLog::FINFO(pData); break; default: break; } } RET_STATUS nsSYN::HWSendDirectly(char* strCommand, int lengh, int headLengh) { return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_Directly, headLengh); } RET_STATUS nsSYN::HWSendWaittimeCMD(char* strCommand, int lengh, int headLengh) { return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitTime, headLengh); } RET_STATUS nsSYN::HWSendWaitSelfCMD(char* strCommand, int lengh, int headLengh) { return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitSelf, headLengh); } #else RET_STATUS nsSYN::HWSendNormal(char* strCommand, int lengh, int headLengh) { RET_STATUS res = RET_STATUS::RET_SUCCEED; if (SyncBoxDevice::m_SingleExample) { res = SyncBoxDevice::m_SingleExample->HWSend(strCommand, lengh); SetWaitAction(strCommand); } return res; } #endif // COM_Deliver ////----------------------------------------------------------------------------- //// SynBoxDevice ////----------------------------------------------------------------------------- nsSYN::SyncBoxDevice::SyncBoxDevice(std::shared_ptr center, nsSCF::SCF SCF, string configfile) : super(center, SCF) { assert(EventCenter); m_SingleExample = this; //add by wxx:自动获取版本号并更新到日志 string version; if (GetVersion(version, hMyModule)) mLog::FINFO("--Func-- driver prepare : version:{$}\n", version.c_str()); else mLog::FINFO("--Func-- driver prepare, v3.0.0.0 \n"); m_DeviceFullUCB.reset(new DeviceFullUCB(center,configfile)); m_MSGUnit.reset(new nsDetail::MSGUnit(center, SyncConsoleUnitType)); #if COM_Deliver m_tDelivermodule.InitSendModle(this, &ProcessClientData, WriteLog); m_nCMDType_Directly = m_tDelivermodule.SetPriority(false, true); m_nCMDType_WaitTime = m_tDelivermodule.SetPriority(false, false, false, true, 100); m_nCMDType_WaitSelf = m_tDelivermodule.SetPriority(false, false, 0, false, 0, true, 500); m_tDelivermodule.SetACKCMDHead(ackCMD, 2); mLog::FINFO("m_nCMDType_Directly[{$}] m_nCMDType_WaitTime[{$}] m_nCMDType_WaitSelf[{$}]", m_nCMDType_Directly, m_nCMDType_WaitTime, m_nCMDType_WaitSelf); #endif // COM_Deliver Register(); OnCallBack(); } nsSYN::SyncBoxDevice::~SyncBoxDevice() { m_SingleExample = nullptr; mLog::FINFO("\n===============log end ===================\n"); } std::string nsSYN::SyncBoxDevice::GetGUID() const { mLog::FINFO("\n===============GetGUID : {$} ===================\n", SyncConsoleUnitType); return SyncConsoleUnitType; } void nsSYN::SyncBoxDevice::Register() { auto Disp = &Dispatch; m_DeviceFullUCB->Register(Disp); Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; }); auto fun_SetDirectExpSignal = [this](auto in, auto& out) { auto value = JSONTo (in); return m_DeviceFullUCB->SetDirectExpSignal(value); }; Disp->Action.Push("SetDirectExpSignal", fun_SetDirectExpSignal); auto fun_SetPulseWidth = [this](auto in, auto& out) { auto value = JSONTo (in); return m_DeviceFullUCB->SetPulseWidth(value); }; Disp->Action.Push("SetPulseWidth", fun_SetPulseWidth); } void nsSYN::SyncBoxDevice::OnCallBack() { auto HWNotProcess = [](const char* value, int length) -> void { mLog::FERROR("This commands didn't need to process!\n"); }; auto HWSignal = [this](const char* value, int length) { assert(value); //mLog::FDEBUG("HWSignal:[{$}]", value); #if COM_Deliver m_tDelivermodule.CheckReceive(value, 5); #endif // COM_Deliver char cChannelValue; cChannelValue = value[9]; //status : on /off string strChannel(value,7,2); int bStatus = ChartoInt(cChannelValue); if (m_DeviceFullUCB) { m_DeviceFullUCB->DealtheSignal(strChannel, bStatus); } }; auto HWCAN = [this](const char* value, int length) -> void { string sCmdID = ((string)value).substr(8, 3); int nlen = ChartoInt(value[11]) * 256 + ChartoInt(value[12]); }; arFrame.clear(); //通信(握手)测试 arFrame["SEC"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //读取版本号 arFrame["SVR"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //产品序列号写入 arFrame["SSN"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //蜂鸣器提示功能 arFrame["SSP"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //输出端口初始化 arFrame["SIT"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //单片机冷启动 arFrame["SRE"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //出厂恢复(放在软件高级配置工具里) arFrame["SFR"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //EEPROM区域COPY(放在测试工具里) arFrame["SEE"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //工作状态查询命令 arFrame["SRF"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //清除工作状态存储区 arFrame["SCF"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //ABS输出命令 arFrame["SAS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //ADC开启/关闭命令 arFrame["SAD"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //ADC读取命令 arFrame["SRA"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //AEC开启/关闭功能 arFrame["SAE"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //PT_STOP时间读取功能 arFrame["SPT"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //接收CAN数据 arFrame["SCR"] = tFrameMapping(HWCAN, tFrameMapping::OverType_directly, false); //发送CAN数据 arFrame["STL"] = tFrameMapping(HWCAN, tFrameMapping::OverType_directly, false); //CAN通讯设置 arFrame["SCS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //PWM波形属性设置 arFrame["SPS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //PWM波形输出使能 arFrame["SPE"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //曝光模式切换 arFrame["SMS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //通道属性设置 arFrame["SCP"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //通道属性读取确认 arFrame["SCD"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //通道属性版本号写入 arFrame["SPW"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //通道属性版本号读出 arFrame["SPR"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //读取通道状态 arFrame["SRS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); //输入通道自动返回状态 arFrame["SAI"] = tFrameMapping(HWSignal, tFrameMapping::OverType_WaitACK, false); //通道输出设置 arFrame["SWS"] = tFrameMapping(HWSignal, tFrameMapping::OverType_WaitACK, false); //arFrame["CO"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false); } RET_STATUS nsSYN::SyncBoxDevice::HWSend(const char* strCommand, int nCMDLengh, int nTimeOut) { if (true) { string strLog(strCommand); strLog.at(strLog.length()-1) = 0x03; strLog.at(strLog.length() - 2) = 0x04; mLog::FDEBUG("==OUT==: [{$}] \n", strLog.c_str()); } if (!m_SCF) return RET_STATUS::RET_FAILED; int retLength; m_SCF.Lock(msTimeOut_Lock) .SendPacket(strCommand, nCMDLengh, nTimeOut, retLength); //Sleep(nTimeOut); return RET_STATUS::RET_SUCCEED; } //V3新方法 void nsSYN::SyncBoxDevice::SubscribeSelf(dios_mqtt_connection* conn) { mLog::FDEBUG("SubscribeSelf"); //订阅GEN所有Action if (nullptr != conn) { SubscribeTopic(conn, "DIOS/DEVICE/SyncBox/Action/#"); } else { mLog::FERROR("SubscribeSelf conn is empty"); } } //----------------------------------------------------------------------------- // CAN口处理 //----------------------------------------------------------------------------- void nsSYN::SyncBoxDriver::HandshakeTest() { int nTimeout = 100; char cCmd[FullUCB_Com_NormalLen] = "SECA00"; int nCmdSize = 6; FormatCmd(cCmd, nCmdSize); mLog::FINFO("CanShakeHand ==OUT==: [{$}] ", cCmd); int ret = 0; m_SCF.Lock(msTimeOut_Lock) .SendPacket(cCmd, nCmdSize, nTimeout, ret); Sleep(nTimeout); } //----------------------------------------------------------------------------- // SyncBoxDriver //----------------------------------------------------------------------------- nsSYN::SyncBoxDriver::SyncBoxDriver() { m_bConnected = false; m_pAttribute.reset(new ResDataObject()); m_pDescription.reset(new ResDataObject()); } nsSYN::SyncBoxDriver::~SyncBoxDriver() { mLog::Close(); mLog::gLogger = nullptr; } auto nsSYN::SyncBoxDriver::CreateDevice(int index) -> std::unique_ptr { mLog::FINFO("CreateDevice: index ={$}", index); if (!m_SCF.isConnected()) { mLog::FERROR("CreateDevice: but scf no connect"); return nullptr; } #if 0 //测试时不需要强校验握手 if (!m_bConnected) { mLog::FERROR("CreateDevice: not recv Hand Shake"); return nullptr; } #endif // 0 if (index == 0) { m_pDYNDevice = new SyncBoxDevice(EventCenter, m_SCF, m_ConfigFileName); auto dev = std::unique_ptr (new IODevice(m_pDYNDevice)); ResDataObject r_config; return dev; } return nullptr; } void nsSYN::SyncBoxDriver::FireNotify(int code, std::string key, std::string content) { EventCenter->OnNotify(code, key, content); } bool SyncBoxDriver::DriverEntry(std::string CfgFileName) { m_ConfigFileName = CfgFileName; mLog::FINFO("ConfigFileName {$}", m_ConfigFileName); return true; } void nsSYN::SyncBoxDriver::Prepare() { string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\SyncBox\Conf\Log4CPP.Config.SYN.xml)"; mLog::FINFO("strLogPath {$}", strLogPath); Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "SYN.FULLUCB"); auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str()); if (!rc) { printf("Load log configfile failed!\n"); } mLog::gLogger = Log4CPP::LogManager::GetLogger("SYN.FULLUCB"); m_SCFDllName = GetConnectDLL(m_ConfigFileName); super::Prepare(); } bool nsSYN::SyncBoxDriver::Connect() { ResDataObject Connection = GetConnectParam(m_ConfigFileName); mLog::FINFO("connections:{$} \n", Connection.encode()); auto erCode = m_SCF.Connect(Connection.encode(), &nsSYN::SyncBoxDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000); if (erCode != SCF_ERR::SCF_SUCCEED) { mLog::FERROR("scf connect failed"); return false; } mLog::FINFO("Connect: make success"); Sleep(200); auto rc = super::Connect(); if (!rc) return false; HandshakeTest(); Sleep(1000); mLog::FINFO("Connect: scf ok"); return true; } bool nsSYN::SyncBoxDriver::ReConnection(nsSCF::SCF& DevSCF) { super::Disconnect(); mLog::FINFO("ReConnection:SCF Disconnect"); ResDataObject Connection = GetConnectParam(m_ConfigFileName); mLog::FINFO("ReConnection:{$}", Connection.encode()); auto erCode = m_SCF.Connect(Connection.encode(), &nsSYN::SyncBoxDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000); if (erCode == SCF_ERR::SCF_SUCCEED) { Sleep(1000); auto rc = super::Connect(); if (!rc) { mLog::FINFO("ReConnection:super Connect failed"); } else { DevSCF = m_SCF; return true; } } else { mLog::FINFO("ReConnection failed"); } return false; } void nsSYN::SyncBoxDriver::Disconnect() { super::Disconnect(); m_SCF.Disconnect(); m_pDYNDevice = nullptr; m_bConnected = false; mLog::FINFO("Disconnect ok"); } bool nsSYN::SyncBoxDriver::isConnected() const { mLog::FINFO("isConnected"); if (super::isConnected()) { return true; } else { return false; } } void nsSYN::SyncBoxDriver::Dequeue(const char* Packet, DWORD Length) { if (!m_bConnected) { if (strncmp(Packet,":SEC",4) == 0) { m_bConnected = true; mLog::FDEBUG("Dequeue:get Hand Shake"); } } #if 0 if (strncmp(Packet, "EC", 2) == 0) mLog::FDEBUG("Dequeue[EC],arFrameLengh[{$}]", arFrame.size()); #endif DecodeFrame(Packet, Length); } PACKET_RET nsSYN::SyncBoxDriver::callbackPackageProcess(const char* RecData, DWORD nLength, DWORD& PacketLength) { //判断是否是整包 /* 指令编码格式: +命令头(0x3A) +命令对象(S表示同步盒,P表示电脑,其他的的再定) +命令字节(由2个字节组合形成) +数据格式(‘A’表示ASIIC码,‘B’表示二进制码,‘C’表示混合或其他模式) +数据长度(由2个字节组合,数据区可以(0~255)) +数据区(根据数据长度发送的结果或数据) +校验和(由2个字节组成,详见注1) +结束位1(0x0D)+结束位2(0x0A) */ #if 0 char tempData[3]{ 0 }; tempData[0] = RecData[0]; if (nLength >= 2) tempData[1] = RecData[1]; if (strncmp(tempData, "EC", 2) == 0) mLog::FDEBUG("RecData[{$}],lengh[{$}]", tempData, nLength); #endif int bHasHead = 0; if (nLength < 3) { //mLog::FERROR ("nLength[{$}] < 3", nLength); PacketLength = 0; return PACKET_NOPACKET; } else if (nLength >= FullUCB_Com_NormalLen) { mLog::FERROR("nLength[{$}] > [{$}] ", nLength, FullUCB_Com_NormalLen); PacketLength = nLength; return PACKET_USELESS; } for (DWORD i = 0; i < nLength - 1; i++) { //寻找包头 if (RecData[i] == FullUCB_Com_STX) { if (i != 0) //包头之前的数据格式不对,全部扔掉 { PacketLength = i; char strtemp[FullUCB_Com_NormalLen] = { 0 }; memcpy(strtemp, RecData, PacketLength); mLog::FERROR("==IN unknown format data ==:[{$}],UselessDataLength={$},TotalLength={$} \n", strtemp, PacketLength, nLength); return PACKET_USELESS; } else { if (RecData[i] == FullUCB_Com_STX) { bHasHead = 1; } } } //寻找包尾 if (RecData[i] == FullUCB_Com_ETX && RecData[i + 1] == FullUCB_Com_EOT) { PacketLength = i + 2; char strtemp[FullUCB_Com_NormalLen] = { 0 }; memcpy(strtemp, RecData + 1, i - 1); strtemp[PacketLength + 1] = 0; if (1 == bHasHead) { if (strncmp(strtemp, "SWS", 3) == 0 && strtemp[6] == 'O') { mLog::FINFO("==IN==: [{$}] not use", strtemp); #if !COM_Deliver CheckWaitState(RecData); #endif return PACKET_USELESS; } else { mLog::FINFO("==IN==: [{$}]", strtemp); #if !COM_Deliver CheckWaitState(RecData); #endif return PACKET_ISPACKET; } } else { mLog::FERROR("==IN==:cmd[{$}][{$}] useless", PacketLength, RecData); return PACKET_USELESS; } } } if (bHasHead) { PacketLength = 0; } return PACKET_NOPACKET; } //Web config 专用 bool nsSYN::SyncBoxDriver::GetDeviceConfig(std::string& Cfg) { Cfg = m_DeviceConfig.encode(); mLog::FINFO("Cfg {$}", Cfg); return true; } bool nsSYN::SyncBoxDriver::SetDeviceConfig(std::string Cfg) { mLog::FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str()); ResDataObject DeviceConfig; DeviceConfig.decode(Cfg.c_str()); ResDataObject DescriptionTempEx; DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"]; mLog::FDEBUG("Attribute:{$}", DescriptionTempEx.encode()); bool bSaveFile = false; //true:重新保存配置文件 string strAccess = ""; for (int i = 0; i < DescriptionTempEx.size(); i++) { string strKey = DescriptionTempEx.GetKey(i); mLog::FINFO("{$}", strKey.c_str()); try { if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0) { strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"]; if ("RW" == strAccess) { //修改对应配置,在其他单元的配置项要同时调用其修改函数修改真实值 //1. 修改内存中的值,用于给上层发消息 (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i]; //2. 拿到Innerkey int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo"); mLog::FINFO("nConfigInfoCount {$}", nConfigInfoCount); string strTemp = ""; //存储AttributeKey for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; if (strTemp == strKey) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"]; break; } } //3. 修改配置文件中的值 if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i])) { mLog::FDEBUG("SetDeviceConfigValue over"); bSaveFile = true; } } else { mLog::FINFO("{$} is not a RW configuration item", strKey.c_str()); } } else { mLog::FINFO("without this attribute {$}", strKey.c_str()); } } catch (ResDataObjectExption& e) { mLog::FERROR("SetDriverConfig crashed: {$}", e.what()); return false; } } if (bSaveFile) { //3. 重新保存配置文件 SaveConfigFile(true); } return true; } bool nsSYN::SyncBoxDriver::SaveConfigFile(bool bSendNotify) { m_ConfigAll["CONFIGURATION"] = m_Configurations; bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str()); mLog::FINFO("SaveConfigFile over {$}", bRt); return true; } bool nsSYN::SyncBoxDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue) { strValue = ""; string strTemp = pInnerKey; if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取 { int pos = 0; ResDataObject resTemp = config; while ((pos = strTemp.find_first_of(',')) != string::npos) { string Key = strTemp.substr(0, pos); string TempValue = resTemp[Key.c_str()].encode(); resTemp.clear(); resTemp.decode(TempValue.c_str()); strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1); } if (strTemp != "") { strValue = (string)resTemp[strTemp.c_str()]; } else { strValue = (string)resTemp; } } return true; } bool nsSYN::SyncBoxDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue) { string strTemp = pInnerKey; mLog::FDEBUG("Begin to change {$} item value to {$}", pInnerKey, szValue); if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取 { try { int pos = 0; ResDataObject* resTemp = &config; while ((pos = strTemp.find_first_of(',')) != string::npos) { string Key = strTemp.substr(0, pos); resTemp = &(*resTemp)[Key.c_str()]; strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1); } if (strTemp != "") { (*resTemp)[strTemp.c_str()] = szValue; } else { *resTemp = szValue; } } catch (ResDataObjectExption& e) { mLog::FERROR("SetDriverConfigvalue crashed: {$}", e.what()); return false; } } return true; } std::string nsSYN::SyncBoxDriver::GetResource() { ResDataObject r_config, temp; if (!temp.loadFile(m_ConfigFileName.c_str())) { return ""; } m_ConfigAll = temp; r_config = temp["CONFIGURATION"]; m_Configurations = r_config; ResDataObject DescriptionTemp; ResDataObject ListTemp; string strTemp = ""; //用于读取字符串配置信息 string strIndex = ""; //用于读取配置信息中的List项 int nTemp = -1; //用于读取整型配置信息 char sstream[10] = { 0 }; //用于转换值 string strValue = ""; //用于存储配置的值 string strType = ""; //用于存储配置的类型 int/float/string... /*** * 1. 通过循环,将所有配置项写到pDeviceConfig * 2. 记录配置项的内部key以及配置类型,类型对应了不同配置文件路径,用于读写真实值 ***/ try { int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo"); //mLog::FINFO(g_pFPDCtrlLog, "ConfigInfo Count: {$}", nConfigInfoCount); m_pAttribute->clear(); m_pDescription->clear(); for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++) { DescriptionTemp.clear(); ListTemp.clear(); //AttributeType strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"]; DescriptionTemp.add(ConfKey::AttributeType, strTemp.c_str()); //DescriptionTemp= m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]; //mLog::FINFO(g_pFPDCtrlLog, "--> {$}: {$}", AttributeType, strTemp.c_str()); strType = strTemp; //记录配置项的类型 //AttributeKey //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值 strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"]; nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"]; GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue); //2. 赋值 strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; if ("int" == strType) { (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str())); //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atoi(strValue.c_str())); } else if ("float" == strType) { (*m_pAttribute).add(strTemp.c_str(), atof(strValue.c_str())); //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atof(strValue.c_str())); } else //其它先按string类型处理 { (*m_pAttribute).add(strTemp.c_str(), strValue.c_str()); //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), strValue.c_str()); } //AttributeAccess strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"]; DescriptionTemp.add(ConfKey::AttributeAccess, strTemp.c_str()); //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeAccess, strTemp.c_str()); //AttributeRangeMin strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"]; if (strTemp != "") //不需要的配置项为空 { DescriptionTemp.add(ConfKey::AttributeRangeMin, strTemp.c_str()); //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMin, strTemp.c_str()); } //AttributeRangeMax strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"]; if (strTemp != "") //不需要的配置项为空 { DescriptionTemp.add(ConfKey::AttributeRangeMax, strTemp.c_str()); //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMax, strTemp.c_str()); } //AttributeList nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"]; if (nTemp > 0) //ListNum不大于0时说明不需要list配置 { for (int nListIndex = 0; nListIndex < nTemp; nListIndex++) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex]; //sprintf_s(sstream, "{$}", nListIndex); auto temKey = std::to_string(nListIndex); ListTemp.add(temKey.c_str(), strTemp.c_str()); //mLog::FINFO(g_pFPDCtrlLog, "list {$}: {$}", nListIndex, strTemp.c_str()); } DescriptionTemp.add(ConfKey::AttributeList, ListTemp.encode()); } //AttributeRequired strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"]; DescriptionTemp.add(ConfKey::AttributeRequired, strTemp.c_str()); //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRequired, strTemp.c_str()); //AttributeDefaultValue strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"]; if (strTemp != "") //不需要的配置项为空 { DescriptionTemp.add(ConfKey::AttributeDefaultValue, strTemp.c_str()); //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeDefaultValue, strTemp.c_str()); } strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; (*m_pDescription).add(strTemp.c_str(), DescriptionTemp); } } catch (ResDataObjectExption& e) { mLog::FERROR("Get config error: {$}", e.what()); return ""; } ResDataObject resDeviceResource; resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute)); resDeviceResource.add(ConfKey::DiosDescription, (*m_pDescription)); ResDataObject DescriptionTempEx; DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource); m_DeviceConfig.clear(); m_DeviceConfig = DescriptionTempEx; string res = DescriptionTempEx.encode(); mLog::FDEBUG("get resource over {$}", DescriptionTempEx.encode()); return res; } std::string nsSYN::SyncBoxDriver::DeviceProbe() { ResDataObject r_config, HardwareInfo; if (r_config.loadFile(m_ConfigFileName.c_str())) { HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]); HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]); HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]); HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]); HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]); } else { HardwareInfo.add("MajorID", "SYN"); HardwareInfo.add("MinorID", "Dr"); HardwareInfo.add("VendorID", "ECOM"); HardwareInfo.add("ProductID", "FULLUCB"); HardwareInfo.add("SerialID", "1234"); } string ret = HardwareInfo.encode(); return ret; } std::string nsSYN::SyncBoxDriver::DriverProbe() { ResDataObject r_config, HardwareInfo; if (r_config.loadFile(m_ConfigFileName.c_str())) { HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]); HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]); HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]); HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]); HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]); } else { HardwareInfo.add("MajorID", "SYN"); HardwareInfo.add("MinorID", "Dr"); HardwareInfo.add("VendorID", "ECOM"); HardwareInfo.add("ProductID", "FULLUCB"); HardwareInfo.add("SerialID", "1234"); } string ret = HardwareInfo.encode(); return ret; } //----------------------------------------------------------------------------- // GetIODriver & CreateIODriver //----------------------------------------------------------------------------- static nsSYN::SyncBoxDriver gIODriver; extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver () // 返回静态对象的引用, 调用者不能删除 ! { return &gIODriver; } extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver () // 返回新对象, 调用者必须自行删除此对象 ! { return new nsSYN::SyncBoxDriver (); }