#include "stdafx.h" #include #include using namespace std::placeholders; #pragma comment(lib, "version.lib") //#include "ResDataObject.h" #include "DIOS.Dev.DAP.VACUE130DAP.h" #include "Helper.JSON.hpp" using namespace DIOS::Dev::Detail::DAP; namespace nsDAP = DIOS::Dev::Detail::DAP; #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”,可能丢失数据 static const int msTimeOut_Lock = 500; #ifdef _WIN64 #ifdef _DEBUG static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64D.dll"; #else static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64.dll"; #endif #endif #ifdef _WIN64 #ifdef _DEBUG static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64D.dll"; #else static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64.dll"; #endif #endif Log4CPP::Logger* mLog::gLogger = nullptr; //----------------------------------------------------------------------------- // VACUE130DAP //----------------------------------------------------------------------------- nsDAP::VACUE130DAP::VACUE130DAP (std::shared_ptr center, nsSCF::SCF SCF, string configfile) : super (center, SCF) { assert (EventCenter); ResDataObject temp; temp.loadFile(configfile.c_str()); m_DapConfig = temp["CONFIGURATION"]; TransJsonText(m_DapConfig); string version; if (GetVersion(version, hMyModule)) mLog::FINFO("\n===============log begin : module version:{$} ===================\n", version.c_str()); else mLog::FINFO("\n===============log begin : version:0.0.0.0 ===================\n"); //获取delta动态库版本号 string strPath; strPath = GetDynamicLibraryPath() + "\\DIOS.Dev.DAP.VACUE130DAP64.dll"; string strVersion; strVersion = GetFileVersion(strPath); mLog::FINFO("=====================log begin :delta dll version{$}===strPath {$}====================", strVersion, strPath); m_MSGUnit.reset(new nsDetail::MSGUnit(center, nsDAP::DAPUnitType)); GetConfData(); Register(); } nsDAP::VACUE130DAP::~VACUE130DAP () { } std::string nsDAP::VACUE130DAP::GetGUID() const { printf("\n===============GetGUID : %s ===================\n", DAPUnitType); return DeviceDriverType; } void nsDAP::VACUE130DAP::Register() { auto Disp = &Dispatch; m_DAPUnit.m_DAP.reset(new DOSEMould(0.0, 1.0, 5000.0, 0.1)); m_DAPUnit.m_ConnectionStatus.reset(new ConnectionStatusMould(0, 0, 1, 1)); superGen::Register (Disp); Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; }); } RET_STATUS nsDAP::VACUE130DAP::HWSend (char* strCommand, int nTimeOut) { if (!m_SCF) return RET_STATUS::RET_FAILED; unsigned char strSendCommand[100] = { 0 }; int len = strlen(strCommand); memcpy(strSendCommand, strCommand, len); strSendCommand[len] = 0x0D; strSendCommand[len + 1] = 0x0A; mLog::FINFO("==OUT==: %s \n", strSendCommand); int retLength; m_SCF.Lock(msTimeOut_Lock) .SendPacket((char*)strSendCommand, strlen((char*)strSendCommand), nTimeOut, retLength); Sleep(nTimeOut); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDAP::VACUE130DAP::ClearDAP() { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsDAP::VACUE130DAP::QueryDAP(float& value) { HWSend("M1"); return RET_STATUS::RET_SUCCEED; } //----------------------------------------------------------------------------- // ProcessCmd //----------------------------------------------------------------------------- void nsDAP::VACUE130DAP::FireNotify (std::string key, std::string content) { EventCenter->OnNotify (1, key, content); } // 收到硬件的通知, 有包到达 // 按照设计, 仅当数据有变化时, 才发通知到上层, 因此里面有大量的 if (..) FireNotify (..) std::string extractBetweenCommas(std::string& str) { size_t start = str.find(','); if (start == std::string::npos) return ""; // 没有逗号 size_t end = str.find(',', start + 1); if (end == std::string::npos) return ""; // 只有一个逗号 return str.substr(start + 1, end - start - 1); } void nsDAP::VACUE130DAP::OnCallBack (const char* strPackage, DWORD Length) { unsigned char strSendCommand[100] = { 0 }; int len = strlen(strPackage); memcpy(strSendCommand, strPackage, len); mLog::FINFO("==OUT==: {$} \n", strSendCommand); if (Length < 18) { return; } if (m_DAPUnit.m_ConnectionStatus->Update(true)) FireNotify(m_DAPUnit.m_ConnectionStatus->GetKey(), m_DAPUnit.m_ConnectionStatus->JSGet()); float dapvalue = 0.0; string temp = strPackage; if (strPackage[1] == 'M') { dapvalue = atof(extractBetweenCommas(temp).c_str()); m_DAPUnit.m_DAP->Update(dapvalue); FireNotify(m_DAPUnit.m_DAP->GetKey(), m_DAPUnit.m_DAP->JSGet()); } } void DIOS::Dev::Detail::DAP::VACUE130DAP::SubscribeSelf(dios_mqtt_connection* conn) { mLog::FINFO("SubScribeSelf"); //子系统当前状态 SubscribeTopic(conn, "DIOS/DEVICE/Subsystem/Notify/CurrentSMState"); //发生器属性 SubscribeTopic(conn, "DIOS/DEVICE/Generator/Notify/POSTKV"); SubscribeTopic(conn, "DIOS/DEVICE/Generator/Notify/POSTMA"); SubscribeTopic(conn, "DIOS/DEVICE/Generator/Notify/POSTMS"); SubscribeTopic(conn, "DIOS/DEVICE/Generator/Notify/POSTMAS"); //遮光器属性 SubscribeTopic(conn, "DIOS/DEVICE/Collimator/Notify/SID"); SubscribeTopic(conn, "DIOS/DEVICE/Collimator/Notify/XSIZE"); SubscribeTopic(conn, "DIOS/DEVICE/Collimator/Notify/YSIZE"); } //----------------------------------------------------------------------------- // DAPDriver //----------------------------------------------------------------------------- std::string nsDAP::VACUE130DAP::GetDynamicLibraryPath() { HMODULE hModule = GetModuleHandle("DIOS.Dev.DAP.VACUE130DAP64.dll"); char path[MAX_PATH]; GetModuleFileName(hModule, path, MAX_PATH); std::string fullPath(path); size_t pos = fullPath.find_last_of("\\/"); mLog::FINFO("Delta dll fullPath {$}", fullPath); return fullPath.substr(0, pos); } std::string nsDAP::VACUE130DAP::GetFileVersion(std::string strFilePathName) { DWORD dwVerSize = GetFileVersionInfoSize(strFilePathName.c_str(), NULL); if (dwVerSize == 0) { return false; } LPVOID pVersionBuffer = malloc(dwVerSize); GetFileVersionInfo(strFilePathName.c_str(), 0, dwVerSize, pVersionBuffer); VS_FIXEDFILEINFO* pInfo; UINT nInfoLen; std::string strVersion = ""; if (VerQueryValue(pVersionBuffer, "\\", (void**)&pInfo, &nInfoLen)) { strVersion = to_string(HIWORD(pInfo->dwFileVersionMS)) + "." + to_string(LOWORD(pInfo->dwFileVersionMS)) + "." + to_string(HIWORD(pInfo->dwFileVersionLS)) + "." + to_string(LOWORD(pInfo->dwFileVersionLS)); } return strVersion; } void nsDAP::VACUE130DAP::GetConfData() { ResDataObject Connection = m_DapConfig["connections"][0]; m_strCommunicateType = (string)Connection["type"]; mLog::FINFO("CommunicateType {$}" m_strCommunicateType); } void nsDAP::VACUE130DAP::ResetDap() { HWSend("R1"); } void DIOS::Dev::Detail::DAP::VACUE130DAP::TestDAP() { HWSend("t"); } void DIOS::Dev::Detail::DAP::VACUE130DAP::QueryStatus() { HWSend("z"); } void DIOS::Dev::Detail::DAP::VACUE130DAP::SetProtocol() { HWSend("c=2"); } void DIOS::Dev::Detail::DAP::VACUE130DAP::GetMeanValue() { HWSend("f"); } nsDAP::DAPDriver::DAPDriver () { } nsDAP::DAPDriver::~DAPDriver () { } auto nsDAP::DAPDriver::CreateDevice (int index) -> std::unique_ptr { if (!m_SCF.isConnected()) return nullptr; m_pDevice = new VACUE130DAP(EventCenter, m_SCF, m_ConfigFileName); auto dev = std::unique_ptr (new IODevice(m_pDevice)); return dev; } void nsDAP::DAPDriver::FireNotify (int code, std::string key, std::string content) { EventCenter->OnNotify (code, key, content); } void nsDAP::DAPDriver::Prepare () { string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\Generator\Conf\Log4CPP.Config.DAP.xml)"; Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "DAP.VACUE130"); auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str()); mLog::gLogger = Log4CPP::LogManager::GetLogger("DAP.VACUE130"); m_SCFDllName = GetConnectDLL(m_ConfigFileName); super::Prepare(); } bool DATA_ACTION nsDAP::DAPDriver::Connect () { ResDataObject Connection = GetConnectParam(m_ConfigFileName); mLog::FINFO("connections:{$}", Connection.encode()); auto erCode = m_SCF.Connect(Connection.encode(), &nsDAP::DAPDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000); if (erCode != SCF_ERR::SCF_SUCCEED) return erCode; auto rc = super::Connect(); if (!rc) return 0; return SCF_ERR::SCF_SUCCEED; } void nsDAP::DAPDriver::Disconnect() { super::Disconnect(); m_SCF.Disconnect(); } bool nsDAP::DAPDriver::isConnected() const { return super::isConnected(); } std::string nsDAP::DAPDriver::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", "DAP"); HardwareInfo.add ("MinorID", "Dr"); HardwareInfo.add ("VendorID", "PTW"); HardwareInfo.add ("ProductID", "HF"); HardwareInfo.add ("SerialID", "Drv"); } string ret = HardwareInfo.encode (); return ret; } std::string nsDAP::DAPDriver::GetResource () { ResDataObject temp; if (!temp.loadFile(m_ConfigFileName.c_str())) return std::string(); auto r_config = temp["CONFIGURATION"]; for (auto& Item : m_ConfigInfo) { string key = Item.GetKey(); if (key == ConfKey::DiosType) { Item.SetCurrentValue(((string)r_config["VendorID"]).c_str()); } else if (key == ConfKey::DiosModel) { Item.SetCurrentValue(((string)r_config["ProductID"]).c_str()); } else if (key == ConfKey::DiosSCFType) { Item.SetCurrentValue(((string)r_config["connections"][0]["type"]).c_str()); } else if (key == ConfKey::DiosSCFPort || key == ConfKey::DiosSCFBaudrate || key == ConfKey::DiosSCFBytesize || key == ConfKey::DiosSCFParity || key == ConfKey::DiosSCFStopbits || key == ConfKey::DiosSCFIP) { if (r_config["connections"][0].GetFirstOf(key.c_str()) >= 0) { Item.SetCurrentValue(((string)r_config["connections"][0][key.c_str()]).c_str()); } } } ResDataObject resAttr, resDescription; for (auto Item : m_ConfigInfo) { resAttr.add(Item.GetKey(), Item.GetCurrentValue()); resDescription.add(Item.GetKey(), Item.GetDescription()); } ResDataObject resDeviceResource; resDeviceResource.add(ConfKey::DiosAttribute, resAttr); resDeviceResource.add(ConfKey::DiosDescription, resDescription); string res = resDeviceResource.encode(); printf("resDeviceResource :%s \n", resDeviceResource.encode()); return res; } std::string nsDAP::DAPDriver::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", "DAP"); HardwareInfo.add ("MinorID", "Dr"); HardwareInfo.add ("VendorID", "PTW"); HardwareInfo.add ("ProductID", "HF"); HardwareInfo.add ("SerialID", "1234"); } string ret = HardwareInfo.encode (); return ret; } void nsDAP::DAPDriver::Dequeue (const char * Packet, DWORD Length) { m_pDevice->OnCallBack(Packet, Length); } PACKET_RET nsDAP::DAPDriver::callbackPackageProcess (const char * RecData, DWORD nLength, DWORD& PacketLength) { mLog::FINFO("nLength < 1, nLength=={$} PacketLength {$}", nLength, PacketLength); if (nLength < 1) { return PACKET_USELESS; } if (RecData[0] == 0x0D && RecData[nLength - 1] == 0x0A) { PacketLength = nLength + 2; char strtemp[100] = { 0 }; memcpy(strtemp, RecData, nLength); strtemp[PacketLength + 1] = 0; mLog::FINFO("==IN ==:{$}", strtemp); return PACKET_ISPACKET; } return PACKET_NOPACKET; } //----------------------------------------------------------------------------- // GetIODriver & CreateIODriver //----------------------------------------------------------------------------- static nsDAP::DAPDriver gIODriver; extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver () // 返回静态对象的引用, 调用者不能删除 ! { return &gIODriver; } extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver () // 返回新对象, 调用者必须自行删除此对象 ! { return new nsDAP::DAPDriver (); } #if 0 #endif RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenCurrentSMState(std::string value) { return RET_STATUS(); } RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenPostKV(const std::string value) { m_fPostKV = atof(value.c_str()); mLog::FINFO("{$}PostKV", m_fPostKV); return RET_STATUS(); } RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenPostMAS(const std::string value) { m_fPostMAS = atof(value.c_str()); mLog::FINFO("{$}PostMAS", m_fPostMAS); return RET_STATUS(); } RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenPostMA(const std::string value) { m_fPostMA = atof(value.c_str()); mLog::FINFO("{$}PostMA", m_fPostMA); return RET_STATUS(); } RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetGenPostMS(const std::string value) { m_fPostMS = atof(value.c_str()); mLog::FINFO("{$}PostMS", m_fPostMS); return RET_STATUS(); } RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetCollSID(const std::string value) { m_fSID = atof(value.c_str()); mLog::FINFO("SID{$}", m_fSID); return RET_STATUS(); } RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetCollXSIZE(const std::string value) { m_fXsize = atof(value.c_str()); mLog::FINFO("Xsize{$}", m_fXsize); return RET_STATUS(); } RET_STATUS DIOS::Dev::Detail::DAP::VACUE130DAP::SetCollYSIZE(const std::string value) { m_fYsize = atof(value.c_str()); mLog::FINFO("Ysize{$}", m_fYsize); return RET_STATUS(); }