/// <summary> /// Envia un mensaje de tipo ShipPositionReport al IDE /// </summary> /// <param name="msg">Mensaje ShipPositionReport</param> private string SendShipPositionReport(Message msg) { DataCenterLogic.DataCenterTypesIDE.ShipPositionReportType shipPositionReport = (DataCenterLogic.DataCenterTypesIDE.ShipPositionReportType)(msg.Body); //Send to IDE if (System.Configuration.ConfigurationManager.AppSettings["send2servers"] != "False") { shipPositionReport.test = DataCenterLogic.DataCenterTypesIDE.testType.Item0; ideClient.ShipPositionReport(shipPositionReport); log.Info("SendShipPositionReport: shipPositionReport sent to IDE"); } //ReferenceID puede ser de un report o "" si es SO //DataUserProvider deberia ser siempre 1005 var pricing = new PricingManager(); decimal?price = pricing.GetPriceForRequest(shipPositionReport.ReferenceId, shipPositionReport.DataUserProvider); //No tengo precio y es Es una reporte NO SAR? if (price == null) { log.Warn(string.Format("SendShipPositionReport: Se manda un reporte {0} de posicion sin precio", shipPositionReport.MessageId)); } //Guarda el mensaje en la base de datos using (var dao = new ShipPositionReportDataAccess()) { dao.Create(TypeHelper.Map2DB(shipPositionReport), 1, price); } log.Info(string.Format("ShipPositionReport stored: price {0}", price)); return(shipPositionReport.MessageId); }
/// <summary> /// Procesa un mensaje de tipo Receipt /// </summary> /// <param name="msg">El mensaje Receipt</param> public void ProcessReceipt(ReceiptType receipt) { decimal?price = null; if (receipt.ReceiptCode == receiptCodeType.Item0 && receipt.ReferenceId.Length > 0) { //Era un recibo correspondiente a un REQUERIMIENTO? var sprm = new ShipPositionRequestDataAccess(); if (sprm.RequestExists(receipt.ReferenceId) == true) { var pman = new PricingManager(); price = pman.GetPriceForRequest(receipt.ReferenceId, receipt.Originator); if (price == null) { log.Warn(string.Format("ProcessReceipt: Se recibio un receipt codigo 0 {0}, no podemos poner precio", receipt.MessageId)); } } else { log.Warn(string.Format("ProcessReceipt: recibo 0 {0} no referido a Requerimiento", receipt.MessageId)); } } using (var dao = new ReceiptDataAccess()) { dao.Create(TypeHelper.Map2DB(receipt), 0, price); } log.Info(string.Format("Receipt successfully processed: price {0}", price)); }
private string SendShipPositionRequest(Message msg) { DataCenterLogic.DataCenterTypesIDE.ShipPositionRequestType shipPositionRequest = (DataCenterLogic.DataCenterTypesIDE.ShipPositionRequestType)(msg.Body); //Send to IDE if (System.Configuration.ConfigurationManager.AppSettings["send2servers"] != "False") { shipPositionRequest.test = DataCenterLogic.DataCenterTypesIDE.testType.Item0; ideClient.ShipPositionRequest(shipPositionRequest); log.Info("SendShipPositionRequest: shipPositionRequest sent to IDE"); } if (ShipPositionRequestHelper.IsPeriodicRequest(shipPositionRequest.RequestType)) { //Es periodico, y pedimos sacarlo de la frecuencia estandard 6hs (RequestType==6), y no es SAR (msgtype==5)? //Debemos imputarnos la presunta reprogramacion PRICING if (shipPositionRequest.RequestType != DataCenterLogic.DataCenterTypesIDE.requestTypeType.Item6 && shipPositionRequest.MessageType == DataCenterLogic.DataCenterTypesIDE.messageTypeType1.Item5) { /************************************************/ var pmgr = new PricingManager(); decimal?price = pmgr.AddASPReprogrMessage(0, shipPositionRequest.DataUserRequestor, shipPositionRequest.DataUserProvider); log.Info(string.Format("SendShipPositionRequest: Reprogramacion presunta par {0} => precio={1}", shipPositionRequest.MessageId, price)); /************************************************/ } } //Guarda el mensaje en la base de datos using (var dao = new ShipPositionRequestDataAccess()) { dao.Create(TypeHelper.Map2DB(shipPositionRequest), 1); } log.Info("ShipPositionRequest stored"); return(shipPositionRequest.MessageId); }
/// <summary> /// Envia un mensaje de tipo Receipt al IDE /// </summary> /// <param name="msg">Mensaje Receipt</param> private string SendReceipt(Message msg) { DataCenterLogic.DataCenterTypesIDE.ReceiptType receipt = (DataCenterLogic.DataCenterTypesIDE.ReceiptType)(msg.Body); //Send to IDE if (System.Configuration.ConfigurationManager.AppSettings["send2servers"] != "False") { receipt.test = DataCenterLogic.DataCenterTypesIDE.testType.Item0; ideClient.Receipt(receipt); log.Info("SendReceipt: receipt sent to IDE"); } //Get price for receipt //El codigo 0 dice todo, si es codigo 0 quiere decir que este recibo se esta mandando por que no se pudo mandar una posicion //Por lo tanto el request debe existir o sino fue de una standing order. decimal?price = null; if (receipt.ReceiptCode == 0) { var pman = new PricingManager(); price = pman.GetPriceForRequest(receipt.ReferenceId, receipt.Originator); if (price == null) { log.Warn(string.Format("SendReceipt: Se manda un receipt codigo 0 {0} sin precio", receipt.MessageId)); } } //Guarda el mensaje en la base de datos using (var dao = new ReceiptDataAccess()) { dao.Create(TypeHelper.Map2DB(receipt), 1, price); } log.Info(string.Format("Receipt stored: price {0}", price)); return(receipt.MessageId); }
/// <summary> /// Procesa un mensaje de tipo ShipPositionRequest /// </summary> /// <param name="msg">Mensaje de la cola con ShipPositionRequest en el body</param> public void ProcessShipPositionRequest(ShipPositionRequestType shipPositionRequest) { log.Info("Procesando ShipPositionRequest"); try { if (ValidateShipPositionRequest(shipPositionRequest) != true) { return; } } catch (Exception ex) { log.Debug(ex); } Ship ship = null; using (var sdao = new ShipDataAccess()) { log.Debug("Verifica si existe el barco en la base de datos"); ship = sdao.getByIMONum(shipPositionRequest.IMONum); if (ship == null) { //The ship is not in our system, send receipt CODE 7 log.Debug("el barco no esta en nuestro sistema, se envia recibo codigo 7"); ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7, string.Format("The ship {0} is not registered", shipPositionRequest.IMONum)); return; } } //Save ship position request to obtain id log.Debug("Guardando SPR en base de datos para obtener id"); var shipPositionRequestDB = TypeHelper.Map2DB(shipPositionRequest); try { using (var spreq = new ShipPositionRequestDataAccess()) { spreq.Create(shipPositionRequestDB, 0); } } catch (Exception ex) { log.Error("Hubo un problema al guardar el mensaje"); log.Debug(ex); return; } //log.Debug("Verificando si ya hay un request activo para el barco"); //using (var asprDa = new ActiveShipPositionRequestDataAccess()) //{ // List<ActiveShipPositionRequest> asprlist = asprDa.GetAll(); // foreach (ActiveShipPositionRequest aspr in asprlist) // { // if (shipPositionRequest.IMONum == aspr.ShipPositionRequest.IMONum && // Convert.ToInt32(shipPositionRequest.AccessType) == aspr.ShipPositionRequest.AccessType && // shipPositionRequest.DataUserRequestor == aspr.ShipPositionRequest.DataUserRequestor // ) // { // log.Debug("Se reemplaza request del barco:" + shipPositionRequest.IMONum); // asprDa.Remove(aspr); // } // } //} #region Historics position report log.Debug("Verificando si es un request para datos archivados"); if (ShipPositionRequestHelper.IsHistoricRequest(shipPositionRequest.RequestType)) { ProcessHistoricShipPositionRequest(shipPositionRequest, ship); return; } #endregion #region Periodic position report log.Debug("Verificando si es un request periodico"); if (ShipPositionRequestHelper.IsPeriodicRequest(shipPositionRequest.RequestType)) { ActiveShipPositionRequestManager asprManager = new ActiveShipPositionRequestManager(); asprManager.AddOrReplace(shipPositionRequestDB); log.Debug("ASPR REPLACED: Barco:" + shipPositionRequest.IMONum); log.Debug("Requestor: " + shipPositionRequest.DataUserRequestor); log.Debug("Access-Request Type: " + shipPositionRequest.AccessType + "-" + shipPositionRequest.RequestType); //Es periodico, y nos saca de la frecuencia estandard 6hs (RequestType==6), y no es SAR (msgtype==5)? //Debemos cobrarle la presunta reprogramacion PRICING if (shipPositionRequest.RequestType != requestTypeType.Item6 && shipPositionRequest.MessageType == messageTypeType1.Item5) { /************************************************/ var pmgr = new PricingManager(); decimal?price = pmgr.AddASPReprogrMessage(0, shipPositionRequest.DataUserRequestor, shipPositionRequest.DataUserProvider); log.Info(string.Format("ProcessShipPositionRequest: Reprogramacion presunta par {0} => precio={1}", shipPositionRequest.MessageId, price)); /************************************************/ } return; } #endregion #region One Time poll log.Debug("Verificando si es un request para unica vez"); if (ShipPositionRequestHelper.IsOneTimePoll(shipPositionRequest.RequestType)) { ActiveShipPositionRequestManager asprManager = new ActiveShipPositionRequestManager(); //Crea Active asprManager.AddNew(shipPositionRequestDB.Id); log.Debug("ASPR ADDED: Barco:" + shipPositionRequest.IMONum); log.Debug("Requestor: " + shipPositionRequest.DataUserRequestor); log.Debug("Access-Request Type: " + shipPositionRequest.AccessType + "-" + shipPositionRequest.RequestType); } #endregion #region Reset Request log.Debug("Verificando si es un request reset"); if (ShipPositionRequestHelper.IsResetRequest(shipPositionRequest.RequestType)) { var asprManager = new ActiveShipPositionRequestManager(); asprManager.RemoveAllForRequestor(shipPositionRequestDB); } #endregion #region Stop Request log.Debug("Verificando si es un request Stop"); if (ShipPositionRequestHelper.IsStopRequest(shipPositionRequest.RequestType)) { ActiveShipPositionRequestManager asprManager = new ActiveShipPositionRequestManager(); asprManager.AddOrReplace(shipPositionRequestDB); log.Debug("ASPR REPLACED: Barco:" + shipPositionRequest.IMONum); log.Debug("Requestor: " + shipPositionRequest.DataUserRequestor); log.Debug("Access-Request Type: " + shipPositionRequest.AccessType + "-" + shipPositionRequest.RequestType); } #endregion return; }
/// <summary> /// Procesa un mensaje de tipo ShipPositionReport /// </summary> /// <param name="msg">El mensaje ShipPositionReport</param> public void ProcessShipPositionReport(DataCenterTypes.ShipPositionReportType shipPositionReport) { var configMgr = new ConfigurationManager(); //Verifica si existe el Data User que requiere la información var cgm = new ContractingGovermentManager(); var ddpm = new DDPVersionManager(); var ddpVersion = ddpm.GetCurrentDDPVersion(); var contractingGoverment = cgm.GetContractingGovermentByLRITId(shipPositionReport.DataUserRequestor, ddpVersion.Id); if (contractingGoverment == null) { string strError = string.Format("Specified LDU '{0}' does not exist", shipPositionReport.DataUserRequestor); //Arma mensaje de Recibo DataCenterLogic.DataCenterTypes.ReceiptType receipt = new DataCenterLogic.DataCenterTypes.ReceiptType(); receipt.DDPVersionNum = DDPVersionManager.currentDDP(); receipt.Destination = shipPositionReport.DCId; receipt.Message = strError; receipt.MessageId = MessageIdManager.Generate(); receipt.MessageType = DataCenterLogic.DataCenterTypes.messageTypeType3.Item7; receipt.Originator = configMgr.Configuration.DataCenterID; receipt.ReceiptCode = DataCenterLogic.DataCenterTypes.receiptCodeType.Item7; receipt.ReferenceId = shipPositionReport.MessageId; receipt.schemaVersion = decimal.Parse(configMgr.Configuration.SchemaVersion); receipt.test = DataCenterLogic.DataCenterTypes.testType.Item1; receipt.TimeStamp = DateTime.UtcNow; Message msgout = new Message(receipt); msgout.Label = "receipt"; //Encola mensaje QueueManager.Instance().EnqueueOut(msgout); log.Error(strError); return; } //Verifica si el existe el Id de referencia if (shipPositionReport.ReferenceId != "") { var sprda = new ShipPositionRequestDataAccess(); if (sprda.RequestExists(shipPositionReport.ReferenceId) == false) { string strError = string.Format("MessageID not found for ReferenceID '{0}'", shipPositionReport.ReferenceId); //Envía Recibo DataCenterLogic.DataCenterTypes.ReceiptType receipt = new DataCenterLogic.DataCenterTypes.ReceiptType(); receipt.DDPVersionNum = DDPVersionManager.currentDDP(); receipt.Destination = shipPositionReport.DCId; receipt.Message = strError; receipt.MessageId = MessageIdManager.Generate(); receipt.MessageType = DataCenterLogic.DataCenterTypes.messageTypeType3.Item7; receipt.Originator = configMgr.Configuration.DataCenterID; receipt.ReceiptCode = DataCenterLogic.DataCenterTypes.receiptCodeType.Item7; receipt.ReferenceId = shipPositionReport.MessageId; receipt.schemaVersion = decimal.Parse(configMgr.Configuration.SchemaVersion); receipt.test = DataCenterLogic.DataCenterTypes.testType.Item1; receipt.TimeStamp = DateTime.UtcNow; Message msgout = new Message(receipt); msgout.Label = "receipt"; //Envia mensaje QueueManager.Instance().EnqueueOut(msgout); log.Error(strError); return; } } //ReferenceID puede ser de un report o "" si es SO //DataUserProvider es el usuario que nos manda (deberiamos tener contrato con el) var pricing = new PricingManager(); decimal?price = pricing.GetPriceForRequest(shipPositionReport.ReferenceId, shipPositionReport.DataUserProvider); //No tengo precio? if (price == null) { log.Warn(string.Format("SendShipPositionReport: Se recibio un reporte {0} de posicion, no podemos poner precio", shipPositionReport.MessageId)); } //Save position report to DB using (var dao = new ShipPositionReportDataAccess()) { dao.Create(TypeHelper.Map2DB(shipPositionReport), 0, price); } log.Info(string.Format("ShipPositionReport successfully processed: price {0}", price)); }
/// <summary> /// Esta funcion es llamada cuando hay un mensaje nuevo en la cola de entrada /// </summary> /// <param name="sender">Object Sender</param> /// <param name="e">Async events</param> private void queue_PeekCompleted(object sender, PeekCompletedEventArgs e) { using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromMinutes(15))) { Message msg = null; string msgId = "NA"; try { msg = inputQueue.Receive(MessageQueueTransactionType.Automatic); if (msg.Label == "receipt") { log.Debug("Se detecto receipt en queue..."); var receipt = (DataCenterLogic.DataCenterTypes.ReceiptType)(msg.Body); msgId = receipt.MessageId; var rman = new ReceiptManager(); rman.ProcessReceipt(receipt); } else if (msg.Label == "pricingNotification") { log.Debug("Se detecto pricingNotification en queue..."); var pricingNotification = (PricingNotificationType)(msg.Body); msgId = pricingNotification.MessageId; var pman = new PricingManager(); pman.ProcessPricingNotification(pricingNotification); } else if (msg.Label == "shipPositionReport") { log.Debug("Se detecto shipPositionReport en queue..."); var shipPositionReport = (DataCenterLogic.DataCenterTypes.ShipPositionReportType)(msg.Body); msgId = shipPositionReport.MessageId; var sprman = new ShipPositionReportManager(); sprman.ProcessShipPositionReport(shipPositionReport); } else if (msg.Label == "pricingUpdate") { log.Debug("Se detecto pricingUpdate en queue..."); var pricingUpdate = (DataCenterLogic.DataCenterTypes.PricingUpdateType)(msg.Body); msgId = pricingUpdate.MessageId; var pman = new PricingManager(); pman.ProcessPricingUpdate(pricingUpdate); } else if (msg.Label == "SARSURPICRequest") { log.Debug("Se detecto SARSURPICRequest en queue..."); var SARSURPICRequest = (DataCenterLogic.DataCenterTypes.SARSURPICType)(msg.Body); msgId = SARSURPICRequest.MessageId; var ssman = new SARSURPICManager(); ssman.ProcessSARSURPICRequest(SARSURPICRequest); } else if (msg.Label == "ddpNotification") { log.Debug("Se detecto ddpNotification en queue..."); var ddpNotification = (DataCenterLogic.DataCenterTypes.DDPNotificationType)(msg.Body); msgId = ddpNotification.MessageId; var DDPman = new DDPManager(); DDPman.ProcessDDPNotification(ddpNotification); } else if (msg.Label == "ddpUpdate") { log.Debug("Se detecto ddpUpdate en queue..."); var DDPman = new DDPManager(); var ddpUpdate = (DataCenterTypes.DDPUpdateType)(msg.Body); msgId = ddpUpdate.MessageId; DDPman.ProcessDDPUpdate(ddpUpdate); } else if (msg.Label == "shipPositionRequest") { log.Debug("Se detecto shipPositionRequest en queue..."); var shipPositionRequest = (DataCenterTypes.ShipPositionRequestType)(msg.Body); msgId = shipPositionRequest.MessageId; var sprm = new ShipPositionRequestManager(); sprm.ProcessShipPositionRequest(shipPositionRequest); } else if (msg.Label == "systemStatus") { log.Debug("Se detecto systemStatus en queue..."); var systemStatus = (DataCenterTypes.SystemStatusType)(msg.Body); msgId = systemStatus.MessageId; var ssm = new SystemStatusManager(); ssm.ProcessSystemStatus(systemStatus); } else if (msg.Label == "") { log.Debug("Se detecto mensaje del ASP en queue... tipo:"); var type = msg.Body.GetType().ToString(); switch (type) { case "Common.PositionMessage": { log.Debug("Position Message"); var aspPos = (Common.PositionMessage)(msg.Body); var spm = new ShipPositionManager(); spm.ProcessASPPosition(aspPos); break; } case "Common.PollResponse": { log.Debug("PollResponse"); var aspPollResponse = (Common.PollResponse)(msg.Body); var spm = new ShipManager(); spm.ProcessPollResponse(aspPollResponse); break; } case "Common.HeartBeatMessage": { log.Debug("HeartBeat"); var aspHb = (Common.HeartBeatMessage)(msg.Body); var spm = new SystemStatusManager(); spm.ProcessAspHeartBeat(aspHb); break; } default: { break; } } } else { log.Error(string.Format("Mensaje no conocido en cola '{0}'", msg.Label)); } scope.Complete(); wrong_count = 0; //Dump message to disk try { if (System.Configuration.ConfigurationManager.AppSettings["save_messages"] == "yes") { string folder = System.Configuration.ConfigurationManager.AppSettings["save_folder"]; if (folder == string.Empty) { folder = "c:\\msgs"; } string fullpath = string.Format("{0}\\{1:yyyyMMdd}\\in", folder, DateTime.UtcNow); Directory.CreateDirectory(fullpath); string xmlstr = OutputMessageManager.messageToString(msg); string lbl = msg.Label; if (lbl == "") { lbl = msg.Body.GetType().ToString(); } File.WriteAllText(string.Format("{0}\\{1}-{2}.txt", fullpath, lbl, msgId), xmlstr); } } catch (Exception ex) { log.Error("Error intentando guardar mensaje de entrada en disco", ex); } } catch (Exception ex) { wrong_count++; try { log.Error("Error handling message" + Environment.NewLine + Environment.NewLine + ex + Environment.NewLine + Environment.NewLine); } catch { } //Poison messages if (wrong_count > 3) { wrong_count = 0; try { var serializer = new System.Xml.Serialization.XmlSerializer(msg.Body.GetType()); var stringWriter = new System.IO.StringWriter(); serializer.Serialize(stringWriter, msg.Body); scope.Complete(); log.Error("MSGDUMP: " + stringWriter.ToString()); } catch (Exception ex2) { log.Error("UNABLE TO DUMP MESSAGE: " + ex.Message, ex2); } } } } inputQueue.BeginPeek(); }