DeviceConfig.cpp
Go to the documentation of this file.
1#include "DeviceConfig.h"
2
4{
5 DeviceConfig::DeviceConfig(std::string type, std::string name) : DeviceConfigBase(type, name)
6 {
7 }
8
9 DeviceConfig::DeviceConfig(std::string type,
10 std::string name,
11 std::shared_ptr<DeviceConfigBase> profile) :
12 DeviceConfigBase(type, name, profile)
13 {
14 }
15
16 DeviceConfigBase::DeviceConfigBase(std::string type, std::string name) : type(type), name(name)
17 {
18 }
19
21 std::string name,
22 std::shared_ptr<DeviceConfigBase> profile) :
23 ControllerConfig(*profile), type(type), name(name)
24 {
25 this->items = profile->items;
26 }
27
29 DeviceConfig::getSlaveConfig(const std::uint32_t vendorID,
30 const std::uint32_t productCode,
31 const std::uint32_t serialNumber)
32 {
33 for (auto& slaveConfig : slaveConfigs)
34 {
35 auto id = slaveConfig->getIdentifier();
36 if (id.getVendorID() == vendorID && id.getProductCode() == productCode &&
37 id.getSerialNumber() == serialNumber)
38 {
39 return *slaveConfig;
40 }
41 }
42 std::stringstream ss;
43 ss << "Slave Config with vendorID: " << vendorID << " , productCode: " << productCode
44 << " , serialNumber: " << serialNumber << " not found";
45 throw ConfigElementNotFoundError(ss.str());
46 }
47
49 DeviceConfig::getSlaveConfig(const std::string type, const std::string name)
50 {
51 for (auto& slaveConfig : slaveConfigs)
52 {
53 if (slaveConfig->getType() == type)
54 {
55 if (name == "" || slaveConfig->getName() == name)
56 {
57 return *slaveConfig;
58 }
59 }
60 }
61 throw ConfigElementNotFoundError("Config Element not found: Type: " + type +
62 ", name: " + name);
63 }
64
67 {
68 if (slaveConfigs.size() == 1)
69 {
70 return **slaveConfigs.begin();
71 }
73 "There are more than one SlaveConfigs in this DeviceConfig");
74 }
75
77 DeviceConfig::getSubdeviceConfig(const std::string& subDeviceName)
78 {
79 auto it = subDeviceConfigs.find(subDeviceName);
80 if (it != subDeviceConfigs.end())
81 {
82 return *it->second;
83 }
84 throw ConfigElementNotFoundError("SubDevice with name: " + subDeviceName);
85 }
86
87 std::list<std::reference_wrapper<DeviceConfigBase>>
88 DeviceConfig::getSubDeviceConfigsWithType(const std::string subDeviceType)
89 {
90 std::list<std::reference_wrapper<DeviceConfigBase>> list;
91 for (auto& subDevice : subDeviceConfigs)
92 {
93 if (subDevice.second->getType() == subDeviceType)
94 {
95 list.push_back(*subDevice.second);
96 }
97 }
98 return list;
99 }
100
101 std::string
103 {
104 return type;
105 }
106
107 std::string
109 {
110 return name;
111 }
112
113 bool
114 DeviceConfig::checkAllItemsRead(std::vector<std::string>& errors) const
115 {
116 bool succ = true;
117 for (auto& slave : slaveConfigs)
118 {
119 succ &= slave->checkAllItemsRead(errors);
120 }
121 for (auto& controller : controllerConfigs)
122 {
123 succ &= controller.second->checkAllItemsRead(errors);
124 }
125 for (auto& sub : subDeviceConfigs)
126 {
127 succ &= sub.second->checkAllItemsRead(errors);
128 }
129 succ &= Config::checkAllItemsRead(errors);
130 return succ;
131 }
132
133 void
134 DeviceConfig::print(std::stringstream& s, int indention) const
135 {
136 for (int i = 0; i < indention; i++)
137 {
138 s << "\t";
139 }
140 s << "Device type=" << getType() << " name=" << getName() << ":" << std::endl;
141
142 for (auto& slave : slaveConfigs)
143 {
144 slave->print(s, indention + 1);
145 }
146
147 for (auto& controller : controllerConfigs)
148 {
149 for (int i = 0; i < indention + 1; i++)
150 {
151 s << "\t";
152 }
153 s << "Controller: " << controller.first << ":" << std::endl;
154 controller.second->print(s, indention + 2);
155 }
156
157 for (auto& sub : subDeviceConfigs)
158 {
159 sub.second->print(s, indention + 1);
160 }
161
162 for (int i = 0; i < indention + 1; i++)
163 {
164 s << "\t";
165 }
166 s << "Config:" << std::endl;
167 Config::print(s, indention + 2);
168 }
169
170 void
171 DeviceConfig::addSlaveConfig(std::shared_ptr<SlaveConfig> slaveConfig)
172 {
173 slaveConfigs.push_back(slaveConfig);
174 }
175
176 void
177 DeviceConfig::addSubDeviceConfig(const std::string name,
178 std::shared_ptr<DeviceConfigBase> subDeviceConfig)
179 {
180 subDeviceConfigs[name] = subDeviceConfig;
181 }
182
183 void
185 {
187 for (auto& i : subDeviceConfigs)
188 {
189 i.second->onParsingFinished();
190 }
191 for (auto& i : slaveConfigs)
192 {
193 i->onParsingFinished();
194 }
195 for (auto& i : controllerConfigs)
196 {
197 i.second->onParsingFinished();
198 }
199 }
200
201 void
202 DeviceConfigBase::print(std::stringstream& s, int indention) const
203 {
204 for (int i = 0; i < indention; i++)
205 {
206 s << "\t";
207 }
208 s << "SubDevice name=" << getName() << ":" << std::endl;
209
210 for (auto& controller : controllerConfigs)
211 {
212 for (int i = 0; i < indention + 1; i++)
213 {
214 s << "\t";
215 }
216 s << "Controller: " << controller.first << ":" << std::endl;
217 controller.second->print(s, indention + 2);
218 }
219
220 for (int i = 0; i < indention + 1; i++)
221 {
222 s << "\t";
223 }
224 s << "Config:" << std::endl;
225 Config::print(s, indention + 2);
226 }
227
228} // namespace armarx::control::hardware_config
The ConfigElementNotFoundError class represents an error that is thrown when trying to get a config v...
Definition Errors.h:25
virtual void print(std::stringstream &s, int indention) const
Print this configuration.
Definition Config.cpp:121
virtual void onParsingFinished()
This method is called when the config is completely read form the HardwareConfig file.
Definition Config.cpp:315
virtual bool checkAllItemsRead(std::vector< std::string > &errors) const
This method is called once the device has read the configuration data it needs.
Definition Config.cpp:76
std::map< std::string, ConfigItemWithMetadata > items
Definition Config.h:252
std::map< std::string, std::shared_ptr< Config > > controllerConfigs
Definition Config.h:376
A base class that does not have Subdevices or Slaves but can be used for Subdevices or DeviceProfiles...
void print(std::stringstream &s, int indention) const override
Print this configuration.
DeviceConfigBase(std::string type, std::string name)
std::list< std::reference_wrapper< DeviceConfigBase > > getSubDeviceConfigsWithType(const std::string subDeviceType)
bool checkAllItemsRead(std::vector< std::string > &errors) const override
This method is called once the device has read the configuration data it needs.
void onParsingFinished() override
This method is called when the config is completely read form the HardwareConfig file.
void print(std::stringstream &s, int indention) const override
Print this configuration.
DeviceConfigBase & getSubdeviceConfig(const std::string &subDeviceName)
SlaveConfig & getSlaveConfig(const std::uint32_t vendorID, const std::uint32_t productCode, const std::uint32_t serialNumber)
Point sub(const Point &x, const Point &y)
Definition point.hpp:46