public NewCameraKey GenerateUniqueCameraKey() { int count = 0; while (count < 5) { count++; // Camera Key must be exactly 12 characters. string randomCameraKey = StringGenerator.GenerateRandomString(12, 12); // Ensure Key does not exist in database (return value is -1). if (_dbQueryService.GetCameraIdFromKey(randomCameraKey) == -1) { // Persist new camera key to database. DatabaseCamera emptyCamera = new DatabaseCamera(); emptyCamera.CameraKey = randomCameraKey; bool success = _dbQueryService.PersistNewCamera(emptyCamera); if (success) { return(new NewCameraKey(randomCameraKey)); } return(null); } } return(null); }
public CameraStatistics getCameraStatisticsForNowById(int cameraId) { DatabaseCamera camera = _dbQueryService.GetCameraById(cameraId); DatabasePerSecondStat mostRecentStat = _dbQueryService.GetLatestPerSecondStatForCamera(cameraId); GraphStatistics graphStatistics = _graphStatisticsService.GetLast30MinutesStatistics(cameraId); if (camera != null) { CameraStatistics cameraStatistics = new CameraStatistics() { CameraInformation = new CameraInformation(camera), DayTimeOfTheWeekAverageCount = 0, DayTimeOfTheWeekAverageCountAvailable = false, DayTimeOfTheWeekAverageCountDisplayString = null, LastUpdatedTime = null, MostRecentPeopleCount = 0, PeriodOfTheDayAverageCount = 0, PeriodOfTheDayAverageCountAvailable = false, PeriodOfTheDayAverageCountDisplayString = null, GraphStatistics = graphStatistics }; if (mostRecentStat != null) { cameraStatistics.LastUpdatedTime = mostRecentStat.DateTime; cameraStatistics.MostRecentPeopleCount = mostRecentStat.NumDetectedObjects; } return(cameraStatistics); } else { return(null); } }
public List <DatabaseCamera> GetCamerasForLocation(int locationId) { List <DatabaseCamera> cameraList = new List <DatabaseCamera>(); using (MySqlConnection conn = GetConnection()) { string query = $"SELECT * FROM {DatabaseCamera.TABLE_NAME} WHERE {DatabaseCamera.LOCATION_ID_LABEL} = {locationId}"; conn.Open(); MySqlCommand cmd = new MySqlCommand(query, conn); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { DatabaseCamera camera = new DatabaseCamera() { CameraId = Convert.ToInt32(reader[DatabaseCamera.CAMERA_ID_LABEL]), CameraName = Convert.ToString(reader[DatabaseCamera.CAMERA_NAME_LABEL]), LocationId = Convert.ToInt32(reader[DatabaseCamera.LOCATION_ID_LABEL]), UserId = Convert.ToInt32(reader[DatabaseCamera.USER_ID_LABEL]), MonitoredArea = Convert.ToString(reader[DatabaseCamera.MONITORED_AREA_LABEL]), Brand = Convert.ToString(reader[DatabaseCamera.BRAND_LABEL]), Model = Convert.ToString(reader[DatabaseCamera.MODEL_LABEL]) }; if (reader[DatabaseCamera.RESOLUTION_LABEL] != DBNull.Value) { camera.Resolution = Convert.ToString(reader[DatabaseCamera.RESOLUTION_LABEL]); } cameraList.Add(camera); } } } return(cameraList); }
public void ShouldRetrieveItem() { DatabaseCamera testCamera = new DatabaseCamera() { CameraId = 1, CameraName = "cameraName", LocationId = 1, UserId = 1, MonitoredArea = "Room" }; DatabasePerSecondStat testStat = new DatabasePerSecondStat() { CameraId = 1, HasSavedImage = false, DateTime = new DateTime(2018, 4, 3, 10, 0, 0), NumDetectedObjects = 23, PerSecondStatId = 2 }; List <DatabasePerSecondStat> listOfStats = new List <DatabasePerSecondStat>(); listOfStats.Add(testStat); Mock <IDatabaseQueryService> mockDBService = new Mock <IDatabaseQueryService>(MockBehavior.Strict); mockDBService.Setup(x => x.GetCameraById(1)).Returns(testCamera); mockDBService.Setup(x => x.GetLatestPerSecondStatForCamera(It.IsAny <int>())).Returns(testStat); mockDBService.Setup(x => x.GetPerSecondStatsForCamera(1)).Returns(listOfStats); CameraService cameraService = new CameraService(mockDBService.Object, new GraphStatisticService(mockDBService.Object), new LocationService(mockDBService.Object)); Assert.That(cameraService.getCameraStatisticsForNowById(1).MostRecentPeopleCount, Is.EqualTo(23)); }
public bool RegisterCamera(CameraDetails cameraDetails) { try { if (cameraDetails.ImageDeleted) { DeleteCameraImage(cameraDetails); } if (cameraDetails.UploadedImage == null || PerformCameraImageUpload(cameraDetails)) { if (cameraDetails.ExistingRoomId <= 0) { DatabaseRoom dbRoom = new DatabaseRoom(cameraDetails); dbRoom.EscapeStringFields(); if (!_dbQueryService.PersistNewRoom(dbRoom)) { return(false); } cameraDetails.ExistingRoomId = _dbQueryService.GetRoomIdByLocationIdAndRoomName(dbRoom.LocationId, dbRoom.RoomName); } DatabaseCamera dbCamera = new DatabaseCamera(cameraDetails); dbCamera.EscapeStringFields(); return(_dbQueryService.PersistExistingCameraByCameraKey(dbCamera, cameraDetails.ImageDeleted)); } } catch (Exception e) { logger.Error(e); } return(false); }
public CameraInformation(DatabaseCamera dbCamera) { CameraId = dbCamera.CameraId; CameraName = dbCamera.CameraName; ImagePath = dbCamera.ImagePath; //TODO: Check if valid TempImagePath = dbCamera.ImagePath; RoomId = dbCamera.RoomId.GetValueOrDefault(0); GraphStatistics = new GraphStatistics(); }
public CameraDetails(DatabaseCamera dbCamera) { CameraId = dbCamera.CameraId; CameraName = dbCamera.CameraName; LocationId = dbCamera.LocationId; UserId = dbCamera.UserId; MonitoredArea = dbCamera.MonitoredArea; Brand = dbCamera.Brand; Model = dbCamera.Model; Resolution = dbCamera.Resolution; }
public CameraStatistics getCameraStatisticsForNowById(int cameraId) { DatabaseCamera camera = _dbQueryService.GetCameraById(cameraId); DatabaseRoom room = null; if (camera != null && camera.RoomId != null) { room = _dbQueryService.GetRoomById(camera.RoomId.Value); } DatabasePerSecondStat mostRecentStat = _dbQueryService.GetLatestPerSecondStatForCamera(cameraId); GraphStatistics graphStatistics = _graphStatisticsService.GetLast30MinutesStatistics(cameraId); if (camera != null) { CameraStatistics cameraStatistics = new CameraStatistics { CameraInformation = new CameraInformation(camera), CameraDetails = new CameraDetails(camera), DayTimeOfTheWeekAverageCount = 0, DayTimeOfTheWeekAverageCountAvailable = false, DayTimeOfTheWeekAverageCountDisplayString = null, LastUpdatedTime = null, MostRecentPeopleCount = null, PeriodOfTheDayAverageCount = 0, PeriodOfTheDayAverageCountAvailable = false, PeriodOfTheDayAverageCountDisplayString = null, GraphStatistics = graphStatistics, TempImagePath = null }; if (room != null) { cameraStatistics.CameraDetails.MonitoredArea = room.RoomName; cameraStatistics.CameraInformation.CameraRoomName = room.RoomName; cameraStatistics.CameraInformation.RoomId = room.RoomId; } if (mostRecentStat != null) { cameraStatistics.LastUpdatedTime = mostRecentStat.DateTime; cameraStatistics.MostRecentPeopleCount = mostRecentStat.NumDetectedObjects; } if (camera.LocationId != null) { cameraStatistics.CameraDetails.Location = new LocationDetails(_dbQueryService.GetLocationById(camera.LocationId.Value)); } cameraStatistics.CameraInformation.TempImagePath = GetTempPathFromFullPath(camera.ImagePath); return(cameraStatistics); } return(null); }
public CameraDetails(DatabaseCamera dbCamera) { CameraId = dbCamera.CameraId; CameraKey = dbCamera.CameraKey; CameraName = dbCamera.CameraName; LocationId = dbCamera.LocationId.GetValueOrDefault(0); UserId = dbCamera.UserId.GetValueOrDefault(0); Brand = dbCamera.Brand; Model = dbCamera.Model; Resolution = dbCamera.Resolution; SavedImagePath = dbCamera.ImagePath; ExistingRoomId = dbCamera.RoomId.GetValueOrDefault(0); }
public GraphStatistics GetSharedRoomStatisticsForPastPeriod(List <int> cameraIds, PastPeriod pastPeriod, DateTime?startDate = null, DateTime?endDate = null) { GraphStatistics graphStatistics = new GraphStatistics(); if (startDate != null && endDate != null) { graphStatistics.StartDate = startDate; graphStatistics.EndDate = endDate; } List <List <DatabasePerSecondStat> > perSecondStatsForEachCamera = new List <List <DatabasePerSecondStat> >(); foreach (var cameraId in cameraIds) { perSecondStatsForEachCamera.Add(GetDatabasePerSecondStatsForPastPeriod(cameraId, pastPeriod, startDate, endDate)); } foreach (var perSecondStats in perSecondStatsForEachCamera) { AddDelimiterStatsForPastPeriod(perSecondStats, pastPeriod, startDate, endDate); AddDummyStatsForDeadPeriods(perSecondStats); } List <string[]> perSecondStatsFormattedStrings = new List <string[]>(); string[] titleString = new string[perSecondStatsForEachCamera.Count + 1]; titleString[0] = "DateTime"; for (int i = 0; i < cameraIds.Count; i++) { int cameraId = cameraIds.ElementAt(i); DatabaseCamera camera = _databaseQueryService.GetCameraById(cameraId); titleString[i + 1] = $"People Detected for {camera.CameraName}"; } perSecondStatsFormattedStrings.Add(titleString); for (int i = 1; i <= perSecondStatsForEachCamera.Count; i++) { List <DatabasePerSecondStat> perSecondStats = perSecondStatsForEachCamera.ElementAt(i - 1); foreach (DatabasePerSecondStat perSecondStat in perSecondStats) { string[] row = new string[perSecondStatsForEachCamera.Count + 1]; row[0] = perSecondStat.DateTime.ToString("o"); row[i] = perSecondStat.NumDetectedObjects.ToString(); perSecondStatsFormattedStrings.Add(row); } } graphStatistics.Stats = perSecondStatsFormattedStrings.ToArray(); graphStatistics.SelectedPeriod = pastPeriod; return(graphStatistics); }
public bool UnclaimCamera(int cameraId) { DatabaseCamera dbCamera = _dbQueryService.GetCameraById(cameraId); DatabaseCamera freshCamera = new DatabaseCamera { CameraKey = dbCamera.CameraKey }; if (_dbQueryService.DeleteAlertsWithCameraId(cameraId) && DeletePerSecondStatsWithCameraId(cameraId) && (dbCamera.ImagePath.IsNullOrEmpty() || DeleteCameraImage(new CameraDetails(dbCamera)))) { return(_dbQueryService.PersistExistingCameraByCameraKey(freshCamera, true)); } return(false); }
private bool DeleteCameraImage(CameraDetails cameraDetails) { DatabaseCamera dbCamera = _dbQueryService.GetCameraById(cameraDetails.CameraId); string imagePath = dbCamera.ImagePath; if (File.Exists(imagePath)) { try { File.Delete(imagePath); return(true); } catch (Exception e) { logger.Error(e); } } return(false); }
private AlertTriggeredEmailInformation GetAlertTriggeredInformation(DatabaseAlert alert, DatabasePerSecondStat earliestStatThatTriggersAlert, DatabaseUser user) { DatabaseCamera camera = _databaseQueryService.GetCameraById(alert.CameraId); AlertTriggeredEmailInformation alertEmailInformation = new AlertTriggeredEmailInformation { CameraName = camera.CameraName, AlertName = alert.AlertName, DateTriggered = earliestStatThatTriggersAlert.DateTime }; alertEmailInformation.Name = GetUserName(user); if (camera.LocationId != null) { alertEmailInformation.LocationName = _databaseQueryService.GetLocationById(camera.LocationId.Value).LocationName; } return(alertEmailInformation); }
public bool PersistNewCamera(DatabaseCamera camera) { using (MySqlConnection conn = GetConnection()) { string query = "INSERT INTO camera(" + "camera_name, location_id, user_id, monitored_area, brand, model, resolution" + ") VALUES " + $"('{camera.CameraName}',{camera.LocationId},{camera.UserId}," + $"'{camera.MonitoredArea}',{formatNullableString(camera.Brand)}," + $"{formatNullableString(camera.Model)},{formatNullableString(camera.Resolution)});"; conn.Open(); MySqlCommand cmd = new MySqlCommand(query, conn); int success = cmd.ExecuteNonQuery(); if (success != 0) { return(true); } } return(false); }
public bool ValidateCameraKey(string cameraKey) { DatabaseCamera dbCamera = _dbQueryService.GetCameraByKey(cameraKey); return(dbCamera != null && dbCamera.UserId.GetValueOrDefault(0) == 0); }
public CameraInformation getCameraInformationById(int cameraId) { DatabaseCamera camera = _dbQueryService.GetCameraById(cameraId); return(new CameraInformation(camera)); }
public bool SaveNewCamera(CameraDetails cameraDetails) { DatabaseCamera camera = new DatabaseCamera(cameraDetails); return(_dbQueryService.PersistNewCamera(camera)); }
public CameraInformation(DatabaseCamera dbCamera) : this(dbCamera.CameraId, dbCamera.MonitoredArea, dbCamera.CameraName) { }
//TODO: Refactor this method, a lot of things aren't used anymore public CameraDetails GetCameraInfoById(int cameraId) { DatabaseCamera camera = _dbQueryService.GetCameraById(cameraId); return(new CameraDetails(camera)); }