8 #include "plaqui/server/string.h"
10 #include "item_conduct.h"
11 #include "item_exclusa.h"
12 #include "item_tank.h"
13 #include "item_pump.h"
14 #include "item_union.h"
15 #include "item_drain.h"
18 Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &rg):Gtk::Window(co),refXml(rg)
20 Gtk::MenuItem *conect=0, *exit=0, *about=0, *mnu_prop=0, *mnu_disconnect;
21 Gtk::Button *btn_get=0, *bar_connect=0;
23 txt_target = txt_command = txt_args = 0;
25 lbl_nombre = lbl_color = lbl_flujo = 0;
27 rg->get_widget("lbl_nombre", lbl_nombre);
28 rg->get_widget("lbl_flujo", lbl_flujo);
29 rg->get_widget("mnu_file_connect", conect);
30 rg->get_widget("mnu_file_disconnect", mnu_disconnect);
31 rg->get_widget("mnu_file_exit", exit);
32 rg->get_widget("mnu_help_about", about);
33 rg->get_widget("mnu_prop", mnu_prop);
34 rg->get_widget_derived("dlgConectar", dlg_conectar);
35 rg->get_widget("btn_get", btn_get);
36 rg->get_widget("txt_view", txt_view);
37 rg->get_widget("txt_target", txt_target);
38 rg->get_widget("txt_command", txt_command);
39 rg->get_widget("txt_args", txt_args);
40 rg->get_widget("bar_connect", bar_connect);
41 rg->get_widget("work_place", work_place);
42 rg->get_widget("ico_conected", ico_conected);
44 dlg_conectar->get_ok_button()->signal_clicked().connect( SigC::slot(*this, &Principal::on_dlg_connect_ok) );
45 mnu_disconnect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_disconnect));
46 conect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
47 bar_connect->signal_clicked().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
48 exit->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_exit));
49 about->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_help_about));
50 btn_get->signal_clicked().connect( SigC::slot(*this, &Principal::on_get_clicked) );
53 is_xml_loaded = false;
55 load_xml_dispatch.connect( SigC::slot(*this, &Principal::loadXML ) );
58 Principal::~Principal()
63 void Principal::on_dlg_connect_ok()
65 if (conexion == NULL) {
68 conexion = new PlaQui::Server::ControlClient(dlg_conectar->get_server_name(), dlg_conectar->get_server_port());
71 txt_view->get_buffer()->insert_at_cursor("NO SE PUDO CREAR OBJETO\n");
77 // Conecto las señales
78 conexion->signal_ok_received().connect( SigC::slot(*this, &Principal::on_conexion_ok) );
79 conexion->signal_error_received().connect( SigC::slot(*this, &Principal::on_conexion_error) );
80 conexion->signal_connected().connect( SigC::slot(*this, &Principal::on_conexion_connected) );
81 conexion->signal_finished().connect( SigC::slot(*this, &Principal::on_conexion_finished) );
82 conexion->signal_frame_received().connect(SigC::slot(*this, &Principal::on_conexion_frame));
86 txt_view->get_buffer()->insert_at_cursor("YA ESTAS CONECTADO\n");
91 void Principal::on_mnu_file_exit()
96 void Principal::on_mnu_file_disconnect()
98 if (conexion == NULL) return;
103 void Principal::on_mnu_file_connect()
105 dlg_conectar->show();
108 void Principal::on_mnu_help_about()
110 // preparo para leer el archivo ChangeLog
111 /* Glib::RefPtr<Gnome::Glade::Xml> xml;
113 Glib::RefPtr<Gtk::TextBuffer> log_buffer;
114 Glib::RefPtr<Glib::IOChannel> log_io;
116 log_buffer = Gtk::TextBuffer::create();
117 log_io = Glib::IOChannel::create_from_file("../ChangeLog", "r");
118 while (log_io->read_line(line) != Glib::IO_STATUS_EOF) {
119 log_buffer->insert_at_cursor(line);
123 xml = Gnome::Glade::Xml::create("client.glade", "dlgAbout");
125 catch(const Gnome::Glade::XmlError &ex) {
126 std::cerr << ex.what() << std::endl;
129 Gtk::Window *dlg = 0;
130 Gtk::Button *btn_cerrar = 0;
131 Gtk::TextView *txt_changelog = 0;
132 xml->get_widget("dlgAbout", dlg);
133 xml->get_widget("btn_close", btn_cerrar);
134 xml->get_widget("txt_changelog", txt_changelog);
135 btn_cerrar->signal_clicked().connect(SigC::slot(*dlg, &Gtk::Dialog::hide));
136 txt_changelog->set_buffer(log_buffer);
140 bool Principal::on_item_clicked(GdkEventButton *e, ViewItem *i)
142 lbl_nombre->set_text(i->get_name());
143 lbl_flujo->set_text(i->get_actual_flow());
144 txt_view->get_buffer()->insert_at_cursor("Selecciono ");
145 txt_view->get_buffer()->insert_at_cursor(i->get_name());
146 txt_view->get_buffer()->insert_at_cursor("\n");
149 void Principal::on_conexion_connected()
151 txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
152 ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
154 // Pido la planta por defecto
155 PlaQui::Server::Command c("plant", "get");
156 c.add_arg("default");
160 void Principal::on_conexion_frame(const std::string &frame)
162 std::cout << "FRAME" << std::endl;
163 read_status_xml(frame);
166 void Principal::on_conexion_finished()
168 txt_view->get_buffer()->insert_at_cursor("HANG UP\n");
169 ico_conected->set( Gtk::Stock::NO , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
173 void Principal::on_conexion_ok(const std::string &body)
175 /* lo paso a la carga del XML */
176 /* verifico que body este completo */
177 if ((body.find("</planta>")>0) && (body.find("<planta>")>0)) {
182 std::cout << body << std::endl;
183 txt_view->get_buffer()->insert_at_cursor("<IN>\n");
184 txt_view->get_buffer()->insert_at_cursor(Glib::locale_to_utf8(body));
185 txt_view->get_buffer()->insert_at_cursor("</IN>\n");
189 void Principal::on_conexion_error(unsigned code)
195 txt_view->get_buffer()->insert_at_cursor("El server dice que hay error : ");
196 txt_view->get_buffer()->insert_at_cursor(s);
197 txt_view->get_buffer()->insert_at_cursor("\n");
200 void Principal::on_get_clicked()
202 if (conexion == NULL) {
203 txt_view->get_buffer()->insert_at_cursor("SIN CONEXION\n");
207 PlaQui::Server::Command command(txt_target->get_text(), txt_command->get_text());
208 command.add_arg( txt_args->get_text() );
209 txt_view->get_buffer()->insert_at_cursor("Enviando comando\n");
211 conexion->send(command);
214 txt_view->get_buffer()->insert_at_cursor("EXCEPTION EN conexion->send !!\n");
219 void Principal::loadXML()
222 if (is_xml_loaded) return;
224 /* Parseo de ejemplo de un XML desde archivo */
226 document = xmlParseMemory(xml_body.c_str(),xml_body.size());
227 if (document == NULL) {
228 std::cout << "EEERRRRRRROOOOOOOOOO" << std::endl;
231 is_xml_loaded = true;
232 /* bien, el archivo se parseo bien! */
233 xmlNodePtr nodo, items;
234 nodo = document->children;
236 if (strcmp((char *)nodo->name, "planta") == 0) {
237 items = nodo->children;
238 while (items != NULL) {
239 if (items->type == XML_ELEMENT_NODE) {
240 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
242 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
244 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
246 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
248 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
250 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
252 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
261 // Ya cargado el XML, mando un msg para empezar a recibir los frames!
262 PlaQui::Server::Command c("transmission", "start");
263 c.add_arg("default");
264 c.add_arg(conexion->get_host());
269 void Principal::loadBomba(xmlNodePtr nodo)
271 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
272 int orientacion=0, x, y;
274 nodo = nodo->children;
275 while (nodo != NULL) {
276 if (nodo->type == XML_ELEMENT_NODE) {
277 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
278 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
279 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
280 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
281 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
282 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
288 // listo, ya recolecte todos los datos, ahora creo el objeto!
289 ViewPump *b = new ViewPump(name, orientacion);
290 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
291 b->set_position(x,y);
292 work_place->put(*b, x, y);
294 // los agrego al hash
298 void Principal::loadCodo(xmlNodePtr nodo)
300 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
301 int orientacion=0, x, y;
303 nodo = nodo->children;
304 while (nodo != NULL) {
305 if (nodo->type == XML_ELEMENT_NODE) {
306 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
307 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
308 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
309 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
310 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
311 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
317 // listo, ya recolecte todos los datos, ahora creo el objeto!
318 ViewItem *b = new ViewCodo(name, orientacion);
319 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
320 b->set_position(x,y);
321 work_place->put(*b, x, y);
323 // los agrego al hash
327 void Principal::loadConduct(xmlNodePtr nodo)
329 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
330 int orientacion=0, x, y;
332 nodo = nodo->children;
333 while (nodo != NULL) {
334 if (nodo->type == XML_ELEMENT_NODE) {
335 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
336 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
337 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
338 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
339 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
340 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
346 // listo, ya recolecte todos los datos, ahora creo el objeto!
347 ViewConduct *b = new ViewConduct(name, orientacion);
348 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
349 b->set_position(x,y);
350 work_place->put(*b, x, y);
352 // los agrego al hash
356 void Principal::loadExclusa(xmlNodePtr nodo)
358 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
359 int orientacion=0, x, y;
361 nodo = nodo->children;
362 while (nodo != NULL) {
363 if (nodo->type == XML_ELEMENT_NODE) {
364 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
365 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
366 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
367 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
368 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
369 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
375 // listo, ya recolecte todos los datos, ahora creo el objeto!
376 ViewExclusa *b = new ViewExclusa(name, orientacion);
377 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
378 b->set_position(x,y);
379 work_place->put(*b, x, y);
381 // los agrego al hash
385 void Principal::loadTank(xmlNodePtr nodo)
387 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
388 int orientacion=0, x, y;
390 nodo = nodo->children;
391 while (nodo != NULL) {
392 if (nodo->type == XML_ELEMENT_NODE) {
393 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
394 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
395 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
396 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
397 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
398 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
404 // listo, ya recolecte todos los datos, ahora creo el objeto!
405 ViewTank *b = new ViewTank(name, orientacion);
406 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
407 b->set_position(x,y);
408 work_place->put(*b, x, y);
410 // los agrego al hash
414 void Principal::loadUnion(xmlNodePtr nodo)
416 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
417 int orientacion=0, x, y;
419 nodo = nodo->children;
420 while (nodo != NULL) {
421 if (nodo->type == XML_ELEMENT_NODE) {
422 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
423 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
424 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
425 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
426 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
427 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
433 // listo, ya recolecte todos los datos, ahora creo el objeto!
434 ViewUnion *b = new ViewUnion(name, orientacion);
435 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
436 b->set_position(x,y);
437 work_place->put(*b, x, y);
439 // los agrego al hash
443 void Principal::loadDrain(xmlNodePtr nodo)
445 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
446 int orientacion=0, x, y;
448 nodo = nodo->children;
449 while (nodo != NULL) {
450 if (nodo->type == XML_ELEMENT_NODE) {
451 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
452 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
453 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
454 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
455 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
456 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
462 // listo, ya recolecte todos los datos, ahora creo el objeto!
463 ViewDrain *b = new ViewDrain(name, orientacion);
464 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
465 b->set_position(x,y);
466 work_place->put(*b, x, y);
468 // los agrego al hash
472 void Principal::read_status_xml(const std::string &frame)
474 std::string item_name;
476 document = xmlParseMemory(frame.c_str(),frame.size());
477 if (document == NULL) {
478 std::cout << "read_status_xml::no se creo documento" << std::endl;
482 xmlNodePtr nodo, items, props;
483 nodo = document->children;
486 if (strcmp((char *)nodo->name, "plantstatus") == 0) {
487 std::cout << "LEGO EL XML!" << std::endl;
488 items = nodo->children;
489 while (items != NULL) {
491 if (items->type == XML_ELEMENT_NODE) {
492 if (xmlStrcmp(items->name, BAD_CAST"conduct")==0) {
493 tmp = get_float_from_xml(items->children);
494 item_name = (char *)xmlGetProp(items, BAD_CAST"name");
495 std::cout << "CONDUCT :: " << item_name << " -> " << tmp << std::endl;
496 mapItems[item_name]->set_actual_flow(tmp);
504 float Principal::get_float_from_xml(xmlNodePtr nodo)
507 while (nodo != NULL) {
508 if (nodo->type == XML_ELEMENT_NODE) {
509 if (xmlStrcmp(nodo->name, BAD_CAST"actual_flow")==0) {
510 tmp = atof( (char *)XML_GET_CONTENT(nodo->children) );
516 std::cout << "get_float == " << tmp << std::endl;