Add object detection, tracking, mobile notifications, and search to any security camera.
no events txt
rorycleara9b9c1c44d37
diff --git a/clearcam/FileServer.h b/clearcam/FileServer.hindex 157191d..b31cd8c 100644--- a/clearcam/FileServer.h+++ b/clearcam/FileServer.h@@ -6,6 +6,7 @@- (void)start;- (NSArray *)fetchAndProcessSegmentsFromCoreDataForDateParam:(NSString *)dateParam context:(NSManagedObjectContext *)context;+- (NSArray *)fetchEventDataFromCoreData:(NSManagedObjectContext *)context;@property (nonatomic, assign) NSInteger segment_length;@property (nonatomic, strong) NSDate *last_req_time;@property (nonatomic, strong) PortScanner *scanner;diff --git a/clearcam/FileServer.m b/clearcam/FileServer.mindex 62eebd3..b4f203c 100644--- a/clearcam/FileServer.m+++ b/clearcam/FileServer.m@@ -50,7 +50,7 @@NSLog(@"Deleted: %@", file);}}- [self deleteAllDayEntitiesInContext:self.context];+ [self deleteAllDayEntitiesAndEventsInContext:self.context];[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"LastDeletedDayIndex"];[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"LastDeletedSegmentIndex"];[[NSUserDefaults standardUserDefaults] synchronize];@@ -398,7 +398,7 @@fullPath = [fullPath substringToIndex:queryRange.location];}- BOOL isDirectory = NO;+ BOOL isDirectory = YES;if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory]) {dprintf(clientSocket, "HTTP/1.1 404 Not Found\r\n\r\n");return;@@ -529,18 +529,31 @@}}-- (void)deleteAllDayEntitiesInContext:(NSManagedObjectContext *)context {- NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"DayEntity"];-+- (void)deleteAllDayEntitiesAndEventsInContext:(NSManagedObjectContext *)context {NSError *fetchError = nil;- NSArray *dayEntities = [context executeFetchRequest:fetchRequest error:&fetchError];++ // Fetch and delete all EventEntity objects+ NSFetchRequest *eventFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"EventEntity"];+ NSArray *eventEntities = [context executeFetchRequest:eventFetchRequest error:&fetchError];++ if (fetchError) {+ NSLog(@"Failed to fetch EventEntity objects: %@", fetchError.localizedDescription);+ return;+ }++ for (NSManagedObject *eventEntity in eventEntities) {+ [context deleteObject:eventEntity];+ }++ // Fetch and delete all DayEntity objects+ NSFetchRequest *dayFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"DayEntity"];+ NSArray *dayEntities = [context executeFetchRequest:dayFetchRequest error:&fetchError];if (fetchError) {NSLog(@"Failed to fetch DayEntity objects: %@", fetchError.localizedDescription);return;}- // Loop through all fetched objects and delete themfor (NSManagedObject *dayEntity in dayEntities) {[context deleteObject:dayEntity];}@@ -548,10 +561,11 @@// Save the context after deletionNSError *saveError = nil;if (![context save:&saveError]) {- NSLog(@"Failed to delete DayEntity objects: %@", saveError.localizedDescription);+ NSLog(@"Failed to delete DayEntity and EventEntity objects: %@", saveError.localizedDescription);} else {- NSLog(@"All DayEntity objects deleted successfully");+ NSLog(@"All DayEntity and EventEntity objects deleted successfully");}}+@enddiff --git a/clearcam/SceneState.h b/clearcam/SceneState.hindex 84ba195..898b38d 100644--- a/clearcam/SceneState.h+++ b/clearcam/SceneState.h@@ -6,7 +6,6 @@@property (nonatomic, strong) NSMutableArray<NSDictionary *> *lastN;@property (nonatomic, strong) NSMutableDictionary *lastN_total;@property (nonatomic, strong) NSArray<NSNumber *> *events;-@property (nonatomic, strong) NSMutableArray<NSDate *> *event_times;@property (nonatomic, strong) NSDictionary *alerts;@property (strong, nonatomic) NSManagedObjectContext *backgroundContext;- (void)processOutput:(NSArray *)array;diff --git a/clearcam/SceneState.m b/clearcam/SceneState.mindex 1850321..3d404e0 100644--- a/clearcam/SceneState.m+++ b/clearcam/SceneState.m@@ -11,7 +11,6 @@self.lastN_total = [[NSMutableDictionary alloc] init];self.events = [SettingsManager sharedManager].events;self.alerts = [SettingsManager sharedManager].alerts;- self.event_times = [[NSMutableArray alloc] init]; //todo init from txt on launch// Get Core Data context from AppDelegateAppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];@@ -43,30 +42,23 @@if (current_state != last_state) {NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths firstObject];- NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"events.txt"];NSFileManager *fileManager = [NSFileManager defaultManager];- if (![fileManager fileExistsAtPath:filePath]) {- [fileManager createFileAtPath:filePath contents:nil attributes:nil];- }+NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];NSDate *date = [NSDate date];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSString *timestamp = [dateFormatter stringFromDate:date];- [self.event_times addObject:date];NSString *contentToWrite = [NSString stringWithFormat:@"%@ class: %@ x%@\n", timestamp, self.events[i], @(current_state)];- NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];- [fileHandle seekToEndOfFile];- [fileHandle writeData:[contentToWrite dataUsingEncoding:NSUTF8StringEncoding]];- [fileHandle closeFile];+ // Handle alerts.txt if neededif (current_state == [self.alerts[self.events[i]] intValue]) {- filePath = [documentsDirectory stringByAppendingPathComponent:@"alerts.txt"];+ NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"alerts.txt"];if (![fileManager fileExistsAtPath:filePath]) {[fileManager createFileAtPath:filePath contents:nil attributes:nil];}- fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];+ NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];[fileHandle seekToEndOfFile];[fileHandle writeData:[contentToWrite dataUsingEncoding:NSUTF8StringEncoding]];[fileHandle closeFile];diff --git a/clearcam/ViewController.m b/clearcam/ViewController.mindex 8647ac3..e3f50ef 100644--- a/clearcam/ViewController.m+++ b/clearcam/ViewController.m@@ -503,7 +503,7 @@ NSMutableDictionary *classColorMap;NSInteger lastDeletedSegmentIndex = [defaults integerForKey:@"LastDeletedSegmentIndex"];@try {- if((double)[[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] unsignedLongLongValue] / (1024.0 * 1024.0) < MIN_FREE_SPACE_MB){+ if ((double)[[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] unsignedLongLongValue] / (1024.0 * 1024.0) < MIN_FREE_SPACE_MB) {NSLog(@"deleting stuff");NSLog(@"NOT ENOUGH SPACE!");@@ -526,6 +526,35 @@ NSMutableDictionary *classColorMap;return;}+ // Fetch event timestamps from Core Data+ NSArray *eventDataArray = [self.fileServer fetchEventDataFromCoreData:self.fileServer.context];++ if (!eventDataArray || eventDataArray.count == 0) {+ NSLog(@"No event timestamps found.");+ return;+ }++ NSMutableArray<NSNumber *> *eventTimestamps = [NSMutableArray array];+ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];+ [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];++ NSCalendar *calendar = [NSCalendar currentCalendar];++ for (NSDictionary *eventData in eventDataArray) {+ NSString *timestampString = eventData[@"timeStamp"];+ NSDate *eventDate = [dateFormatter dateFromString:timestampString];++ if (eventDate) {+ NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:eventDate];++ // Convert to seconds since midnight+ double eventSecondsSinceMidnight = (components.hour * 3600) + (components.minute * 60) + components.second;+ [eventTimestamps addObject:@(eventSecondsSinceMidnight)];+ }+ }+++BOOL deletedFile = NO;for (NSInteger dayIndex = lastDeletedDayIndex; dayIndex < dayEntities.count; dayIndex++) {@@ -551,27 +580,16 @@ NSMutableDictionary *classColorMap;continue;}- // Get segment timestamp (seconds since midnight)double segmentSecondsSinceMidnight = segmentTimeStampNumber.doubleValue;-NSLog(@"Checking segment: %@ (Seconds since midnight: %.2f)", segmentURL, segmentSecondsSinceMidnight);- // Validate event times- if (!self.yolo.scene.event_times || self.yolo.scene.event_times.count == 0) {- NSLog(@"Warning: No event times found, skipping timestamp check.");- }-- // Check if the segment is within ±60 seconds of any event time+ // Check if the segment is within ±60 seconds of any event timestampBOOL shouldSkipDeletion = NO;- for (NSDate *eventTime in self.yolo.scene.event_times) {- NSCalendar *calendar = [NSCalendar currentCalendar];- NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:eventTime];-- // Convert event time to seconds since midnight- double eventSecondsSinceMidnight = (components.hour * 3600) + (components.minute * 60) + components.second;+ for (NSNumber *eventTimestamp in eventTimestamps) {+ double eventSecondsSinceMidnight = eventTimestamp.doubleValue;double timeDifference = fabs(segmentSecondsSinceMidnight - eventSecondsSinceMidnight);- NSLog(@"Comparing with event time: %@ (Event Seconds: %.2f, Difference: %.2f sec)", eventTime, eventSecondsSinceMidnight, timeDifference);+ NSLog(@"Comparing with event time: %.2f sec (Difference: %.2f sec)", eventSecondsSinceMidnight, timeDifference);if (timeDifference <= 60) { // Within one minuteshouldSkipDeletion = YES;@@ -623,7 +641,7 @@ NSMutableDictionary *classColorMap;[defaults synchronize];return;}- }+ }} @catch (NSException *exception) {NSLog(@"Exception in ensureFreeDiskSpace: %@", exception.reason);}@@ -631,7 +649,6 @@ NSMutableDictionary *classColorMap;NSLog(@"Free space = %f MB", (double)[[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] unsignedLongLongValue] / (1024.0 * 1024.0));}-- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {@try {if (self.isRecording && self.assetWriter.status == AVAssetWriterStatusWriting) {