예제 #1
0
        private void SendDailyServiceCheckSMS()
        {
            try
            {
                if (DateTime.Now.Hour == 7 && DailyServiceCheckSMSDate < DateTime.Now)
                {
                    this.IsBusy = true;

                    if (this.AttendanceConfig.is_enable_sms_service)
                    {
                        string smsContent = "Attendance service running...";

                        using (ZkTecoClient bioMatrixClient = new ZkTecoClient("basecampzkteco.ddns.net"))
                        {
                            try
                            {
                                bool isConnected = bioMatrixClient.ConnectToZKTeco();
                                if (!isConnected)
                                {
                                    smsContent = "Failed to connect to the biometric device";
                                }
                            }
                            catch (Exception ex)
                            {
                                smsContent = "Failed to connect to the biometric device";
                                logger.Error(ex.Message, ex);
                            }
                        }

                        BasecampSMSSender smssender = new BasecampSMSSender("*****@*****.**", "abc987");

                        string res = smssender.SendSms("8801714042726", smsContent);
                        logger.Info($"SMS- {smsContent} {Environment.NewLine}Status: {res}");

                        DailyServiceCheckSMSDate = DateTime.Now;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }
            finally
            {
                this.IsBusy = false;
            }
        }
예제 #2
0
        private void ProcessDailyAttendanceLog()
        {
            bool isValidTimeRange = ((DateTime.Now.TimeOfDay > this.AttendanceConfig.job_start_time &&
                                      DateTime.Now.TimeOfDay < this.AttendanceConfig.job_end_time) ||
                                     DateTime.Now.TimeOfDay > (new TimeSpan(23, 54, 00)));

            if (!(isValidTimeRange &&
                  this.ProcessAttendanceLog_LastRunTime.AddMinutes(this.AttendanceConfig.interval_minute) < DateTime.Now &&
                  !this.IsBusy))
            {
                return;
            }

            logger.Info($"Process Attendance Log - Start".ToUpper());

            this.IsBusy = true;


            this.ProcessAttendanceLog_LastRunTime = DateTime.Now;
            DateTime lastProcessDate = DateTime.MinValue;
            ICollection <BiometricLogModel> biometricLogData = new HashSet <BiometricLogModel>();



            try
            {
                logger.Info($"Get Biometric Data - Start");


                using (ZkTecoClient biometricClient = new ZkTecoClient("basecampzkteco.ddns.net"))
                {
                    biometricLogData         = biometricClient.GetBiometricData();
                    SentBiometricDeviceError = false;
                    logger.Info($"{biometricLogData.Count} records found.");
                }

                logger.Info("Get Biometric Data - End");

                using (AttendanceDbContext dbContext = new AttendanceDbContext())
                {
                    if (dbContext.BiometricLogs.Any())
                    {
                        lastProcessDate = dbContext.BiometricLogs.Max(x => x.datetime_record);
                    }

                    biometricLogData = biometricLogData.Where(x => x.DateTimeRecord > lastProcessDate)
                                       .OrderBy(x => x.DateTimeRecord).ToList();


                    try
                    {
                        foreach (var log in biometricLogData)
                        {
                            dbContext.BiometricLogs.Add(new Data.Models.BiometricLog
                            {
                                machine_id      = log.MachineNumber,
                                ind_reg_iD      = log.IndRegID,
                                datetime_record = log.DateTimeRecord
                            });
                        }

                        ///Save BiometricLogs
                        dbContext.SaveChanges();

                        ICollection <BiometricLog> unprocessedBiometricLogData = dbContext.BiometricLogs
                                                                                 .Where(x => !x.is_processed).OrderBy(x => x.datetime_record).ToList();

                        foreach (DateTime date in unprocessedBiometricLogData.Select(x => x.datetime_record.Date).Distinct())
                        {
                            this.MakeStudentAbsent(dbContext, date);
                            this.MakeStaffAbsent(dbContext, date);
                        }

                        foreach (var log in unprocessedBiometricLogData)
                        {
                            try
                            {
                                bool isStudentLog = this.UpdateStudentAttendanceStatus(log, dbContext);
                                if (!isStudentLog)
                                {
                                    this.UpdateStaffAttendanceStatus(log, dbContext);
                                }

                                log.is_processed = true;

                                dbContext.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                logger.Info(ex.Message, ex);
                            }
                        }
                        //Update Student/Staff Attendance Status
                        dbContext.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        logger.Info(ex.Message, ex);
                    }
                }
            }
            catch (ZkTecoClientException ex)
            {
                if (!this.SentBiometricDeviceError)
                {
                    this.SentBiometricDeviceError = true;
                    this.SendServiceStetusSms(ex.Message);
                }

                logger.Error(ex.Message, ex);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }
            finally
            {
                this.IsBusy = false;
                logger.Info($"Process Attendance Log - End".ToUpper());
            }
        }