Beispiel #1
0
        public async Task <AccessResponse> FisrtReadAsync(Device device)
        {
            try
            {
                CultureInfo  ci         = new CultureInfo("en-US");
                TimeZoneInfo hrBrasilia = TZConvert.GetTimeZoneInfo("E. South America Standard Time");
                Device       deviceOut  = new Device();
                device = await _deviceRepository.AddAsync(device);

                foreach (Access accessIn in await _accessRepository.ListAsync())
                {
                    string firstKey  = accessIn.Device.FirstBlock + "" + accessIn.Device.SecondBlock + "" + accessIn.Device.ThirdBlock + "" + accessIn.Device.FourthBlock;
                    string secondKey = device.FirstBlock + "" + device.SecondBlock + "" + device.ThirdBlock + "" + device.FourthBlock;
                    if (firstKey == secondKey)
                    {
                        deviceOut = accessIn.Device;
                    }
                }

                Access    access    = new Access();
                AccessLog accessLog = new AccessLog();
                if (deviceOut.Code != 0)
                {
                    return(new AccessResponse($"Device {deviceOut.Code} already exists."));
                }
                else if (deviceOut.Code == 0)
                {
                    access = new Access {
                        Entry = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, hrBrasilia).ToString(), Exit = "Waiting", CodeDevice = device.Code, Device = device, CodeControl = 1
                    };
                    await _accessRepository.AddAsync(access);

                    await _unitOfWork.CompleteAsync();
                }
                access.AccessLogs = await _accessLogRepository.ListByAccessCodeAsync(access.Code);

                return(new AccessResponse(access));
            }
            catch (Exception e)
            {
                return(new AccessResponse($"An error occurred when saving the access: {e.Message}"));
            }
        }