Example #1
0
        public void IniciarServico()
        {
            logger.Debug("IniciarServico");

            // Inicializa Map de Instrumentos Monitorados
            instrumentosMonitorados = new Dictionary <string, DadosInstrumento>();

            // Inicializa Gerenciador de Alertas Cadastrados
            gerenciadorAlertas = new GerenciadorAlertas();

            // Inicializa base de dados
            dbAlertas = new GdbAlertas();

            // Inicializa conexão com MDS
            mdsSocket = new SocketPackage();
            mdsSocket.OnConnectionOpened += new ConnectionOpenedHandler(Mds_OnConnectionOpened);
            mdsSocket.OnRequestReceived  += new MessageReceivedHandler(Mds_OnRequestReceived);
            mdsSocket.IpAddr              = ConfigurationManager.AppSettings["AlertasMDSIp"].ToString();
            mdsSocket.Port = ConfigurationManager.AppSettings["AlertasMDSPort"].ToString();
            mdsSocket.OpenConnection();

            // Configura timeout de conexão com MDS e ativa thread de monitoração
            TimeoutMDS = 300;
            if (ConfigurationManager.AppSettings["TimeoutMDS"] != null)
            {
                TimeoutMDS = Convert.ToInt32(ConfigurationManager.AppSettings["TimeoutMDS"].ToString());
            }
            TimeoutMDS       *= 1000;
            thrMonitorConexao = new Thread(new ThreadStart(MonitorConexaoMDS));
            thrMonitorConexao.Start();

            // Inicializa server Alertas
            serverAlertas = new SocketPackage();
            serverAlertas.OnClientConnected += new ClientConnectedHandler(serverAlertas_OnClientConnected);
            serverAlertas.OnRequestReceived += new MessageReceivedHandler(serverAlertas_OnRequestReceived);

            int portaServer = 55555;

            if (ConfigurationManager.AppSettings["AlertasServerPort"] != null)
            {
                portaServer = Convert.ToInt32(ConfigurationManager.AppSettings["AlertasServerPort"].ToString());
            }
            serverAlertas.StartListen(portaServer);

            _status = ServicoStatus.EmExecucao;
        }
Example #2
0
        void Mds_OnRequestReceived(object sender, MessageEventArgs args)
        {
            String        codigoMensagem = args.TipoMsg;
            SocketPackage mdsSocket      = sender as SocketPackage;

            if (codigoMensagem.Equals("AR"))
            {
                List <DadosAlerta> alertasAtingidos = gerenciadorAlertas.Checar(args.Message, instrumentosMonitorados);
                if (alertasAtingidos.Count > 0)
                {
                    // Serializa alertas atingidos e envia para AlertasCliente
                    string alertasAtingidosSerializados = JsonConvert.SerializeObject(alertasAtingidos);

                    StringBuilder retornoAtingidos = new StringBuilder();
                    retornoAtingidos.Append("AT");
                    retornoAtingidos.Append(alertasAtingidosSerializados);

                    logger.Debug("Server - Enviando alertas atingidos: [" + retornoAtingidos.ToString() + "]");
                    serverAlertas.SendToAll(retornoAtingidos.ToString());

                    // Atualiza status de alertas na base de dados
                    foreach (DadosAlerta detalheAlertaAtingido in alertasAtingidos)
                    {
                        dbAlertas.AtualizarAlertaAtingido(
                            detalheAlertaAtingido.IdAlerta,
                            detalheAlertaAtingido.DataAtingimento,
                            detalheAlertaAtingido.Cotacao);

                        logger.Debug("Alerta atingido: Id=[" + detalheAlertaAtingido.IdAlerta +
                                     "] DataAtingimento=[" + detalheAlertaAtingido.DataAtingimento +
                                     "] Valor=[" + detalheAlertaAtingido.Cotacao + "]");
                    }
                }
            }
            else if (codigoMensagem.Equals("PG"))
            {
                pingPending = false;
                logger.Debug("Recebida mensagem PG=[" + args.Message + "] " + mdsSocket.IpAddr + ":" + mdsSocket.Port);
            }
            else
            {
                logger.Error("Recebida mensagem com código inválido: [" + args.Message + "]");
            }
        }
Example #3
0
        void Mds_OnConnectionOpened(object sender, ConnectionOpenedEventArgs args)
        {
            try
            {
                SocketPackage mdsSocket = sender as SocketPackage;

                // Obtem alertas cadastrados na base
                Dictionary <String, DadosAlerta> alertasCadastrados = dbAlertas.ListarAlertas();

                if (alertasCadastrados != null && alertasCadastrados.Count > 0)
                {
                    gerenciadorAlertas.Carregar(alertasCadastrados);
                    RecadastrarInstrumentosMDS(mdsSocket);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Mds_OnConnectionOpened(): " + ex.Message, ex);
            }
        }
Example #4
0
        private void RecadastrarInstrumentosMDS(SocketPackage mdsSocket)
        {
            DateTime dataHoraCadastro = DateTime.Now;

            HashSet <String> instrumentosCadastrados = gerenciadorAlertas.ObterInstrumentos();

            foreach (String instrumento in instrumentosCadastrados)
            {
                if (!instrumentosMonitorados.Keys.Contains(instrumento))
                {
                    DadosInstrumento dadosInstrumento = new DadosInstrumento();
                    dadosInstrumento.minimo = Decimal.MaxValue;
                    dadosInstrumento.maximo = Decimal.MinValue;
                    instrumentosMonitorados.Add(instrumento, dadosInstrumento);

                    StringBuilder requisicaoAlerta = new StringBuilder();

                    requisicaoAlerta.Append("AL");
                    requisicaoAlerta.Append("  ");
                    requisicaoAlerta.Append(dataHoraCadastro.ToString(string.Format("{0}{1}", "yyyyMMddHHmmssmmm", "0")));
                    string instrumentoFormatado = String.Format("{0,-20}", instrumento);
                    requisicaoAlerta.Append(instrumentoFormatado);
                    requisicaoAlerta.Append("1");

                    try
                    {
                        if (mdsSocket != null && mdsSocket.IsConectado())
                        {
                            mdsSocket.Send(requisicaoAlerta.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error("RecadastrarInstrumentosMDS():" + ex.Message, ex);
                    }
                }
            }
        }