Beispiel #1
0
        public async Task <object> AutorizacionIngreso(SendEvent_DTO evento, int gap, int intentos, int timeOut,
                                                       string path, string user, string password)
        {
            GetCardHolderDC_DTO   persona  = new GetCardHolderDC_DTO();
            List <GetBadgeDC_DTO> tarjetas = new List <GetBadgeDC_DTO>();

            int    badgekey = 0;
            string badgeID  = "";

            bool salir   = false;
            bool success = false;

            string resultado = "OK";
            ResLastLocationDC_DTO   lastEvent     = new ResLastLocationDC_DTO();
            List <LastLocation_DTO> listLocations = new List <LastLocation_DTO>();

            ManagementScope IngresoScope = _dataConduITMgr.GetManagementScope(path, user, password);

            #region OBTENER PERSONA

            if (evento.documento != "" && evento.documento != null && evento.esVisitante == false)
            {
                try
                {
                    ObjectQuery cardHolderSearcher =
                        new ObjectQuery(@"SELECT * FROM Lnl_CardHolder WHERE OPHONE = '" + evento.documento + /*"' AND SSNO = '" + ssno +*/ "'");
                    ManagementObjectSearcher getCardHolder = new ManagementObjectSearcher(IngresoScope, cardHolderSearcher);

                    foreach (ManagementObject queryObj in getCardHolder.Get())
                    {
                        persona.id = int.Parse(queryObj["ID"].ToString());
                        try { persona.apellidos = queryObj["LASTNAME"].ToString(); } catch { persona.apellidos = null; }
                        try { persona.nombres = queryObj["FIRSTNAME"].ToString(); } catch { persona.nombres = null; }
                        try { persona.ssno = queryObj["SSNO"].ToString(); } catch { persona.ssno = null; }
                        try { persona.status = queryObj["STATE"].ToString(); } catch { persona.status = null; }
                        try { persona.documento = queryObj["OPHONE"].ToString(); } catch { persona.documento = null; }
                        try { persona.empresa = queryObj["TITLE"].ToString(); } catch { persona.empresa = null; }
                        try { persona.ciudad = queryObj["DEPT"].ToString(); } catch { persona.ciudad = null; }
                        try { persona.instalacion = queryObj["BUILDING"].ToString(); } catch { persona.instalacion = null; }
                        try { persona.piso = queryObj["FLOOR"].ToString(); } catch { persona.piso = null; }
                        try { persona.area = queryObj["DIVISION"].ToString(); } catch { persona.area = null; }
                        try { persona.email = queryObj["EMAIL"].ToString(); } catch { persona.email = null; }
                        persona.permiteVisitantes = (bool)queryObj["ALLOWEDVISITORS"];

                        #region OBTENER BADGE ACTIVO
                        ObjectQuery badgeSearcher         = new ObjectQuery(@"SELECT * FROM Lnl_Badge WHERE PERSONID = '" + queryObj["ID"].ToString() + "'  AND STATUS = 1");
                        ManagementObjectSearcher getBadge = new ManagementObjectSearcher(IngresoScope, badgeSearcher);

                        foreach (ManagementObject queryObjBadge in getBadge.Get())
                        {
                            GetBadgeDC_DTO item = new GetBadgeDC_DTO();
                            item.badgeID = queryObj["ID"].ToString();
                            try
                            {
                                item.activacion = DateTime.ParseExact(queryObjBadge["ACTIVATE"].ToString().Substring(0, 14),
                                                                      "yyyyMMddHHmmss", null);
                            }
                            catch
                            {
                                item.activacion = null;
                            }
                            try
                            {
                                item.desactivacion = DateTime.ParseExact(queryObjBadge["DEACTIVATE"].ToString().Substring(0, 14),
                                                                         "yyyyMMddHHmmss", null);
                            }
                            catch
                            {
                                item.desactivacion = null;
                            }
                            try
                            {
                                item.estado = queryObjBadge["STATUS"].ToString();
                            }
                            catch
                            {
                                item.desactivacion = null;
                            }
                            try { item.type = int.Parse(queryObjBadge["TYPE"].ToString()); }
                            catch { item.type = null; }
                            try { item.badgekey = int.Parse(queryObjBadge["BADGEKEY"].ToString()); }
                            catch { item.badgekey = 0; }

                            tarjetas.Add(item);
                        }
                        #endregion

                        persona.Badges = tarjetas;
                    }

                    if (persona.id == 0)
                    {
                        evento.PersonaConocida = false;
                    }
                    //throw new Exception("no se encontró una persona registrada con esos datos");

                    if (persona.Badges.Count > 0)
                    {
                        badgekey = persona.Badges[0].badgekey;
                        badgeID  = persona.Badges[0].badgeID;
                    }
                    else
                    {
                        throw new Exception("la persona no tiene un badge activo");
                    }
                }
                catch (Exception ex)
                {
                    evento.PersonaConocida = false;
                    //throw new Exception(ex.Message);
                }
            }
            else
            {
                if (evento.documento == "" || evento.documento == null)
                {
                    evento.PersonaConocida = false;
                }
            }
            #endregion



            #region EVENTO
            EvaluacionEventoDC_DTO eval   = new EvaluacionEventoDC_DTO();
            SendEvent_DTO          acceso = new SendEvent_DTO
            {
                source    = evento.source,
                device    = evento.device,
                subdevice = evento.subdevice
            };
            ReaderPathDC_DTO lectora = new ReaderPathDC_DTO
            {
                panelID  = evento.panelId,
                readerID = evento.readerId,
            };

            if (evento.documento != null && evento.documento != "")
            {
                eval = GetDescripcion(tipoEvento.IB, evento);
            }
            else
            {
                eval = GetDescripcion(tipoEvento.IBNI, evento);
            }

            evento.description = eval.descripcionEvento;

            //EVENTO PARA LA PGR
            bool enviado = SendIncomingEvent(IngresoScope, evento);

            //LOGICA DE ACCESO
            if (eval.alarmaEvento == false)
            {
                acceso.isAccessGranted = true;
                acceso.isAccessDeny    = null;
                acceso.badgeId         = int.Parse(badgeID);
                SendIncomingEvent(IngresoScope, acceso);
                OpenDoor(IngresoScope, lectora.panelID, lectora.readerID);
            }
            else
            {
                acceso.isAccessGranted = null;
                acceso.isAccessDeny    = true;
                acceso.badgeId         = evento.badgeId;
                SendIncomingEvent(IngresoScope, acceso);

                if ((bool)evento.validaIdentidad == false)
                {
                    #region ULTIMA UBICACION
                    try
                    {
                        int intento = 1;
                        do
                        {
                            ManagementObjectSearcher LastLocation = GetLastLocationByDoor(IngresoScope,
                                                                                          int.Parse(evento.panelId), int.Parse(evento.readerId), gap);

                            foreach (ManagementObject queryObj in LastLocation.Get())
                            {
                                LastLocation_DTO lastLocation = new LastLocation_DTO();
                                try { lastLocation.badgeId = queryObj["BADGEID"].ToString(); } catch { lastLocation.badgeId = "0"; }
                                try { lastLocation.eventTime = queryObj["EVENTTIME"].ToString(); } catch { lastLocation.eventTime = null; }
                                try { lastLocation.panelId = (int)queryObj["PANELID"]; } catch { lastLocation.panelId = 0; }
                                try { lastLocation.readerId = (int)queryObj["READERID"]; } catch { lastLocation.readerId = 0; }
                                listLocations.Add(lastLocation);
                            }

                            if (listLocations.Count > 0)
                            {
                                resultado = "OK";
                                salir     = true;
                            }

                            if (intento == intentos && salir != true)
                            {
                                resultado = "No se presento un evento en la ventana de tiempo requerida";
                                salir     = true;
                            }
                            else
                            {
                                System.Threading.Thread.Sleep(timeOut);
                                intento++;
                            }
                        } while (salir == false);

                        if (resultado == "OK")
                        {
                            success = true;
                        }

                        lastEvent = new ResLastLocationDC_DTO
                        {
                            locations = listLocations,
                            result    = resultado,
                            success   = success
                        };

                        if (lastEvent.success)
                        {
                            eval.descripcionEvento = eval.descripcionEvento + "|" + evento.documentoRfId;
                        }
                        else
                        {
                            eval.descripcionEvento = eval.descripcionEvento + "|NA";
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    #endregion
                }
            }
            if (enviado)
            {
                return(eval);
            }
            else
            {
                throw new Exception("No se pudo enviar el evento");
            }

            #endregion
        }
        public async Task <object> LastEvent(string documento, int gap, int intentos, int timeout)
        {
            bool     salir        = false;
            DateTime tiempoEvento = new DateTime();
            int      personaId    = 0;
            string   resultado    = "OK";

            List <LastLocation_DTO>  listLocations = new List <LastLocation_DTO>();
            ManagementObjectSearcher cardHolder    = await _cardHolder_REP.GetCardHolder(documento, "", _path, _user, _pass);

            try
            {
                foreach (ManagementObject queryObj in cardHolder.Get())
                {
                    personaId = int.Parse(queryObj["ID"].ToString());
                }

                if (personaId == 0)
                {
                    throw new Exception("no se encontró una persona registrada con esos datos");
                }
                else
                {
                    int intento = 1;
                    do
                    {
                        ManagementObjectSearcher LastLocation = await _badge_REP.GetLastLocation(personaId, _path, _user, _pass);

                        listLocations.Clear();

                        foreach (ManagementObject queryObj in LastLocation.Get())
                        {
                            LastLocation_DTO lastLocation = new LastLocation_DTO();
                            try { lastLocation.badgeId = queryObj["BADGEID"].ToString(); } catch { lastLocation.badgeId = "0"; }
                            try { lastLocation.eventTime = queryObj["EVENTTIME"].ToString(); } catch { lastLocation.eventTime = null; }
                            try { lastLocation.panelId = (int)queryObj["PANELID"]; } catch { lastLocation.panelId = 0; }
                            try { lastLocation.readerId = (int)queryObj["READERID"]; } catch { lastLocation.readerId = 0; }
                            listLocations.Add(lastLocation);
                        }

                        foreach (LastLocation_DTO location in listLocations)
                        {
                            if (string.IsNullOrEmpty(location.eventTime))
                            {
                                throw new Exception("no se encontró un evento asociado");
                            }
                            else
                            {
                                tiempoEvento = DateTime.ParseExact(location.eventTime.Substring(0, 14), "yyyyMMddHHmmss", null);
                                TimeSpan difTime = tiempoEvento - DateTime.Now;

                                if (difTime.Duration() < new TimeSpan(0, 0, gap))
                                {
                                    salir = true;
                                    break;
                                }
                            }
                        }

                        if (intento == intentos && salir != true)
                        {
                            resultado = "No se presento un evento en la ventana de tiempo requerida";
                            salir     = true;
                        }

                        else
                        {
                            System.Threading.Thread.Sleep(timeout);
                            intento++;
                        }
                    } while (salir == false);
                }

                object result = new
                {
                    locations = listLocations,
                    result    = resultado
                };

                return(result);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public async Task <ResLastLocation_DTO> LastEventDoor(int panelID, int readerID, int gap, int intentos, int timeout)
        {
            bool     salir        = false;
            bool     success      = false;
            DateTime tiempoEvento = new DateTime();
            string   resultado    = "OK";
            List <LastLocation_DTO> listLocations = new List <LastLocation_DTO>();

            try
            {
                int intento = 1;
                do
                {
                    ManagementObjectSearcher LastLocation = await _reader_REP.GetLastLocationByDoor(panelID, readerID, gap, _path, _user, _pass);

                    foreach (ManagementObject queryObj in LastLocation.Get())
                    {
                        LastLocation_DTO lastLocation = new LastLocation_DTO();
                        try { lastLocation.badgeId = queryObj["BADGEID"].ToString(); } catch { lastLocation.badgeId = "0"; }
                        try { lastLocation.eventTime = queryObj["EVENTTIME"].ToString(); } catch { lastLocation.eventTime = null; }
                        try { lastLocation.panelId = (int)queryObj["PANELID"]; } catch { lastLocation.panelId = 0; }
                        try { lastLocation.readerId = (int)queryObj["READERID"]; } catch { lastLocation.readerId = 0; }
                        listLocations.Add(lastLocation);
                    }

                    if (listLocations.Count > 0)
                    {
                        resultado = "OK";
                        salir     = true;
                    }

                    if (intento == intentos && salir != true)
                    {
                        resultado = "No se presento un evento en la ventana de tiempo requerida";
                        salir     = true;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(timeout);
                        intento++;
                    }
                } while (salir == false);

                if (resultado == "OK")
                {
                    success = true;
                }

                ResLastLocation_DTO result = new ResLastLocation_DTO
                {
                    locations = listLocations,
                    result    = resultado,
                    success   = success
                };

                return(result);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }