Ejemplo n.º 1
0
 /// <summary>
 /// Obtiene un barco en base a un numero IMO.
 /// </summary>
 /// <param name="IMONum">numero IMO del barco a buscar</param>
 /// <returns>Referencia al barco</returns>
 static public Ship getByIMONum(string IMONum)
 {
     using (var sda = new ShipDataAccess())
     {
         return(sda.getByIMONum(IMONum));
     }
 }
Ejemplo n.º 2
0
        /// <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>
        /// 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
            }
        }
        public bool TryReprog(ShipPositionRequest spr)
        {
            bool reprogOk = true;

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

            if (ShipPositionRequestHelper.IsOneTimePoll(spr.RequestType) == true)
            {
                var ship = ShipManager.getByIMONum(spr.IMONum);

                try
                {
                    var aspman = new AspManager();
                    // El 8 asegura un start frame posterior a la hora actual
                    aspman.CreatePollMessage(DateTime.UtcNow.AddMinutes(8), 0, ship, PollAction.Report);
                    log.Debug("Solitud Enviada (OneTimePoll) a IMO:" + ship.IMONum);
                }
                catch (Exception ex)
                {
                    log.Error("Error al enviar la solicitud" + Environment.NewLine + Environment.NewLine + ex + Environment.NewLine + Environment.NewLine);
                    reprogOk = false;
                }

                return(reprogOk);
            }

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

            int requestPeriod = ShipPositionRequestHelper.GetMinutes(spr.RequestType);

            log.Debug("Request de " + requestPeriod + " minutos");

            //Get faster active request in minutes
            int minPeriod = GetMinPeriodForShip(spr.IMONum, spr.Id);

            log.Debug("Request activo mas frecuente del barco de " + minPeriod + " minutos");


            //See if we need to reprogram the equipment on ship
            log.Debug("Verificando si es necesario reprogramar el equipo");
            if (requestPeriod < minPeriod || minPeriod == -1)
            {
                using (var sdao = new ShipDataAccess())
                {
                    var ship = sdao.getByIMONum(spr.IMONum);
                    //Reprogram ASP
                    log.Debug("Se envia PollMessage");
                    try
                    {
                        var aspman = new AspManager();
                        // El 8 asegura un start frame posterior a la hora actual
                        aspman.CreatePollMessage(DateTime.UtcNow.AddMinutes(8), requestPeriod, ship, PollAction.Reprogram);
                    }
                    catch (Exception ex)
                    {
                        log.Error("Hubo un problema al enviar la solicitud" + ex);
                        reprogOk = false;
                    }
                    finally
                    {
                    }
                }
            }

            return(reprogOk);
        }