#include "stdafx.h"
|
#include <sstream>
|
#include <fstream>
|
#include <iostream>
|
|
// µ¥Àý»ñÈ¡
|
RecipeManager& RecipeManager::getInstance() {
|
static RecipeManager instance;
|
return instance;
|
}
|
|
// ¹¹Ô캯Êý
|
RecipeManager::RecipeManager() : m_recipeFolder("Recipe") {}
|
|
// ÉèÖÃÅä·½Îļþ¼Ð
|
void RecipeManager::setRecipeFolder(const std::string& folderPath) {
|
m_recipeFolder = folderPath;
|
}
|
|
// ¼ÓÔØÅä·½£¨Èç¹ûÎļþ²»´æÔÚ£¬¼ÓÔØÄ¬ÈÏÊý¾Ý£©
|
bool RecipeManager::loadRecipe(const std::string& recipeName) {
|
std::string filePath = m_recipeFolder + "/" + recipeName + ".xml";
|
pugi::xml_document doc;
|
|
if (!doc.load_file(filePath.c_str())) {
|
std::cerr << "Recipe file not found: " << filePath << ". Loading default recipe." << std::endl;
|
generateDefaultRecipe();
|
return false; // Îļþ²»´æÔÚ£¬µ«¼ÓÔØÁËĬÈÏÊý¾Ý
|
}
|
|
m_axes.clear();
|
|
auto recipe = doc.child("Recipe");
|
for (auto axisNode : recipe.child("Axes").children("Axis")) {
|
AxisInfo axisInfo;
|
axisInfo.id = axisNode.attribute("id").as_int();
|
axisInfo.number = axisNode.attribute("number").value();
|
axisInfo.description = axisNode.attribute("description").value();
|
axisInfo.startAddress = axisNode.attribute("start_address").value();
|
axisInfo.jogDistance = axisNode.attribute("jog_distance").as_double();
|
axisInfo.manualSpeed = axisNode.attribute("manual_speed").as_double();
|
axisInfo.autoSpeed = axisNode.attribute("auto_speed").as_double();
|
axisInfo.accelerationTime = axisNode.attribute("acceleration_time").as_double();
|
axisInfo.decelerationTime = axisNode.attribute("deceleration_time").as_double();
|
|
for (auto positionNode : axisNode.child("Positions").children("Position")) {
|
std::string description = positionNode.attribute("description").value();
|
double positionValue = positionNode.attribute("value").as_double();
|
axisInfo.positions.emplace_back(description, positionValue);
|
}
|
|
m_axes[axisInfo.id] = axisInfo;
|
}
|
|
return true;
|
}
|
|
// ±£´æÅä·½
|
bool RecipeManager::saveRecipe(const std::string& recipeName) {
|
// Éú³ÉÎļþ·¾¶
|
std::string filePath = m_recipeFolder + "/" + recipeName + ".xml";
|
|
// ´´½¨ XML Îĵµ¶ÔÏó
|
pugi::xml_document doc;
|
|
// Èç¹ûÖáÊý¾ÝΪ¿Õ£¬Éú³ÉĬÈÏÅä·½
|
if (m_axes.empty()) {
|
generateDefaultRecipe();
|
}
|
|
// Ìí¼ÓÅä·½¸ù½Úµã
|
auto recipe = doc.append_child("Recipe");
|
|
// Ìí¼ÓÖáÁбí½Úµã
|
auto axesNode = recipe.append_child("Axes");
|
|
// ±éÀúËùÓÐÖáÊý¾Ý²¢Ð´Èë XML
|
for (const auto& axisEntry : m_axes) {
|
const AxisInfo& axisInfo = axisEntry.second;
|
|
auto axisNode = axesNode.append_child("Axis");
|
axisNode.append_attribute("id") = axisInfo.id;
|
axisNode.append_attribute("number") = axisInfo.number.c_str();
|
axisNode.append_attribute("description") = axisInfo.description.c_str();
|
axisNode.append_attribute("start_address") = axisInfo.startAddress.c_str();
|
axisNode.append_attribute("jog_distance") = axisInfo.jogDistance;
|
axisNode.append_attribute("manual_speed") = axisInfo.manualSpeed;
|
axisNode.append_attribute("auto_speed") = axisInfo.autoSpeed;
|
axisNode.append_attribute("acceleration_time") = axisInfo.accelerationTime;
|
axisNode.append_attribute("deceleration_time") = axisInfo.decelerationTime;
|
|
// Ìí¼Ó¶¨Î»µãÁбí
|
auto positionsNode = axisNode.append_child("Positions");
|
for (const auto& position : axisInfo.positions) {
|
auto positionNode = positionsNode.append_child("Position");
|
positionNode.append_attribute("description") = position.first.c_str();
|
positionNode.append_attribute("value") = position.second;
|
}
|
}
|
|
// ±£´æ XML Îļþ
|
return doc.save_file(filePath.c_str());
|
}
|
|
// Éú³ÉĬÈÏÅä·½
|
void RecipeManager::generateDefaultRecipe() {
|
m_axes.clear();
|
|
for (int axisId = 1; axisId <= 12; ++axisId) {
|
AxisInfo axisInfo;
|
axisInfo.id = axisId;
|
axisInfo.number = "M100-M" + std::to_string(axisId);
|
axisInfo.description = "Default_Axis" + std::to_string(axisId);
|
axisInfo.startAddress = "D" + std::to_string(100 + axisId * 10);
|
axisInfo.jogDistance = 0.5;
|
axisInfo.manualSpeed = 10.0;
|
axisInfo.autoSpeed = 15.0;
|
axisInfo.accelerationTime = 0.2;
|
axisInfo.decelerationTime = 0.3;
|
|
for (int posId = 1; posId <= 25; ++posId) {
|
axisInfo.positions.emplace_back("Position " + std::to_string(posId), posId * 10.0);
|
}
|
|
m_axes[axisId] = axisInfo;
|
}
|
}
|
|
// »ñÈ¡ËùÓÐÖáÐÅÏ¢
|
const std::map<int, AxisInfo>& RecipeManager::getAxes() const {
|
return m_axes;
|
}
|
|
// »ñÈ¡µ¥¸öÖáÐÅÏ¢
|
AxisInfo RecipeManager::getAxis(int axisId) const {
|
auto it = m_axes.find(axisId);
|
if (it != m_axes.end()) {
|
return it->second;
|
}
|
|
// ·µ»ØÒ»¸öÎÞЧµÄ AxisInfo
|
return AxisInfo{ -1, "", "", "", 0.0, 0.0, 0.0, 0.0, 0.0, {}};
|
}
|
|
// ¸üÐÂÖáÐÅÏ¢
|
bool RecipeManager::updateAxis(const AxisInfo& axisInfo) {
|
if (m_axes.find(axisInfo.id) == m_axes.end()) {
|
return false; // Öá²»´æÔÚ
|
}
|
m_axes[axisInfo.id] = axisInfo;
|
return true;
|
}
|
|
// Ìí¼ÓеÄÖáÐÅÏ¢
|
bool RecipeManager::addAxis(const AxisInfo& axisInfo) {
|
if (m_axes.find(axisInfo.id) != m_axes.end()) {
|
return false; // ÖáÒÑ´æÔÚ
|
}
|
m_axes[axisInfo.id] = axisInfo;
|
return true;
|
}
|
|
// ɾ³ýÖáÐÅÏ¢
|
bool RecipeManager::deleteAxis(int axisId) {
|
return m_axes.erase(axisId) > 0;
|
}
|
|
// »ñÈ¡ËùÓÐÖá±àºÅ
|
std::vector<int> RecipeManager::getAllAxisID() const {
|
std::vector<int> axisNumbers;
|
for (const auto& axis : m_axes) {
|
int axisId = axis.first;
|
axisNumbers.push_back(axisId);
|
}
|
|
return axisNumbers;
|
}
|
|
// »ñȡָ¶¨Ò³µÄ¶¨Î»µã
|
std::vector<std::pair<std::string, double>> RecipeManager::getPositions(int axisId, int pageNumber, int pageSize) const {
|
std::vector<std::pair<std::string, double>> result;
|
|
// ¼ì²éÖáÊÇ·ñ´æÔÚ
|
auto it = m_axes.find(axisId);
|
if (it == m_axes.end()) {
|
return result; // Èç¹ûÖá ID ²»´æÔÚ£¬·µ»Ø¿Õ½á¹û
|
}
|
|
// »ñȡָ¶¨ÖáµÄËùÓж¨Î»µã
|
const auto& positions = it->second.positions;
|
|
// È·¶¨·ÖÒ³·¶Î§
|
int startIndex = (pageNumber - 1) * pageSize;
|
int endIndex = startIndex + pageSize;
|
|
// ±éÀú¶¨Î»µã£¬°´·ÖÒ³ÌáÈ¡Êý¾Ý
|
int index = 0;
|
for (const auto& pos : positions) {
|
const std::string& description = pos.first; // ¼ü£ºÃèÊö
|
double value = pos.second; // Öµ£ºÎ»ÖÃÖµ
|
|
if (index >= startIndex && index < endIndex) {
|
result.emplace_back(description, value);
|
}
|
|
++index;
|
if (index >= endIndex) {
|
break; // ´ïµ½·ÖÒ³½áÊøµã
|
}
|
}
|
|
return result;
|
}
|