Example #1
0
        /// <summary>
        /// Reprograma el barco al periodo de request mas reciente
        /// </summary>
        /// <param name="ship">El barco a reprogramar</param>
        public static void ReprogramShip(Ship ship)
        {
            //REFACTOR: GetMinPeriodForShip en otro lado
            var asprda    = new ActiveShipPositionRequestManager();
            int minPeriod = asprda.GetMinPeriodForShip(ship.IMONum, 0);

            //Quiere decir que hay que reprogramar cada 6hs
            if (minPeriod == -1)
            {
                minPeriod = 360;
            }

            //.AddMinutes(8) asegura un timeframe en el futuro
            var aspman = new AspManager();

            aspman.CreatePollMessage(DateTime.UtcNow.AddMinutes(8), minPeriod, ship, PollAction.Reprogram);
            ChangeShipStatus(ShipStatus.Polling, ship);
        }
        /// <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;
        }
Example #3
0
        /// <summary>
        /// Thread principal donde todas las acciones ocurren
        /// </summary>
        private void MainThread()
        {
            mRun = true;
            long t0 = 0;

            TimeSpan tsTimeOut = TimeSpan.FromMinutes(1);

            if (mBasicConfiguration.DCDebug == "true")
            {
                tsTimeOut = TimeSpan.FromSeconds(5);
            }
            DateTime DateTimeIn5MinutesInc = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, DateTime.UtcNow.Hour, 0, 0);

            while (mRun == true)
            {
                //Each minute
                if (TimeSpan.FromTicks(DateTime.Now.Ticks - t0) > tsTimeOut)
                {
                    log.Debug(string.Format("Ejecutando Scheduled Task cada {0} {1}", tsTimeOut, mBasicConfiguration.DCDebug == "true" ? "Segundos" : "Minutos"));

                    try
                    {
                        //Check if we need to send a new system status
                        SendSystemStatus();


                        int min;
                        int hour;

                        if (mBasicConfiguration.DCDebug == "true")
                        {
                            min  = DateTimeIn5MinutesInc.Minute;
                            hour = DateTimeIn5MinutesInc.Hour;
                        }
                        else
                        {
                            min  = DateTime.UtcNow.Minute;
                            hour = DateTime.UtcNow.Hour;
                        }

                        //Check for other actions in the database
                        //solo en los minutos 0,15,30,45
                        //if (new int[] { 0, 15, 30, 45 }.Contains(min))
                        //{
                        //log.Debug("Cuarto de hora .. procesando ASPR y SO");
                        ActiveShipPositionRequestManager ardao = new ActiveShipPositionRequestManager();
                        ardao.DCDebug = mBasicConfiguration.DCDebug;
                        ardao.Process(hour, min);

                        //Check standing orders
                        //ProcessStandingOrders();
                        //}

                        //Check delayed reports and retry programming if necessary
                        var sm = new ShipManager();
                        sm.CheckShipStatus();

                        if (mBasicConfiguration.DCDebug == "true")
                        {
                            DateTimeIn5MinutesInc = DateTimeIn5MinutesInc.AddMinutes(5);
                            log.Debug(DateTimeIn5MinutesInc.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Secheduler Error ", ex);
                    }

                    t0 = DateTime.Now.Ticks;
                }

                //Delay 1s
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }
        }