示例#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);
            }
        }
        protected void TimeToProcessMetric(TelemetryClient appInsights, OnNotificationEventArgs eventData)
        {
            // Calculate time to process
            if (!string.IsNullOrEmpty(ClientTimeZone))
            {
                DateTime eventTime     = DateTime.Parse(eventData.Time);
                DateTime convertedNow  = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, clientTimeZoneInfo);
                TimeSpan timeToProcess = convertedNow - eventTime;

                appInsights.TrackMetric("TelemetryTimeToProcess", timeToProcess.Seconds, toDictionary(eventData));
            }
        }
        protected Dictionary <string, string> toDictionary(OnNotificationEventArgs eventData)
        {
            Dictionary <string, string> retValue = new Dictionary <string, string>(6);

            retValue.Add("DeviceId", eventData.DeviceId);
            retValue.Add("Time", eventData.Time);
            retValue.Add("ValueLabel", eventData.ValueLabel);
            retValue.Add("ValueUnits", eventData.ValueUnits);
            retValue.Add("Type", eventData.Type);
            retValue.Add("Value", eventData.Value);

            return(retValue);
        }
示例#4
0
        private void Transport_OnNotification(object sender, OnNotificationEventArgs e)
        {
            try
            {
#if !__ANDROID__
                updateMutex.WaitOne(5 * 1000);
#endif
                // Log this event
                this.AppController.TrackAppEvent(e);

                IRoomConfig roomConfig = this.levelConfig.GetRoomConfigForSensorDeviceId(e.DeviceId);
                if (roomConfig != null)
                {
                    var sensor = roomConfig.RoomSensors.FirstOrDefault <IRoomSensor>(s => s.DeviceId.Equals(e.DeviceId) && s.Telemetry.Equals(e.ValueLabel));

                    bool IsChanged = false;

                    if (sensor != null)
                    {
                        // Check if value was changed
                        IsChanged = sensor.LastMeasurement == null || (!string.IsNullOrEmpty(sensor.LastMeasurement.Value) && !sensor.LastMeasurement.Value.Equals(e.Value));
                        sensor.LastMeasurement = e;
                        if (IsChanged && this.OnRoomSensorChanged != null)
                        {
                            UpdateRoomStatus(roomConfig, sensor);
                            this.AppController.BeginInvokeOnMainThread(() =>
                            {
                                this.OnRoomSensorChanged.Invoke(roomConfig, sensor);
                            });
                        }
                    }
                }
                else
                {
                    this.AppController.TrackAppEvent("Error: no config found for " + e.DeviceId);
                }
            }catch (Exception ex)
            {
                this.AppController.TrackAppEvent("Transport_OnNotification Error");
                this.AppController.TrackAppException(ex);
            }
            finally
            {
                #if !__ANDROID__
                updateMutex.ReleaseMutex();
                 #endif
            }
        }
示例#5
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);
            }
        }
示例#6
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);
            }
        }
        private void Transport_OnNotification(object sender, OnNotificationEventArgs e)
        {
            IRoomConfig roomConfig = this.levelConfig.GetRoomConfigForSensorDeviceId(e.DeviceId);

            if (roomConfig != null)
            {
                var sensor = roomConfig.RoomSensors.FirstOrDefault <IRoomSensor>(s => s.DeviceId.Equals(e.DeviceId) && s.Telemetry.Equals(e.ValueLabel));

                bool IsChanged = false;

                if (sensor != null)
                {
                    // Check if value was changed
                    IsChanged = sensor.LastMeasurement == null || (!string.IsNullOrEmpty(sensor.LastMeasurement.Value) && !sensor.LastMeasurement.Value.Equals(e.Value));
                    sensor.LastMeasurement = e;
                    if (IsChanged && this.OnRoomSensorChanged != null)
                    {
                        UpdateRoomStatus(roomConfig, sensor);
                        this.OnRoomSensorChanged.Invoke(roomConfig, sensor);
                    }
                }
            }
        }
        public int SendTelemetry(TelemetryClient appInsights, EventData[] notifications)
        {
            int        sentCount = 0;
            ITransport transport = TransportFactory.Transport;

            foreach (EventData message in notifications)
            {
                string jsonBody = Encoding.UTF8.GetString(message.GetBytes());

                // if (!string.IsNullOrEmpty(jsonBody))
                try {
                    //     jsonBody = jsonBody.Replace("DeviceID", "DeviceId");

                    OnNotificationEventArgs eventData = JsonConvert.DeserializeObject <OnNotificationEventArgs>(jsonBody);

                    if (!string.IsNullOrEmpty(eventData.DeviceId))
                    {
                        // double delay = NotificationDelay(eventData.Time);
                        //  if (delay >= -10.0 && delay < 60.0)
                        //  {
                        RoomConfig config = GetRoomConfigForDevice(eventData.DeviceId);

                        if (config != null)
                        {
                            transport.SendJson(jsonBody, config);
                            sentCount++;
                            if (appInsights != null)
                            {
                                this.TimeToProcessMetric(appInsights, eventData);
                                appInsights.TrackEvent(eventData.ValueLabel);
                            }
                            else
                            {
                                Console.Out.WriteLine(string.Format("Sucessfully sent {0}  device ID {1} location {2} ", eventData.ValueLabel, eventData.DeviceId, config.Location));
                            }
                        }
                        else
                        {
                            Console.Out.WriteLine("No configuration for device id {0}", eventData.DeviceId);
                            if (appInsights != null)
                            {
                                appInsights.TrackEvent("TelemetryNoDeviceConfig", toDictionary(eventData));
                            }
                        }

                        /*}
                         * else
                         * {
                         *  Console.Out.WriteLine("Telemetry is too old. Telemetry time: {0}", eventData.Time);
                         *  if (appInsights != null)
                         *  {
                         *      appInsights.TrackEvent("TelemetryExpired", toDictionary(eventData));
                         *  }
                         * }*/
                    }
                    else
                    {
                        string errMsg = String.Format("No device id in the message {0}", jsonBody);
                        Console.Out.WriteLine(errMsg);

                        if (appInsights != null)
                        {
                            appInsights.TrackTrace(errMsg);
                        }
                    }
                }
                catch (Exception ex)
                {
                    string errMsg = String.Format("Error {0} for message {1}", ex.Message, jsonBody);
                    Console.Out.WriteLine(errMsg);
                    if (appInsights != null)
                    {
                        appInsights.TrackException(ex);
                        appInsights.TrackTrace(errMsg);
                    }
                }
            }

            return(sentCount);
        }