123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using WebApplication3.Models;
- using WebApplication3.Clases;
-
-
- namespace WebApplication3.Controllers
- {
- //[Route("api/servicio")]
- //[ApiController]
- [ApiExplorerSettings(IgnoreApi = true)]
- public class ServicioController : ControllerBase
- {
- private readonly AquiferContext _context;
-
- public ServicioController(AquiferContext context)
- {
- _context = context;
- }
-
- // GET: api/Servicio
- [HttpGet]
- public async Task<ActionResult<IEnumerable<Servicio>>> GetServicios()
- {
- return await _context.Servicios.ToListAsync();
- }
-
- // GET: api/Servicio/5
- [HttpGet("{id}")]
- public async Task<ActionResult<Servicio>> GetServicio(uint id)
- {
- var servicio = await _context.Servicios.FindAsync(id);
-
- if (servicio == null)
- {
- return NotFound();
- }
-
- return servicio;
- }
-
- // PUT: api/Servicio/5
- // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
- [HttpPut("{id}")]
- public async Task<IActionResult> PutServicio(uint id, Servicio servicio)
- {
- if (id != servicio.Id)
- {
- return BadRequest();
- }
-
- _context.Entry(servicio).State = EntityState.Modified;
-
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!ServicioExists(id))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
-
- return NoContent();
- }
-
- // POST: api/Servicio
- // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
- [HttpPost]
- public async Task<ActionResult<Servicio>> PostServicio(Servicio servicio)
- {
- servicio.Id = GetLastId()+1;
- servicio.Terminado = 0;
- _context.Servicios.Add(servicio);
- try
- {
- await _context.SaveChangesAsync();
-
- }
- catch (DbUpdateException ex)
- {
- Logger.WriteLog(ex.InnerException.Message);
- }
-
- return CreatedAtAction("GetServicio", new { id = servicio.Id }, servicio);
- }
-
- // DELETE: api/Servicio/5
- [HttpDelete("{id}")]
- public async Task<IActionResult> DeleteServicio(uint id)
- {
- var servicio = await _context.Servicios.FindAsync(id);
- if (servicio == null)
- {
- return NotFound();
- }
-
- _context.Servicios.Remove(servicio);
- await _context.SaveChangesAsync();
-
- return NoContent();
- }
-
- private bool ServicioExists(uint id)
- {
- return _context.Servicios.Any(e => e.Id == id);
- }
-
- // GET: api/Servicio/simula
- //[HttpGet("simularest")]
- //public Task<ActionResult<Servicio>> SimulaRest()
- //{
- // string respuesta = "";
-
- // Proceso proceso = Proceso.GetInstance();
- // proceso.setParametersModflow(@"C:\Users\Admin\Desktop\TestExtract\simulaicionsimulacion_test", "AA.modflow.in");
- // try
- // {
- // if (!proceso.Started)
- // {
- // proceso.Start();
- // respuesta = "Id: " + proceso.GetPid();
- // Servicio s = new Servicio();
- // s.Id = GetLastId() + 1;
- // s.Pid = (uint)proceso.GetPid();
- // s.Terminado = 0;
- // return PostServicio(s);
- // }
- // }
- // catch (Exception ex)
- // {
- // respuesta = ex.Message;
- // throw;
- // }
-
-
- // return null;
-
- //}
-
- [NonAction]
- public async Task<ActionResult<Servicio>> SimulaModflow(string workingDirectory, string arguments)
- {
- string respuesta = "";
-
- Proceso proceso = Proceso.GetInstance();
- proceso.ResetProcess();
- proceso.setParametersModflow(workingDirectory, arguments);
-
- try
- {
- if (!proceso.Started)
- {
- proceso.Start();
- respuesta = "Id: " + proceso.GetPid();
- Servicio s = new ();
- s.Id = GetLastId() + 1;
- s.Pid = (uint)proceso.GetPid();
- s.Terminado = 0;
- s.Inicio = DateTime.Now.ToString();
- s.Tipo = "MODFLOW";
- Logger.WriteLog("MODFLOW " + s.Pid);
- var postServicio = await PostServicio(s);
- proceso.WaitForExit();
- return postServicio;
- }
- }
- catch (Exception ex)
- {
- respuesta = ex.Message;
- throw;
- }
-
-
- return null;
-
- }
-
- [NonAction]
- public async Task<ActionResult<Servicio>> SimulaZoneBudget(string workingDirectory, string budgetFile, string zoneFile)
- {
- string respuesta = "";
-
- Logger.WriteLog("working directory: " + workingDirectory + " | budgetFile: " + budgetFile + " | zoneFile: " + zoneFile);
- Proceso proceso = Proceso.GetInstance();
- proceso.ResetProcess();
- proceso.setParametersZoneBudget(workingDirectory);
-
- try
- {
- if (!proceso.Started)
- {
- proceso.Start();
- respuesta = "Id: " + proceso.GetPid();
- Servicio s = new ();
- s.Id = GetLastId() + 1;
- s.Pid = (uint)proceso.GetPid();
- s.Terminado = 0;
- s.Inicio = DateTime.Now.ToString();
- s.Tipo = "ZONEBUDGET";
- Logger.WriteLog("ZONEBUDGET "+s.Pid);
- var postServicio = await PostServicio(s);
-
- string[] prompts = { "outputZoneBudget.zblst", budgetFile, "test", zoneFile, "A" };
-
- proceso.ExecuteZonebudget(prompts);
-
- return postServicio;
- }
- }
- catch (Exception ex)
- {
- respuesta = ex.Message;
- throw;
- }
-
- return null;
- }
-
- [NonAction]
- public async Task<ActionResult<Servicio>> GetIsolinesWithPython(string workingDirectory,string headFile, int rows, int cols)
- {
-
- Proceso proceso = Proceso.GetInstance();
- int isolines = 30;
- proceso.ResetProcess();
- proceso.setParametersPython(workingDirectory,headFile,rows,cols,isolines);
- proceso.Start();
- Servicio s = new ();
- s.Id = GetLastId() + 1;
- s.Pid = (uint)proceso.GetPid();
- s.Terminado = 0;
- s.Inicio = DateTime.Now.ToString();
- s.Tipo = "PYTHON";
- var postServicio = await PostServicio (s); ;
- proceso.WaitForExit();
- return postServicio;
- }
-
- [NonAction]
- public async Task<ActionResult<Servicio>> GetGeoJsonFromSVG(string workingDirectory, string headFile)
- {
- Proceso proceso = Proceso.GetInstance();
- proceso.ResetProcess();
- proceso.SetParametersSVG2Json(workingDirectory, headFile);
- proceso.Start();
- Servicio s = new ();
- s.Id = GetLastId() + 1;
- s.Pid = (uint)proceso.GetPid();
- s.Terminado = 0;
- s.Inicio = DateTime.Now.ToString();
- s.Tipo = "SVG2GEOJSON";
- var postServicio = await PostServicio(s); ;
- proceso.WaitForExit();
- return postServicio;
- }
-
- // GET: api/Servicio/stopped
- [HttpGet("stopped")]
- public bool HasStopped()
- {
- Proceso proceso = Proceso.GetInstance();
- return proceso.HasExited();
- }
-
- // GET: api/Servicio/detected
- [HttpGet("detected")]
- public bool HasDetected()
- {
- Proceso proceso = Proceso.GetInstance();
- return proceso.Detected;
- }
-
- [HttpGet("lastid")]
- public uint GetLastId()
- {
- if(!_context.Servicios.Any())
- {
- return 0;
- }
-
- Servicio last = _context.Servicios.OrderByDescending(s => s.Id).First();
-
- if (last != null)
- {
- return last.Id;
- }
- else
- {
- return 0;
- }
- }
-
- [NonAction]
- public async Task<int> SetTerminado(int pid)
- {
- Servicio servicio = _context.Servicios.Where(x => x.Pid == pid).OrderByDescending(x => x.Id).First(); ;
- //Servicio servicio = _context.Servicios.FromSqlRaw("SELECT * FROM servicio WHERE pid = {0} ORDER BY id DESC", pid).First();
-
- servicio.Terminado = 1;
- servicio.Fin = DateTime.Now.ToString();
- _context.Entry(servicio).State = EntityState.Modified;
- int result = 0;
- try
- {
- result = await _context.SaveChangesAsync();
- }
- catch (DbUpdateConcurrencyException ex)
- {
- Logger.WriteLog(ex.Message);
- Logger.WriteLog(ex.InnerException.Message);
- }
-
- Logger.WriteLog("TERMINADO " + pid);
-
- return result;
-
- }
- }
- }
|