public JsonResult CambiarEstado(int idInteligente, EstadoDispositivo estado) { SGEContext context = new SGEContext(); BaseRepositorio <Inteligente> repoInteligente = new BaseRepositorio <Inteligente>(context); Inteligente inteligente = repoInteligente.Single(i => i.Id == idInteligente); inteligente.Context = context; switch (estado) { case EstadoDispositivo.AhorroEnergia: inteligente.ColocarEnAhorroEnergia(); break; case EstadoDispositivo.Apagado: inteligente.Apagar(); break; case EstadoDispositivo.Encendido: inteligente.Encender(); break; default: return(Json(new { success = false, error = "Estado desconocido" })); } repoInteligente.Update(inteligente); return(Json(new { success = true })); }
public JsonResult ejecutarSimplex() { Dictionary <string, double> resultadoSimplex = ejecucionSimplex(); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var user = UserManager.FindById(User.Identity.GetUserId()); SGEContext db = new SGEContext(); if (resultadoSimplex == null) { return(Json(new { success = false, error = "No se puede ejecutar el simplex" })); } BaseRepositorio <Inteligente> repoInteligente = new BaseRepositorio <Inteligente>(db); var includesCliente = new List <Expression <Func <Inteligente, object> > >() { i => i.RegistroDeActivaciones, i => i.Clientes }; Inteligente inteligente = null; foreach (KeyValuePair <string, double> item in resultadoSimplex) { if (item.Value > 0 && item.Key != "TotalHorasRestantes" && item.Key != "ConsumoRestanteTotal") { inteligente = repoInteligente.Single(i => i.Nombre == item.Key && i.Clientes.Any(c => c.NombreUsuario == user.UserName), includesCliente); inteligente.Encender(); repoInteligente.Update(inteligente); } } return(Json(new { success = true })); }
public JsonResult EjecutarRegla(int IdRegla) { SGEContext db = new SGEContext(); BaseRepositorio <Regla> repoRegla = new BaseRepositorio <Regla>(db); var includesRegla = new List <Expression <Func <Regla, object> > >() { r => r.Acciones, r => r.Condiciones, r => r.Inteligente }; Regla regla = repoRegla.Single(r => r.ReglaId == IdRegla, includesRegla); BaseRepositorio <Condicion> repoCondicion = new BaseRepositorio <Condicion>(db); var includesCondicion = new List <Expression <Func <Condicion, object> > >() { c => c.Sensor, c => c.Operador }; regla.Acciones.ToList().ForEach(a => a.Dispositivo = regla.Inteligente); regla.Condiciones.ToList().ForEach(c => c = repoCondicion.Single(co => co.CondicionId == c.CondicionId, includesCondicion)); regla.Condiciones.ToList().ForEach(c => c.Sensor.Dispositivo = regla.Inteligente); regla.Condiciones.ToList().ForEach(c => c.Sensor.TipoSensor = db.Sensores.Include("Catalogos").First(s => s.Id == c.SensorId)); regla.Condiciones.ToList().ForEach(c => c.Sensor.TipoSensor.Dispositivo = regla.Inteligente); regla.Ejecutar(); repoRegla.Update(regla); return(Json(new { success = true })); }
//• Consumo por hogar/periodo. public static decimal consumoPorHogarYPeriodo(int idUsuario, DateTime fechaDesde, DateTime fechaHasta) { SGEContext context = new SGEContext(); BaseRepositorio <Cliente> repoCliente = new BaseRepositorio <Cliente>(context); decimal consumo = 0; var includesCliente = new List <Expression <Func <Cliente, object> > >() { c => c.Inteligentes }; Cliente cliente = repoCliente.Single(u => u.Id == idUsuario, includesCliente); var includesInteligente = new List <Expression <Func <Inteligente, object> > >() { i => i.RegistroDeActivaciones }; foreach (Inteligente inteligente in cliente.Inteligentes) { BaseRepositorio <Inteligente> repoInteligente = new BaseRepositorio <Inteligente>(context); Inteligente inte = repoInteligente.Single(i => i.Id == inteligente.Id, includesInteligente); consumo += inte.ObtenerConsumoPeriodo(fechaDesde, fechaHasta); } foreach (Estandar estandar in cliente.Estandars) { consumo += estandar.ConsumoAproximado((int)Math.Ceiling(fechaHasta.Subtract(fechaDesde).TotalHours)); } return(consumo); }
public JsonResult EliminarRegla(int IdRegla) { SGEContext db = new SGEContext(); BaseRepositorio <Regla> repoRegla = new BaseRepositorio <Regla>(db); Regla regla = repoRegla.Single(r => r.ReglaId == IdRegla); repoRegla.Delete(regla); return(Json(new { success = true })); }
public JsonResult Agregar(int idCatalogo) { var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var user = UserManager.FindById(User.Identity.GetUserId()); SGEContext db = new SGEContext(); BaseRepositorio <Cliente> repoCliente = new BaseRepositorio <Cliente>(db); Cliente cliente = repoCliente.Single(c => c.NombreUsuario == user.UserName); BaseRepositorio <Catalogo> repoCatalogo = new BaseRepositorio <Catalogo>(db); Catalogo Catalogo = repoCatalogo.Single(c => c.Id == idCatalogo); Inteligente inteligente = new Inteligente() { ConsumoEnergia = Catalogo.ConsumoEnergia, IdentificadorFabrica = Catalogo.IdentificadorFabrica, Catalogo = Catalogo, CatalogoId = Catalogo.Id }; string nombreInteligente = Catalogo.Nombre + "_" + DateTime.Now.ToString("ddMMyyHHmmss"); nombreInteligente = nombreInteligente.Replace(" ", "_"); inteligente.Nombre = nombreInteligente; inteligente.Clientes.Add(cliente); BaseRepositorio <Inteligente> repoInteligente = new BaseRepositorio <Inteligente>(db); repoInteligente.Create(inteligente); db = new SGEContext(); List <Sensor> sensores = db.Sensores.Where(s => s.Catalogos.Any(c => c.Id == Catalogo.Id)).ToList(); foreach (Sensor sensor in sensores) { SGEContext db2 = new SGEContext(); SensorFisico sensorFisico = new SensorFisico() { //TipoSensor = sensor, //Dispositivo = inteligente, IdDispositivo = inteligente.Id, IdTipoSensor = sensor.Id, Descripcion = sensor.Descripcion }; sensorFisico.Mediciones = null; //repoSensorFisico.Create(sensorFisico); db2.SensoresFisicos.Add(sensorFisico); db2.SaveChanges(); } return(Json(new { success = true })); }
private decimal ObtenerConsumoUltimoMes() { var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var user = UserManager.FindById(User.Identity.GetUserId()); SGEContext contexto = new SGEContext(); BaseRepositorio <Cliente> repoCliente = new BaseRepositorio <Cliente>(contexto); Cliente cliente = repoCliente.Single(c => c.NombreUsuario == user.UserName); DateTime fDesde = DateTime.Now.AddMonths(-1); DateTime fHasta = DateTime.Now; return(Reporte.consumoPorHogarYPeriodo(cliente.Id, fDesde, fHasta)); }
// GET: Cliente/Inteligentes/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BaseRepositorio <Inteligente> repoInteligente = new BaseRepositorio <Inteligente>(); Inteligente inteligente = repoInteligente.Single(i => i.Id == id); if (inteligente == null) { return(HttpNotFound()); } return(View(inteligente)); }
public void LevantarDispositivosArchivo() { BaseRepositorio <Inteligente> repoInteligente = new BaseRepositorio <Inteligente>(); foreach (Core.Entidades.Dispositivo dispositivo in DispositivosHelper.GetInstace().Dispositivos) { if (repoInteligente.Single(d => d.Nombre == dispositivo.Tipo) == null) { repoInteligente.Create(new Inteligente() { Nombre = dispositivo.Tipo, ConsumoEnergia = (decimal)dispositivo.Consumo, IdentificadorFabrica = dispositivo.Id.Substring(0, 14) }); } } }
public void ProcesarDatosEnre() { BaseRepositorio <Transformador> repoTransformador = new BaseRepositorio <Transformador>(); BaseRepositorio <Zona> repoZona = new BaseRepositorio <Zona>(); TransformadoresHelper transHelper = new TransformadoresHelper(); foreach (Core.Entidades.Transformador transformador in transHelper.Transformadores) { if (repoTransformador.Single(t => t.codigo == transformador.codigo) == null) { repoTransformador.Create(new Transformador() { codigo = transformador.codigo, Latitud = (double)transformador.Latitud, Longitud = (double)transformador.Longitud, ZonaId = repoZona.Single(z => z.codigo == transformador.Zona).Id }); } } }
//• Consumo por transformador por periodo. public static decimal consumoTransformadorPorPeriodo(int idTransformador, DateTime fechaDesde, DateTime fechaHasta) { decimal consumo = 0; BaseRepositorio <Transformador> repoTransformador = new BaseRepositorio <Transformador>(); var includesTransformador = new List <Expression <Func <Transformador, object> > >() { t => t.Clientes }; Transformador transformador = repoTransformador.Single(t => t.Id == idTransformador, includesTransformador); if (transformador != null) { foreach (Cliente cliente in transformador.Clientes) { consumo += consumoPorHogarYPeriodo(cliente.Id, fechaDesde, fechaHasta); } } return(consumo); }
public void ProcesarDatosEnre() { ZonasHelper zonaHelper = new ZonasHelper(); BaseRepositorio <Zona> repoZona = new BaseRepositorio <Zona>(); foreach (Core.Entidades.Zona zona in zonaHelper.Zonas) { if (repoZona.Single(z => z.codigo == zona.codigo) == null) { repoZona.Create(new Zona() { Id = zona.Id, codigo = zona.codigo, Nombre = zona.Nombre, Latitud = (double)zona.Latitud, Longitud = (double)zona.Longitud, Radio = zona.Radio }); } } }
public JsonResult EditarRegla(string nombreRegla, int idInteligente, int idRegla, long[] idsAcciones, List <Condicion> condiciones) { SGEContext db = new SGEContext(); BaseRepositorio <Regla> repoRegla = new BaseRepositorio <Regla>(db); Regla reglaExistente = repoRegla.Single(r => r.ReglaId == idRegla); repoRegla.Delete(reglaExistente); Regla regla = new Regla() { Nombre = nombreRegla, IdInteligente = idInteligente, Condiciones = condiciones }; regla.Acciones = db.Acciones.Where(a => idsAcciones.Any(x => x == a.Id)).ToList(); repoRegla.Create(regla); return(Json(new { success = true })); }
public ActionResult DeleteConfirmed(int id) { var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var user = UserManager.FindById(User.Identity.GetUserId()); SGEContext contexto = new SGEContext(); BaseRepositorio <Cliente> repoCliente = new BaseRepositorio <Cliente>(contexto); Cliente cliente = repoCliente.Single(c => c.NombreUsuario == user.UserName); BaseRepositorio <Inteligente> repoInteligente = new BaseRepositorio <Inteligente>(contexto); var includesInteligente = new List <Expression <Func <Inteligente, object> > >() { i => i.Clientes }; Inteligente inteligente = repoInteligente.Single(i => i.Id == id, includesInteligente); inteligente.Clientes.Remove(cliente); repoInteligente.Update(inteligente); return(RedirectToAction("Index")); }
public JsonResult Buscar(string fechaDesde, string fechaHasta) { var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var user = UserManager.FindById(User.Identity.GetUserId()); SGEContext contexto = new SGEContext(); BaseRepositorio <Cliente> repoCliente = new BaseRepositorio <Cliente>(contexto); Cliente cliente = repoCliente.Single(c => c.NombreUsuario == user.UserName); DateTime fDesde = Convert.ToDateTime(fechaDesde); DateTime fHasta = DateTime.Now; if (!String.IsNullOrEmpty(fechaHasta)) { fHasta = Convert.ToDateTime(fechaHasta); } var consumo = Reporte.consumoPorHogarYPeriodo(cliente.Id, fDesde, fHasta); return(Json(new { success = true, resultado = consumo })); }
// GET: Cli/CargaMasiva/Details/5 public JsonResult SubirArchivo(HttpPostedFileBase File) { if (!File.ContentType.Contains("json")) { return(Json(new { success = false, error = "El archivo debe ser de formato JSON" })); } try { using (StreamReader r = new StreamReader(File.InputStream)) { string json = r.ReadToEnd(); List <Inteligente> inteligentes = JsonConvert.DeserializeObject <List <Inteligente> >(json); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var user = UserManager.FindById(User.Identity.GetUserId()); SGEContext contexto = new SGEContext(); BaseRepositorio <Cliente> repoCliente = new BaseRepositorio <Cliente>(contexto); Cliente cliente = repoCliente.Single(c => c.NombreUsuario == user.UserName); BaseRepositorio <Catalogo> repoCatalogo = new BaseRepositorio <Catalogo>(contexto); BaseRepositorio <Inteligente> repoInteligente = new BaseRepositorio <Inteligente>(contexto); foreach (Inteligente inteligente in inteligentes) { Catalogo Catalogo = repoCatalogo.Single(c => c.Id == inteligente.CatalogoId); if (Catalogo == null) { return(Json(new { success = false, error = "El dispositivo '" + inteligente.Nombre + "' esta asociado a un catálogo inexistente" })); } string nombreInteligente = Catalogo.Nombre + "_" + DateTime.Now.ToString("ddMMyyHHmmss"); nombreInteligente = nombreInteligente.Replace(" ", "_"); inteligente.Nombre = nombreInteligente; inteligente.Clientes.Clear(); inteligente.Clientes.Add(cliente); if (inteligente.Id != 0) { repoInteligente.Update(inteligente); } else { repoInteligente.Create(inteligente); } List <Sensor> sensores = contexto.Sensores.Where(s => s.Catalogos.Any(c => c.Id == Catalogo.Id)).ToList(); foreach (Sensor sensor in sensores) { SGEContext db2 = new SGEContext(); SensorFisico sensorFisico = new SensorFisico() { //TipoSensor = sensor, //Dispositivo = inteligente, IdDispositivo = inteligente.Id, IdTipoSensor = sensor.Id, Descripcion = sensor.Descripcion }; sensorFisico.Mediciones = null; //repoSensorFisico.Create(sensorFisico); db2.SensoresFisicos.Add(sensorFisico); db2.SaveChanges(); } } } } catch (Exception ex) { return(Json(new { success = false, error = "El archivo JSON no es valido, por favor verifique el mismo", mensaje = ex.Message })); } return(Json(new { success = true })); }