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 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 }));
        }
示例#3
0
        public T Put <V>(T obj) where V : AbstractValidator <T>
        {
            Validate(obj, Activator.CreateInstance <V>());

            repository.Update(obj);
            return(obj);
        }
        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 }));
        }
        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"));
        }
示例#6
0
        // 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 }));
        }