/// <summary>
        /// Valida la combinacion de accesType y requestType para un ShipPositionRequest.
        /// Si hay algun error esta funcion envia un Receipt al requisitor, informandole el problema.
        /// </summary>
        /// <param name="shipPositionRequest">El mensaje ShipPositionRequest a ser analizado.</param>
        /// <returns>Verdadero, si el mensaje es valido, False otra cosa</returns>
        private bool ValidateShipPositionRequest(DataCenterLogic.DataCenterTypes.ShipPositionRequestType shipPositionRequest)
        {
            var ddpm       = new DDPVersionManager();
            var ddpVersion = ddpm.GetCurrentDDPVersion();

            log.Info("Validando SPR");
            #region Port state with time trigger could not be used with RequestType 1,7 or 9:
            //log.Debug("Port state with time trigger could not be used with RequestType 1,7 or 9");
            if (shipPositionRequest.AccessType == DataCenterLogic.DataCenterTypes.accessTypeType.Item3 &&
                (shipPositionRequest.RequestType == DataCenterLogic.DataCenterTypes.requestTypeType.Item1 ||
                 shipPositionRequest.RequestType == DataCenterLogic.DataCenterTypes.requestTypeType.Item7 ||
                 shipPositionRequest.RequestType == DataCenterLogic.DataCenterTypes.requestTypeType.Item9))
            {
                log.Error("Validacion erronea, se envia receipt");
                ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                           string.Format("Invalid request parameters: Port could not be used with RequestType 1,7 or 9"));
                return(false);
            }
            #endregion
            #region Access type 0 only with message 4 and request 0
            //log.Debug("AccessType 0 (reset) solo valido con RT 0 y MS 4");
            if (shipPositionRequest.AccessType == DataCenterLogic.DataCenterTypes.accessTypeType.Item0 || shipPositionRequest.RequestType == requestTypeType.Item0)
            {
                //Invalid request parameters: AccessType 0 only valid with MessageType 4
                if (shipPositionRequest.MessageType != DataCenterLogic.DataCenterTypes.messageTypeType1.Item4)
                {
                    ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                               string.Format("Invalid request parameters: AccessType 0 only valid with MessageType 4"));
                    log.Error("Validacion erronea, se envia receipt");
                    return(false);
                }

                // Invalid request parameters: AccessType 0 only valid with RequestType 0
                if (shipPositionRequest.RequestType != DataCenterLogic.DataCenterTypes.requestTypeType.Item0 || shipPositionRequest.AccessType != DataCenterLogic.DataCenterTypes.accessTypeType.Item0)
                {
                    ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                               string.Format("Invalid request parameters: AccessType 0 only valid with RequestType 0"));
                    log.Info("Validacion erronea, se envia receipt");
                    return(false);
                }
            }
            #endregion
            #region REVISAR

            /*
             * log.Debug("Port State");
             * //Invalid request parameters: Port state and RequestTypes 1,7 and 9 only valid with AccessType 5
             * if( shipPositionRequest.ItemElementName == DataCenterLogic.DataCenterTypes.ItemChoiceType.Port ||
             *  shipPositionRequest.ItemElementName == DataCenterLogic.DataCenterTypes.ItemChoiceType.PortFacility
             *  && ( shipPositionRequest.RequestType == DataCenterLogic.DataCenterTypes.requestTypeType.Item1 ||
             *       shipPositionRequest.RequestType == DataCenterLogic.DataCenterTypes.requestTypeType.Item7 ||
             *       shipPositionRequest.RequestType == DataCenterLogic.DataCenterTypes.requestTypeType.Item9 )
             *  && shipPositionRequest.AccessType != DataCenterLogic.DataCenterTypes.accessTypeType.Item5 )
             * {
             * ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
             *  string.Format("Invalid request parameters: Port state and RequestTypes 1,7 and 9 only valid with AccessType 5"));
             *
             * return false;
             * } */
            #endregion
            #region Invalid request parameters: RequestTypes 1,7 and 9 are the only valid for MessageType 5
            //log.Debug("Si es mensaje de tipo 5 (Sar) RT solo puede ser 1 o 9");
            if (shipPositionRequest.MessageType == DataCenterLogic.DataCenterTypes.messageTypeType1.Item5 &&
                shipPositionRequest.RequestType != DataCenterLogic.DataCenterTypes.requestTypeType.Item1 &&
                shipPositionRequest.RequestType != DataCenterLogic.DataCenterTypes.requestTypeType.Item7 &&
                shipPositionRequest.RequestType != DataCenterLogic.DataCenterTypes.requestTypeType.Item9)
            {
                log.Error("Validacion erronea, se envia receipt");
                ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                           string.Format("Invalid request parameters: RequestTypes 1,7 and 9 are the only valid for MessageType 5"));
                return(false);
            }
            #endregion
            #region Distance is only valid with access type 3
            log.Debug("Distance is only valid with access type 3");
            if (shipPositionRequest.Distance != "0" && shipPositionRequest.AccessType != DataCenterLogic.DataCenterTypes.accessTypeType.Item3)
            {
                log.Info("Validacion erronea, se envia receipt");
                ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                           string.Format("Invalid request parameters: Distance is only valid with access type 3"));
                return(false);
            }
            #endregion
            #region Request Duration not valid for one time poll
            log.Debug("Request Duration not valid for one time poll");
            if (ShipPositionRequestHelper.IsOneTimePoll(shipPositionRequest.RequestType) &&
                (shipPositionRequest.RequestDuration != null && (shipPositionRequest.RequestDuration.startTimeSpecified == true || shipPositionRequest.RequestDuration.stopTimeSpecified == true)))
            {
                log.Info("Validacion erronea, se envia receipt");
                ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                           string.Format("Invalid request parameters: Request Duration not valid for one time poll"));
                return(false);
            }
            #endregion
            #region Port State
            log.Debug("Validacion Para PortStates");
            if (shipPositionRequest.AccessType == DataCenterLogic.DataCenterTypes.accessTypeType.Item3 ||
                shipPositionRequest.AccessType == DataCenterLogic.DataCenterTypes.accessTypeType.Item5)
            {
                if (shipPositionRequest.ItemElementName != ItemChoiceType.Port && shipPositionRequest.ItemElementName != ItemChoiceType.PortFacility)
                {
                    log.Info("invalid fields for port state request, ItemElementName not port or portfacility");
                    ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                               string.Format("Invalid fields for port state request, ItemElementName not port or portfacility"));
                    return(false);
                }
                string portName = shipPositionRequest.Item.ToString();
                using (var pda = new PlaceDataAccess())
                {
                    if (pda.PortExists(portName, ddpVersion.Id) == false && pda.PortFacilityExists(portName, ddpVersion.Id) == false)
                    {
                        log.Info("invalid fields for port state request, Item" + shipPositionRequest.Item.ToString() + " doesn't exists");
                        ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                                   string.Format("invalid fields for port state request, Item" + shipPositionRequest.Item.ToString() + " doesn't exists")
                                                   );
                        return(false);
                    }
                }
            }
            #endregion
            #region AccessType2
            log.Debug("Validacion Para AccessType2");
            if (shipPositionRequest.AccessType == DataCenterLogic.DataCenterTypes.accessTypeType.Item2 &&
                shipPositionRequest.RequestType != requestTypeType.Item7)
            {
                ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                           string.Format("Access type 2 only valid for archived data"));
                return(false);
            }
            #endregion
            //Verifica si existe el Data User que requiere la información
            var cgm = new ContractingGovermentManager();

            var contractingGoverment = cgm.GetContractingGovermentByLRITId(shipPositionRequest.DataUserRequestor, ddpVersion.Id);
            if (contractingGoverment == null)
            {
                string strError = string.Format("Specified LDU '{0}' does not exist", shipPositionRequest.DataUserRequestor);

                //Arma mensaje de Recibo
                DataCenterLogic.DataCenterTypes.ReceiptType receipt = new DataCenterLogic.DataCenterTypes.ReceiptType();

                var cmgr = new ConfigurationManager();

                receipt.DDPVersionNum = DDPVersionManager.currentDDP();
                receipt.Destination   = shipPositionRequest.DataUserRequestor;
                receipt.Message       = strError;
                receipt.MessageId     = MessageIdManager.Generate();
                receipt.MessageType   = DataCenterLogic.DataCenterTypes.messageTypeType3.Item7;
                receipt.Originator    = cmgr.Configuration.DataCenterID;
                receipt.ReceiptCode   = DataCenterLogic.DataCenterTypes.receiptCodeType.Item7;
                receipt.ReferenceId   = shipPositionRequest.MessageId;
                receipt.schemaVersion = decimal.Parse(cmgr.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(false);
            }

            log.Info("Validacion Ok");
            return(true);
        }
