Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        private List <Appointment> FilterAppointments(OnScheduleUpdateEventArgs eventData, int eventsExpiration)
        {
            if (eventData == null || eventData.Schedule == null)
            {
                return(new List <Appointment>());
            }

            DateTime expirationTime = DateTime.Now.AddMinutes(eventsExpiration * -1);

            //return all engagements  and not finished yet or finished not leater then eventsExpiration minutes
            return(eventData.Schedule.Where <Appointment>(a =>
                                                          expirationTime.CompareTo(DateTime.ParseExact(a.EndTime, OnScheduleUpdateEventArgs.DateTimeFormat, CultureInfo.InvariantCulture)) <= 0)
                   .ToList <Appointment>());
        }
        /// <summary>
        /// Check if we have any engagements
        /// </summary>
        /// <param name="roomCfg"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        internal static RoomStatus CalculateRoomStatus(IRoomConfig roomCfg, OnScheduleUpdateEventArgs e)
        {
            if (e.Schedule == null || roomCfg == null || e.Schedule.Length == 0)
            {
                return(RoomStatus.Unknown);
            }

            Appointment currentAppointment = e.Schedule.SingleOrDefault <Appointment>(a => DateTime.Parse(a.StartTime) >= DateTime.Now && DateTime.Parse(a.EndTime) <= DateTime.Now);

            if (currentAppointment != null)
            {
                roomCfg.CurrentAppointment = currentAppointment;
            }
        }
Ejemplo n.º 4
0
        private Appointment[] FilterAppointments(OnScheduleUpdateEventArgs eventData)
        {
            if (eventData == null || eventData.Schedule == null)
            {
                return(new Appointment[0]);
            }

            DateTime expirationTime = DateTime.Now.AddSeconds(sbConfig.EventLeewaySeconds * -1);

            //return all engagements  and not finished yet or finished not leater then eventsExpiration seconds
            Appointment[] retVal = eventData.Schedule.Where <Appointment>(a =>
                                                                          expirationTime.CompareTo(DateTime.ParseExact(a.EndTime, OnScheduleUpdateEventArgs.DateTimeFormat, CultureInfo.InvariantCulture)) <= 0).ToArray <Appointment>();

            return(retVal);
        }
