ServicioController.cs 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using WebApplication3.Models;
  9. using WebApplication3.Clases;
  10. namespace WebApplication3.Controllers
  11. {
  12. [Route("api/servicio")]
  13. [ApiController]
  14. public class ServicioController : ControllerBase
  15. {
  16. private readonly AquiferContext _context;
  17. public ServicioController(AquiferContext context)
  18. {
  19. _context = context;
  20. }
  21. // GET: api/Servicio
  22. [HttpGet]
  23. public async Task<ActionResult<IEnumerable<Servicio>>> GetServicios()
  24. {
  25. return await _context.Servicios.ToListAsync();
  26. }
  27. // GET: api/Servicio/5
  28. [HttpGet("{id}")]
  29. public async Task<ActionResult<Servicio>> GetServicio(uint id)
  30. {
  31. var servicio = await _context.Servicios.FindAsync(id);
  32. if (servicio == null)
  33. {
  34. return NotFound();
  35. }
  36. return servicio;
  37. }
  38. // PUT: api/Servicio/5
  39. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  40. [HttpPut("{id}")]
  41. public async Task<IActionResult> PutServicio(uint id, Servicio servicio)
  42. {
  43. if (id != servicio.Id)
  44. {
  45. return BadRequest();
  46. }
  47. _context.Entry(servicio).State = EntityState.Modified;
  48. try
  49. {
  50. await _context.SaveChangesAsync();
  51. }
  52. catch (DbUpdateConcurrencyException)
  53. {
  54. if (!ServicioExists(id))
  55. {
  56. return NotFound();
  57. }
  58. else
  59. {
  60. throw;
  61. }
  62. }
  63. return NoContent();
  64. }
  65. // POST: api/Servicio
  66. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  67. [HttpPost]
  68. public async Task<ActionResult<Servicio>> PostServicio(Servicio servicio)
  69. {
  70. servicio.Id = GetLastId()+1;
  71. servicio.Terminado = 0;
  72. _context.Servicios.Add(servicio);
  73. try
  74. {
  75. await _context.SaveChangesAsync();
  76. }
  77. catch (DbUpdateException ex)
  78. {
  79. Logger.WriteLog(ex.InnerException.Message);
  80. }
  81. return CreatedAtAction("GetServicio", new { id = servicio.Id }, servicio);
  82. }
  83. // DELETE: api/Servicio/5
  84. [HttpDelete("{id}")]
  85. public async Task<IActionResult> DeleteServicio(uint id)
  86. {
  87. var servicio = await _context.Servicios.FindAsync(id);
  88. if (servicio == null)
  89. {
  90. return NotFound();
  91. }
  92. _context.Servicios.Remove(servicio);
  93. await _context.SaveChangesAsync();
  94. return NoContent();
  95. }
  96. private bool ServicioExists(uint id)
  97. {
  98. return _context.Servicios.Any(e => e.Id == id);
  99. }
  100. // GET: api/Servicio/simula
  101. [HttpGet("simularest")]
  102. public Task<ActionResult<Servicio>> SimulaRest()
  103. {
  104. string respuesta = "";
  105. Proceso proceso = Proceso.GetInstance();
  106. proceso.setParametersModflow(@"C:\Users\Admin\Desktop\TestExtract\simulaicionsimulacion_test", "AA.modflow.in");
  107. try
  108. {
  109. if (!proceso.Started)
  110. {
  111. proceso.Start();
  112. respuesta = "Id: " + proceso.GetPid();
  113. Servicio s = new Servicio();
  114. s.Id = GetLastId() + 1;
  115. s.Pid = (uint)proceso.GetPid();
  116. s.Terminado = 0;
  117. return PostServicio(s);
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. respuesta = ex.Message;
  123. throw;
  124. }
  125. return null;
  126. }
  127. [NonAction]
  128. public async Task<ActionResult<Servicio>> SimulaModflow(string workingDirectory, string arguments)
  129. {
  130. string respuesta = "";
  131. Proceso proceso = Proceso.GetInstance();
  132. Logger logger = Logger.GetInstance();
  133. proceso.ResetProcess();
  134. proceso.setParametersModflow(workingDirectory, arguments);
  135. try
  136. {
  137. if (!proceso.Started)
  138. {
  139. proceso.Start();
  140. respuesta = "Id: " + proceso.GetPid();
  141. Servicio s = new Servicio();
  142. s.Id = GetLastId() + 1;
  143. s.Pid = (uint)proceso.GetPid();
  144. s.Terminado = 0;
  145. s.Inicio = DateTime.Now.ToString();
  146. s.Tipo = "MODFLOW";
  147. Logger.WriteLog("MODFLOW " + s.Pid);
  148. var postServicio = await PostServicio(s);
  149. proceso.WaitForExit();
  150. return postServicio;
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. respuesta = ex.Message;
  156. throw;
  157. }
  158. return null;
  159. }
  160. [NonAction]
  161. public async Task<ActionResult<Servicio>> SimulaZoneBudget(string workingDirectory, string budgetFile, string zoneFile)
  162. {
  163. string respuesta = "";
  164. Logger logger = Logger.GetInstance();
  165. Logger.WriteLog("working directory: " + workingDirectory + " | budgetFile: " + budgetFile + " | zoneFile: " + zoneFile);
  166. Proceso proceso = Proceso.GetInstance();
  167. proceso.ResetProcess();
  168. proceso.setParametersZoneBudget(workingDirectory);
  169. try
  170. {
  171. if (!proceso.Started)
  172. {
  173. proceso.Start();
  174. respuesta = "Id: " + proceso.GetPid();
  175. Servicio s = new Servicio();
  176. s.Id = GetLastId() + 1;
  177. s.Pid = (uint)proceso.GetPid();
  178. s.Terminado = 0;
  179. s.Inicio = DateTime.Now.ToString();
  180. s.Tipo = "ZONEBUDGET";
  181. Logger.WriteLog("ZONEBUDGET "+s.Pid);
  182. var postServicio = await PostServicio(s);
  183. string[] prompts = { "outputZoneBudget.zblst", budgetFile, "test", zoneFile, "A" };
  184. proceso.ExecuteZonebudget(prompts);
  185. return postServicio;
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. respuesta = ex.Message;
  191. throw;
  192. }
  193. return null;
  194. }
  195. [NonAction]
  196. public async Task<ActionResult<Servicio>> GetIsolinesWithPython(string workingDirectory,string headFile, int rows, int cols)
  197. {
  198. Logger logger = Logger.GetInstance();
  199. Proceso proceso = Proceso.GetInstance();
  200. int isolines = 30;
  201. proceso.ResetProcess();
  202. proceso.setParametersPython(workingDirectory,headFile,rows,cols,isolines);
  203. proceso.Start();
  204. Servicio s = new Servicio();
  205. s.Id = GetLastId() + 1;
  206. s.Pid = (uint)proceso.GetPid();
  207. s.Terminado = 0;
  208. s.Inicio = DateTime.Now.ToString();
  209. s.Tipo = "PYTHON";
  210. var postServicio = await PostServicio (s); ;
  211. proceso.WaitForExit();
  212. return postServicio;
  213. }
  214. // GET: api/Servicio/stopped
  215. [HttpGet("stopped")]
  216. public bool HasStopped()
  217. {
  218. Proceso proceso = Proceso.GetInstance();
  219. return proceso.HasExited();
  220. }
  221. // GET: api/Servicio/detected
  222. [HttpGet("detected")]
  223. public bool HasDetected()
  224. {
  225. Proceso proceso = Proceso.GetInstance();
  226. return proceso.Detected;
  227. }
  228. [HttpGet("lastid")]
  229. public uint GetLastId()
  230. {
  231. if(_context.Servicios.Count() == 0)
  232. {
  233. return 0;
  234. }
  235. Servicio last = _context.Servicios.OrderByDescending(s => s.Id).First();
  236. if (last != null)
  237. {
  238. return last.Id;
  239. }
  240. else
  241. {
  242. return 0;
  243. }
  244. }
  245. [NonAction]
  246. public async Task<int> setTerminado(int pid)
  247. {
  248. Servicio servicio = _context.Servicios.FromSqlRaw("SELECT * FROM servicio WHERE pid = {0} ORDER BY id DESC", pid).First();
  249. servicio.Terminado = 1;
  250. servicio.Fin = DateTime.Now.ToString();
  251. _context.Entry(servicio).State = EntityState.Modified;
  252. int result = 0;
  253. try
  254. {
  255. result = await _context.SaveChangesAsync();
  256. }
  257. catch (DbUpdateConcurrencyException)
  258. {
  259. }
  260. Logger.WriteLog("TERMINADO " + pid);
  261. return result;
  262. }
  263. }
  264. }