public void setContent(ExcelWorksheet ws, int Row, int Col, int cont, DataSensores lectura)
        {
            cont++;

            ws.Cells[Row, Col++].Value = lectura.TipoSensor ?? "---";
            ws.Cells[Row, Col++].Value = lectura?.FechaRegistro.ToString() ?? "---";
            ws.Cells[Row, Col++].Value = lectura.Medida;
            ws.Cells[Row, Col].Value   = lectura.UnidadMedida ?? "---";
            Col = 0;
        }
        public bool CheckMinMax(DataSensores dataSensor)
        {
            var Sensor = db.Sensores.FirstOrDefault(x => x.TipoSensor.NombreTipoSensor == dataSensor.TipoSensor && x.Activo);

            if (Sensor != null)
            {
                var  Max         = Sensor.Max != null ? Sensor.Max : 0;
                var  Min         = Sensor.Max != null ? Sensor.Min : 0;
                bool TodoBienMAX = true;
                bool TodoBienMIN = true;
                //SI MIN ES 0 REVISO MAX
                if (Min == 0)
                {
                    if (!CheckRange(null, Max, dataSensor.Medida))
                    {
                        TodoBienMAX = false;
                    }
                }
                //SI MAX ES 0 REVISO MIN
                if (Max == 0)
                {
                    if (!CheckRange(Min, null, dataSensor.Medida))
                    {
                        TodoBienMIN = false;
                    }
                }
                //SI NINGUNO ES 0 REVISO AMBOS
                if (Min != 0 && Max != 0)
                {
                    if (!CheckRange(null, Max, dataSensor.Medida))
                    {
                        TodoBienMAX = false;
                    }
                    if (!CheckRange(Min, null, dataSensor.Medida))
                    {
                        TodoBienMIN = false;
                    }
                }
                if (TodoBienMAX == false || TodoBienMIN == false)
                {
                    dataSensor.Chequeado    = true;
                    dataSensor.Error        = true;
                    dataSensor.EncimaNormal = TodoBienMIN;
                    dataSensor.DebajoNormal = TodoBienMAX;
                    db.SaveChanges();
                    return(false);
                }
                dataSensor.Chequeado = true;
                db.SaveChanges();
                return(true);
            }
            return(false);
        }
        public async Task <bool> CalcularNotificacionesAsync(Empresa empresa)
        {
            int PrimeraNotif = empresa.PrimeraNotificacion;
            int SegundaNotif = empresa.SegundaNotificacion;
            int TerceraNotif = empresa.TerceraNotificacion;
            var Errores      = db.DataSensores.Where(x => x.Error && x.Notificado == false &&
                                                     x.IdEmpresa == empresa.IdEmpresa).ToList();
            int          Conteo          = Errores.Count();
            DataSensores ErrorANotificar = Errores.FirstOrDefault();

            if (PrimeraNotif != 0 && SegundaNotif != 0 && TerceraNotif != 0)
            {
                if (Conteo >= PrimeraNotif && Conteo < SegundaNotif)
                {
                    try
                    {
                        await NotificarAsync(ErrorANotificar, empresa.EmailNotificacion);
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }

                if (Conteo >= SegundaNotif && Conteo < TerceraNotif)
                {
                    try
                    {
                        await NotificarAsync(ErrorANotificar, empresa.EmailNotificacion);
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }

                if (Conteo >= TerceraNotif)
                {
                    try
                    {
                        await NotificarAsync(ErrorANotificar, empresa.EmailNotificacion);
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
        public async Task <bool> NotificarAsync(DataSensores error, string correoDestino)
        {
            string     path          = HostingEnvironment.MapPath("~\\");
            string     logoImage     = Path.Combine(path, "Content\\Images\\AnubisLogoEmail.jpeg");
            string     rounderup     = Path.Combine(path, "Content\\Images\\rounder-up.png");
            string     divider       = Path.Combine(path, "Content\\Images\\divider.png");
            string     rounderdwn    = Path.Combine(path, "Content\\Images\\rounder-dwn.png");
            Attachment logoImageAtt  = new Attachment(logoImage);
            Attachment rounderupAtt  = new Attachment(rounderup);
            Attachment dividerAtt    = new Attachment(divider);
            Attachment rounderdwnAtt = new Attachment(rounderdwn);

            logoImageAtt.ContentDisposition.Inline          = true;
            logoImageAtt.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
            string logoImgId = "headerimg1";

            logoImageAtt.ContentId = logoImgId;

            rounderupAtt.ContentDisposition.Inline          = true;
            rounderupAtt.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
            string rounderupId = "headerimg2";

            rounderupAtt.ContentId = rounderupId;

            dividerAtt.ContentDisposition.Inline          = true;
            dividerAtt.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
            string dividerId = "headerimg3";

            dividerAtt.ContentId = dividerId;

            rounderdwnAtt.ContentDisposition.Inline          = true;
            rounderdwnAtt.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
            string rounderdwnId = "headerimg4";

            rounderdwnAtt.ContentId = rounderdwnId;


            var email = new MailMessage("*****@*****.**", correoDestino);

            email.Attachments.Add(logoImageAtt);
            email.Attachments.Add(rounderupAtt);
            email.Attachments.Add(dividerAtt);
            email.Attachments.Add(rounderdwnAtt);

            email.Subject    = "Nueva Alerta - Sensor" + " " + error.TipoSensor;
            email.IsBodyHtml = true;
            var not = new NotificacionCorreo
            {
                Usuario    = "Adminsitrador",
                TipoSensor = error.TipoSensor,
                Medicion   = error.UnidadMedida,


                Logo       = logoImgId,
                divider    = dividerId,
                rounderdwn = rounderdwnId,
                rounderup  = rounderupId
            };

            if (error.Medida.ToString().Length > 5)
            {
                not.MedidaSensor = error.Medida.ToString().Substring(0, 5);
            }
            else
            {
                not.MedidaSensor = error.Medida.ToString();
            }
            try
            {
                var bodyAprobadoProveedor = emailSvc.RenderViewToString(new MailerController(), "PlantillaAnubis", "~/Views/Mailer/PlantillaAnubis.cshtml", not);
                email.Body = bodyAprobadoProveedor;
                await emailSvc.SendEmailAsync(email);

                error.Notificado = true;
                db.SaveChanges();
            }
            catch (Exception e)
            {
                return(false);
            }

            return(true);
        }