Ejemplo n.º 1
0
        public ActionResult RecibirEvento(int?id)
        {
            System.Web.Caching.CacheItemRemovedCallback callback = new System.Web.Caching.CacheItemRemovedCallback(OnRemove);
            Stream req = Request.InputStream;

            req.Seek(0, System.IO.SeekOrigin.Begin);
            string json = new StreamReader(req).ReadToEnd();

            string _mensaje      = "";
            int    tiempoEnCache = 5; //Si no lo encuentra por defecto son 5 (expresado en minutos)..

            try
            {
                DtoEventos evento = Newtonsoft.Json.JsonConvert.DeserializeObject <DtoEventos>(json);
                int        tiempo = RepositorioConfiguraciones.ObtenerTiempoDelay(evento.Id_Arduino, evento.Id_Senal);
                guardarEvento(evento); //Todos los eventos se guardan
                if (tiempo != -1)
                {
                    tiempoEnCache = tiempo;
                }
                if (evento.Id_Senal == 1) //Si la señal es de luz lo manejo, sino lo guardo de una..
                {
                    if (estaEnCache("NotifacionLuz" + evento.Id_Arduino))
                    {
                        System.Web.HttpContext.Current.Cache.Remove("NotifacionLuz" + evento.Id_Arduino);
                        if (evento.Valor == 1)
                        {
                            System.Web.HttpContext.Current.Cache.Insert("NotifacionLuz" + evento.Id_Arduino, evento, null, DateTime.Now.AddMinutes(tiempoEnCache), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, callback);
                        }
                    }
                    else
                    {
                        if (evento.Valor == 1)
                        {
                            System.Web.HttpContext.Current.Cache.Insert("NotifacionLuz" + evento.Id_Arduino, evento, null, DateTime.Now.AddMinutes(tiempoEnCache), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, callback);
                        }
                        else
                        {
                            guardarNotificacion(evento);
                        }
                    }
                }
                else
                {
                    guardarNotificacion(evento);
                }

                return(new HttpStatusCodeResult(HttpStatusCode.Created));
            }
            catch (Exception ex)
            {
                _mensaje = ex.Message;
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
        }
Ejemplo n.º 2
0
        public static void guardarNotificacion(DtoEventos evento)
        {
            DtoNotificaciones notificacion = new DtoNotificaciones();

            notificacion.Id_Arduino         = evento.Id_Arduino;
            notificacion.Id_Senal           = evento.Id_Senal;
            notificacion.Valor              = evento.Valor;
            notificacion.Fecha_Notificacion = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Argentina Standard Time"));
            RepositorioNotificaciones.Guardar(notificacion);

            enviarCorreoNotificacion(notificacion);
        }
Ejemplo n.º 3
0
        private static void OnRemove(string key,
                                     object cacheItem,
                                     System.Web.Caching.CacheItemRemovedReason reason)
        {
            String test = reason.ToString();

            if (reason.ToString() == "Expired")
            {
                DtoEventos evento = (DtoEventos)cacheItem;

                guardarNotificacion(evento);
            }
        }
Ejemplo n.º 4
0
        private Boolean estaEnCache(String keyCache)
        {
            DtoEventos eventoEnCache = (DtoEventos)System.Web.HttpContext.Current.Cache.Get(keyCache);

            if (eventoEnCache == null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 5
0
        public static List <DtoEventos> ObtenerEventos()
        {
            List <DtoEventos> listaEventos = new List <DtoEventos>();
            Acceso            acceso       = new Acceso();

            try
            {
                acceso.conectarBD();
                acceso.storedProcedure("pr_eventos_sf");

                SqlDataReader leerBD = acceso.leerDatos();
                while (leerBD.Read())
                {
                    DtoEventos evento = new DtoEventos();
                    evento.Id_Evento  = (int)leerBD["id_evento"];
                    evento.Id_Arduino = (int)leerBD["id_arduino"];
                    evento.Id_Senal   = (int)leerBD["id_senal"];
                    evento.Valor      = (int)leerBD["valor"];
                    if (leerBD["fecha_evento"] != DBNull.Value)
                    {
                        evento.Fecha_Evento = (DateTime)leerBD["fecha_evento"];
                    }
                    listaEventos.Add(evento);
                }
            }
            catch (Exception ex)
            {
                var a = "Error message: " + ex.Message;

                if (ex.InnerException != null)
                {
                    a = a + " Inner exception: " + ex.InnerException.Message;
                }

                a = a + " Stack trace: " + ex.StackTrace;

                System.ArgumentException bdEX = new System.ArgumentException("Mensaje: " + a, ex);


                throw bdEX;
            }

            finally
            {
                acceso.closeConexion();
            }

            return(listaEventos);
        }
Ejemplo n.º 6
0
        public static void Guardar(DtoEventos dtoNuevo)
        {
            Acceso acceso = new Acceso();

            try
            {
                acceso.conectarBD();
                acceso.storedProcedure("pr_eventos_g");
                acceso.agregarParametros("id_arduino", dtoNuevo.Id_Arduino);
                acceso.agregarParametros("id_senal", dtoNuevo.Id_Senal);
                acceso.agregarParametros("valor", dtoNuevo.Valor);
                acceso.agregarParametros("fecha_evento", dtoNuevo.Fecha_Evento);
                acceso.executeNonQuery();
            }
            catch (Exception ex)
            {
                var a = "Error message: " + ex.Message;

                if (ex.InnerException != null)
                {
                    a = a + " Inner exception: " + ex.InnerException.Message;
                }

                a = a + " Stack trace: " + ex.StackTrace;

                System.ArgumentException bdEX = new System.ArgumentException("Mensaje: " + a, ex);


                throw bdEX;
            }

            finally
            {
                acceso.closeConexion();
            }
        }
Ejemplo n.º 7
0
 public void guardarEvento(DtoEventos evento)
 {
     evento.Fecha_Evento = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Argentina Standard Time"));
     RepositorioEventos.Guardar(evento);
 }