Skip to content

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

Remote switch (#82)

Rory Clear97ccc82a6422
diff --git a/clearcam.py b/clearcam.py
index cfbd0ac..419b12c 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -394,6 +394,7 @@ class RollingClassCounter:
return counts, max_reached
def is_active(self, offset=0):
+ if not alerts_on: return False
if not getattr(self, "is_on", False): return False
if not self.sched: return True
now = time.localtime()
@@ -775,8 +776,9 @@ class HLSStreamer:
"-safe", "0",
"-i", str(concat_list_path),
"-loglevel", "quiet",
- "-vf", "scale=-2:240,fps=24",
+ "-vf", "scale=-2:240,fps=24,format=yuv420p", # needed for android playback
"-c:v", "libx264",
+ "-pix_fmt", "yuv420p", # needed for android playback
"-preset", "veryslow",
"-crf", "32",
"-an",
@@ -790,7 +792,9 @@ class HLSStreamer:
"-f", "concat",
"-safe", "0",
"-i", str(concat_list_path),
- "-c", "copy",
+ "-c:v", "libx264",
+ "-pix_fmt", "yuv420p", # needed for android
+ "-an", # No audio
str(output_path)
]
try:
@@ -2658,9 +2662,11 @@ def start_cam(rtsp, cam_name, yolo_variant='n'):
live_link = dict()
+alerts_on = True
is_live_lock = threading.Lock()
def check_upload_link(cam_name="clearcampy"):
global live_link
+ global alerts_on
query_params = urllib.parse.urlencode({
"name": quote(cam_name),
"session_token": userID
@@ -2673,7 +2679,10 @@ def check_upload_link(cam_name="clearcampy"):
if response.status == 200:
response_data = json.loads(response.read().decode('utf-8'))
upload_link = response_data.get("upload_link")
- with is_live_lock: live_link[cam_name] = upload_link
+ alerts_on_res = response_data.get("alerts_on")
+ with is_live_lock:
+ live_link[cam_name] = upload_link
+ alerts_on = (alerts_on_res == 1)
else:
raise Exception(f"HTTP Error: {response.status}")
except Exception as e:
diff --git a/ios/clearcam.xcodeproj/project.pbxproj b/ios/clearcam.xcodeproj/project.pbxproj
index fe7ab00..b1a44c8 100644
--- a/ios/clearcam.xcodeproj/project.pbxproj
+++ b/ios/clearcam.xcodeproj/project.pbxproj
@@ -184,7 +184,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
- MARKETING_VERSION = 1.2.11;
+ MARKETING_VERSION = 1.2.12;
PRODUCT_BUNDLE_IDENTIFIER = roryclear.clearcam;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
@@ -217,7 +217,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
- MARKETING_VERSION = 1.2.11;
+ MARKETING_VERSION = 1.2.12;
PRODUCT_BUNDLE_IDENTIFIER = roryclear.clearcam;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
diff --git a/ios/clearcam/DeviceStreamViewController.m b/ios/clearcam/DeviceStreamViewController.m
index eae1d62..5498aa0 100644
--- a/ios/clearcam/DeviceStreamViewController.m
+++ b/ios/clearcam/DeviceStreamViewController.m
@@ -136,9 +136,7 @@
NSURL *localURL = [NSURL fileURLWithPath:tempPath];
[[NSFileManager defaultManager] removeItemAtURL:localURL error:nil];
- if ([decryptedData writeToURL:localURL atomically:YES]) {
- NSLog(@"✅ New segment saved: %@", fileName);
-
+ if ([decryptedData writeToURL:localURL atomically:YES]) {
//thumbnail save
AVAsset *videoAsset = [AVAsset assetWithURL:localURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:videoAsset];
diff --git a/ios/clearcam/LiveViewController.m b/ios/clearcam/LiveViewController.m
index 6c8a1bd..c9b8636 100644
--- a/ios/clearcam/LiveViewController.m
+++ b/ios/clearcam/LiveViewController.m
@@ -4,6 +4,7 @@
@interface LiveViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray<NSString *> *deviceNames;
+@property (nonatomic, strong) NSMutableArray<NSNumber *> *alertsOnStates;
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) UIRefreshControl *refreshControl;
@end
@@ -14,6 +15,7 @@
[super viewDidLoad];
self.title = @"Live Cameras";
self.deviceNames = [NSMutableArray array];
+ self.alertsOnStates = [NSMutableArray array];
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
@@ -128,22 +130,32 @@
if (jsonError) return;
NSArray<NSString *> *deviceNames = json[@"device_names"];
+ NSArray<NSNumber *> *alertsOnArray = json[@"alerts_on"];
+
if (deviceNames) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.deviceNames removeAllObjects];
- for (NSString *deviceName in deviceNames) {
- NSString *decodedName = [deviceName stringByRemovingPercentEncoding];
+ [self.alertsOnStates removeAllObjects];
+
+ for (NSInteger i = 0; i < deviceNames.count; i++) {
+ NSString *decodedName = [deviceNames[i] stringByRemovingPercentEncoding];
if (decodedName) {
[self.deviceNames addObject:decodedName];
+ // If alerts_on array is valid and has a matching index
+ BOOL alertsOn = NO;
+ if (alertsOnArray && i < alertsOnArray.count) {
+ alertsOn = [alertsOnArray[i] boolValue];
+ }
+ [self.alertsOnStates addObject:@(alertsOn)];
}
}
[self.tableView reloadData];
[self updateTableViewBackground];
});
}
+
}];
[task resume];
- //[self updateTableViewBackground];
}
#pragma mark - UITableViewDataSource
@@ -212,11 +224,102 @@
[cell.contentView addSubview:greenDot];
[cell.contentView addSubview:onlineLabel];
- cell.backgroundColor = [UIColor systemBackgroundColor];
+ // Alerts toggle section - switch centered with label beneath
+ CGFloat toggleSectionWidth = 80;
+ CGFloat toggleSectionX = tableView.frame.size.width - toggleSectionWidth - 15;
+ UISwitch *alertSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(toggleSectionX + (toggleSectionWidth - 51)/2, 35, 0, 0)];
+ alertSwitch.tag = indexPath.row;
+ [alertSwitch addTarget:self action:@selector(alertSwitchToggled:) forControlEvents:UIControlEventValueChanged];
+
+ BOOL alertsOn = NO;
+ if (indexPath.row < self.alertsOnStates.count) {
+ alertsOn = [self.alertsOnStates[indexPath.row] boolValue];
+ }
+ [alertSwitch setOn:alertsOn animated:NO];
+
+ UILabel *alertsLabel = [[UILabel alloc] initWithFrame:CGRectMake(toggleSectionX, 75, toggleSectionWidth, 15)];
+ alertsLabel.text = NSLocalizedString(@"alerts", nil);
+ alertsLabel.font = [UIFont systemFontOfSize:13];
+ alertsLabel.textColor = [UIColor secondaryLabelColor];
+ alertsLabel.textAlignment = NSTextAlignmentCenter;
+
+ [cell.contentView addSubview:alertSwitch];
+ [cell.contentView addSubview:alertsLabel];
+
+ cell.backgroundColor = [UIColor systemBackgroundColor];
+
return cell;
}
+- (void)alertSwitchToggled:(UISwitch *)sender {
+ NSInteger index = sender.tag;
+ if (index >= self.deviceNames.count) return;
+
+ NSString *deviceName = self.deviceNames[index];
+ BOOL newState = sender.isOn;
+
+ NSString *sessionToken = [[StoreManager sharedInstance] retrieveSessionTokenFromKeychain];
+ if (!sessionToken) {
+ NSLog(@"No session token available.");
+ [sender setOn:!newState animated:YES];
+ return;
+ }
+
+ NSDictionary *body = @{
+ @"session_token": sessionToken,
+ @"device_name": [deviceName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]], // url encoding
+ @"alerts_on": @(newState)
+ };
+
+ NSError *jsonError;
+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&jsonError];
+ if (jsonError) {
+ NSLog(@"JSON encode error: %@", jsonError);
+ [sender setOn:!newState animated:YES];
+ return;
+ }
+
+ NSURL *url = [NSURL URLWithString:@"https://rors.ai/toggle_alerts"];
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
+ request.HTTPMethod = @"POST";
+ [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
+ request.HTTPBody = jsonData;
+
+ NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
+ if (error) {
+ NSLog(@"Toggle request failed: %@", error);
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [sender setOn:!newState animated:YES];
+ });
+ return;
+ }
+
+ NSError *parseError;
+ NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
+ if (parseError || ![json isKindOfClass:[NSDictionary class]]) {
+ NSLog(@"JSON parse error: %@", parseError);
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [sender setOn:!newState animated:YES];
+ });
+ return;
+ }
+
+ BOOL success = [json[@"success"] boolValue];
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (success) {
+ NSLog(@"Toggle updated successfully for %@: %@", deviceName, newState ? @"ON" : @"OFF");
+ self.alertsOnStates[index] = @(newState);
+ } else {
+ NSLog(@"Toggle update failed: %@", json[@"message"]);
+ [sender setOn:!newState animated:YES];
+ }
+ });
+ }];
+ [task resume];
+}
+
+
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
diff --git a/ios/clearcam/ViewController.m b/ios/clearcam/ViewController.m
index 41ab373..6ec0038 100644
--- a/ios/clearcam/ViewController.m
+++ b/ios/clearcam/ViewController.m
@@ -848,11 +848,11 @@ NSMutableDictionary *classColorMap;
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (error) {
- NSLog(@"❌ Upload failed: %@", error);
+ NSLog(@"Upload failed: %@", error);
} else if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
- NSLog(@"✅ Uploaded segment.mp4.aes: %.2f KB", (float)fileData.length / 1024.0);
+ NSLog(@"Uploaded segment.mp4.aes: %.2f KB", (float)fileData.length / 1024.0);
} else {
- NSLog(@"❌ Upload failed with status %ld", (long)httpResponse.statusCode);
+ NSLog(@"Upload failed with status %ld", (long)httpResponse.statusCode);
}
}] resume];
}
diff --git a/ios/clearcam/en.lproj/Localizable.strings b/ios/clearcam/en.lproj/Localizable.strings
index b0c8e0a..33ceff5 100644
--- a/ios/clearcam/en.lproj/Localizable.strings
+++ b/ios/clearcam/en.lproj/Localizable.strings
@@ -113,3 +113,5 @@
"clearcam_user_id" = "Clearcam User ID";
"log_in" = "Log In";
"continue_without_account" = "Continue without an account";
+
+"alerts" = "Alerts";
diff --git a/ios/clearcam/es.lproj/Localizable.strings b/ios/clearcam/es.lproj/Localizable.strings
index 025530e..c9bdef6 100644
--- a/ios/clearcam/es.lproj/Localizable.strings
+++ b/ios/clearcam/es.lproj/Localizable.strings
@@ -195,3 +195,5 @@
"clearcam_user_id" = "ID de usuario de Clearcam";
"log_in" = "Iniciar sesión";
"continue_without_account" = "Continuar sin cuenta";
+
+"alerts" = "Alertas";
diff --git a/ios/clearcam/hi.lproj/Localizable.strings b/ios/clearcam/hi.lproj/Localizable.strings
index 29a8a55..7b48f34 100644
--- a/ios/clearcam/hi.lproj/Localizable.strings
+++ b/ios/clearcam/hi.lproj/Localizable.strings
@@ -195,3 +195,5 @@
"clearcam_user_id" = "Clearcam यूज़र आईडी";
"log_in" = "लॉग इन";
"continue_without_account" = "बिना खाता के जारी रखें";
+
+"alerts" = "अलर्ट";
diff --git a/ios/clearcam/ja.lproj/Localizable.strings b/ios/clearcam/ja.lproj/Localizable.strings
index 9e22ba3..9139182 100644
--- a/ios/clearcam/ja.lproj/Localizable.strings
+++ b/ios/clearcam/ja.lproj/Localizable.strings
@@ -195,3 +195,5 @@
"clearcam_user_id" = "ClearcamユーザーID";
"log_in" = "ログイン";
"continue_without_account" = "アカウントなしで続ける";
+
+"alerts" = "アラート";
diff --git a/ios/clearcam/ko.lproj/Localizable.strings b/ios/clearcam/ko.lproj/Localizable.strings
index 0881d25..bf4e75b 100644
--- a/ios/clearcam/ko.lproj/Localizable.strings
+++ b/ios/clearcam/ko.lproj/Localizable.strings
@@ -195,3 +195,5 @@
"clearcam_user_id" = "Clearcam 사용자 ID";
"log_in" = "로그인";
"continue_without_account" = "계정 없이 계속하기";
+
+"alerts" = "알림";
diff --git a/ios/clearcam/pt-BR.lproj/Localizable.strings b/ios/clearcam/pt-BR.lproj/Localizable.strings
index 7d6150b..6635c7d 100644
--- a/ios/clearcam/pt-BR.lproj/Localizable.strings
+++ b/ios/clearcam/pt-BR.lproj/Localizable.strings
@@ -206,3 +206,5 @@
"clearcam_user_id" = "UserID do Clearcam";
"log_in" = "Entrar";
"continue_without_account" = "Continuar sem conta";
+
+"alerts" = "Alertas";
diff --git a/ios/clearcam/zh-Hans.lproj/Localizable.strings b/ios/clearcam/zh-Hans.lproj/Localizable.strings
index 5678ee9..d6ca4e6 100644
--- a/ios/clearcam/zh-Hans.lproj/Localizable.strings
+++ b/ios/clearcam/zh-Hans.lproj/Localizable.strings
@@ -194,3 +194,5 @@
"clearcam_user_id" = "Clearcam 用户ID";
"log_in" = "登录";
"continue_without_account" = "继续使用(无账户)";
+
+"alerts" = "警报";