+ xml << "</list>" << endl;
+ return new HTTPResponse(HTTPMessage::OK, xml.str());
+}
+
+HTTPResponse* Server::cmd_plant_get(const Command& command) {
+ if (!command.get_args().size()) {
+ return new HTTPResponse(HTTPMessage::CONFLICT,
+ "<response desc=\"Faltan argumentos.\" />");
+ }
+ Glib::Mutex::Lock lock(plants_mutex);
+ string plant = command.get_args()[0];
+ if (plants.find(plant) == plants.end()) {
+ return new HTTPResponse(HTTPMessage::NOT_FOUND,
+ string("<response desc=\"No existe la planta ") + plant + "\" />");
+ }
+ // TODO try/catch?
+ string xml = plants[plant]->get_xml();
+ if (xml.length()) {
+ return new HTTPResponse(HTTPMessage::OK, xml);
+ } else {
+ return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
+ ("<response desc=\"No se pudo obtener el XML de la planta ") + plant + "\" />");
+ }
+}
+
+HTTPResponse* Server::cmd_plant_set(const Command& command) {
+ const Command::Arguments& args = command.get_args();
+ if (args.size() < 4) {
+ return new HTTPResponse(HTTPMessage::CONFLICT,
+ "<response desc=\"Faltan argumentos.\" />");
+ }
+ string plant = args[0];
+ string element = args[1];
+ string key = args[2];
+ if (key != "open") {
+ return new HTTPResponse(HTTPMessage::NOT_FOUND,
+ string("<response desc=\"La clave '") + key + "' es inválida.\" />");
+ }
+ string value = args[3];
+ Glib::Mutex::Lock lock(plants_mutex);
+ PlantList::iterator p = plants.find(plant);
+ if (p == plants.end()) {
+ return new HTTPResponse(HTTPMessage::NOT_FOUND,
+ string("<response desc=\"No existe la planta '") + plant + "'.\" />");
+ }
+ bool open = true;
+ if ((value == "false") || (value == "0") || (value == "off")
+ || (value == "no")) {
+ open = false;
+ }
+ if (!plants[plant]->set_open(element, open)) {
+ return new HTTPResponse(HTTPMessage::CONFLICT,
+ string("<response desc=\"No se pudo cambiar el estado del elemento '") + element + "'.\" />");
+ }
+ return new HTTPResponse(HTTPMessage::OK,
+ string("<response desc=\"Se cambió el estado del elemento '") + element + "'.\" />");
+}
+
+HTTPResponse* Server::cmd_plant_set_frequency(const Command& command) {
+ if (command.get_args().size() < 2) {
+ return new HTTPResponse(HTTPMessage::CONFLICT,
+ "<response desc=\"Faltan argumentos.\" />");
+ }
+ Glib::Mutex::Lock lock(plants_mutex);
+ const string name = command.get_args()[0];
+ if (plants.find(name) == plants.end()) {
+ return new HTTPResponse(HTTPMessage::NOT_FOUND,
+ string("<response desc=\"No existe la planta ") + name + "\" />");
+ }
+ unsigned hz;
+ to(command.get_args()[1], hz);
+ plants[name]->set_frequency(hz);
+ return new HTTPResponse(HTTPMessage::OK,
+ string("<response desc=\"La planta '") + name + "' fue pausada.\" />");
+}
+
+HTTPResponse* Server::cmd_plant_start(const Command& command) {
+ if (!command.get_args().size()) {
+ return new HTTPResponse(HTTPMessage::CONFLICT,
+ "<response desc=\"Faltan argumentos.\" />");
+ }
+ Glib::Mutex::Lock lock(plants_mutex);
+ const string name = command.get_args()[0];
+ if (plants.find(name) == plants.end()) {
+ return new HTTPResponse(HTTPMessage::NOT_FOUND,
+ string("<response desc=\"No existe la planta ") + name + "\" />");
+ }
+ plants[name]->set_paused(false);
+ return new HTTPResponse(HTTPMessage::OK,
+ string("<response desc=\"La planta '") + name + "' fue reanudada.\" />");