LAPTOP-T815PCOQ\25526
2024-11-26 592f34bbed79c54aa7b3b323e93534678c5a1cea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#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(5000 + 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;
}