Advertisement
Rado9635

budynek

Nov 22nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.55 KB | None | 0 0
  1. #include "Service.h"
  2. #include <string>
  3. #include "cpprest/http_client.h"
  4.  
  5.  
  6. using std::string;
  7. using std::to_string;
  8.  
  9. void Service::initRestOpHandlers() {
  10. _listener.support(methods::GET,std::bind(&Service::handleGet,this,std::placeholders::_1));
  11. //_listener.support(methods::PUT,std::bind(&Service::handlePut,this,std::placeholders::_1));
  12. }
  13.  
  14.  
  15.  
  16. void Service::handleGet(http_request message) {
  17. vector<string> path = requestPath(message);
  18.  
  19. if(path.empty()) {
  20. message.reply(status_codes::BadRequest);
  21. }
  22. else {
  23.  
  24.  
  25.  
  26. if (path[0]=="T_pcob") {
  27.  
  28. //zapytanie o dzien tygodnia i czas symulacji
  29. web::http::client::http_client hour_time(U("https://closingtime.szyszki.de/api/details"));
  30. hour_time.request(methods::GET).then([=](http_response response2)
  31. {
  32. if(response2.status_code() == status_codes::OK)
  33. {
  34. json::value jsonFromAPI_hour = response2.extract_json().get();
  35. //std::cout << jsonFromAPI_hour<<std::endl;
  36. dayOfWeek = jsonFromAPI_hour[U("dayOfWeek")].as_number().to_int32();
  37. symSecs = jsonFromAPI_hour[U("symSec")].as_number().to_int32();
  38.  
  39. //std::cout << dayOfWeek<<symSecs<<std::endl;
  40. }
  41. });
  42.  
  43. //Pytanie na server o godzine
  44. web::http::client::http_client hour(U("https://closingtime.szyszki.de/api/give_me_hour_my_lord"));
  45. hour.request(methods::GET).then([=](http_response response3)
  46. {
  47. if(response3.status_code() == status_codes::OK)
  48. {
  49. json::value jsonFromAPI_hour = response3.extract_json().get();
  50. // std::cout << jsonFromAPI_hour<<std::endl;
  51.  
  52. symTimeHour = jsonFromAPI_hour[U("symTimeHour")].as_number().to_int32();
  53. //std::cout << symTimeHour<<std::endl;
  54. }
  55. });
  56.  
  57.  
  58.  
  59.  
  60. //zapytanie do wymiennika o Tzco
  61. web::http::client::http_client wymiennik(U("https://selfcontrol.szyszki.de/controller/details"));
  62. wymiennik.request(methods::GET).then([=](http_response response)
  63. {
  64. if(response.status_code() == status_codes::OK)
  65. {
  66. json::value jsonFromAPI = response.extract_json().get();
  67. //std::cout << jsonFromAPI<<std::endl;
  68.  
  69.  
  70. Tzco = jsonFromAPI[U("actual_temp")].as_string();
  71.  
  72.  
  73. }
  74. });
  75.  
  76.  
  77.  
  78. //zapytanie do dostawcy o temperature otoczeni
  79. web::http::client::http_client dostawca(U("https://ivegotthepower.szyszki.de/mpec/data"));
  80. dostawca.request(methods::GET).then([=](http_response response)
  81. {
  82.  
  83. if(response.status_code() == status_codes::OK)
  84. {
  85.  
  86. json::value jsonFromAPI = response.extract_json().get();
  87. //std::cout << jsonFromAPI<<std::endl;
  88.  
  89. AirTemp = jsonFromAPI[U("AirTemp")].as_string();
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. std::cout<<symSecs<<std::endl;
  97. std::cout<<dayOfWeek<<std::endl;
  98. std::cout<<symTimeHour<<std::endl;
  99. std::cout<<Tzco<<std::endl;
  100. std::cout<<AirTemp<<std::endl;
  101.  
  102. // wyznaczenie temperatuty zadanej
  103.  
  104. if (dayOfWeek!=0 and dayOfWeek!=6 and symTimeHour>=7 and symTimeHour<22){
  105. T_zadana = 20;
  106.  
  107. }
  108. else{
  109. T_zadana = 15;
  110. }
  111.  
  112.  
  113. //int T_pcob = 12;
  114.  
  115. // obliczenia
  116.  
  117. rtU.T_zCo=std::stod(Tzco);
  118. rtU.T_otoczenia=std::stod(AirTemp); // '<Root>/T_otoczenia'
  119. rtU.F_cob=std::stod(F_cob);
  120. rtU.T_zadana=T_zadana;
  121.  
  122. this->step();
  123.  
  124. std::cout<<rtY.T_pco<<std::endl;
  125. std::cout<<rtY.T_ref<<std::endl;
  126. std::cout<<rtY.T_r<<std::endl;
  127. std::cout<<rtY.Sterowanie<<std::endl;
  128.  
  129. string log_Tpco = std::to_string(rtY.T_pco);
  130. string log_Tref = std::to_string(rtY.T_ref);
  131. string log_Tr = std::to_string(rtY.T_r);
  132. string log_Sterowanie = std::to_string(rtY.Sterowanie);
  133. string log_timestamp = std::to_string(symSecs);
  134. //odesłanie Tpcob na zapytanie GET
  135.  
  136. web::json::value jsonTpcob;
  137. jsonTpcob["T_pcob"] = json::value::string(log_Tpco);
  138. message.reply(status_codes::OK,jsonTpcob);
  139.  
  140.  
  141.  
  142.  
  143. //wysłanie logów do bazy
  144.  
  145. web::json::value json_v ;
  146. web::json::value json_return;
  147.  
  148. json_v["status"] = web::json::value::string("running");
  149. json_v["tag_name"] = web::json::value::string("Radek");
  150. json_v["water_intake_Fcob"]= web::json::value::string(log_Sterowanie);
  151. json_v["return_water_temp_Tpcob"] = web::json::value::string(log_Tpco);
  152. json_v["room_temp_Tr"] = web::json::value::string(log_Tr);
  153. json_v["timestamp"] = web::json::value::string(log_timestamp);
  154. //https://jsonplaceholder.typicode.com/posts
  155.  
  156. //https://anoldlogcabinforsale.szyszki.de/building/log
  157. web::http::client::http_client Dominika(U("https://anoldlogcabinforsale.szyszki.de/building/log"));
  158. Dominika.request(web::http::methods::POST, U("/"), json_v)
  159. .then([=](const web::http::http_response& response1) {
  160. return response1.extract_json();
  161.  
  162.  
  163. })
  164. .then([&json_return](const pplx::task<web::json::value>& task) {
  165. try {
  166.  
  167. web::json::value json_return = task.get();
  168. std::cout << json_return<<std::endl;
  169.  
  170. }
  171. catch (const web::http::http_exception& e) {
  172. std::cout << "error " << e.what() << std::endl;
  173. }
  174. })
  175. .wait();
  176.  
  177. std::cout << json_return.serialize() << std::endl;
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. }
  186.  
  187. }
  188.  
  189. );
  190. }
  191.  
  192. else {
  193. message.reply(status_codes::BadRequest);
  194. }
  195. }
  196.  
  197. }
  198.  
  199. void Service::step()
  200. {
  201. double rtb_T_Tr; //
  202. double rtb_Gain3; // double to double
  203. double rtb_Gain1; //
  204. double rtb_T_PCO; //
  205. double rtb_IntegralGain; //
  206. double rtb_Integrator; //
  207. double rtb_Filter; //
  208. double rtb_Gain4; //
  209.  
  210.  
  211. // DiscreteIntegrator: '<S1>/Discrete-Time Integrator'
  212. rtb_T_Tr = rtDW.DiscreteTimeIntegrator_DSTATE;
  213.  
  214. // Sum: '<Root>/Sum1' incorporates:
  215. // Inport: '<Root>/T_zadana'
  216.  
  217. rtb_Gain3 = rtU.T_zadana - rtb_T_Tr;
  218.  
  219. // Gain: '<S3>/Proportional Gain'
  220. rtb_Gain1 = rtCP_ProportionalGain_Gain * rtb_Gain3;
  221.  
  222. // Gain: '<S3>/Derivative Gain'
  223. rtb_T_PCO = rtCP_DerivativeGain_Gain * rtb_Gain3;
  224.  
  225.  
  226. // Gain: '<S3>/Integral Gain'
  227. rtb_IntegralGain = rtCP_IntegralGain_Gain * rtb_Gain3;
  228.  
  229.  
  230. // Outport: '<Root>/T_r'
  231. rtY.T_r = rtb_T_Tr;
  232.  
  233.  
  234. // Sum: '<S1>/Sum1' incorporates:
  235. // Inport: '<Root>/T_otoczenia'
  236.  
  237.  
  238. rtb_Gain3 = rtb_T_Tr - rtU.T_otoczenia;
  239.  
  240.  
  241. // Gain: '<S1>/Gain1'
  242. rtb_Gain3 *= rtCP_Gain1_Gain;
  243.  
  244.  
  245. // Gain: '<S1>/Gain'
  246. rtb_Gain3 *= rtCP_pooled2;
  247.  
  248.  
  249. // DiscreteIntegrator: '<S3>/Integrator'
  250. rtb_Integrator = rtDW.Integrator_DSTATE;
  251.  
  252. // DiscreteIntegrator: '<S3>/Filter'
  253. rtb_Filter = rtDW.Filter_DSTATE;
  254.  
  255. // Sum: '<S3>/SumD'
  256. rtb_T_PCO -= rtb_Filter;
  257.  
  258. // Gain: '<S3>/Filter Coefficient'
  259. rtb_Filter = rtCP_FilterCoefficient_Gain * rtb_T_PCO;
  260.  
  261. // Sum: '<S3>/Sum'
  262. rtb_Gain1 = rtb_Gain1 + rtb_Integrator + rtb_Filter;
  263.  
  264. // Saturate: '<S3>/Saturate'
  265. rtb_T_PCO = T_PCO_initial_value;
  266.  
  267. rtb_Integrator = rtCP_pooled1;
  268. if (rtb_Gain1 > rtb_Integrator) {
  269. rtb_Gain1 = rtb_Integrator;
  270. } else {
  271. if (rtb_Gain1 < rtb_T_PCO) {
  272. rtb_Gain1 = rtb_T_PCO;
  273. }
  274. }
  275.  
  276. // End of Saturate: '<S3>/Saturate'
  277.  
  278. // Outport: '<Root>/Sterowanie'
  279. rtY.Sterowanie = rtb_Gain1;
  280.  
  281. // Product: '<Root>/Product' incorporates:
  282. // Inport: '<Root>/F_cob'
  283.  
  284. rtb_Gain1 *= rtU.F_cob;
  285.  
  286. // Gain: '<S2>/Gain1'
  287. rtb_Gain1 *= rtCP_Gain1_Gain_h;
  288.  
  289. // Outport: '<Root>/T_ref' incorporates:
  290. // Inport: '<Root>/T_zadana'
  291.  
  292. rtY.T_ref = rtU.T_zadana;
  293.  
  294. // DiscreteIntegrator: '<S2>/Discrete-Time Integrator'
  295. rtb_T_PCO = rtDW.DiscreteTimeIntegrator_DSTATE_n;
  296.  
  297. // Outport: '<Root>/T_pco '
  298. rtY.T_pco = rtb_T_PCO;
  299.  
  300. // Sum: '<S1>/Sum2'
  301. rtb_Gain4 = rtb_T_PCO - rtb_T_Tr;
  302.  
  303. // Gain: '<S1>/Gain2'
  304. rtb_Gain4 *= rtCP_pooled4;
  305.  
  306. // Gain: '<S1>/Gain3'
  307. rtb_Gain4 *= rtCP_pooled2;
  308.  
  309. // Sum: '<S1>/Sum'
  310. rtb_Integrator = rtb_Gain4 - rtb_Gain3;
  311.  
  312. // Sum: '<S2>/Sum2'
  313. rtb_Gain4 = rtb_T_PCO - rtb_T_Tr;
  314.  
  315. // Gain: '<S2>/Gain2'
  316. rtb_Gain4 *= rtCP_pooled4;
  317.  
  318. // Gain: '<S2>/Gain4'
  319. rtb_Gain4 *= rtCP_pooled5;
  320.  
  321. // Sum: '<S2>/Sum1' incorporates:
  322. // Inport: '<Root>/T_zCo'
  323.  
  324. rtb_Gain3 = rtU.T_zCo - rtb_T_PCO;
  325.  
  326. // Product: '<S2>/Product'
  327. rtb_Gain3 *= rtb_Gain1;
  328.  
  329. // Gain: '<S2>/Gain3'
  330. rtb_Gain3 *= rtCP_pooled5;
  331.  
  332. // Sum: '<S2>/Sum'
  333. rtb_T_Tr = rtb_Gain3 - rtb_Gain4;
  334.  
  335. // Update for DiscreteIntegrator: '<S1>/Discrete-Time Integrator'
  336. rtDW.DiscreteTimeIntegrator_DSTATE += rtCP_pooled1 * rtb_Integrator;
  337.  
  338. // Update for DiscreteIntegrator: '<S3>/Integrator'
  339. rtDW.Integrator_DSTATE += rtCP_pooled1 * rtb_IntegralGain;
  340.  
  341. // Update for DiscreteIntegrator: '<S3>/Filter'
  342. rtDW.Filter_DSTATE += rtCP_pooled1 * rtb_Filter;
  343.  
  344. // Update for DiscreteIntegrator: '<S2>/Discrete-Time Integrator'
  345. rtDW.DiscreteTimeIntegrator_DSTATE_n += rtCP_pooled1 * rtb_T_Tr;
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement