ServicioController.cs 9.9KB

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