| | |
| | | |
| | | int CHsmsPassive::loadReports(const char* pszFilepath) |
| | | { |
| | | m_strReportFilepath = pszFilepath; |
| | | m_bReportUtf8 = false; |
| | | m_bReportUtf8Bom = false; |
| | | // 兼容 UTF-8/BOM 与本地编码读取 |
| | | CFile file; |
| | | if (!file.Open(pszFilepath, CFile::modeRead | CFile::shareDenyNone)) { |
| | |
| | | // UTF-8 BOM |
| | | if (nLen >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { |
| | | offset = 3; |
| | | m_bReportUtf8 = true; |
| | | m_bReportUtf8Bom = true; |
| | | } |
| | | |
| | | // UTF-16 LE BOM |
| | |
| | | MultiByteToWideChar(CP_UTF8, 0, buffer.data() + off, |
| | | static_cast<int>(buffer.size() - off), temp.data(), need); |
| | | content = temp.c_str(); |
| | | m_bReportUtf8 = true; |
| | | return true; |
| | | }; |
| | | |
| | |
| | | return m_reports; |
| | | } |
| | | |
| | | unsigned int CHsmsPassive::getMaxReportId() const |
| | | { |
| | | unsigned int maxId = 0; |
| | | for (auto item : m_reports) { |
| | | if (item && item->getReportId() > maxId) { |
| | | maxId = item->getReportId(); |
| | | } |
| | | } |
| | | return maxId; |
| | | } |
| | | |
| | | SERVO::CReport* CHsmsPassive::getReport(int rptid) |
| | | { |
| | | for (auto item : m_reports) { |
| | |
| | | return false; |
| | | } |
| | | |
| | | int CHsmsPassive::deleteReport(int rptid) |
| | | { |
| | | if (!removeReport(rptid)) { |
| | | return -1; |
| | | } |
| | | return writeReportsToFile(m_strReportFilepath); |
| | | } |
| | | |
| | | int CHsmsPassive::addReport(int rptid, const std::vector<unsigned int>& vids) |
| | | { |
| | | if (getReport(rptid) != nullptr) { |
| | | return -1; |
| | | } |
| | | SERVO::CReport* pReport = new SERVO::CReport(rptid, vids); |
| | | for (auto vid : vids) { |
| | | SERVO::CVariable* pVariable = getVariable((int)vid); |
| | | if (pVariable != nullptr) { |
| | | pReport->addVariable(pVariable); |
| | | } |
| | | } |
| | | m_reports.push_back(pReport); |
| | | return writeReportsToFile(m_strReportFilepath); |
| | | } |
| | | |
| | | int CHsmsPassive::updateReport(int rptid, const std::vector<unsigned int>& vids) |
| | | { |
| | | for (auto iter = m_reports.begin(); iter != m_reports.end(); ++iter) { |
| | | if ((*iter)->getReportId() == rptid) { |
| | | delete (*iter); |
| | | SERVO::CReport* pReport = new SERVO::CReport(rptid, vids); |
| | | for (auto vid : vids) { |
| | | SERVO::CVariable* pVariable = getVariable((int)vid); |
| | | if (pVariable != nullptr) { |
| | | pReport->addVariable(pVariable); |
| | | } |
| | | } |
| | | *iter = pReport; |
| | | return writeReportsToFile(m_strReportFilepath); |
| | | } |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | void CHsmsPassive::clearAllReport() |
| | | { |
| | | for (auto item : m_reports) { |
| | |
| | | m_reports.clear(); |
| | | } |
| | | |
| | | int CHsmsPassive::writeReportsToFile(const std::string& filepath) |
| | | { |
| | | if (filepath.empty()) return -1; |
| | | |
| | | CFile file; |
| | | if (!file.Open(filepath.c_str(), CFile::modeCreate | CFile::modeWrite)) { |
| | | return -1; |
| | | } |
| | | |
| | | if (m_bReportUtf8 && m_bReportUtf8Bom) { |
| | | const BYTE bom[3] = { 0xEF, 0xBB, 0xBF }; |
| | | file.Write(bom, 3); |
| | | } |
| | | |
| | | // header |
| | | const std::string headerAnsi = "RPTID,(VID1,VID2,...)\r\n"; |
| | | if (m_bReportUtf8) { |
| | | CStringA header = AnsiToUtf8(headerAnsi); |
| | | file.Write(header.GetString(), header.GetLength()); |
| | | } |
| | | else { |
| | | file.Write(headerAnsi.data(), (UINT)headerAnsi.size()); |
| | | } |
| | | |
| | | for (auto rpt : m_reports) { |
| | | if (rpt == nullptr) continue; |
| | | std::string line; |
| | | line.reserve(64); |
| | | line += std::to_string(rpt->getReportId()); |
| | | line += ",("; |
| | | |
| | | const auto& vids = rpt->getVids(); |
| | | for (size_t i = 0; i < vids.size(); ++i) { |
| | | line += std::to_string(vids[i]); |
| | | if (i + 1 < vids.size()) { |
| | | line.push_back(','); |
| | | } |
| | | } |
| | | line += ")\r\n"; |
| | | |
| | | if (m_bReportUtf8) { |
| | | CStringA out = AnsiToUtf8(line); |
| | | file.Write(out.GetString(), out.GetLength()); |
| | | } |
| | | else { |
| | | file.Write(line.data(), (UINT)line.size()); |
| | | } |
| | | } |
| | | |
| | | file.Close(); |
| | | return 0; |
| | | } |
| | | |
| | | int CHsmsPassive::loadCollectionEvents(const char* pszFilepath) |
| | | { |
| | | m_strCollectionEventFilepath = pszFilepath; |
| | | m_bCollectionUtf8 = false; |
| | | m_bCollectionUtf8Bom = false; |
| | | CFile file; |
| | | if (!file.Open(pszFilepath, CFile::modeRead | CFile::shareDenyNone)) { |
| | | return -1; |
| | |
| | | // UTF-8 BOM |
| | | if (nLen >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { |
| | | offset = 3; |
| | | m_bCollectionUtf8 = true; |
| | | m_bCollectionUtf8Bom = true; |
| | | } |
| | | |
| | | // UTF-16 LE BOM |
| | |
| | | MultiByteToWideChar(CP_UTF8, 0, buffer.data() + off, |
| | | static_cast<int>(buffer.size() - off), temp.data(), need); |
| | | content = temp.c_str(); |
| | | m_bCollectionUtf8 = true; |
| | | return true; |
| | | }; |
| | | |
| | |
| | | return m_collectionEvents; |
| | | } |
| | | |
| | | unsigned int CHsmsPassive::getMaxCollectionEventId() const |
| | | { |
| | | unsigned int maxId = 0; |
| | | for (auto item : m_collectionEvents) { |
| | | if (item && item->getEventId() > maxId) { |
| | | maxId = item->getEventId(); |
| | | } |
| | | } |
| | | return maxId; |
| | | } |
| | | |
| | | int CHsmsPassive::deleteCollectionEvent(unsigned short CEID) |
| | | { |
| | | for (auto iter = m_collectionEvents.begin(); iter != m_collectionEvents.end(); ++iter) { |
| | | if ((*iter)->getEventId() == CEID) { |
| | | delete (*iter); |
| | | m_collectionEvents.erase(iter); |
| | | return writeCollectionEventsToFile(m_strCollectionEventFilepath); |
| | | } |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | int CHsmsPassive::addCollectionEvent(unsigned int CEID, const char* name, const char* desc, const std::vector<unsigned int>& rptids) |
| | | { |
| | | if (getEvent((unsigned short)CEID) != nullptr) { |
| | | return -1; |
| | | } |
| | | auto* pEvent = new SERVO::CCollectionEvent(CEID, name, desc, const_cast<std::vector<unsigned int>&>(rptids)); |
| | | for (auto rptid : rptids) { |
| | | SERVO::CReport* pReport = getReport((int)rptid); |
| | | if (pReport != nullptr) { |
| | | pEvent->addReport(pReport); |
| | | } |
| | | } |
| | | m_collectionEvents.push_back(pEvent); |
| | | return writeCollectionEventsToFile(m_strCollectionEventFilepath); |
| | | } |
| | | |
| | | int CHsmsPassive::updateCollectionEvent(unsigned int CEID, const char* name, const char* desc, const std::vector<unsigned int>& rptids) |
| | | { |
| | | for (auto iter = m_collectionEvents.begin(); iter != m_collectionEvents.end(); ++iter) { |
| | | if ((*iter)->getEventId() == CEID) { |
| | | delete (*iter); |
| | | auto* pEvent = new SERVO::CCollectionEvent(CEID, name, desc, const_cast<std::vector<unsigned int>&>(rptids)); |
| | | for (auto rptid : rptids) { |
| | | SERVO::CReport* pReport = getReport((int)rptid); |
| | | if (pReport != nullptr) { |
| | | pEvent->addReport(pReport); |
| | | } |
| | | } |
| | | *iter = pEvent; |
| | | return writeCollectionEventsToFile(m_strCollectionEventFilepath); |
| | | } |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | void CHsmsPassive::clearAllCollectionEvent() |
| | | { |
| | | for (auto item : m_collectionEvents) { |
| | |
| | | return result; |
| | | } |
| | | |
| | | int CHsmsPassive::writeCollectionEventsToFile(const std::string& filepath) |
| | | { |
| | | if (filepath.empty()) return -1; |
| | | |
| | | CFile file; |
| | | if (!file.Open(filepath.c_str(), CFile::modeCreate | CFile::modeWrite)) { |
| | | return -1; |
| | | } |
| | | |
| | | if (m_bCollectionUtf8 && m_bCollectionUtf8Bom) { |
| | | const BYTE bom[3] = { 0xEF, 0xBB, 0xBF }; |
| | | file.Write(bom, 3); |
| | | } |
| | | |
| | | const std::string headerAnsi = "CEID,CE Name,Descriptions,Attached RPTID\r\n"; |
| | | if (m_bCollectionUtf8) { |
| | | CStringA header = AnsiToUtf8(headerAnsi); |
| | | file.Write(header.GetString(), header.GetLength()); |
| | | } |
| | | else { |
| | | file.Write(headerAnsi.data(), (UINT)headerAnsi.size()); |
| | | } |
| | | |
| | | for (auto ev : m_collectionEvents) { |
| | | if (ev == nullptr) continue; |
| | | std::string line; |
| | | line.reserve(128); |
| | | line += std::to_string(ev->getEventId()); |
| | | line.push_back(','); |
| | | line += ev->getName(); |
| | | line.push_back(','); |
| | | line += ev->getDescription(); |
| | | line.push_back(','); |
| | | line.push_back('('); |
| | | auto rptIds = ev->getReportIds(); |
| | | for (size_t i = 0; i < rptIds.size(); ++i) { |
| | | line += std::to_string(rptIds[i]); |
| | | if (i + 1 < rptIds.size()) { |
| | | line.push_back(','); |
| | | } |
| | | } |
| | | line += ")\r\n"; |
| | | |
| | | if (m_bCollectionUtf8) { |
| | | CStringA out = AnsiToUtf8(line); |
| | | file.Write(out.GetString(), out.GetLength()); |
| | | } |
| | | else { |
| | | file.Write(line.data(), (UINT)line.size()); |
| | | } |
| | | } |
| | | |
| | | file.Close(); |
| | | return 0; |
| | | } |
| | | |
| | | int CHsmsPassive::init(CModel* pModel, const char* pszName, unsigned int port) |
| | | { |
| | | m_pModel = pModel; |