sensor-command-dialog.cc
Go to the documentation of this file.
1 /*
2 * Roboception GmbH
3 * Munich, Germany
4 * www.roboception.com
5 *
6 * Copyright (c) 2017 Roboception GmbH
7 * All rights reserved
8 *
9 * Author: Raphael Schaller
10 */
11 #include "sensor-command-dialog.h"
12 
13 #include "event-ids.h"
14 #include "resources.h"
15 
16 #include "rcdiscover/utils.h"
17 #include "discover-frame.h"
18 
19 #include <sstream>
20 
21 #include <wx/panel.h>
22 #include <wx/sizer.h>
23 #include <wx/textctrl.h>
24 #include <wx/stattext.h>
25 #include <wx/combobox.h>
26 #include <wx/dataview.h>
27 #include <wx/html/helpctrl.h>
28 #include <wx/cshelp.h>
29 
30 SensorCommandDialog::SensorCommandDialog(wxHtmlHelpController *help_ctrl,
31  wxWindow *parent, wxWindowID id,
32  std::string title,
33  const int additional_grid_rows,
34  const wxPoint &pos,
35  long style,
36  const wxString &name) :
37  wxDialog(parent, id, std::move(title), pos, wxSize(-1,-1), style, name),
38  sensors_(nullptr),
39  mac_{{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}},
40  sensor_list_(nullptr),
41  help_ctrl_(help_ctrl)
42 {
43  panel_ = new wxPanel(this, -1);
44  vbox_ = new wxBoxSizer(wxVERTICAL);
45 
46  grid_ = new wxFlexGridSizer(2 + additional_grid_rows, 2, 10, 25);
47 
48  auto *sensors_text = new wxStaticText(panel_, wxID_ANY, "rc_visard");
49  grid_->Add(sensors_text);
50 
51  auto *sensors_box = new wxBoxSizer(wxHORIZONTAL);
52  sensors_ = new wxChoice(panel_, ID_Sensor_Combobox);
53  sensors_box->Add(sensors_, 1);
54  grid_->Add(sensors_box, 1, wxEXPAND);
55 
56  auto *mac_text = new wxStaticText(panel_, wxID_ANY, "MAC address");
57  grid_->Add(mac_text);
58 
59  auto *mac_box = new wxBoxSizer(wxHORIZONTAL);
60  int i = 0;
61  for (auto& m : mac_)
62  {
63  if (i > 0)
64  {
65  mac_box->Add(new wxStaticText(panel_, ID_MAC_Textbox, ":"));
66  }
67  m = new wxTextCtrl(panel_, wxID_ANY, wxEmptyString,
68  wxDefaultPosition, wxSize(35, -1));
69  mac_box->Add(m, 1);
70  ++i;
71  }
72  grid_->Add(mac_box, 1, wxEXPAND);
73 
74  vbox_->Add(grid_, 0, wxALL | wxEXPAND, 15);
75 
76  panel_->SetSizer(vbox_);
77 
78  Connect(ID_Sensor_Combobox,
79  wxEVT_CHOICE,
80  wxCommandEventHandler(SensorCommandDialog::onSensorSelected));
81 }
82 
84  const wxDataViewListModel *sensor_list,
85  const std::vector<bool>& show)
86 {
87  sensor_list_ = sensor_list;
88 
89  if (sensor_list != nullptr)
90  {
91  sensors_->Clear();
92 
93  sensors_->Append("<Custom>");
94 
95  const auto rows = sensor_list->GetCount();
96  unsigned int sensors_row = 0;
97  for (typename std::decay<decltype(rows)>::type i = 0; i < rows; ++i)
98  {
99  if (show.empty() || show[i])
100  {
101  wxVariant hostname{};
102  wxVariant mac{};
103  sensor_list->GetValueByRow(hostname, i, DiscoverFrame::NAME);
104  sensor_list->GetValueByRow(mac, i, DiscoverFrame::MAC);
105  const auto s = wxString::Format("%s - %s", hostname.GetString(), mac.GetString());
106  sensors_->Append(s);
107  row_map_.emplace(i, sensors_row + 1);
108  row_map_inv_.emplace(sensors_row + 1, i);
109  ++sensors_row;
110  }
111  }
112  }
113 
114  clear();
115 }
116 
117 void SensorCommandDialog::setActiveSensor(const unsigned int row)
118 {
119  clear();
120 
121  const auto found = row_map_.find(static_cast<int>(row));
122  if (found != row_map_.cend())
123  {
124  sensors_->Select(found->second);
125  fillMac();
126  }
127  else
128  {
129  sensors_->Select(0);
130  }
131 }
132 
134 {
135  return vbox_;
136 }
137 
139 {
140  return panel_;
141 }
142 
144 {
145  return grid_;
146 }
147 
148 std::array<uint8_t, 6> SensorCommandDialog::getMac() const
149 {
150  std::array<uint8_t, 6> mac;
151  for (uint8_t i = 0; i < 6; ++i)
152  {
153  const auto s = mac_[i]->GetValue().ToStdString();
154 
155  try
156  {
157  const auto v = std::stoul(s, nullptr, 16);
158  if (v > 0xff)
159  {
160  throw std::invalid_argument("");
161  }
162  mac[i] = static_cast<uint8_t>(v);
163  }
164  catch(const std::invalid_argument&)
165  {
166  throw std::runtime_error(
167  std::string("Each MAC address segment must contain ") +
168  "a hex value ranging from 0x00 to 0xff.");
169  }
170  }
171  return mac;
172 }
173 
175 {
176  const auto mac = getMac();
177 
178  std::ostringstream mac_string;
179  bool first = true;
180  for (const auto m : mac)
181  {
182  if (!first)
183  {
184  mac_string << ":";
185  }
186  mac_string << std::hex << std::setfill('0') << std::setw(2);
187  mac_string << static_cast<unsigned int>(m);
188  first = false;
189  }
190  return mac_string.str();
191 }
192 
193 void SensorCommandDialog::displayHelp(const std::string &section)
194 {
195  const std::string url = std::string("help.htm#") + section;
196  help_ctrl_->Display(url);
197 
198  // need second call otherwise it does not jump to the section if the
199  // help is displayed the first time
200  help_ctrl_->Display(url);
201 }
202 
204 {
205  clearMac();
206 }
207 
209 {
210  if (sensors_->GetSelection() != wxNOT_FOUND)
211  {
212  if (sensors_->GetSelection() == 0)
213  {
214  clearMac();
215  }
216  else
217  {
218  fillMac();
219  }
220  }
221 }
222 
224 {
225  const int row = sensors_->GetSelection();
226 
227  if (row == wxNOT_FOUND)
228  {
229  return;
230  }
231 
232  wxVariant mac_string{};
233  sensor_list_->GetValueByRow(mac_string,
234  row_map_inv_.at(static_cast<unsigned int>(row)),
236 
237  const auto mac = split<6>(mac_string.GetString().ToStdString(), ':');
238 
239  for (uint8_t i = 0; i < 6; ++i)
240  {
241  mac_[i]->ChangeValue(mac[i]);
242  mac_[i]->SetEditable(false);
243  }
244 }
245 
247 {
248  sensors_->SetSelection(0);
249 
250  for (uint8_t i = 0; i < 6; ++i)
251  {
252  mac_[i]->Clear();
253  mac_[i]->SetEditable(true);
254  }
255 }
256 
257 BEGIN_EVENT_TABLE(SensorCommandDialog, wxDialog)
258 END_EVENT_TABLE()
std::string getMacString() const
std::unordered_map< unsigned int, unsigned int > row_map_
SensorCommandDialog()=default
void onSensorSelected(wxCommandEvent &event)
Event handler for selection of an device from drop down menu.
const wxDataViewListModel * sensor_list_
virtual void clear()
Reset and clear all fields.
wxFlexGridSizer * getGrid()
Base class for dialogs for sending commands to a camera.
wxHtmlHelpController * help_ctrl_
std::array< wxTextCtrl *, 6 > mac_
void setDiscoveredSensors(const wxDataViewListModel *sensor_list, const std::vector< bool > &show={})
Set list of discovered devices to provide a drop down menu to the user.
void fillMac()
Fill MAC address according to selected device.
void clearMac()
Clear MAC address.
void displayHelp(const std::string &section)
std::unordered_map< unsigned int, unsigned int > row_map_inv_
void setActiveSensor(const unsigned int row)
Select a specific device of the list set by setDiscoveredSensors.
std::array< uint8_t, 6 > getMac() const


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Sun Apr 18 2021 02:16:32