Ejemplo n.º 1
0
        /// <summary>
        /// Logs the start and finish occupancy time for the specified bathroom
        /// </summary>
        /// <param name="bathId"></param>
        /// <param name="occupiedTime"></param>
        /// <param name="freedTime"></param>
        public static void LogBathUsage(Bathroom bathroom)
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["kkcloudFreeDB"].ConnectionString;
            LoggingCN = new SqlConnection(connectionString);

            try
            {
                SqlCommand command = LoggingCN.CreateCommand();

                if (LoggingCN.State != ConnectionState.Open)
                    LoggingCN.Open();
                command.Parameters.AddWithValue("@bathId", bathroom.ID);
                command.Parameters.AddWithValue("@occupiedTime", bathroom.LastOccupiedTime);
                command.Parameters.AddWithValue("@freedTime", DateTime.Now);
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "LogBathUsage";
                command.ExecuteScalar();
            }
            catch (Exception ex) {
                throw ex;
            }
            finally
            {
                if (LoggingCN != null)
                    if (LoggingCN.State != ConnectionState.Closed)
                        LoggingCN.Close();
            }
        }
Ejemplo n.º 2
0
 public static void LogBathUsage(Bathroom bathroom)
 {
     new Thread(() =>
     {
         Photon.DataAccess.Logger.LogBathUsage(bathroom);
     }).Start();
 }
        /// <summary>
        /// Send notification to subscribed users
        /// </summary>
        /// <param name="bathId"></param>
        /// <param name="isOccupied"></param>
        public void Publish(Bathroom bathroom)
        {
            List<User> usersInLine = (CacheManager.Get(Constants.BathLines) as List<BathroomLine>).First(a=>a.Bathroom.ID == bathroom.ID).UsersLine;

            if (usersInLine.Count > 0)
            {
                // If the bath was freed notify all the users in the line
                if (!bathroom.IsOccupied)
                {
                    (CacheManager.Get(Constants.OccupiedByFirstInLine) as List<bool>)[bathroom.ID-1] = true;

                    for (int i = 0; i < usersInLine.Count; i++)
                    {
                        //string userId = bathLine[i].ID;
                        User user = usersInLine[i];

                        // If the user was first in line for the bath, send him/her a notification
                        if (i == 0)
                        {
                            //BathStatus bathStatus = new BathStatus()
                            //{
                            //    Title = "Hey!",
                            //    Message = "¡El baño " + bathroom.ID.ToString() + " está libre y es tu turno!",
                            //    BathId = bathroom.ID,
                            //    IsOccupied = bathroom.IsOccupied
                            //};

                            Notification notification = new Notification()
                            {
                                User = user,
                                Bathroom = bathroom,
                                Title = "¡Hey!",
                                Message = "¡El baño " + bathroom.ID.ToString() + " está libre y es tu turno!",
                                AudioFile = Constants.ToiletFlushSoundFile
                            };

                            PhotonHub.SendMessage(notification);
                        }
                        // Send an advance notification to the rest of the users in the line
                        else
                        {
                            //BathStatus bathStatus = new BathStatus()
                            //{
                            //    Title = "Hey!",
                            //    Message = "El baño " + bathroom.ID.ToString() + " está libre y sos el " + (i + 1) + "º en la fila",
                            //    BathId = bathroom.ID,
                            //    IsOccupied = bathroom.IsOccupied
                            //};
                            Notification notification = new Notification()
                            {
                                User = user,
                                Bathroom = bathroom,
                                Title = "¡Hey!",
                                Message = "El baño " + bathroom.ID.ToString() + " está libre y sos el " + (i + 1) + "º en la fila",
                                AudioFile = Constants.ToiletFlushSoundFile
                            };
                            PhotonHub.SendMessage(notification);
                        }
                    }
                }
                // If the bath was occupied, ask the first user in line if he is in there, in order to prevent
                // another user from taking his/her place, making him/her go to the end of the line again
                else
                {
                    if (usersInLine.Count > 0)
                    {
                        PhotonHub.SendMessage(new Notification
                        {
                            Bathroom = bathroom,
                            Message = "Se ocupó el baño " + bathroom.ID + ", ¿fuiste vos?",
                            Title = "Baño ocupado",
                            AudioFile = Constants.DoorShutSoundFile,
                            User = usersInLine.First(),
                            Type = NotificationType.WITH_BUTTON
                        });
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Uses a PIR motion sensor, a Proximity sensor and a Photovoltaic sensor
        /// </summary>
        /// <param name="bathroom">The bathroom where the device is implanted</param>
        private static void ProcessDeviceMethod1(Bathroom bathroom)
        {
            Device device = bathroom.PhotonDevice;
            // Milliseconds elapsed since the last time the PIR sensor reported status change
            TimeSpan PIRSpan;
            int PIRMs;
            // Milliseconds elapsed since the last time the Photo sensor reported status change
            TimeSpan PhotoSpan;
            int PhotoMs;
            // Milliseconds elapsed since the last time the Proximity sensor reported status change
            TimeSpan ProximitySpan;
            int ProximityMs;

            int PIRSecondsRequiredToOccupy = int.Parse(ConfigurationManager.AppSettings[Constants.PIRSecondsRequiredToOccupy]);
            int PIRSecondsRequiredToFree = int.Parse(ConfigurationManager.AppSettings[Constants.PIRSecondsRequiredToFree]);
            int lightOnThreshold = int.Parse(ConfigurationManager.AppSettings[Constants.LightOnThreshold]);
            int proximityThreshold = int.Parse(ConfigurationManager.AppSettings[Constants.ProximityThreshold]);
            LogController lC = new LogController();

            while (true)
            {
                PIRSpan = DateTime.Now - device.LastPIRReportTime;
                PIRMs = (int)PIRSpan.TotalMilliseconds;

                PhotoSpan = DateTime.Now - device.LastPhotoReportTime;
                PhotoMs = (int)PhotoSpan.TotalMilliseconds;

                ProximitySpan = DateTime.Now - device.LastProximityReportTime;
                ProximityMs = (int)ProximitySpan.TotalMilliseconds;

                // If there is a person close the proximity sensor, the bath is occupied
                if (device.ProximityValue < proximityThreshold)
                {
                    if (!bathroom.IsOccupied && ProximityMs > 3000)
                    {
                        lC.LogStateChange(bathroom.ID, true);
                    }
                }
                // If the Proximity Sensor was not helpful to determine the bath status
                else
                {
                    // Avoid changing the state when the user is moving from the WC area to the basin area
                    if (ProximityMs > 5000)
                    {
                        // If there is movement and the light was turned on a few seconds ago, the bath is occupied
                        if (device.PIRSensorValue == 1 && device.PhotoSensorValue >= lightOnThreshold && PhotoMs < 3000)
                        {
                            if (!bathroom.IsOccupied)
                            {
                                lC.LogStateChange(bathroom.ID, true);
                            }
                        }
                        // If there is not movement and the light is off, the bath is free
                        else if (device.PIRSensorValue == 0 && device.PhotoSensorValue < lightOnThreshold)
                        {
                            if (bathroom.IsOccupied)
                            {
                                lC.LogStateChange(bathroom.ID, false);
                            }
                        }
                        // If the PhotoSensor was not helpful to determine the bath status
                        else
                        {
                            // If the device is reporting '1' and the bathroom is free
                            if (device.PIRSensorValue == 1 && !bathroom.IsOccupied)
                            {
                                // Avoid logging the state change more than once in a row
                                if (device.LastPIRReportTime > bathroom.LastOccupiedTime)
                                {
                                    // If some seconds have passed with uninterrupted 'true' state
                                    if (PIRMs > PIRSecondsRequiredToOccupy * 1000)
                                    {
                                        lC.LogStateChange(bathroom.ID, true);
                                    }
                                }
                            }
                            // If the device is reporting '0' and the bathroom is occupied
                            else if (device.PIRSensorValue == 0 && bathroom.IsOccupied)
                            {
                                // Avoid logging the state change more than once in a row
                                if (device.LastPIRReportTime > bathroom.LastFreedTime)
                                {
                                    // If some seconds have passed with a uninterrupted 'false' state
                                    if (PIRMs > PIRSecondsRequiredToFree * 1000)
                                    {
                                        lC.LogStateChange(bathroom.ID, false);
                                    }
                                }
                            }
                        }
                    }
                }

                Thread.Sleep(2000);
            }
        }