public List<IncomeSMS> GetNotSendIncomeSMS(GsmModem modem, SMSContext context) { try { List<IncomeSMS> list = context.IncomeSMS.Where(x => (x.Status == IncomeSMS.IncomeSMSStatus.Checked) && (x.RecipientNumber == modem.ServicePhone.PhoneNumber)).ToList(); return list; } catch (Exception e) { logger.Error("Ошибка чтения из базы данных. Не удалось получить список не отправленных входящих сообщений.\r\nТекст ошибки:", e); return null; } }
public List<IncomeClientSMS> GetIncomeSMSClient(GsmModem modem, IncomeClientSMS.IncomeClientSMSStatus Status, SMSContext context) { try { List<IncomeClientSMS> list = context.IncomeClientSMS.Where(x => (x.Status == Status) && x.RecipientNumber.Equals(modem.ServicePhone.PhoneNumber)).ToList(); return list; } catch (Exception e) { logger.Error("Ошибка чтения из базы данных. Не удалось получить список не отправленных сообщений клиента.\r\nТекст ошибки:", e); return null; } }
private void ModemHandleElapsed(object sender, ElapsedEventArgs e, GsmModem modem) { int sync = Interlocked.CompareExchange(ref modem.processing, 1, 0); if (sync == 0) { SetChekedMessages(modem); ShortMessageCollection messages = modem.ReadSMS(); if (messages != null && messages.Count > 0) messages.Concat(notProcessingSMS[modem.ServicePhone.PhoneNumber]); else messages = notProcessingSMS[modem.ServicePhone.PhoneNumber]; if (messages != null) { if (messages.Count > 0) { messages = ProcessingNewIncomeSMSMessages(messages, modem); foreach (ShortMessage message in messages) message.IsRead = false; notProcessingSMS[modem.ServicePhone.PhoneNumber] = messages; } ProcessingCheckedMessages(modem); ProcessingNewIncomeClientWebMessages(modem); ProcessingNotSentIncomeClientEmail(modem); messages.Clear(); modem.processing = 0; } } }
private List<IncomeClientEmail> GetNotSendIncomeClientEmail(GsmModem modem, SMSContext context) { try { List<IncomeClientEmail> result = context.IncomeClientEmail.Where(x => x.Client.ClientPhone.Select(y => y.ServicePhone.ServicePhoneId).Contains(modem.ServicePhone.ServicePhoneId) && x.IsSent != true).ToList(); return result; } catch (Exception e) { logger.Error("Ошибка чтения не отправленных сообщений из базы данных.\r\nТекст ошибки:", e); return null; } }
public bool Init(Form parent) { try { this.parent = parent; string Email = GSM.Settings.Default.Email; string Password = GSM.Settings.Default.Password; string ImapServer = GSM.Settings.Default.ImapServer; int ImapPort = GSM.Settings.Default.ImapPort; string SmtpServer = GSM.Settings.Default.SmtpServer; int SmtpPort = GSM.Settings.Default.SmtpPort; bool ImapSSL = GSM.Settings.Default.ImapSSL; bool SmtpSSL = GSM.Settings.Default.SmtpSSL; emailBox = new EmailBox(Email, Password, ImapServer, SmtpServer, ImapPort, SmtpPort, SmtpSSL, ImapSSL, emailInterval, new ElapsedEventHandler(EmailTimer_ElapsedEventHandler)); List<Modem> modems = LoadModemList(); gsmModemList = new ConcurrentDictionary<string, GsmModem>(); foreach (Modem modemModel in modems) { GsmModem modem = new GsmModem(modemInterval, modemModel.PortName, modemModel.ServicePhone, modemModel.ModemName); modem.modemTimer.Elapsed += new ElapsedEventHandler((sender, e) => ModemTimer_ElapsedEventHandler(sender, e, modem)); modem.modemTimer.Start(); gsmModemList.AddOrUpdate(modem.ServicePhone.PhoneNumber, modem, (iindex, item) => modem); notProcessingSMS.Add(modem.ServicePhone.PhoneNumber, new ShortMessageCollection()); } emailBox.emailTimer.Start(); return true; } catch { return false; } }
public void SetChekedMessages(GsmModem modem) { try { IList<IncomeSMS> incomeSMSs = null; IList<IncomeClientSMS> IncomeClientSMSs = null; using (SMSContext context = new SMSContext()) { incomeSMSs = context.IncomeSMS.Where(x => x.Status == IncomeSMS.IncomeSMSStatus.Received && x.RecipientNumber.Equals(modem.ServicePhone.PhoneNumber)).ToList(); IncomeClientSMSs = context.IncomeClientSMS.Where(x => x.Status == IncomeClientSMS.IncomeClientSMSStatus.Received && x.RecipientNumber.Equals(modem.ServicePhone.PhoneNumber)).ToList(); } if (incomeSMSs != null) { using (SMSContext context = new SMSContext()) { foreach (IncomeSMS incomeSMS in incomeSMSs) { incomeSMS.Status = IncomeSMS.IncomeSMSStatus.Checked; context.Entry(incomeSMS).State = System.Data.Entity.EntityState.Modified; } context.SaveChanges(); } } using (SMSContext context = new SMSContext()) { foreach (IncomeClientSMS incomeClientSMS in IncomeClientSMSs) { incomeClientSMS.Status = IncomeClientSMS.IncomeClientSMSStatus.Checked; context.Entry(incomeClientSMS).State = System.Data.Entity.EntityState.Modified; } context.SaveChanges(); } } catch (Exception e) { logger.Error("Ошибка обработки базы данных. Не удалось обновить статусы сообщений.\r\nТекст ошибки:", e); } }
public void ProcessingNotSentIncomeClientEmail(GsmModem modem) { using (SMSContext context = new SMSContext()) { List<IncomeClientEmail> messages = GetNotSendIncomeClientEmail(modem, context); foreach (IncomeClientEmail message in messages) { if (message.Client != null && ClientIsNotBlocked(message.Client)) { IncomeSMS incomeSMS = GetIncomeSMS(message.SecretKey, message.Client.ClientId, context); if (incomeSMS != null) { message.IsSent = modem.SendSMS(incomeSMS.SenderNumber, message.Text); SaveIncomeClientEmail(message, context); } } } } }
public ShortMessageCollection ProcessingNewIncomeSMSMessages(ShortMessageCollection messages, GsmModem modem) { IncomeSMSSaveMutex.WaitOne(); ShortMessageCollection notSend = new ShortMessageCollection(); try { using (SMSContext context = new SMSContext()) { ServicePhone ServicePhone = GetServicePhoneById(modem.ServicePhone.ServicePhoneId, context); foreach (ShortMessage message in messages) { bool NotSaved = true; bool isGarbage = true; try { int firstWordLength = message.Text.Contains(" ") ? message.Text.Substring(0, message.Text.IndexOf(" ")).Length : 0; string firstWord = message.Text.Contains(" ") ? message.Text.Substring(0, message.Text.IndexOf(" ")).Replace("\n", "").Replace("\r", "") : null; Client client = GetClientByPhone(message.Sender, context); bool haveFirstWord = !String.IsNullOrEmpty(firstWord); bool ClientSMSAndNotAnswer = false; if (client != null && haveFirstWord) { isGarbage = false; IncomeSMS incomeSMS = GetIncomeSMS(firstWord, client.ClientId, context); if (incomeSMS == null) ClientSMSAndNotAnswer = true; else { message.Recipient = incomeSMS.SenderNumber; message.Sender = ServicePhone.PhoneNumber; message.Text = message.Text.Substring(firstWordLength + 1); message.IsRead = true; if (ClientIsNotBlocked(client)) { IncomeClientSMS incomeClientSMS = SaveIncomeClientSMS(message, ServicePhone, incomeSMS, client, context); if (incomeClientSMS != null) NotSaved = false; int answerId = AddAnswer(incomeSMS.MessageId, incomeClientSMS.IncomeClientSMSId, Answer.AnswerSource.SMS); } } } if ((client == null || ClientSMSAndNotAnswer) && message.Sender != null && message.Sender.Length > 10) { isGarbage = false; client = haveFirstWord ? GetClientByShortKey(firstWord, ServicePhone, context) : null; string text; if (ServicePhone.Type == ServicePhone.PhoneType.Private) { client = GetClientByServicePhone(ServicePhone, context); text = message.Text; } else { text = message.Text.Substring(firstWord.Length + 1); } IncomeSMS incomeSMS = null; incomeSMS = context.IncomeSMS.FirstOrDefault(x => x.SenderNumber.Equals(message.Sender) && x.RecipientNumber.Equals(ServicePhone.PhoneNumber) && DbFunctions.DiffSeconds(x.DateTime, message.SentDate) <= 30 && x.Status != IncomeSMS.IncomeSMSStatus.Sent); if (incomeSMS != null) { incomeSMS.Text += message.Text; context.SaveChanges(); NotSaved = false; message.IsRead = true; } else if (incomeSMS == null && client != null) { if (ClientIsNotBlocked(client)) { incomeSMS = SaveIncomeSMS(client, text, message.Sender, ServicePhone.PhoneNumber, message.SentDate, firstWord, context); if (incomeSMS != null) NotSaved = false; } message.IsRead = true; } } if (NotSaved & !isGarbage) { notSend.Add(message); message.IsRead = true; } } catch { if (NotSaved & !isGarbage) { notSend.Add(message); message.IsRead = true; } } } foreach (ShortMessage message in messages.Where(x => x.IsRead == false)) { bool Saved = false; try { Client client = GetClientByPhone(message.Sender, context); if (client != null) { IncomeClientSMS incomeClientSMS = null; incomeClientSMS = context.IncomeClientSMS.FirstOrDefault(x => x.Client.ClientId == client.ClientId && DbFunctions.DiffSeconds(x.DateTime, message.SentDate) <= 15 && x.Status != IncomeClientSMS.IncomeClientSMSStatus.Sent); if (incomeClientSMS != null) { incomeClientSMS.Text += message.Text; context.Entry(incomeClientSMS).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); Saved = true; } } else { IncomeSMS incomeSMS = null; incomeSMS = context.IncomeSMS.FirstOrDefault(x => x.SenderNumber.Equals(message.Sender) && x.RecipientNumber.Equals(ServicePhone.PhoneNumber) && DbFunctions.DiffSeconds(x.DateTime, message.SentDate) <= 30 && x.Status != IncomeSMS.IncomeSMSStatus.Sent); if (incomeSMS != null) { incomeSMS.Text += message.Text; context.Entry(incomeSMS).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); Saved = true; } } } catch { if (!Saved) notSend.Add(message); } } } return notSend; } catch (Exception e) { notSend = messages; return notSend; } finally { IncomeSMSSaveMutex.ReleaseMutex(); } }
public void ProcessingNewIncomeClientWebMessages(GsmModem modem) { List<IncomeClientWeb> webMessages; using (SMSContext context = new SMSContext()) { webMessages = context.IncomeClientWeb.Where(x => x.Client.ClientPhone.Select(y => y.ServicePhone.ServicePhoneId).Contains(modem.ServicePhone.ServicePhoneId) && x.IsSent != true && x.Client.IsBlocked != true).ToList(); } foreach (IncomeClientWeb incomeClientWeb in webMessages) { IncomeSMS incomeSMS; using (SMSContext context = new SMSContext()) { incomeSMS = context.IncomeSMS.FirstOrDefault(x => x.MessageId == incomeClientWeb.MessageId); if (incomeSMS != null) { if (incomeSMS.MessageType == IncomeSMS.Type.Email) { if (!String.IsNullOrEmpty(incomeSMS.SenderAddress)) emailBox.SendEmail(incomeClientWeb.Text, "Re:" + incomeSMS.ShortKey, incomeSMS.SenderAddress, SendIncomeClientWebAnswerSetResult, incomeClientWeb); } else if (!String.IsNullOrEmpty(incomeSMS.SenderNumber)) if (modem.SendSMS(incomeSMS.SenderNumber, incomeClientWeb.Text)) SaveIncomeClientWebSendResult(incomeClientWeb, context); } } } }
public void ProcessingCheckedMessages(GsmModem modem) { IncomeSMSSaveMutex.WaitOne(); try { using (SMSContext context = new SMSContext()) { IList<IncomeSMS> IncomeSmsList = GetNotSendIncomeSMS(modem, context); foreach (IncomeSMS incomeSMS in IncomeSmsList) { bool SMSresult = false; if (incomeSMS.Client != null && incomeSMS.SMSIsSend != true) { SMSresult = modem.SendSMS(incomeSMS.Client.PhoneNumber, incomeSMS.SecretKey + " " + incomeSMS.Text); incomeSMS.SMSIsSend = SMSresult; } else { incomeSMS.SMSIsSend = true; } if (incomeSMS.EmailIsSend != true) emailBox.SendEmail(incomeSMS.Text, "Сообщение №" + incomeSMS.SecretKey, incomeSMS.Client.Email, SendEmailSetResult, incomeSMS); if (SMSresult && incomeSMS.EmailIsSend == true) ChangeIncomeSMSStatus(incomeSMS, IncomeSMS.IncomeSMSStatus.Sent, context); else ChangeIncomeSMS(incomeSMS, context); } List<IncomeClientSMS> IncomeClientSmsList = GetIncomeSMSClient(modem, IncomeClientSMS.IncomeClientSMSStatus.Checked, context); foreach (IncomeClientSMS IncomeClientSms in IncomeClientSmsList) { bool result; if (IncomeClientSms.IncomeSMS.MessageType == IncomeSMS.Type.Email) emailBox.SendEmail(IncomeClientSms.Text, "Re: " + IncomeClientSms.IncomeSMS.ShortKey, IncomeClientSms.IncomeSMS.SenderAddress, SendIncomeClientSMSSetResult, IncomeClientSms); else { result = modem.SendSMS(IncomeClientSms.IncomeSMS.SenderNumber, IncomeClientSms.Text); if (result) ChangeIncomeClientSMSStatus(IncomeClientSms, IncomeClientSMS.IncomeClientSMSStatus.Sent, context); } } } } catch (Exception e) { throw e; } finally { IncomeSMSSaveMutex.ReleaseMutex(); } }
private void ModemTimer_ElapsedEventHandler(object sender, ElapsedEventArgs e, GsmModem modem) { ((System.Timers.Timer)sender).Stop(); try { ModemHandleElapsed(sender, e, modem); } catch (Exception ex) { logger.Error("Ошибка выполнения операции с модемом " + (modem.ModemName ?? "") + ", подключенного к порту " + (modem.PortName ?? "") + ":", ex); } finally { modem.port.Dispose(); ((System.Timers.Timer)sender).Enabled = true; } }