Ejemplo n.º 5
0
        private void Transport_OnScheduleUpdate(object sender, OnScheduleUpdateEventArgs e)
        {
            IRoomConfig roomConfig = this.levelConfig.GetRoomConfig(e.RoomId);

            if (roomConfig == null)
            {
                return;
            }

            Appointment currentAppointment = null;

            if (e.Schedule != null && e.Schedule.Length > 0)
            {
                //Assume event started early to add leeway
                DateTime leeWayStartTime = DateTime.Now.AddSeconds(roomConfig.EventLeewaySeconds);
                currentAppointment = e.Schedule.SingleOrDefault <Appointment>(a => leeWayStartTime >= DateTime.Parse(a.StartTime));
            }

            bool IsChanged = false;

            if (roomConfig.CurrentAppointment != null)
            {
                IsChanged = !new AppointmentComparer().Equals(roomConfig.CurrentAppointment, currentAppointment);
            }
            else
            {
                IsChanged = roomConfig.CurrentAppointment != currentAppointment;
            }

            if (currentAppointment != null)
            {
                roomConfig.CurrentAppointment = currentAppointment;
            }
            else
            {
                roomConfig.CurrentAppointment = null;
            }

            if (IsChanged && this.OnRoomScheduleStatusChanged != null)
            {
                UpdateRoomStatus(roomConfig, null);
                this.OnRoomScheduleStatusChanged(roomConfig, currentAppointment);
            }
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
0
        private void Transport_OnScheduleUpdate(object sender, OnScheduleUpdateEventArgs e)
        {
            try
            {
#if !__ANDROID__
                updateMutex.WaitOne(5 * 1000);
#endif


                // Log this event
                this.AppController.TrackAppEvent(e);

                IRoomConfig roomConfig = this.levelConfig.GetRoomConfig(e.RoomId);
                if (roomConfig == null)
                {
                    // Log this event
                    this.AppController.TrackAppEvent("Error: no config for RoomId:" + e.RoomId);
                    return;
                }

                Appointment currentAppointment = null;
                if (e.Schedule != null && e.Schedule.Length > 0)
                {
                    //Assume event started early to add leeway
                    DateTime leeWayStartTime = DateTime.Now.AddSeconds(roomConfig.EventLeewaySeconds);
                    currentAppointment = e.Schedule.FirstOrDefault <Appointment>(a => leeWayStartTime >= a.StartDateTime);
                }

                // save Schedule information for the room
                if (this.LevelSchedule.ContainsKey(e.RoomId))
                {
                    this.LevelSchedule[e.RoomId] = e.Schedule;
                }
                else
                {
                    this.LevelSchedule.Add(e.RoomId, e.Schedule);
                }

                bool IsChanged = false;
                if (roomConfig.CurrentAppointment != null)
                {
                    IsChanged = !new AppointmentComparer().Equals(roomConfig.CurrentAppointment, currentAppointment);
                }
                else
                {
                    IsChanged = roomConfig.CurrentAppointment != currentAppointment;
                }

                if (currentAppointment != null)
                {
                    roomConfig.CurrentAppointment = currentAppointment;
                }
                else
                {
                    roomConfig.CurrentAppointment = null;
                }

                if (IsChanged && this.OnRoomScheduleStatusChanged != null)
                {
                    UpdateRoomStatus(roomConfig, null);
                    this.AppController.BeginInvokeOnMainThread(() =>
                    {
                        this.OnRoomScheduleStatusChanged(roomConfig, currentAppointment);
                    });
                }
            }
            catch (Exception ex)
            {
                this.AppController.TrackAppEvent("Transport_OnScheduleUpdate Error");
                this.AppController.TrackAppException(ex);
            }
            finally
            {
#if !__ANDROID__
                updateMutex.ReleaseMutex();
#endif
            }
        }
        public async void ReadMessageAsync(string Location, int eventsExpiration)
        {
            if (string.IsNullOrEmpty(Location))
            {
                this.LogEvent(EventTypeConsts.Error, "Invalid argument Locaton", " value is null or empty");
                return;
            }
            ReaderMutex.WaitOne(MutexWaitTime); // Wait one minute for mutex
            try
            {
                string[] Locations = Location.Split(';');

                string sXml = await HttpHelper.GetStringResponse(this.UrlAddress);


                if (!string.IsNullOrEmpty(sXml))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(sXml);

                    XmlNodeList eventsNodes = doc.SelectNodes("Appointments/Appointment");

                    List <Appointment> Appointments = new List <Appointment>();
                    foreach (string tmpLocation in Locations)
                    {
                        Appointment[] roomAppointments = ReadEventForLocation(eventsNodes, tmpLocation, eventsExpiration);
                        if (roomAppointments != null)
                        {
                            Appointments.AddRange(roomAppointments);
                        }
                    }


                    OnScheduleUpdateEventArgs eventData = new OnScheduleUpdateEventArgs()
                    {
                        RoomId = Location
                    };


                    if (Appointments.Count > 0)
                    {
                        eventData.Schedule = Appointments.Distinct <Appointment>(new AppointmentComparer()).ToArray();
                    }
                    else
                    {
                        eventData.Schedule = new Appointment[0];
                    }


                    var dispatcher = DispatcherHelper.GetDispatcher;
                    //CoreApplication.GetCurrentView().Dispatcher;
                    await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        if (OnScheduleUpdate != null)
                        {
                            OnScheduleUpdate.Invoke("", eventData);
                        }
                    });
                }
                else
                {
                    this.LogEvent(EventTypeConsts.Error, "Empty response from WebService", this.UrlAddress);
                }
            }
            catch (Exception ex)
            {
                this.LogEvent(EventTypeConsts.Error, "Webservice read message error", ex.Message + " " + ex.StackTrace);
            }finally
            {
                ReaderMutex.ReleaseMutex();
            }
        }