/// <summary> /// Does the work. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public override void DoWork(object sender, ElapsedEventArgs e) { bool isDataConnectionAvailable = false; try { // Disable and wait until finish execution this.timer.Enabled = false; messenger = messengerService.Messenger; if (messenger == null) { return; } if (logger.IsInfoEnabled) { logger.Info("Checking unprocessed message"); } // Get unprocessed messages List <IncomingMessage> unProcessedMsgs = database.IncomingMesages.Where(msg => msg.Status == IncomingMessage.ProcessingStatus.NotProcessed).ToList <IncomingMessage>(); if (unProcessedMsgs.Count > 0) { logger.Info("Processing " + unProcessedMsgs.Count + " messages"); foreach (IncomingMessage msg in unProcessedMsgs) { MessageInformation msgInfo = EntityHelper.FromCommonRepresentation <MessageInformation>(msg.MsgContent); logger.Info("Processing message from " + msgInfo.PhoneNumber); // Check for matching employee string employeeID = msgInfo.Content; Employee employee = database.Employees.Find(employeeID); Gateway gateway = database.Gateways.Find(GlobalConstants.DefaultGatewayID); if (employee != null && gateway != null) { if (messenger.InitializeDataConnection()) { isDataConnectionAvailable = true; // Send MMS Mms mms = Mms.NewInstance(employee.EmployeeName, gateway.GatewayPhoneNumber); // Multipart mixed mms.MultipartRelatedType = MultimediaMessageConstants.ContentTypeApplicationMultipartMixed; mms.PresentationId = "<0000>"; mms.TransactionId = EntityHelper.GenerateGuid(); mms.AddToAddress(msgInfo.PhoneNumber, MmsAddressType.PhoneNumber); MultimediaMessageContent multimediaMessageContent = new MultimediaMessageContent(); multimediaMessageContent.SetContent(employee.EmployeePhoto, 0, employee.EmployeePhoto.Length); multimediaMessageContent.ContentId = EntityHelper.GenerateGuid(); multimediaMessageContent.Type = employee.PhotoImageType; mms.AddContent(multimediaMessageContent); if (messenger.Send(mms)) { msg.Status = IncomingMessage.ProcessingStatus.Processed; } else { msg.Status = IncomingMessage.ProcessingStatus.Error; msg.ErrorMsg = messenger.LastError.Message; } database.SaveChanges(); } else { logger.Error("Unable to establish a data connection through the modem. Check if you modem support MMS"); if (messenger.LastError != null) { logger.Error(messenger.LastError.ToString()); } } } else { // Employee not found msg.Status = IncomingMessage.ProcessingStatus.Error; msg.ErrorMsg = "Employee not found"; database.SaveChanges(); } } } } catch (Exception ex) { logger.Error("Error polling messages", ex); } finally { if (isDataConnectionAvailable) { messengerService.StartMessenger(); } this.timer.Enabled = true; } }