Goby v2
liaison_commander.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef LIAISONCOMMANDER20110609H
24 #define LIAISONCOMMANDER20110609H
25 
26 #include <Wt/WAbstractListModel>
27 #include <Wt/WBorder>
28 #include <Wt/WBreak>
29 #include <Wt/WColor>
30 #include <Wt/WComboBox>
31 #include <Wt/WCssDecorationStyle>
32 #include <Wt/WDateTime>
33 #include <Wt/WDialog>
34 #include <Wt/WGroupBox>
35 #include <Wt/WLabel>
36 #include <Wt/WLineEdit>
37 #include <Wt/WPushButton>
38 #include <Wt/WSpinBox>
39 #include <Wt/WStackedWidget>
40 #include <Wt/WString>
41 #include <Wt/WStringListModel>
42 #include <Wt/WTableCell>
43 #include <Wt/WTableView>
44 #include <Wt/WText>
45 #include <Wt/WTimer>
46 #include <Wt/WTreeTable>
47 #include <Wt/WTreeTableNode>
48 #include <Wt/WTreeView>
49 #include <Wt/WVBoxLayout>
50 #include <Wt/WValidator>
51 
52 #include <Wt/Dbo/FixedSqlConnectionPool>
53 #include <Wt/Dbo/Impl>
54 #include <Wt/Dbo/QueryModel>
55 #include <Wt/Dbo/Session>
56 #include <Wt/Dbo/SqlTraits>
57 #include <Wt/Dbo/WtSqlTraits>
58 #include <Wt/Dbo/backend/Sqlite3>
59 
60 #include "goby/common/liaison_container.h"
61 #include "goby/moos/moos_node.h"
62 #include "goby/moos/protobuf/liaison_config.pb.h"
63 
64 namespace goby
65 {
66 namespace common
67 {
68 class LiaisonTreeTableNode : public Wt::WTreeTableNode
69 {
70  public:
71  LiaisonTreeTableNode(const Wt::WString& labelText, Wt::WIconPair* labelIcon = 0,
72  Wt::WTreeTableNode* parentNode = 0)
73  : Wt::WTreeTableNode(labelText, labelIcon, parentNode)
74  {
75  this->labelArea()->setHeight(Wt::WLength(2.5, Wt::WLength::FontEm));
76  }
77 };
78 
80 {
81  std::string protobuf_name;
82  std::vector<unsigned char> bytes;
83  long long utime;
84  Wt::WDateTime time;
85  std::string comment;
86  std::string address;
87  int last_ack;
88  // serialized NetworkAckSet
89  std::vector<unsigned char> acks;
90 
91  template <class Action> void persist(Action& a)
92  {
93  Wt::Dbo::field(a, protobuf_name, "protobuf_name");
94  Wt::Dbo::field(a, bytes, "bytes");
95  Wt::Dbo::field(a, utime, "utime");
96  Wt::Dbo::field(a, time, "time");
97  Wt::Dbo::field(a, comment, "comment");
98  Wt::Dbo::field(a, address, "address");
99  Wt::Dbo::field(a, last_ack, "last_ack");
100  Wt::Dbo::field(a, acks, "acks");
101  }
102 };
103 
105 {
106  public:
107  LiaisonCommander(ZeroMQService* zeromq_service, const protobuf::LiaisonConfig& cfg,
108  Wt::WContainerWidget* parent = 0);
109  void moos_inbox(CMOOSMsg& msg);
110  void loop();
111 
112  private:
113  void focus() { commander_timer_.start(); }
114 
115  void unfocus() { commander_timer_.stop(); }
116 
117  private:
118  ZeroMQService* zeromq_service_;
119  const protobuf::ProtobufCommanderConfig& pb_commander_config_;
120  std::set<std::string> display_subscriptions_;
121 
122  Wt::WVBoxLayout* main_layout_;
123 
124  struct ControlsContainer : Wt::WGroupBox
125  {
126  ControlsContainer(MOOSNode* moos_node,
127  const protobuf::ProtobufCommanderConfig& pb_commander_config,
128  Wt::WStackedWidget* commands_div, Wt::WContainerWidget* parent = 0);
129  void switch_command(int selection_index);
130 
131  void clear_message();
132  void send_message();
133 
134  void increment_incoming_messages(const Wt::WMouseEvent& event);
135  void decrement_incoming_messages(const Wt::WMouseEvent& event);
136  void remove_incoming_message(const Wt::WMouseEvent& event);
137 
138  struct CommandContainer : Wt::WGroupBox
139  {
140  CommandContainer(MOOSNode* moos_node,
141  const protobuf::ProtobufCommanderConfig& pb_commander_config,
142  const std::string& protobuf_name, Wt::Dbo::Session* session);
143 
144  // Wt::WStackedWidget* master_field_info_stack);
145 
146  void generate_root();
147 
148  void generate_tree(Wt::WTreeTableNode* parent, google::protobuf::Message* message);
149  void generate_tree_row(Wt::WTreeTableNode* parent, google::protobuf::Message* message,
150  const google::protobuf::FieldDescriptor* field_desc);
151 
152  void generate_tree_field(Wt::WFormWidget*& value_field,
153  google::protobuf::Message* message,
154  const google::protobuf::FieldDescriptor* field_desc,
155  int index = -1);
156 
157  Wt::WLineEdit*
158  generate_single_line_edit_field(google::protobuf::Message* message,
159  const google::protobuf::FieldDescriptor* field_desc,
160  const std::string& current_value,
161  const std::string& default_value,
162  Wt::WValidator* validator, int index = -1);
163 
164  Wt::WComboBox*
165  generate_combo_box_field(google::protobuf::Message* message,
166  const google::protobuf::FieldDescriptor* field_desc,
167  const std::vector<Wt::WString>& strings, int current_value,
168  const std::string& default_value, int index = -1);
169 
170  void generate_field_info_box(Wt::WFormWidget*& value_field,
171  const google::protobuf::FieldDescriptor* field_desc);
172 
173  void set_time_field(Wt::WFormWidget* value_field,
174  const google::protobuf::FieldDescriptor* field_desc);
175 
176  /* void queue_default_value_field( */
177  /* Wt::WFormWidget*& value_field, */
178  /* const google::protobuf::FieldDescriptor* field_desc); */
179 
180  void dccl_default_value_field(Wt::WFormWidget*& value_field,
181  const google::protobuf::FieldDescriptor* field_desc);
182 
183  void dccl_default_modify_field(Wt::WFormWidget*& modify_field,
184  const google::protobuf::FieldDescriptor* field_desc);
185 
186  std::string
187  string_from_dccl_double(double* value,
188  const google::protobuf::FieldDescriptor* field_desc);
189 
190  // void handle_field_focus(int field_info_index);
191 
192  void handle_toggle_single_message(const Wt::WMouseEvent& mouse,
193  google::protobuf::Message* message,
194  const google::protobuf::FieldDescriptor* field_desc,
195  Wt::WPushButton* field, Wt::WTreeTableNode* parent);
196 
197  void handle_line_field_changed(google::protobuf::Message* message,
198  const google::protobuf::FieldDescriptor* field_desc,
199  Wt::WLineEdit* field, int index);
200 
201  void handle_combo_field_changed(google::protobuf::Message* message,
202  const google::protobuf::FieldDescriptor* field_desc,
203  Wt::WComboBox* field, int index);
204 
205  void handle_repeated_size_change(int size, google::protobuf::Message* message,
206  const google::protobuf::FieldDescriptor* field_desc,
207  Wt::WTreeTableNode* parent);
208 
209  void handle_database_double_click(const Wt::WModelIndex& index,
210  const Wt::WMouseEvent& event);
211 
212  enum DatabaseDialogResponse
213  {
214  RESPONSE_EDIT,
215  RESPONSE_MERGE,
216  RESPONSE_CANCEL
217  };
218 
219  void handle_database_dialog(DatabaseDialogResponse response,
220  boost::shared_ptr<google::protobuf::Message> message);
221 
222  MOOSNode* moos_node_;
223  boost::shared_ptr<google::protobuf::Message> message_;
224 
225  std::map<Wt::WFormWidget*, const google::protobuf::FieldDescriptor*> time_fields_;
226  goby::uint64 latest_time_;
227 
228  Wt::WGroupBox* tree_box_;
229  Wt::WTreeTable* tree_table_;
230 
231  // Wt::WStackedWidget* field_info_stack_;
232  // std::map<const google::protobuf::FieldDescriptor*, int> field_info_map_;
233 
234  Wt::Dbo::Session* session_;
235  Wt::Dbo::QueryModel<Wt::Dbo::ptr<CommandEntry> >* query_model_;
236 
237  Wt::WGroupBox* query_box_;
238  Wt::WTreeView* query_table_;
239 
240  boost::posix_time::ptime last_reload_time_;
241 
242  boost::shared_ptr<Wt::WDialog> database_dialog_;
243 
244  const protobuf::ProtobufCommanderConfig& pb_commander_config_;
245  };
246 
247  MOOSNode* moos_node_;
248  const protobuf::ProtobufCommanderConfig& pb_commander_config_;
249  std::map<std::string, int> commands_;
250  Wt::WLabel* command_label_;
251  Wt::WComboBox* command_selection_;
252  Wt::WContainerWidget* buttons_div_;
253  Wt::WLabel* comment_label_;
254  Wt::WLineEdit* comment_line_;
255  Wt::WPushButton* send_button_;
256  Wt::WPushButton* clear_button_;
257  Wt::WStackedWidget* commands_div_;
258 
259  Wt::WPanel* incoming_message_panel_;
260  Wt::WStackedWidget* incoming_message_stack_;
261 
262  // Wt::WPanel* master_field_info_panel_;
263  // Wt::WStackedWidget* master_field_info_stack_;
264 
265  Wt::Dbo::Session session_;
266  };
267 
268  Wt::WStackedWidget* commands_div_;
269  ControlsContainer* controls_div_;
270 
271  Wt::WTimer commander_timer_;
272 
273  // static database objects
274  static boost::posix_time::ptime last_db_update_time_;
275  static boost::mutex dbo_mutex_;
276  static boost::shared_ptr<Wt::Dbo::backend::Sqlite3> sqlite3_;
277  static boost::shared_ptr<Wt::Dbo::FixedSqlConnectionPool> connection_pool_;
278 };
279 } // namespace common
280 } // namespace goby
281 
282 #endif
The global namespace for the Goby project.
google::protobuf::uint64 uint64
an unsigned 64 bit integer