Exemplo n.º 1
0
        public void ToMySqlDateStringTest()
        {
            DateTime dateTime        = new DateTime(2000, 08, 31, 10, 55, 24);
            string   mySqlDateString = "2000-08-31";

            Assert.AreEqual(mySqlDateString, MySqlDateTimeConverter.ToMySqlDateString(dateTime));
        }
Exemplo n.º 2
0
        public void CheckIfSQLFormatTestToPass()
        {
            // Does not verify validity, only format.
            string test = "1901-28-99 30:60:90";

            Assert.IsTrue(MySqlDateTimeConverter.CheckIfSQLFormat(test));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks the validity of the attributes of the PerSecondStat object.
        /// </summary>
        /// <returns>Boolean indicating if the PerSecondStat object is valid.</returns>
        public bool isValidSecondStat()
        {
            if (NumTrackedPeople < 0 || DateTime.CheckIfSQLFormat() == false)
            {
                return(false);
            }

            DateTime toValidate = MySqlDateTimeConverter.ToDateTime(DateTime);

            return(DateTimeTools.ValidateDateTime(toValidate));
        }
Exemplo n.º 4
0
        public void ToMySqlDateTimeStringTest()
        {
            DateTime dateTime            = new DateTime(2000, 08, 31, 10, 55, 24);
            string   mySqlDateTimeString = "2000-08-31 10:55:24";

            if (MySqlDateTimeConverter.CheckIfSQLFormat(mySqlDateTimeString) == false)
            {
                Assert.Fail();
            }

            Assert.AreEqual(mySqlDateTimeString, MySqlDateTimeConverter.ToMySqlDateTimeString(dateTime));
        }
Exemplo n.º 5
0
        // Creates an error response body custom to the characteristics of the received DataMessage object.
        public InvalidDataMessageResponseBody CreateInvalidDataMessageResponseBody(DataMessage message)
        {
            if (message == null || message.IsEmpty())
            {
                return(new InvalidDataMessageResponseBody(true, null));
            }
            else
            {
                List <string> temp = new List <string>();

                for (int c = 0; c < message.GetLength(); c++)
                {
                    if (String.IsNullOrWhiteSpace(message.RealTimeStats[c].CameraKey))
                    {
                        temp.Add("CameraKey");
                    }

                    // Does not query the camera keys again in order to avoid server slow down.

                    if (message.RealTimeStats[c].NumTrackedPeople < 0)
                    {
                        temp.Add("NumTrackedPeople");
                    }

                    if (MySqlDateTimeConverter.CheckIfSQLFormat(message.RealTimeStats[c].DateTime) == false)
                    {
                        temp.Add("DateTime");
                    }
                    else if (message.RealTimeStats[c].DateTime.ToDateTime().ValidateDateTime() == false)
                    {
                        temp.Add("DateTime");
                    }
                }

                // Removes all duplicates from the list of failed attributes.
                temp = temp.Distinct().ToList <string>();

                // Counter for next step.
                int      x = 0;
                string[] failedAttributes = new string[temp.Count];

                // Store values in an array of strings.
                foreach (string distinctAttribute in temp)
                {
                    failedAttributes[x] = distinctAttribute;
                    x++;
                }

                return(new InvalidDataMessageResponseBody(false, failedAttributes));
            }
        }
Exemplo n.º 6
0
        public List <DateTime> CheckIfCalculationsRequired(DataMessage dataMessage)
        {
            List <DateTime> hoursToCalculateAverages = new List <DateTime>();

            foreach (PerSecondStat second in dataMessage.RealTimeStats)
            {
                // Create this function in the Helper Sevices DateTimeTools class.
                // Regular Expression that checks if the PerSecondStat is the last second of that hour.
                if (MySqlDateTimeTools.IsLastSecondOfHour(second.DateTime))
                {
                    hoursToCalculateAverages.Add(MySqlDateTimeConverter.ToDateTime(second.DateTime).GetHourBeginning());
                }
            }

            return(hoursToCalculateAverages);
        }
        public bool PersistNewPerHourStats(List <DatabasePerHourStat> perHourStats)
        {
            // Define the bulk insert query without any values to insert.
            string bulkInsertCommand = $"INSERT INTO {DatabasePerHourStat.TABLE_NAME} "
                                       + $"({DatabasePerHourStat.DATE_DAY_LABEL},{DatabasePerHourStat.DATE_HOUR_LABEL},{DatabasePerHourStat.MAX_DETECTED_OBJECT_LABEL},"
                                       + $"{DatabasePerHourStat.MIN_DETECTED_OBJECT_LABEL},{DatabasePerHourStat.AVG_DETECTED_OBJECT_LABEL}) VALUES ";

            // Append the values one by one to the bulk insert query.
            DatabasePerHourStat lastHourStat = perHourStats.Last();

            foreach (DatabasePerHourStat hourStat in perHourStats)
            {
                string dateDay     = MySqlDateTimeConverter.ToMySqlDateString(hourStat.Day);
                string dateHour    = hourStat.Hour.ToString();
                string hourMax     = hourStat.MaximumDetectedObjects.ToString();
                string hourMin     = hourStat.MinimumDetectedObjects.ToString();
                string hourAverage = hourStat.AverageDetectedObjects.ToString();

                bulkInsertCommand += $"('{dateDay}',{dateHour},{hourMax},{hourMin},{hourAverage})";

                if (hourStat != lastHourStat)
                {
                    bulkInsertCommand += ",";
                }
            }

            // Open connection and execute bulk insert command.
            using (MySqlConnection conn = GetConnection())
            {
                conn.Open();

                try
                {
                    MySqlCommand cmd = new MySqlCommand(bulkInsertCommand, conn);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    // Write to Log
                    return(false); // This is probably not going to be executed...
                }
            }
            return(true);
        }
Exemplo n.º 8
0
        // Once a DataMessage is verified, this method allows persisting all contents (PerSecondStat objects) into the database.
        public bool StoreStatsFromDataMessage(DataMessage verifiedMessage)
        {
            List <DatabasePerSecondStat> dbSecondsToPersist = new List <DatabasePerSecondStat>();

            // Remove any possible duplicates.
            List <PerSecondStat> distinctPerSecondStats = verifiedMessage.RealTimeStats.Distinct().ToList();

            for (int y = 0; y < distinctPerSecondStats.Count; y++)
            {
                PerSecondStat stat = distinctPerSecondStats[y];

                int cameraId = _dbQueryService.GetCameraIdFromKey(stat.CameraKey);

                // Only persist PerSecondStats from valid Cameras (with valid CameraKeys).
                if (cameraId > 0)
                {
                    DatabasePerSecondStat dbPerSecondStat = new DatabasePerSecondStat();
                    dbPerSecondStat.CameraId           = cameraId;
                    dbPerSecondStat.DateTime           = MySqlDateTimeConverter.ToDateTime(stat.DateTime);
                    dbPerSecondStat.HasSavedImage      = stat.HasSavedImage;
                    dbPerSecondStat.PerHourStatId      = null;
                    dbPerSecondStat.NumDetectedObjects = stat.NumTrackedPeople;
                    dbPerSecondStat.DateTimeReceived   = DateTime.Now;

                    // If PerSecondStat has a key frame.
                    if (stat.HasSavedImage && String.IsNullOrWhiteSpace(stat.FrameAsJpg) == false)
                    {
                        // Save it to the server.
                        dbPerSecondStat.FrameJpgPath = SaveKeyImage(stat);
                    }
                    else
                    {
                        dbPerSecondStat.HasSavedImage = false;
                        dbPerSecondStat.FrameJpgPath  = null;
                    }

                    dbSecondsToPersist.Add(dbPerSecondStat);
                }
            }

            return(_dbQueryService.PersistNewPerSecondStats(dbSecondsToPersist));
        }
Exemplo n.º 9
0
        public void CheckIfSQLFormatTestToFail_8()
        {
            string test = "2000-08-30  10-03:45";

            Assert.IsFalse(MySqlDateTimeConverter.CheckIfSQLFormat(test));
        }
Exemplo n.º 10
0
        public void ToDateTimeTest()
        {
            DateTime expected = new DateTime(1901, 08, 30, 10, 59, 20);

            Assert.AreEqual(expected, MySqlDateTimeConverter.ToDateTime("1901-08-30 10:59:20"));
        }