void ModbusService::render_GET(const http_request& req, http_response** res) { //api/modbus/holdings/{connectorname}/{devaddress}/{baseaddress}/{count} string connectorName = req.get_arg("connectorname"); string strDevAddress = req.get_arg("devaddress"); string strBaseAddress = req.get_arg("baseaddress"); string strCount = req.get_arg("value"); int devAddress = Converter::stoi(strDevAddress); int baseAddress = Converter::stoi(strBaseAddress); int count = Converter::stoi(strCount); Document document; StringBuffer buffer; PrettyWriter<StringBuffer> wr(buffer); Value jsonvalue; IModbus* modbus = mm->Find(connectorName); if (modbus!=NULL) { uint16_t* target = new uint16_t[count]; if (modbus->getHoldings(devAddress,baseAddress,count,target)) { document.SetArray(); for(int i=0;i<count;i++) { Value jsonvalue; jsonvalue.SetUint(target[i]); document.PushBack(jsonvalue, document.GetAllocator()); } } else { document.SetObject(); jsonvalue.SetObject(); jsonvalue.SetString("Error",document.GetAllocator()); document.AddMember("Result",jsonvalue,document.GetAllocator()); jsonvalue.SetString("Modbus getHoldings failed",document.GetAllocator()); document.AddMember("Message",jsonvalue,document.GetAllocator()); } delete[] target; target = NULL; } else { document.SetObject(); jsonvalue.SetString("Error",document.GetAllocator()); document.AddMember("Result",jsonvalue,document.GetAllocator()); jsonvalue.SetString("Connector not found",document.GetAllocator()); document.AddMember("Message",jsonvalue,document.GetAllocator()); } document.Accept(wr); std::string json = buffer.GetString(); *res = new http_response(http_response_builder(json, 200,"application/json").string_response()); }
void FoldersService::render_DELETE(const http_request& req, http_response** res) { if (req.get_user()=="a" && req.get_pass()=="a") { string strid = req.get_arg("id"); CUUID id = CUUID::Parse(strid); HisDevFolder* folder = dynamic_cast<HisDevFolder*>(root->GetFolder()->Find(id)); if (folder!=NULL) { HisDevFolder* parentfolder = dynamic_cast<HisDevFolder*>(folder->GetParent()); parentfolder->Remove(id); delete(folder); root->Save(); *res = new http_string_response("", 200, "application/json"); return; } } else { string message = "Autentication error"; *res = new http_string_response(message.c_str(), 401, "application/json"); return; } *res = new http_string_response("", 403, "application/json"); }
void ModbusService::render_PUT(const http_request& req, http_response** res) { string connectorName = req.get_arg("connectorname"); string strDevAddress = req.get_arg("devaddress"); string strBaseAddress = req.get_arg("baseaddress"); string strValue = req.get_arg("value"); int devAddress = Converter::stoi(strDevAddress); int baseAddress = Converter::stoi(strBaseAddress); int value = Converter::stoi(strValue); Document document; StringBuffer buffer; PrettyWriter<StringBuffer> wr(buffer); document.SetObject(); Value jsonvalue; IModbus* modbus = mm->Find(connectorName); if (modbus!=NULL) { if (modbus->setHolding(devAddress,baseAddress,value)) { jsonvalue.SetString("OK",document.GetAllocator()); document.AddMember("Result",jsonvalue,document.GetAllocator()); } else { jsonvalue.SetString("Error",document.GetAllocator()); document.AddMember("Result",jsonvalue,document.GetAllocator()); jsonvalue.SetString("Modbus setHolding failed",document.GetAllocator()); document.AddMember("Message",jsonvalue,document.GetAllocator()); } } else { jsonvalue.SetString("Error",document.GetAllocator()); document.AddMember("Result",jsonvalue,document.GetAllocator()); jsonvalue.SetString("Connector not found",document.GetAllocator()); document.AddMember("Message",jsonvalue,document.GetAllocator()); } document.Accept(wr); std::string json = buffer.GetString(); *res = new http_response(http_response_builder(json, 200,"application/json").string_response()); }
//using the render method you are able to catch each type of request you receive void cat_resource::render(const http_request& req, http_response** res) { string file = req.get_arg("file"); cout << "file: " << file << endl; //it is possible to send a response initializing an http_string_response //that reads the content to send in response from a string. *res = new http_response(http_response_builder(file, 200).file_response()); }
//using the render method you are able to catch each type of request you receive void hello_world_resource::render(const http_request& req, http_response** res) { //it is possible to store data inside the resource object that can be altered //through the requests std::cout << "Data was: " << data << std::endl; std::string datapar = req.get_arg("data"); set_some_data(datapar == "" ? "no data passed!!!" : datapar); std::cout << "Now data is:" << data << std::endl; //it is possible to send a response initializing an http_string_response //that reads the content to send in response from a string. *res = new http_response(http_response_builder("Hello World!!!", 200).string_response()); }
void FoldersService::render_PUT(const http_request& req, http_response** res) { if (req.get_user()=="a" && req.get_pass()=="a") { string strid = req.get_arg("id"); string content = req.get_content(); if (UpdateFolder(strid,content)) { *res = new http_string_response("", 200, "application/json"); return; } } else { string message = "Autentication error"; *res = new http_string_response(message.c_str(), 401, "application/json"); return; } *res = new http_string_response("", 403, "application/json"); }
void FoldersService::render_GET(const http_request& req, http_response** res) { Document respjsondoc; respjsondoc.SetArray(); string strid = req.get_arg("id"); HisDevFolder* folder = root->GetFolder(); string path = req.get_path(); if(!strid.empty()) { CUUID id = CUUID::Parse(strid); folder = dynamic_cast<HisDevFolder*>(folder->Find(id)); } if (path.find("/api/folders")!=string::npos) { if (folder!=NULL) { FoldersToJson(folder,respjsondoc); } } else if (path.find("/api/folder")!=string::npos) { if (folder!=NULL) { FolderToJson(folder,respjsondoc); } } StringBuffer buffer; PrettyWriter<StringBuffer> wr(buffer); respjsondoc.Accept(wr); std::string json = buffer.GetString(); *res = new http_string_response(json, 200, "application/json"); }
void render_POST(const http_request& req, http_response** res) { *res = new http_string_response( req.get_arg("arg1")+req.get_arg("arg2"), 200, "text/plain" ); }