* do not remove dynamic devices when stopping the Diffractometer device.
[diffractometer.git] / src / AxisAdapter.cpp
blob32d71656b70c93b24b1d20dda8fd0213fcd303b7
1 #include "AxisAdapter.h"
2 #include "TangoHKLAdapter.h"
3 #include "macros.h"
5 namespace Diffractometer_ns {
7 AxisAdapter::AxisAdapter(TangoHKLAdapter * hklAdapter, HklAxis *axis_r, HklAxis *axis_w,
8 HklAxis *axis_r_real, HklAxis *axis_w_real) :
9 _hklAdapter(hklAdapter),
10 _axis_r(axis_r),
11 _axis_w(axis_w),
12 _axis_r_real(axis_r_real),
13 _axis_w_real(axis_w_real)
15 _device_name = "";
16 _proxy = NULL;
17 this->from_HklAxis();
18 _state = Tango::UNKNOWN;
21 AxisAdapter::~AxisAdapter(void)
25 std::string const AxisAdapter::get_name(void) const
27 return ((HklParameter *)(_axis_r))->name;
30 Tango::Group *AxisAdapter::connect(std::string const & device_name)
32 Tango::Group *group;
33 std::string name = this->get_name();
35 // Create a new Group for the axe.
36 group = new Tango::Group(name);
37 if(group){
38 group->add(device_name);
40 // read positions from proxy;
41 Tango::GroupAttrReplyList reply;
42 std::vector<double> positions;
43 reply = group->read_attribute(AXIS_ATTRIBUTE_POSITION_NAME);
44 if (reply.has_failed()) {
45 group->remove_all();
46 delete group;
47 group = NULL;
48 }else{
49 reply[0] >> positions;
50 _device_name = device_name;
51 _proxy = group->get_device(1);
52 hkl_axis_set_value_unit(_axis_r, positions[0]);
53 hkl_axis_set_value_unit(_axis_w, positions[1]);
54 _read = positions[0];
55 _write = positions[1];
57 // now read the range
59 * here an hack due to the
60 * get_attribute_config signature
61 * we need a non const char or
62 * string etc...
64 std::string attr_name = AXIS_ATTRIBUTE_POSITION_NAME;
65 Tango::AttributeInfo info = _proxy->get_attribute_config(attr_name);
67 // Check if min and max value have been set
68 char const *token;
69 token = info.min_value.c_str();
70 if (!strstr(token, "Not specified"))
71 _min = atof(token);
73 token = info.max_value.c_str();
74 if (!strstr(token, "Not specified"))
75 _max = atof(token);
78 return group;
81 bool const AxisAdapter::is_ready(void) const
83 if (_proxy)
84 return true;
85 else
86 return false;
89 void AxisAdapter::write(double value)
91 _hklAdapter->write_axis(*this, value);
94 void AxisAdapter::from_HklAxis(void)
96 _read = hkl_axis_get_value_unit(_axis_r);
97 _write = hkl_axis_get_value_unit(_axis_w);
98 _read_real = hkl_axis_get_value_unit(_axis_r_real);
99 _write_real = hkl_axis_get_value_unit(_axis_w_real);
100 hkl_axis_get_range_unit(_axis_r, &_min, &_max);
103 void AxisAdapter::to_HklAxis(void)
105 hkl_axis_set_value_unit(_axis_r, _read);
106 hkl_axis_set_value_unit(_axis_w, _write);
107 hkl_axis_set_range_unit(_axis_r, _min, _max);
108 hkl_axis_set_range_unit(_axis_w, _min, _max);
109 hkl_axis_set_value_unit(_axis_r_real, _read_real);
110 hkl_axis_set_value_unit(_axis_w_real, _write_real);
111 hkl_axis_set_range_unit(_axis_r_real, _min, _max);
112 hkl_axis_set_range_unit(_axis_w_real, _min, _max);
115 void AxisAdapter::from_proxy(bool simulated)
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_real = positions[0];
125 _write_real = 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();
150 if(!simulated){
151 _read = _read_real;
152 _write = _write_real;
153 }else{
154 _read = hkl_axis_get_value_unit(_axis_r);
155 _write = hkl_axis_get_value_unit(_axis_w);
160 void AxisAdapter::to_proxy(void)
162 #ifdef WRITE_TO_PROXY_ALLOWED
163 if(this->is_ready()){
164 Tango::DeviceAttribute attr(AXIS_ATTRIBUTE_POSITION_NAME, _write_real);
165 _state = Tango::MOVING;
166 _proxy->write_attribute(attr);
168 #endif