libvisiontransfer  10.8.0
deviceparameters.cpp
1 /*******************************************************************************
2  * Copyright (c) 2024 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include "visiontransfer/deviceparameters.h"
16 #include "visiontransfer/internal/parametertransfer.h"
17 #include "visiontransfer/exceptions.h"
18 #include "visiontransfer/common.h"
19 
20 using namespace visiontransfer;
21 using namespace visiontransfer::internal;
22 using namespace visiontransfer::param;
23 
24 namespace visiontransfer {
25 
26 /*************** Pimpl class containing all private members ***********/
27 
28 class DeviceParameters::Pimpl {
29 public:
30  Pimpl(const DeviceInfo& device);
31  Pimpl(const char* address, const char* service);
32 
33  bool isConnected() const;
34 
35  template<typename T>
36  void writeParameter(const char* id, const T& value) {
37  paramTrans.writeParameterTransactionGuarded(id, value);
38  }
39 
40  int readIntParameter(const char* id);
41  double readDoubleParameter(const char* id);
42  bool readBoolParameter(const char* id);
43 
44  void writeIntParameter(const char* id, int value);
45  void writeDoubleParameter(const char* id, double value);
46  void writeBoolParameter(const char* id, bool value);
47  void writeBoolParameterUnguarded(const char* id, bool value);
48 
49  std::map<std::string, ParameterInfo> getAllParameters();
50 
51  bool hasParameter(const std::string& name) const;
52 
53  Parameter getParameter(const std::string& name) const;
54 
55  ParameterSet const& getParameterSet() const;
56 
57  Parameter pollParameter(const std::string& name, bool blockingCall);
58 
59  void setParameterUpdateCallback(std::function<void(const std::string& uid)> callback, bool threaded);
60 
61  void transactionStartQueue();
62  void transactionCommitQueue(int maxWaitMilliseconds);
63 
64  void persistParameters(const std::vector<std::string>& uids, bool blocking=true);
65 
66  void setConnectionStateChangeCallback(std::function<void(visiontransfer::ConnectionState)> callback);
67 
68 private:
69  std::map<std::string, ParameterInfo> serverSideEnumeration;
70 
71 #ifndef DOXYGEN_SHOULD_SKIP_THIS
72  template<typename T>
73  void setNamedParameterInternal(const std::string& name, T value);
74 #endif
75 
76  ParameterTransfer paramTrans;
77 };
78 
79 /******************** Stubs for all public members ********************/
80 
82  pimpl(new Pimpl(device)) {
83  // All initialization in the pimpl class
84 }
85 
86 DeviceParameters::DeviceParameters(const char* address, const char* service):
87  pimpl(new Pimpl(address, service)) {
88  // All initialization in the pimpl class
89 }
90 
91 DeviceParameters::~DeviceParameters() {
92  delete pimpl;
93 }
94 
96  return pimpl->isConnected();
97 }
98 
99 int DeviceParameters::readIntParameter(const char* id) {
100  return pimpl->readIntParameter(id);
101 }
102 
103 double DeviceParameters::readDoubleParameter(const char* id) {
104  return pimpl->readDoubleParameter(id);
105 }
106 
107 bool DeviceParameters::readBoolParameter(const char* id) {
108  return pimpl->readBoolParameter(id);
109 }
110 
111 void DeviceParameters::writeIntParameter(const char* id, int value) {
112  pimpl->writeIntParameter(id, value);
113 }
114 
115 void DeviceParameters::writeDoubleParameter(const char* id, double value) {
116  pimpl->writeDoubleParameter(id, value);
117 }
118 
119 void DeviceParameters::writeBoolParameter(const char* id, bool value) {
120  pimpl->writeBoolParameter(id, value);
121 }
122 
123 void DeviceParameters::writeBoolParameterUnguarded(const char* id, bool value) {
124  pimpl->writeBoolParameterUnguarded(id, value);
125 }
126 
127 std::map<std::string, ParameterInfo> DeviceParameters::getAllParameters()
128 {
129  return pimpl->getAllParameters();
130 }
131 
132 #ifndef DOXYGEN_SHOULD_SKIP_THIS
133 // Deprecated versions
134 template<>
135 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, double value) {
136  pimpl->writeParameter(name.c_str(), value);
137 }
138 template<>
139 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, int value) {
140  pimpl->writeParameter(name.c_str(), value);
141 }
142 template<>
143 void VT_EXPORT DeviceParameters::setNamedParameter(const std::string& name, bool value) {
144  pimpl->writeParameter(name.c_str(), value);
145 }
146 // New versions (extensible for access for other types)
147 template<>
148 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, double value) {
149  pimpl->writeParameter(name.c_str(), value);
150 }
151 template<>
152 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, int value) {
153  pimpl->writeParameter(name.c_str(), value);
154 }
155 template<>
156 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, bool value) {
157  pimpl->writeParameter(name.c_str(), value);
158 }
159 template<>
160 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, const std::string& value) {
161  pimpl->writeParameter(name.c_str(), value);
162 }
163 template<>
164 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, std::string value) {
165  pimpl->writeParameter(name.c_str(), value);
166 }
167 template<>
168 void VT_EXPORT DeviceParameters::setParameter(const std::string& name, std::vector<double> tensorValues) {
169  std::stringstream ss;
170  int i=0;
171  for (const auto& v: tensorValues) {
172  if (i++) ss << " ";
173  ss << v;
174  }
175  pimpl->writeParameter(name.c_str(), ss.str());
176 }
177 
178 template<>
179 int VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
180  return pimpl->readIntParameter(name.c_str());
181 }
182 template<>
183 double VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
184  return pimpl->readDoubleParameter(name.c_str());
185 }
186 template<>
187 bool VT_EXPORT DeviceParameters::getNamedParameter(const std::string& name) {
188  return pimpl->readBoolParameter(name.c_str());
189 }
190 #endif
191 
192 #if VISIONTRANSFER_CPLUSPLUS_VERSION >= 201103L
193 bool DeviceParameters::hasParameter(const std::string& name) const {
194  return pimpl->hasParameter(name);
195 }
196 
197 Parameter DeviceParameters::getParameter(const std::string& name) const {
198  return pimpl->getParameter(name); // copied here
199 }
200 
202  return pimpl->getParameterSet(); // copied here
203 }
204 
205 Parameter DeviceParameters::pollParameter(const std::string& name, bool blockingCall) {
206  return pimpl->pollParameter(name, blockingCall); // copied here
207 }
208 
209 void DeviceParameters::setParameterUpdateCallback(std::function<void(const std::string& uid)> callback, bool threaded) {
210  pimpl->setParameterUpdateCallback(callback, threaded);
211 }
212 
213 DeviceParameters::TransactionLock::TransactionLock(DeviceParameters::Pimpl* pimpl)
214 : pimpl(pimpl) {
215  pimpl->transactionStartQueue();
216 }
217 
218 DeviceParameters::TransactionLock::~TransactionLock() {
219  pimpl->transactionCommitQueue(-1); // non-blocking
220 }
221 
223  pimpl->transactionCommitQueue(waitMaxMilliseconds);
224 }
225 
226 std::unique_ptr<DeviceParameters::TransactionLock> DeviceParameters::transactionLock() {
227  return std::make_unique<DeviceParameters::TransactionLock>(pimpl);
228 }
229 
230 void DeviceParameters::saveParameter(const char* uid, bool blockingCall) {
231  pimpl->persistParameters({uid}, blockingCall);
232 }
233 void DeviceParameters::saveParameter(const std::string& uid, bool blockingCall) {
234  pimpl->persistParameters({uid}, blockingCall);
235 }
237  pimpl->persistParameters({param.getUid()}, blockingCall);
238 }
239 void DeviceParameters::saveParameters(const std::vector<std::string>& uids, bool blockingCall) {
240  pimpl->persistParameters(uids, blockingCall);
241 }
242 void DeviceParameters::saveParameters(const std::set<std::string>& uids, bool blockingCall) {
243  std::vector<std::string> uidvec;
244  for (auto const& u: uids) uidvec.push_back(u);
245  pimpl->persistParameters(uidvec, blockingCall);
246 }
247 void DeviceParameters::saveParameters(std::initializer_list<std::string> uids, bool blockingCall) {
248  std::vector<std::string> uidvec;
249  for (auto const& u: uids) uidvec.push_back(u);
250  pimpl->persistParameters(uidvec, blockingCall);
251 }
252 
253 void DeviceParameters::setConnectionStateChangeCallback(std::function<void(visiontransfer::ConnectionState)> callback) {
254  pimpl->setConnectionStateChangeCallback(callback);
255 }
256 
257 #endif
258 
259 /******************** Implementation in pimpl class *******************/
260 
261 DeviceParameters::Pimpl::Pimpl(const char* address, const char* service)
262  : paramTrans(address, service) {
263 }
264 
265 DeviceParameters::Pimpl::Pimpl(const DeviceInfo& device)
266  : paramTrans(device.getIpAddress().c_str(), "7683") {
267 }
268 
269 bool DeviceParameters::Pimpl::isConnected() const {
270  return paramTrans.isConnected();
271 }
272 
273 int DeviceParameters::Pimpl::readIntParameter(const char* id) {
274  return paramTrans.readIntParameter(id);
275 }
276 
277 double DeviceParameters::Pimpl::readDoubleParameter(const char* id) {
278  return paramTrans.readDoubleParameter(id);
279 }
280 
281 bool DeviceParameters::Pimpl::readBoolParameter(const char* id) {
282  return paramTrans.readBoolParameter(id);
283 }
284 
285 void DeviceParameters::Pimpl::writeIntParameter(const char* id, int value) {
286  paramTrans.writeParameterTransactionGuarded(id, value);
287 }
288 
289 void DeviceParameters::Pimpl::writeDoubleParameter(const char* id, double value) {
290  paramTrans.writeParameterTransactionGuarded(id, value);
291 }
292 
293 void DeviceParameters::Pimpl::writeBoolParameter(const char* id, bool value) {
294  paramTrans.writeParameterTransactionGuarded(id, value);
295 }
296 
297 void DeviceParameters::Pimpl::writeBoolParameterUnguarded(const char* id, bool value) {
298  paramTrans.writeParameterTransactionUnguarded(id, value);
299 }
300 
301 std::map<std::string, ParameterInfo> DeviceParameters::Pimpl::getAllParameters() {
302  serverSideEnumeration = paramTrans.getAllParameters();
303  return serverSideEnumeration;
304 }
305 
306 bool DeviceParameters::Pimpl::hasParameter(const std::string& name) const {
307  return paramTrans.hasParameter(name);
308 }
309 
310 Parameter DeviceParameters::Pimpl::getParameter(const std::string& name) const {
311  return paramTrans.getParameter(name);
312 }
313 
314 Parameter DeviceParameters::Pimpl::pollParameter(const std::string& name, bool blockingCall) {
315  return paramTrans.pollParameter(name, blockingCall);
316 }
317 
318 ParameterSet const& DeviceParameters::Pimpl::getParameterSet() const {
319  return paramTrans.getParameterSet();
320 }
321 
322 void DeviceParameters::Pimpl::setParameterUpdateCallback(std::function<void(const std::string& uid)> callback, bool threaded) {
323  paramTrans.setParameterUpdateCallback(callback, threaded);
324 }
325 
326 void DeviceParameters::Pimpl::transactionStartQueue() {
327  paramTrans.transactionStartQueue();
328 }
329 
330 void DeviceParameters::Pimpl::transactionCommitQueue(int maxWaitMilliseconds) {
331  paramTrans.transactionCommitQueue(maxWaitMilliseconds);
332 }
333 
334 void DeviceParameters::Pimpl::persistParameters(const std::vector<std::string>& uids, bool blocking) {
335  paramTrans.persistParameters(uids, blocking);
336 }
337 
338 void DeviceParameters::Pimpl::setConnectionStateChangeCallback(std::function<void(visiontransfer::ConnectionState)> callback) {
339  paramTrans.setConnectionStateChangeCallback(callback);
340 }
341 
342 
343 } // namespace
344 
visiontransfer::DeviceParameters::getAllParameters
std::map< std::string, ParameterInfo > getAllParameters()
Enumerates all simple parameters as reported by the device [DEPRECATED].
Definition: deviceparameters.cpp:127
visiontransfer::DeviceParameters::hasParameter
bool hasParameter(const std::string &name) const
Tests whether a specific named parameter is available for this device. [C++11]
Definition: deviceparameters.cpp:193
visiontransfer::DeviceParameters::transactionLock
std::unique_ptr< TransactionLock > transactionLock()
Obtain a scoped TransactionLock for the current thread [C++11]
Definition: deviceparameters.cpp:226
visiontransfer::DeviceParameters::getParameter
visiontransfer::param::Parameter getParameter(const std::string &name) const
Returns a Parameter object for the named device parameter. Throws ParameterException for invalid or i...
Definition: deviceparameters.cpp:197
visiontransfer::DeviceInfo
Aggregates information about a discovered device.
Definition: deviceinfo.h:59
visiontransfer::internal::ParameterTransfer
Allows a configuration of device parameters over the network.
Definition: parametertransfer.h:72
visiontransfer::DeviceParameters::pollParameter
visiontransfer::param::Parameter pollParameter(const std::string &name, bool blockingCall=true)
Polls a parameter to report with updated internal values. Only meaningful if parameter....
Definition: deviceparameters.cpp:205
visiontransfer::DeviceParameters::setParameter
void setParameter(const std::string &name, T value)
Set a parameter by name. ParameterException for invalid names or values.
visiontransfer::DeviceParameters::setConnectionStateChangeCallback
void setConnectionStateChangeCallback(std::function< void(visiontransfer::ConnectionState)> callback)
Install a handler that will be called when the connection state changes (e.g. socket is disconnected)...
Definition: deviceparameters.cpp:253
visiontransfer::param::ParameterSet
Definition: parameterset.h:59
visiontransfer::DeviceParameters::getParameterSet
visiontransfer::param::ParameterSet getParameterSet() const
Returns all API-accessible parameters as reported by the device. [C++11]
Definition: deviceparameters.cpp:201
visiontransfer::DeviceParameters::isConnected
bool isConnected() const
Returns true if a remote connection is currently established and the full host-device handshake has s...
Definition: deviceparameters.cpp:95
visiontransfer::DeviceParameters::getNamedParameter
T getNamedParameter(const std::string &name)
Get a parameter by name, specifying the return type (int, double or bool). Throws ParameterException ...
visiontransfer::param::Parameter
Definition: parameter.h:71
visiontransfer::DeviceParameters::DeviceParameters
DeviceParameters(const DeviceInfo &device)
Connects to parameter server of a Nerian stereo device by using the device information from device en...
Definition: deviceparameters.cpp:81
visiontransfer::DeviceParameters::setParameterUpdateCallback
void setParameterUpdateCallback(std::function< void(const std::string &uid)> callback, bool threaded=true)
Sets the optional user parameter update callback. This is then called for all asynchronous value or m...
Definition: deviceparameters.cpp:209
visiontransfer::DeviceParameters::setNamedParameter
void setNamedParameter(const std::string &name, T value)
Set a parameter by name. ParameterException for invalid names.
visiontransfer::param::Parameter::getUid
VT_EXPORT std::string getUid() const
Definition: parameter.cpp:734
visiontransfer::DeviceParameters::saveParameter
void saveParameter(const char *uid, bool blockingCall=true)
Save the specified parameter to persistent device storage.
Definition: deviceparameters.cpp:230
visiontransfer::DeviceParameters::saveParameters
void saveParameters(const std::vector< std::string > &uids, bool blockingCall=true)
See saveParameter(const char*) [C++11]
Definition: deviceparameters.cpp:239
visiontransfer::DeviceParameters::TransactionLock::commitAndWait
void commitAndWait(int maxWaitMilliseconds=1000)
Send and commit the batch transaction and wait for direct(*) parameter dependencies to be reported as...
Definition: deviceparameters.cpp:222
Allied Vision