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));
88 txt_view->get_buffer()->insert_at_cursor("no se puede correr conexion->run()!!!\n");
91 txt_view->get_buffer()->insert_at_cursor("YA ESTAS CONECTADO\n");
96 void Principal::on_mnu_file_exit()
101 void Principal::on_mnu_file_disconnect()
103 if (conexion == NULL) return;
105 PlaQui::Server::Command c("connection", "stop");
106 c.add_arg(conexion->get_host());
107 c.add_arg(conexion->get_port());
111 void Principal::on_mnu_file_connect()
113 dlg_conectar->show();
116 void Principal::on_mnu_help_about()
118 // preparo para leer el archivo ChangeLog
119 /* Glib::RefPtr<Gnome::Glade::Xml> xml;
121 Glib::RefPtr<Gtk::TextBuffer> log_buffer;
122 Glib::RefPtr<Glib::IOChannel> log_io;
124 log_buffer = Gtk::TextBuffer::create();
125 log_io = Glib::IOChannel::create_from_file("../ChangeLog", "r");
126 while (log_io->read_line(line) != Glib::IO_STATUS_EOF) {
127 log_buffer->insert_at_cursor(line);
131 xml = Gnome::Glade::Xml::create("client.glade", "dlgAbout");
133 catch(const Gnome::Glade::XmlError &ex) {
134 std::cerr << ex.what() << std::endl;
137 Gtk::Window *dlg = 0;
138 Gtk::Button *btn_cerrar = 0;
139 Gtk::TextView *txt_changelog = 0;
140 xml->get_widget("dlgAbout", dlg);
141 xml->get_widget("btn_close", btn_cerrar);
142 xml->get_widget("txt_changelog", txt_changelog);
143 btn_cerrar->signal_clicked().connect(SigC::slot(*dlg, &Gtk::Dialog::hide));
144 txt_changelog->set_buffer(log_buffer);
148 bool Principal::on_item_clicked(GdkEventButton *e, ViewItem *i)
150 lbl_nombre->set_text(i->get_name());
151 lbl_flujo->set_text(i->get_actual_flow());
152 txt_view->get_buffer()->insert_at_cursor("Selecciono ");
153 txt_view->get_buffer()->insert_at_cursor(i->get_name());
154 txt_view->get_buffer()->insert_at_cursor("\n");
157 void Principal::on_conexion_connected()
159 txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
160 ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
162 // Pido la planta por defecto
163 PlaQui::Server::Command c("plant", "get");
164 c.add_arg("default");
168 void Principal::on_conexion_frame(const std::string &frame)
170 read_status_xml(frame);
173 void Principal::on_conexion_finished()
175 txt_view->get_buffer()->insert_at_cursor("HANG UP\n");
176 ico_conected->set( Gtk::Stock::NO , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
180 void Principal::on_conexion_ok(const std::string &body)
182 /* lo paso a la carga del XML */
183 /* verifico que body este completo */
184 if ((body.find("</planta>")>0) && (body.find("<planta>")>0)) {
188 // Ya cargado el XML, mando un msg para empezar a recibir los frames!
189 PlaQui::Server::Command c("transmission", "start");
190 c.add_arg("default");
195 std::cout << body << std::endl;
196 txt_view->get_buffer()->insert_at_cursor("<IN>\n");
197 txt_view->get_buffer()->insert_at_cursor(Glib::locale_to_utf8(body));
198 txt_view->get_buffer()->insert_at_cursor("</IN>\n");
202 void Principal::on_conexion_error(unsigned code)
208 txt_view->get_buffer()->insert_at_cursor("El server dice que hay error : ");
209 txt_view->get_buffer()->insert_at_cursor(s);
210 txt_view->get_buffer()->insert_at_cursor("\n");
214 void Principal::on_get_clicked()
216 if (conexion == NULL) {
217 txt_view->get_buffer()->insert_at_cursor("SIN CONEXION\n");
222 PlaQui::Server::Command command(txt_target->get_text(), txt_command->get_text());
223 command.add_arg( txt_args->get_text() );
224 txt_view->get_buffer()->insert_at_cursor("Enviando comando\n");
226 conexion->send(command);
229 txt_view->get_buffer()->insert_at_cursor("EXCEPTION EN conexion->send !!\n");
234 void Principal::loadXML()
237 if (is_xml_loaded) return;
239 /* Parseo de ejemplo de un XML desde archivo */
241 document = xmlParseMemory(xml_body.c_str(),xml_body.size());
242 if (document == NULL) {
243 std::cout << "EEERRRRRRROOOOOOOOOO" << std::endl;
246 is_xml_loaded = true;
247 /* bien, el archivo se parseo bien! */
248 xmlNodePtr nodo, items;
249 nodo = document->children;
251 if (strcmp((char *)nodo->name, "planta") == 0) {
252 items = nodo->children;
253 while (items != NULL) {
254 if (items->type == XML_ELEMENT_NODE) {
255 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
257 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
259 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
261 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
263 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
265 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
267 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
278 void Principal::loadBomba(xmlNodePtr nodo)
280 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
281 int orientacion=0, x, y;
283 nodo = nodo->children;
284 while (nodo != NULL) {
285 if (nodo->type == XML_ELEMENT_NODE) {
286 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
287 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
288 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
289 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
290 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
291 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
297 // listo, ya recolecte todos los datos, ahora creo el objeto!
298 ViewPump *b = new ViewPump(name, orientacion);
299 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
300 b->set_position(x,y);
301 work_place->put(*b, x, y);
303 // los agrego al hash
307 void Principal::loadCodo(xmlNodePtr nodo)
309 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
310 int orientacion=0, x, y;
312 nodo = nodo->children;
313 while (nodo != NULL) {
314 if (nodo->type == XML_ELEMENT_NODE) {
315 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
316 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
317 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
318 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
319 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
320 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
326 // listo, ya recolecte todos los datos, ahora creo el objeto!
327 ViewItem *b = new ViewCodo(name, orientacion);
328 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
329 b->set_position(x,y);
330 work_place->put(*b, x, y);
332 // los agrego al hash
336 void Principal::loadConduct(xmlNodePtr nodo)
338 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
339 int orientacion=0, x, y;
341 nodo = nodo->children;
342 while (nodo != NULL) {
343 if (nodo->type == XML_ELEMENT_NODE) {
344 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
345 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
346 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
347 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
348 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
349 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
355 // listo, ya recolecte todos los datos, ahora creo el objeto!
356 ViewConduct *b = new ViewConduct(name, orientacion);
357 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
358 b->set_position(x,y);
359 work_place->put(*b, x, y);
361 // los agrego al hash
365 void Principal::loadExclusa(xmlNodePtr nodo)
367 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
368 int orientacion=0, x, y;
370 nodo = nodo->children;
371 while (nodo != NULL) {
372 if (nodo->type == XML_ELEMENT_NODE) {
373 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
374 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
375 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
376 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
377 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
378 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
384 // listo, ya recolecte todos los datos, ahora creo el objeto!
385 ViewExclusa *b = new ViewExclusa(name, orientacion);
386 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
387 b->set_position(x,y);
388 work_place->put(*b, x, y);
390 // los agrego al hash
394 void Principal::loadTank(xmlNodePtr nodo)
396 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
397 int orientacion=0, x, y;
399 nodo = nodo->children;
400 while (nodo != NULL) {
401 if (nodo->type == XML_ELEMENT_NODE) {
402 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
403 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
404 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
405 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
406 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
407 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
413 // listo, ya recolecte todos los datos, ahora creo el objeto!
414 ViewTank *b = new ViewTank(name, orientacion);
415 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
416 b->set_position(x,y);
417 work_place->put(*b, x, y);
419 // los agrego al hash
423 void Principal::loadUnion(xmlNodePtr nodo)
425 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
426 int orientacion=0, x, y;
428 nodo = nodo->children;
429 while (nodo != NULL) {
430 if (nodo->type == XML_ELEMENT_NODE) {
431 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
432 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
433 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
434 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
435 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
436 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
442 // listo, ya recolecte todos los datos, ahora creo el objeto!
443 ViewUnion *b = new ViewUnion(name, orientacion);
444 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
445 b->set_position(x,y);
446 work_place->put(*b, x, y);
448 // los agrego al hash
452 void Principal::loadDrain(xmlNodePtr nodo)
454 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
455 int orientacion=0, x, y;
457 nodo = nodo->children;
458 while (nodo != NULL) {
459 if (nodo->type == XML_ELEMENT_NODE) {
460 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
461 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
462 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
463 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
464 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
465 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
471 // listo, ya recolecte todos los datos, ahora creo el objeto!
472 ViewDrain *b = new ViewDrain(name, orientacion);
473 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
474 b->set_position(x,y);
475 work_place->put(*b, x, y);
477 // los agrego al hash
481 void Principal::read_status_xml(const std::string &frame)
483 std::string item_name;
485 document = xmlParseMemory(frame.c_str(),frame.size());
486 if (document == NULL) {
487 std::cout << "read_status_xml::no se creo documento" << std::endl;
491 xmlNodePtr nodo, items, props;
492 nodo = document->children;
495 if (strcmp((char *)nodo->name, "plantastatus") == 0) {
496 items = nodo->children;
497 while (items != NULL) {
499 if (items->type == XML_ELEMENT_NODE) {
500 if (xmlStrcmp(items->name, BAD_CAST"conduct")==0) {
501 tmp = get_float_from_xml(items->children);
502 item_name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
503 mapItems[item_name]->set_actual_flow(tmp);
511 float Principal::get_float_from_xml(xmlNodePtr nodo)
514 while (nodo != NULL) {
515 if (nodo->type == XML_ELEMENT_NODE) {
516 if (xmlStrcmp(nodo->name, BAD_CAST"actual_flow")==0) {
517 tmp = atof( (char *)XML_GET_CONTENT(nodo->children) );