public void NotifyMms(string messageId) { try { //2b03219a-2e44-414d-817d-8314e1f42c42 _logger.DebugFormat("收到的messageId={0}的彩信上行操作", messageId); var unitId = UnitMapping.UnitId; if (!unitId.HasValue) return; _logger.DebugFormat("收到的UnitId={0}的彩信上行操作", unitId.Value); var openMasConfig = new OpenMasConfigRepository().GetOpenMasConfigByUnit(unitId.Value); //调用上行彩信获取接口获取短消息 var mms = new Mms(openMasConfig.MmsMasService); var message = mms.GetMessage(messageId); if (message == null) { _logger.DebugFormat("收到的UnitId={0}的彩信上行操作,但获取的信息为null", unitId.Value); return; } var notifyMessage = new NotifyMessage(); notifyMessage.ApplicationId = message.ApplicationId; notifyMessage.Content = message.Content; notifyMessage.ExtendCode = message.ExtendCode; notifyMessage.From = message.From; notifyMessage.MessageId = messageId; notifyMessage.MessageType = MessageType.Sms; notifyMessage.ReceivedTime = message.ReceivedTime; notifyMessage.To = message.To; notifyMessage.Title = message.Subject; notifyMessage.Unit = openMasConfig.Unit; _logger.DebugFormat("收到的messageId={0}的彩信内容为:{1}", messageId, message.Content); _logger.DebugFormat("全文序列化内容为:{0}", Newtonsoft.Json.JsonConvert.SerializeObject(message)); //业务逻辑,彩信内容可以从message中获取 _logger.InfoFormat("接收到OpenMas发来的彩信messageId={0}", messageId); var repository = new NotifyMessageRepository(); repository.SaveOrUpdate(notifyMessage); } catch (Exception ex) { _logger.ErrorFormat("接收到OpenMas发来的彩信messageId={0},但处理时发生异常{1}", messageId, ex); } }
public void SingleSend(NpcMmsSend npcMmsSend) { var trans = TransactionManager.BeginTransaction(); trans.Begin(); var mmsBuilder = new MmsBuilder(); try { var parList = new List<ParInfo>(); var config = _openMasConfigService.GetConfigOfUnit(npcMmsSend.Unit.Id); #region 创建彩信 var count = 1; foreach (var content in npcMmsSend.NpcMms.NpcMmsContents.OrderBy(o => o.OrderSort)) { if (npcMmsSend.NpcMms.NpcMmsContents.Count == 1&&string.IsNullOrEmpty(content.Content)) content.LayoutType = LayoutType.PicTop; var parInfo = new ParInfo(); parInfo.Dur = content.DueTime + "s"; var textContent = GetTextContent(content.Content + (count == npcMmsSend.NpcMms.NpcMmsContents.Count && !string.IsNullOrEmpty(config.Signature) ? "【" + config.Signature + "】" : string.Empty)); var picContent = GetMediaContent(content.UrlOfPic, MediaType.Pic); var voiceContent = GetMediaContent(content.UrlOfVoice, MediaType.Voice); if (content.LayoutType == LayoutType.PicBottom) { if (textContent != null) { mmsBuilder.AddContent(textContent); parInfo.Text = new TextInfo { Src = textContent.ContentId, Region = "text" }; } if (picContent != null) { mmsBuilder.AddContent(picContent); parInfo.Img = new ImgInfo { Src = picContent.ContentId, Region = "image" }; } } else { if (picContent != null) { mmsBuilder.AddContent(picContent); parInfo.Img = new ImgInfo { Src = picContent.ContentId, Region = "image" }; } if (textContent != null) { mmsBuilder.AddContent(textContent); parInfo.Text = new TextInfo { Src = textContent.ContentId, Region = "text" }; } } if (voiceContent != null) { parInfo.Audio = new AudioInfo { Src = voiceContent.ContentId }; mmsBuilder.AddContent(voiceContent); } count++; parList.Add(parInfo); } var smil = CommonUtil.BuilderSmil(GetLayoutInfo(npcMmsSend.NpcMms.LayoutType, "image", "text"), parList); mmsBuilder.AddContent(GetSmilContent(smil)); var mmsXml = mmsBuilder.BuildContentToXml(); var mms = new Mms(config.MmsMasService); string messageId; if (npcMmsSend.TimeOfExceptSend == null) { messageId = mms.SendMessage(npcMmsSend.NpcMmsReceivers.Select(o => o.TelNum).ToArray(), npcMmsSend.Title, mmsXml, config.MmsExtensionNo.ToString(CultureInfo.InvariantCulture), config.MmsAppAccount, config.MmsAppPwd); } else { messageId = mms.SendMessage(npcMmsSend.NpcMmsReceivers.Select(o => o.TelNum).ToArray(), npcMmsSend.Title, mmsXml, config.MmsExtensionNo.ToString(CultureInfo.InvariantCulture), config.MmsAppAccount, config.MmsAppPwd, npcMmsSend.TimeOfExceptSend.Value); } #endregion npcMmsSend.SendStatus = SendStatus.Done; npcMmsSend.MessageId = messageId; _npcMmsSendRepository.Save(npcMmsSend); trans.Commit(); _logger.ErrorFormat("npcMmsSendId={0}发送成功,返回MessageId:{1}", npcMmsSend.Id, messageId); } catch (Exception exception) { _logger.ErrorFormat("id={0}发送时出错:{1}", npcMmsSend.Id, exception); trans.Rollback(); throw; } }
/// <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; } }