示例#2
0
        /// <summary>
        /// Verifica si la ultima posicion de cada uno de nuestros barcos esta en alguna standing order.
        /// Tinen en cuenta hay un STOP para ese requestor.
        /// Tiene en cuenta la titulacion
        /// </summary>
        public void Process()
        {
            ShipDataAccess sda = null;

            try
            {
                sda = new ShipDataAccess();

                var cgm        = new ContractingGovermentManager();
                var ddpm       = new DDPVersionManager();
                var ddpVersion = ddpm.GetCurrentDDPVersion();

                log.Info("Procesando standing orders.");
                foreach (Ship ship in sda.GetAll())
                {
                    ShipPosition pos = null;
                    using (var spda = new ShipPositionDataAccess())
                    {
                        pos = spda.GetLastShipPosition(ship.IMONum);
                    }

                    if (pos == null)
                    {
                        log.Info(string.Format("No hay ultima posicion para {0}", ship.IMONum));
                        continue;
                    }

                    List <StandingOrder> orders = new List <StandingOrder>();
                    using (var soda = new StandingOrderDataAccess())
                    {
                        orders = soda.GetOrdersForPosition(pos, ddpVersion);
                    }

                    if (orders.Count == 0)
                    {
                        log.Info(string.Format("No hay ninguna SO para {0},posid:{1},ddpVer:{2}", ship.IMONum, pos.Id, ddpVersion.Id));
                        continue;
                    }

                    var sprm = new ShipPositionReportManager();

                    foreach (StandingOrder order in orders)
                    {
                        var cg = order.Place.ContractingGoverment;

                        //Skipeo las SO de argentina (no chekeo si mis barcos estan en mis SO)
                        if (cg.LRITId == 1005)
                        {
                            continue;
                        }

                        using (var asprda = new ActiveShipPositionRequestDataAccess())
                        {
                            if (asprda.IsStopRequired(ship, cg.LRITId.ToString()) == true)
                            {
                                log.Info("Stop Activo Para el Barco: " + ship.IMONum + " Requestor: " + cg.LRITId.ToString());
                                continue;
                            }
                        }

                        log.Info(string.Format("El barco {0} esta dentro de la standing order {1}", ship.IMONum, order.PlaceId));

                        if (cgm.IsEntitled(cg, pos, ddpVersion, false) == false)
                        {
                            log.Info("no esta titulado para recibir la informacion, se envia recibo");
                            ReceiptManager.SendReceipt(cg.LRITId.ToString(), "0", DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item0,
                                                       string.Format("Not entitled to recieve"));
                        }
                        else
                        {
                            sprm.SendReport(cg.LRITId.ToString(), pos, "", DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item1, DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Error procesando StandingOrders", ex);
            }
            finally
            {
                sda.Dispose();
            }
        }
        /// <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 pedido de posicion historica.
        /// Los pedidos de posicion historicas son RequestType 7 y RequestType 9
        /// </summary>
        /// <param name="shipPositionRequest">Mensaje de ShipPositionRequest</param>
        /// <param name="ship">Barco al que se hace referencia</param>
        private void ProcessHistoricShipPositionRequest(DataCenterLogic.DataCenterTypes.ShipPositionRequestType shipPositionRequest, Ship ship)
        {
            log.Info("Procesando ShipPositionRequest Historico");
            log.Debug("Obteniendo CgId de LRITId");

            //Obtener Contracting goverment ID
            ContractingGovermentManager cgmgr = new ContractingGovermentManager();
            var ddpm       = new DDPVersionManager();
            var currentDDP = ddpm.GetCurrentDDPVersion();

            var contractingGoverment = cgmgr.GetContractingGovermentByLRITId(shipPositionRequest.DataUserRequestor, currentDDP.Id);

            if (contractingGoverment == null)
            {
                log.Info("CGID no válido se envia recibo");
                ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                           string.Format("Invalid Contracting Government ID"));
                return;
            }

            ShipPositionManager spm = new ShipPositionManager();

            List <ShipPosition> positions = new List <ShipPosition>();

            log.Debug("Verificando si se pide ultima posicion");
            if (ShipPositionRequestHelper.IsMostRecentPosition(shipPositionRequest.RequestType))
            {
                ShipPosition pos = spm.GetLastShipPosition(shipPositionRequest.IMONum);
                if (pos != null)
                {
                    positions.Add(pos);
                }
            }
            else
            {
                log.Debug("No, se piden mas posiciones");
                log.Debug("Verificando titulacion");
                positions = spm.GetShipPositionHistory(shipPositionRequest.IMONum,
                                                       shipPositionRequest.RequestDuration.startTime,
                                                       shipPositionRequest.RequestDuration.stopTime);
            }

            if (positions.Count == 0)
            {
                log.Info("No hay posiciones");
                //The ship didnt send message in that period
                ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item6,
                                           string.Format("No archived data for the request"));
                return;
            }

            var configMgr = new ConfigurationManager();

            log.Debug("Procesando Posiciones");

            int not_entitled_count = 0;

            var ddpVersion = ddpm.DDPFromDate(DateTime.UtcNow);
            var cgOld      = cgmgr.GetContractingGovermentByLRITId(shipPositionRequest.DataUserRequestor, ddpVersion.Id);

            foreach (ShipPosition position in positions)
            {
                bool verifyWatersOf = true;
                if (shipPositionRequest.AccessType == accessTypeType.Item3 || shipPositionRequest.AccessType == accessTypeType.Item5)
                {
                    verifyWatersOf = false;
                }

                //Solo valido titulacion si el access type es distinto a 6 (SAR)
                if (shipPositionRequest.AccessType != accessTypeType.Item6 && (ddpVersion == null || cgmgr.IsEntitled(cgOld, position, ddpVersion, verifyWatersOf) == false))
                {
                    not_entitled_count++;
                    continue;
                }

                var sprm = new ShipPositionReportManager();

                if (ShipPositionRequestHelper.IsMostRecentPosition(shipPositionRequest.RequestType))
                {
                    sprm.SendReport(shipPositionRequest.DataUserRequestor, position, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item4, DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item3);
                }
                else
                {
                    if (shipPositionRequest.AccessType == accessTypeType.Item3 || shipPositionRequest.AccessType == accessTypeType.Item5)
                    {
                        sprm.SendReport(shipPositionRequest.DataUserRequestor, position, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item3, DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item1);
                    }
                    else if (shipPositionRequest.AccessType == accessTypeType.Item1)
                    {
                        sprm.SendReport(shipPositionRequest.DataUserRequestor, position, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item1, DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item1);
                    }
                    else if (shipPositionRequest.AccessType == accessTypeType.Item2)
                    {
                        sprm.SendReport(shipPositionRequest.DataUserRequestor, position, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item2, DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item1);
                    }
                    else
                    {
                        sprm.SendReport(shipPositionRequest.DataUserRequestor, position, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item4, DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item1);
                    }
                }

                log.Debug("Report Enviado");
            }

            //Hubo alguna posicion para la que no tuvo permiso
            if (not_entitled_count != 0)
            {
                ReceiptManager.SendReceipt(shipPositionRequest.DataUserRequestor, shipPositionRequest.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                           string.Format("Not entitled to recieve {0} positions", not_entitled_count));
                log.Debug("Receipt Enviado");
            }

            return;
        }
        /// <summary>
        /// Esta es la funcion principal del manejador de pedidos activos.
        /// Itera y procesa todos los pedidos activo.
        /// Es encargada de enviar (cuando se cumplan las condiciones) los mensajes a la cola de salida.
        /// </summary>
        public void Process(int hour, int mins) //hora: 0-23, minutes={0, 15, 30, 45}
        {
            //log.Info("Procesando Active Ship Position Request");
            using (var asprda = new ActiveShipPositionRequestDataAccess())
            {
                List <ActiveShipPositionRequest> asprlist = asprda.GetAll();
                foreach (ActiveShipPositionRequest aspr in asprlist)
                {
                    try
                    {
                        //Verifica si el tiempo de inicio ya paso
                        var      spm    = new ShipPositionManager();
                        var      spr    = aspr.ShipPositionRequest;
                        DateTime UtcNow = DateTime.UtcNow;
                        //log.Debug("Verificando estado del ASPR");
                        if (aspr.Status == 0)
                        {
                            //Condicion para activar el envio
                            if (RequestReadyToActive(aspr, UtcNow) == true)
                            {
                                if (TryReprog(spr) == true)
                                {
                                    asprda.Update(aspr.Id, 1, UtcNow);
                                    log.Debug("Reprogramado OK ==> estado ACTIVO");
                                }
                                else
                                {
                                    log.Error("Reprogramado ERROR");
                                }
                            }
                        }
                        else
                        {
                            //Status = 1
                            if (spr.StopTime != null && UtcNow >= spr.StopTime)
                            {
                                var IMONum         = aspr.ShipPositionRequest.IMONum;
                                var currentReqType = ShipPositionRequestHelper.GetMinutes(aspr.ShipPositionRequest.RequestType);
                                var minReqType     = GetMinPeriodForShip(IMONum, 0);

                                log.Debug("Se cumplio Stop Time del request eliminando request activo");

                                asprda.Remove(aspr);

                                //Verifica si el request tiene frecuencia distinta a la actual y reprograma si es necesario
                                //si el request caduco reprograma para que reporte cada 6 horas
                                log.Debug("Verificando si hay que reprogramar el equipo");
                                if (minReqType == currentReqType)
                                {
                                    using (var sda = new ShipDataAccess())
                                    {
                                        int newMinPeriod = GetMinPeriodForShip(IMONum, 0);

                                        if (newMinPeriod == -1)
                                        {
                                            newMinPeriod = 360;
                                        }

                                        //Reprogram ASP
                                        try
                                        {
                                            var aspman = new AspManager();
                                            aspman.CreatePollMessage(DateTime.UtcNow.AddMinutes(8), newMinPeriod, sda.getByIMONum(spr.IMONum), PollAction.Reprogram);
                                        }
                                        catch (Exception ex)
                                        {
                                            ShipManager.ChangeShipStatus(ShipStatus.Error, sda.getByIMONum(spr.IMONum));
                                            log.Error("Hubo un error en la reprogramacion del barco" + Environment.NewLine + Environment.NewLine + ex + Environment.NewLine + Environment.NewLine);
                                        }
                                    }
                                }
                                //log.Debug("ASPR " + aspr.Id + " REMOVED: Status: " + aspr.Status + " Barco:" + spr.IMONum);
                                //log.Debug("Requestor: " + spr.DataUserRequestor);
                                //log.Debug("Access-Request Type: " + spr.AccessType + "-" + spr.RequestType);
                                continue;
                            }

                            //log.Debug("Verificando si es request de unica vez");
                            //ONE TIME POLL
                            if (ShipPositionRequestHelper.IsOneTimePoll(spr.RequestType) == true)
                            {
                                //log.Debug("verificando si hay nueva posicion registrada");
                                ShipPosition spos = spm.GetLastShipPosition(spr.IMONum);
                                if (spos != null && spos.TimeStamp > aspr.LastTime)
                                {
                                    log.Debug("Nueva posicion con fecha > LastTime ... verificando titulacion");
                                    CheckEntitlementAndSendReportOrReceipt(spr, spos);
                                    asprda.Remove(aspr);
                                    ShipManager.ChangeShipStatus(ShipStatus.Ok, ShipManager.getByIMONum(spr.IMONum));
                                    log.Debug("ASPR " + aspr.Id + " REMOVED: Status: " + aspr.Status + " Barco:" + spr.IMONum);
                                    log.Debug("Requestor: " + spr.DataUserRequestor);
                                    log.Debug("Access-Request Type: " + spr.AccessType + "-" + spr.RequestType);
                                    continue;
                                }
                            }
                            //PERIODIC REPORT
                            else if (ShipPositionRequestHelper.IsPeriodicRequest(spr.RequestType) == true)
                            {
                                if (spr.RequestType == 2 && new int[] { 0, 15, 30, 45 }.Contains(mins) == false) //15 minute periodic rate
                                {
                                    continue;
                                }
                                if (spr.RequestType == 3 && new int[] { 0, 30 }.Contains(mins) == false) //30 minute periodic rate
                                {
                                    continue;
                                }
                                if (spr.RequestType == 4 && mins != 0) //1 hour periodic rate
                                {
                                    continue;
                                }
                                if (spr.RequestType == 5 && (new int[] { 0, 3, 6, 9, 12, 15, 18, 21 }.Contains(hour) == false || mins != 0)) //3 hour periodic rate
                                {
                                    continue;
                                }
                                if (spr.RequestType == 6 && (new int[] { 0, 6, 12, 18 }.Contains(hour) == false || mins != 0)) //6 hour periodic rate
                                {
                                    continue;
                                }
                                if (spr.RequestType == 10 && (new int[] { 0, 12 }.Contains(hour) == false || mins != 0)) //12 hour periodic rate
                                {
                                    continue;
                                }
                                if (spr.RequestType == 11 && (hour != 0 || mins != 0)) //24 hour periodic rate
                                {
                                    continue;
                                }

                                int minutes = ShipPositionRequestHelper.GetMinutes(spr.RequestType);
                                log.Debug("Procesando requerimiento periodico de " + minutes + " minutos");

                                //TimeSpan delta = TimeSpan.FromMinutes(minutes);
                                //if (DCDebug == "true")
                                //  delta = TimeSpan.FromSeconds(minutes);
                                //if (UtcNow >= aspr.LastTime.AddSeconds(delta.TotalSeconds))
                                //{

                                //log.Debug("se cumplio el periodo");
                                ShipPosition spos = spm.GetLastShipPosition(spr.IMONum);
                                if (spos != null)
                                {
                                    //La ultima posicion para el barco es mas nueva que la ultima que mande?
                                    if (spos.TimeStamp > aspr.LastTime)
                                    {
                                        CheckEntitlementAndSendReportOrReceipt(spr, spos);
                                        ShipManager.ChangeShipStatus(ShipStatus.Ok, ShipManager.getByIMONum(spr.IMONum));
                                    }
                                    else
                                    if (spos.TimeStamp < aspr.LastTime)
                                    {
                                        //Feature-add: +30' estados del Active Ship Position Report
                                        log.Info("No new position since last report");
                                        ReceiptManager.SendReceipt(spr.DataUserRequestor, spr.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item7,
                                                                   string.Format("No new position since last report"));
                                        return;
                                    }
                                    asprda.Update(aspr.Id, 1, spos.TimeStamp);
                                    log.Debug("ASPR " + aspr.Id + " UPDATED: Status: " + aspr.Status + " Barco:" + spr.IMONum);
                                    log.Debug("Requestor: " + spr.DataUserRequestor);
                                    log.Debug("Access-Request Type: " + spr.AccessType + "-" + spr.RequestType);
                                }
                                else
                                {
                                    log.Error("Hubo un problema al verificar la titulacion y enviar la respuesta");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error procesando ASPR", ex);
                    }
                } //foreach
            }
        }
        /// <summary>
        /// Verifica si el DataUserRequestor esta titulado para recibir un mensaje de posicion
        /// </summary>
        /// <param name="spr">Mensaje ShipPositionRequest</param>
        /// <param name="spos">Posicion del barco</param>
        private void CheckEntitlementAndSendReportOrReceipt(ShipPositionRequest spr, ShipPosition spos)
        {
            var cgm  = new ContractingGovermentManager();
            var ddpm = new DDPVersionManager();

            //Febrero 2011
            var ddpVersion           = ddpm.DDPFromDate(spos.TimeStamp);
            var contractingGoverment = cgm.GetContractingGovermentByLRITId(spr.DataUserRequestor, ddpVersion.Id);

            //Solo verifico aguas internas del que lo pide si el acceso es COASTAL o FLAG
            bool verifyWatersOf = true;

            if (spr.AccessType == 3 || spr.AccessType == 5)
            {
                verifyWatersOf = false;
            }

            //Solo verifico titulacion si es distinto a SAR
            if (spr.AccessType != 6 && cgm.IsEntitled(contractingGoverment, spos, ddpVersion, verifyWatersOf) == false)
            {
                log.Debug("No titulado");
                ReceiptManager.SendReceipt(spr.DataUserRequestor, spr.MessageId, DataCenterLogic.DataCenterTypesIDE.receiptCodeType.Item0,
                                           string.Format("Not entitled to recieve"));
                return;
            }

            var sprm = new ShipPositionReportManager();

            /*************************** MEDITAR *****************************/

            //Default Coastal
            var responseType = DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item1;
            //Periodic report
            var messageType = DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item1;

            //Flag
            if (spr.AccessType == 2)
            {
                responseType = DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item2;
            }
            //Port
            else
            if (spr.AccessType == 3 || spr.AccessType == 5)
            {
                responseType = DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item3;
            }
            //Coastal
            else
            if (spr.AccessType == 1)
            {
                responseType = DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item1;
            }
            //SAR
            else
            {
                responseType = DataCenterLogic.DataCenterTypesIDE.responseTypeType.Item4;
                messageType  = DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item3;
            }

            //si es onetimepoll => msgtype polled report
            if (spr.RequestType == 1)
            {
                messageType = DataCenterLogic.DataCenterTypesIDE.messageTypeType.Item3;
            }

            /*************************************************************/

            sprm.SendReport(spr.DataUserRequestor, spos, spr.MessageId, responseType, messageType);
        }
示例#7
0
        /// <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();
        }