Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

events times started

roryclearc08f75d114fc
diff --git a/SegmentsModel.xcdatamodeld/SegmentsModel.xcdatamodel/contents b/SegmentsModel.xcdatamodeld/SegmentsModel.xcdatamodel/contents
index fe8c24b..a4cdbb3 100644
--- a/SegmentsModel.xcdatamodeld/SegmentsModel.xcdatamodel/contents
+++ b/SegmentsModel.xcdatamodeld/SegmentsModel.xcdatamodel/contents
@@ -1,9 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23605" systemVersion="24D60" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
+<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23605" systemVersion="24D70" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
<entity name="DayEntity" representedClassName="DayEntity" syncable="YES" codeGenerationType="class">
<attribute name="date" optional="YES" attributeType="String"/>
<relationship name="segments" optional="YES" toMany="YES" deletionRule="Nullify" ordered="YES" destinationEntity="SegmentEntity" inverseName="day" inverseEntity="SegmentEntity"/>
</entity>
+ <entity name="EventEntity" representedClassName="EventEntity" syncable="YES" codeGenerationType="class">
+ <attribute name="timeStamp" optional="YES" attributeType="Float" defaultValueString="0.0" usesScalarValueType="YES"/>
+ </entity>
<entity name="FrameEntity" representedClassName="FrameEntity" syncable="YES" codeGenerationType="class">
<attribute name="aspect_ratio" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
<attribute name="frame_timeStamp" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
diff --git a/clearcam/SceneState.h b/clearcam/SceneState.h
index 3ce6508..84ba195 100644
--- a/clearcam/SceneState.h
+++ b/clearcam/SceneState.h
@@ -1,4 +1,5 @@
#import <Foundation/Foundation.h>
+#import <CoreData/CoreData.h>
@interface SceneState : NSObject
@@ -7,6 +8,7 @@
@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;
- (void)writeToFileWithString:(NSString *)customString;
diff --git a/clearcam/SceneState.m b/clearcam/SceneState.m
index 2b039cd..6d15a2d 100644
--- a/clearcam/SceneState.m
+++ b/clearcam/SceneState.m
@@ -1,5 +1,6 @@
#import "SceneState.h"
#import "SettingsManager.h"
+#import "AppDelegate.h"
@implementation SceneState
@@ -11,6 +12,10 @@
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 AppDelegate
+ AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
+ self.backgroundContext = appDelegate.persistentContainer.newBackgroundContext;
}
return self;
}
@@ -35,7 +40,7 @@
int current_state = (int)roundf((totalValue ? [totalValue floatValue] : 0.0) / 10.0);
int last_state = (int)roundf((last_totalValue ? [last_totalValue floatValue] : 0.0) / 10.0);
self.lastN_total[self.events[i]] = totalValue;
- if(current_state != last_state){
+ if (current_state != last_state) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"events.txt"];
@@ -55,8 +60,8 @@
[fileHandle seekToEndOfFile];
[fileHandle writeData:[contentToWrite dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
-
- if(current_state == [self.alerts[self.events[i]] intValue]){
+
+ if (current_state == [self.alerts[self.events[i]] intValue]) {
filePath = [documentsDirectory stringByAppendingPathComponent:@"alerts.txt"];
if (![fileManager fileExistsAtPath:filePath]) {
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
@@ -66,8 +71,19 @@
[fileHandle writeData:[contentToWrite dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
}
+
+ // **Add EventEntity to Core Data (SegmentsModel)**
+ [self.backgroundContext performBlockAndWait:^{
+ NSManagedObject *newEvent = [NSEntityDescription insertNewObjectForEntityForName:@"EventEntity"
+ inManagedObjectContext:self.backgroundContext];
+ [newEvent setValue:@([date timeIntervalSince1970]) forKey:@"timeStamp"]; // Float value of timestamp
+
+ NSError *saveError = nil;
+ if (![self.backgroundContext save:&saveError]) {
+ NSLog(@"Failed to save EventEntity: %@, %@", saveError, saveError.userInfo);
+ }
+ }];
}
-
}
if(self.lastN.count > 10){
[self.lastN removeObjectAtIndex:0];