Пример #1
0
        private void ProcessMessage(string sBody)
        {
            // Check if this is sensort notification
            if (NotificationEventSchema.IsValid(sBody))
            {
                OnNotificationEventArgs eventData = JsonConvert.DeserializeObject <OnNotificationEventArgs>(sBody);

                if (eventData != null && OnNotification != null)
                {
                    OnNotification.Invoke(sBody, eventData);
                }
            }
            else if (ScheduleUpdateEventSchema.IsValid(sBody))
            {
                OnScheduleUpdateEventArgs eventData = JsonConvert.DeserializeObject <OnScheduleUpdateEventArgs>(sBody);

                eventData.Schedule = this.FilterAppointments(eventData);
                if (eventData != null && OnScheduleUpdate != null)
                {
                    OnScheduleUpdate.Invoke(sBody, eventData);
                }
            }
            else
            {
                /// Unknow schema - probably an error
                this.LogEvent(EventTypeConsts.Error, "Unknown json format", sBody);
            }
        }
Пример #2
0
        static async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            try{
                string msgBody = Encoding.UTF8.GetString(message.Body);
                if (string.IsNullOrEmpty(msgBody))
                {
                    throw new ArgumentNullException("Message body is null or empty");
                }



                //  Check if this is a schedule notification
                string timestamp = DateTime.Now.ToString("dd/MM/yy hh:mm:ss");
                if (ScheduleUpdateEventSchema.IsValid(msgBody))
                {
                    ScheduleData eventData = JsonConvert.DeserializeObject <ScheduleData>(msgBody);
                    // Remove expired events
                    eventData.Schedule = ServiceBusClient.FilterAppointments(eventData);

                    msgBody = JsonConvert.SerializeObject(eventData);

                    byte[] messageBytes = Encoding.UTF8.GetBytes(msgBody);

                    IoT.Message pipeMessage = new IoT.Message(messageBytes);

                    await ioTHubModuleClient.SendEventAsync("ScheduleOutput", pipeMessage);

                    Console.WriteLine($"{timestamp} sucessfully handling ScheduleOutput message as {msgBody}");
                    // Check if this is sensort notification
                }
                else if (NotificationEventSchema.IsValid(msgBody))
                {
                    //TODO: process events from sensors
                    //await ioTHubModuleClient.SendEventAsync("SensorsOutput", pipeMessage);
                    Console.WriteLine($"{timestamp} sucessfully handling SensorsOutput message {msgBody}");
                }
                else
                {
                    // Process the message
                    Console.WriteLine($"Unknown message format: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{msgBody}");
                    throw new ArgumentException("Unknown message format");
                }
            }catch (Exception ex) {
                Console.WriteLine($"Error processing message: {ex.Message} {ex.StackTrace}");
            }finally{
                // Complete the message so that it is not received again.
                // This can be done only if the subscriptionClient is opened in ReceiveMode.PeekLock mode (which is default).
                await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
            }
        }
Пример #3
0
        private async void ProcessMessage(string sBody, int eventsExpiration)
        {
            // Check if this is sensort notification
            if (NotificationEventSchema.IsValid(sBody))
            {
                OnNotificationEventArgs eventData = JsonConvert.DeserializeObject <OnNotificationEventArgs>(sBody);

                if (eventData != null && OnNotification != null)
                {
                    var dispatcher = DispatcherHelper.GetDispatcher;
                    await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                    {
                        double fValue = 0.0;
                        if (double.TryParse(eventData.Value, out fValue))
                        {
                            eventData.Value = fValue.ToString("F1");
                        }

                        OnNotification.Invoke(sBody, eventData);
                    });
                }
            }
            else if (ScheduleUpdateEventSchema.IsValid(sBody))
            {
                OnScheduleUpdateEventArgs eventData = JsonConvert.DeserializeObject <OnScheduleUpdateEventArgs>(sBody);

                eventData.Schedule = this.FilterAppointments(eventData, eventsExpiration);

                if (eventData != null && OnScheduleUpdate != null)
                {
                    var dispatcher = DispatcherHelper.GetDispatcher;
                    await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        OnScheduleUpdate.Invoke(sBody, eventData);
                    });
                }
            }
            else
            {
                /// Unknow schema - probably an error
                this.LogEvent(EventTypeConsts.Error, "Unknown json format", sBody);
            }
        }
Пример #4
0
        private void ProcessMessage(string sBody, int eventsExpiration)
        {
            // Check if this is sensort notification
            if (NotificationEventSchema.IsValid(sBody))
            {
                OnNotificationEventArgs eventData = JsonConvert.DeserializeObject <OnNotificationEventArgs>(sBody);

                if (eventData != null && OnNotification != null)
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        double fValue = 0.0;
                        if (double.TryParse(eventData.Value, out fValue))
                        {
                            eventData.Value = fValue.ToString("F1");
                        }

                        OnNotification.Invoke(sBody, eventData);
                    });
                }
            }
            else if (ScheduleUpdateEventSchema.IsValid(sBody))
            {
                OnScheduleUpdateEventArgs eventData = JsonConvert.DeserializeObject <OnScheduleUpdateEventArgs>(sBody);

                List <Appointment> filteredList = this.FilterAppointments(eventData, eventsExpiration);

                if (filteredList != null && OnScheduleUpdate != null)
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        OnScheduleUpdate.Invoke(sBody, filteredList);
                    });
                }
            }
            else
            {
                /// Unknow schema - probably an error
                this.LogEvent(EventTypeConsts.Error, "Unknown json format", sBody);
            }
        }