Example #1
0
        private async Task PublishScadaData(Topic topic, ScadaMessage scadaMessage)
        {
            string verboseMessage = $"{baseLogString} entering PublishScadaData method.";

            Logger.LogVerbose(verboseMessage);

            try
            {
                ScadaPublication   scadaPublication = new ScadaPublication(topic, scadaMessage);
                IPublisherContract publisherClient  = PublisherClient.CreateClient();
                await publisherClient.Publish(scadaPublication, MicroserviceNames.ScadaModelProviderService);

                Logger.LogInformation($"{baseLogString} PublishScadaData => SCADA service published data of topic: {scadaPublication.Topic}, publisher name: {MicroserviceNames.ScadaModelProviderService}");
            }
            catch (Exception e)
            {
                string errorMessage = $"{baseLogString} PublishScadaData => exception {e.Message}";
                Logger.LogError(errorMessage, e);
                throw e;
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine($"{baseLogString} PublishScadaData => MeasurementCache content: ");

            var enumerableMeasurementCache = await MeasurementsCache.GetEnumerableDictionaryAsync();

            foreach (long gid in enumerableMeasurementCache.Keys)
            {
                ModbusData data = enumerableMeasurementCache[gid];

                if (data is AnalogModbusData analogModbusData)
                {
                    sb.AppendLine($"Analog data line: [gid] 0x{gid:X16}, [value] {analogModbusData.Value}, [alarm] {analogModbusData.Alarm}");
                }
                else if (data is DiscreteModbusData discreteModbusData)
                {
                    sb.AppendLine($"Discrete data line: [gid] 0x{gid:X16}, [value] {discreteModbusData.Value}, [alarm] {discreteModbusData.Alarm}");
                }
                else
                {
                    sb.AppendLine($"UNKNOWN data type: {data.GetType()}");
                }
            }

            Logger.LogDebug(sb.ToString());
        }
        public IEnumerable <OutageMailMessage> GetUnreadMessages()
        {
            var outageMailMessages = new List <OutageMailMessage>();

            try
            {
                if (!client.IsConnected)
                {
                    if (!client.Connect())
                    {
                        Logger.LogError($"{baseLogString} GetUnreadMessages => client could not connect to the email server.");
                        return(outageMailMessages);
                    }
                }

                Message[] messages = client.Folders["INBOX"].Search("UNSEEN", MessageFetchMode.Full);

                foreach (Message message in messages)
                {
                    OutageMailMessage outageMessage = mapper.MapMail(message);

                    outageMailMessages.Add(outageMessage);
                    message.Seen = true;

                    OutageTracingModel tracingModel = parser.Parse(outageMessage);

                    publisher.Publish(new OutageEmailPublication(Topic.OUTAGE_EMAIL, new EmailToOutageMessage(tracingModel.Gid)), MicroserviceNames.OmsEmailService).Wait();
                }
            }
            catch (Exception e)
            {
                Logger.LogError($"{baseLogString} GetUnreadMessages => Message: {e.Message}", e);
            }

            return(outageMailMessages);
        }
        public IEnumerable <OutageMailMessage> GetUnreadMessages()
        {
            if (!client.IsConnected)
            {
                throw new NullReferenceException("ImapClient is a null value (not connected).");
            }

            Message[] messages = client.Folders["INBOX"].Search("UNSEEN", MessageFetchMode.Full);

            List <OutageMailMessage> outageMailMessages = new List <OutageMailMessage>();

            foreach (Message message in messages)
            {
                OutageMailMessage outageMessage = mapper.MapMail(message);

                outageMailMessages.Add(outageMessage);
                message.Seen = true;

                OutageTracingModel tracingModel = parser.Parse(outageMessage);

                if (tracingModel.IsValidReport)
                {
                    dispatcher.Dispatch(tracingModel.Gid);
                }

                try
                {
                    publisher.Publish(new OutageEmailPublication(Topic.OUTAGE_EMAIL, new EmailToOutageMessage(tracingModel.Gid)), MicroserviceNames.OmsEmailService).Wait();
                }
                catch (Exception)
                {
                    Logger.LogError("[ImapEmailClient::GetUnreadMessages] Sending to PubSub Engine failed.");
                }
            }
            return(outageMailMessages);
        }