MotionLib  1.0.0
SamBuCa motion library
DeviceBase.cpp
1 /*
2  * Copyright (C) 2021 CERN
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * Created on: 2021-11-23
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #include "DeviceBase.hpp"
26 
27 #include <algorithm>
28 #include <map>
29 #include <sstream>
30 
31 #include <ccut/async.hpp>
32 #include <ccut/utils.hpp>
33 
34 #include "util/serialize.hpp"
35 
36 namespace smc {
37 
38 static const std::string s_empty;
39 static const std::map<DeviceType, std::string> s_deviceTypesToStr{
40  {DeviceType::AXIS, "axis"},
41  {DeviceType::POSITION_SENSOR, "position-sensor"},
42  {DeviceType::GPIO, "gpio"},
43  {DeviceType::TRIGGER, "trigger"},
44  {DeviceType::PLATFORM, "platform"},
45  {DeviceType::AXIS_POSITION_MONITOR, "axis-position-monitor"},
46  {DeviceType::UNKNOWN, "unknown"}};
47 static const std::map<std::string, DeviceType> s_deviceTypesFromStr{
48  ccut::flip(s_deviceTypesToStr)};
49 static const std::set<DeviceType> s_deviceTypesKeys{
50  ccut::keys(s_deviceTypesToStr)};
51 
52 const std::string &to_string(DeviceType type)
53 {
54  std::map<DeviceType, std::string>::const_iterator it =
55  s_deviceTypesToStr.find(type);
56  return (it != s_deviceTypesToStr.end()) ? it->second : s_empty;
57 }
58 
59 template<>
60 DeviceType from_string<DeviceType>(const std::string &str)
61 {
62  std::map<std::string, DeviceType>::const_iterator it =
63  s_deviceTypesFromStr.find(str);
64  return (it != s_deviceTypesFromStr.end()) ? it->second :
66 }
67 
68 template<>
69 const std::set<DeviceType> &getEnumValues<DeviceType>()
70 {
71  return s_deviceTypesKeys;
72 }
73 
74 DeviceBase::DeviceBase(const std::string &uid) : m_uid{uid} {}
75 
76 std::string DeviceBase::uid() const
77 {
78  return m_uid;
79 }
80 
81 bool DeviceBase::canConvert(const units::Value &value, units::unit_t unit) const
82 {
83  return value.canConvert(unit);
84 }
85 
86 bool DeviceBase::convert(units::Value &value, units::unit_t unit) const
87 {
88  return value.convert(unit);
89 }
90 
91 std::future<std::set<std::string>> DeviceBase::listConfig() const
92 {
93  std::promise<std::set<std::string>> prom;
94  prom.set_value(std::set<std::string>());
95  return prom.get_future();
96 }
97 
98 std::future<std::string> DeviceBase::getConfig(const std::string &name) const
99 {
100  return ccut::make_future_error<std::string>(Exception(
101  ErrorCode::InvalidArguments, "no such config value: " + name));
102 }
103 
104 std::future<void> DeviceBase::setConfig(const std::string &name,
105  const std::string &) const
106 {
107  return ccut::make_future_error(Exception(ErrorCode::InvalidArguments,
108  "no such config value: " + name));
109 }
110 
111 std::string DeviceBase::toString() const
112 {
113  std::stringstream ss;
114  ss << "Device{" << to_string(type()) << ", " << m_uid << "}";
115  return ss.str();
116 }
117 
118 } // namespace smc
virtual std::future< std::set< std::string > > listConfig() const
list device configuration options
Definition: DeviceBase.cpp:91
virtual bool convert(units::Value &value, units::unit_t unit) const
Convert value in a manner specific to this device.
Definition: DeviceBase.cpp:86
virtual bool canConvert(const units::Value &value, units::unit_t unit) const
Check if unit conversion is possible in a manner specific to this device.
Definition: DeviceBase.cpp:81
virtual std::future< void > setConfig(const std::string &name, const std::string &value) const
set device configuration value
Definition: DeviceBase.cpp:104
std::string toString() const
debug and logging operation
Definition: DeviceBase.cpp:111
virtual std::future< std::string > getConfig(const std::string &name) const
get device configuration value
Definition: DeviceBase.cpp:98
DeviceBase(const std::string &uid)
Construct a new ABaseDevice, linking pointer to platform object.
Definition: DeviceBase.cpp:74
DeviceId uid() const
Get the address of device.
Definition: DeviceBase.cpp:76
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
const std::string & to_string(Axis::State state)
convert State to string
Definition: Axis.cpp:78
DeviceType
List of possible device types supported.
Definition: DeviceBase.hpp:52
@ AXIS_POSITION_MONITOR
device representing link with axis and position
@ UNKNOWN
unknown device type
@ PLATFORM
special device representing the platform itself
@ AXIS
actuator device
@ GPIO
gpio like device
@ TRIGGER
hardware/software trigger device
@ POSITION_SENSOR
position sensor device
main motion-lib namespace
Definition: Client.cpp:30