* new from/to_proxy for the AxisAdpater class
[diffractometer.git] / src / AxisAdapter.cpp
blobee0c0a47baab57d29dece94e34c06811e9a11758
1 #include <AxisAdapter.h>
2 #include <macros.h>
4 namespace DiffractometerDevice_ns {
6 AxisAdapter::AxisAdapter(HklAxis *read, HklAxis *write)
8 _device_name = "";
9 _proxy = NULL;
10 _axis_r = read;
11 _axis_w = write;
12 this->from_HklAxis();
13 _state = Tango::UNKNOWN;
16 AxisAdapter::~AxisAdapter(void)
20 std::string const AxisAdapter::get_name(void) const
22 return ((HklParameter *)(_axis_r))->name;
25 Tango::Group *AxisAdapter::connect(std::string const & device_name)
27 Tango::Group *group;
28 std::string name = this->get_name();
30 // Create a new Group for the axe.
31 group = new Tango::Group(name);
32 if(group){
33 group->add(device_name);
35 // read positions from proxy;
36 Tango::GroupAttrReplyList reply;
37 std::vector<double> positions;
38 reply = group->read_attribute(AXIS_ATTRIBUTE_POSITION_NAME);
39 if (reply.has_failed()) {
40 group->remove_all();
41 delete group;
42 group = NULL;
43 }else{
44 reply[0] >> positions;
45 _device_name = device_name;
46 _proxy = group->get_device(1);
47 hkl_axis_set_value_unit(_axis_r, positions[0]);
48 hkl_axis_set_value_unit(_axis_w, positions[1]);
49 _read = positions[0];
50 _write = positions[1];
52 // now read the range
54 * here an hack due to the
55 * get_attribute_config signature
56 * we need a non const char or
57 * string etc...
59 std::string attr_name = AXIS_ATTRIBUTE_POSITION_NAME;
60 Tango::AttributeInfo info = _proxy->get_attribute_config(attr_name);
62 // Check if min and max value have been set
63 char const *token;
64 token = info.min_value.c_str();
65 if (!strstr(token, "Not specified"))
66 _min = atof(token);
68 token = info.max_value.c_str();
69 if (!strstr(token, "Not specified"))
70 _max = atof(token);
73 return group;
76 bool const AxisAdapter::is_ready(void) const
78 if (_proxy)
79 return true;
80 else
81 return false;
84 void AxisAdapter::write(double value, bool simulated)
86 if(simulated){
87 _read = _write = value;
88 this->to_HklAxis();
89 } else if (this->is_ready()) {
90 #ifdef WRITE_TO_PROXY_ALLOWED
91 // only write to the proxies
92 Tango::DeviceAttribute attr(AXIS_ATTRIBUTE_POSITION_NAME, value);
93 _proxy->command_inout(AXIS_COMMAND_STOP_NAME);
94 _proxy->write_attribute(attr);
95 #endif
96 _state = Tango::MOVING;
100 void AxisAdapter::from_HklAxis(void)
102 _read = hkl_axis_get_value_closest_unit(_axis_r, _axis_r);
103 _write = hkl_axis_get_value_closest_unit(_axis_w, _axis_r);
104 hkl_axis_get_range_unit(_axis_r, &_min, &_max);
107 void AxisAdapter::to_HklAxis(void)
109 hkl_axis_set_value_unit(_axis_r, _read);
110 hkl_axis_set_value_unit(_axis_w, _write);
111 hkl_axis_set_range_unit(_axis_r, _min, _max);
112 hkl_axis_set_range_unit(_axis_w, _min, _max);
115 void AxisAdapter::from_proxy(void)
117 if(this->is_ready()){
118 Tango::DeviceAttribute attr;
119 std::vector<double> positions;
121 // read the position attribute
122 attr = _proxy->read_attribute(AXIS_ATTRIBUTE_POSITION_NAME);
123 attr >> positions;
124 _read = positions[0];
125 _write = positions[1];
127 // read the range
129 * here an hack due to the
130 * get_attribute_config signature
131 * we need a non const char or
132 * string etc...
134 std::string attr_name = AXIS_ATTRIBUTE_POSITION_NAME;
135 Tango::AttributeInfo info = _proxy->get_attribute_config(attr_name);
137 // Check if min and max value have been set
138 char const *token;
139 token = info.min_value.c_str();
140 if (!strstr(token, "Not specified"))
141 _min = atof(token);
143 token = info.max_value.c_str();
144 if (!strstr(token, "Not specified"))
145 _max = atof(token);
147 // read the state
148 _state = _proxy->state();
152 void AxisAdapter::to_proxy(void)
154 #ifdef WRITE_TO_PROXY_ALLOWED
155 if(this->is_ready()){
156 Tango::DeviceAttribute attr(AXIS_ATTRBIUTE_POSITION_NAME, _write);
157 _state = Tango::MOVING;
158 _proxy->write_attribute(attr);
160 #endif