Skip to content

Commit

Permalink
#105 Can now enable and disable cameras using a POST to the camera en…
Browse files Browse the repository at this point in the history
…d point.
  • Loading branch information
Dan Done committed Oct 9, 2022
1 parent bc76221 commit 8f92c0e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ An example appsettings.json configuration file can be found [here](#example-apps
* Mode [optional] (Default: ```Contains```)
* `Contains`: The entire detected object must be contained within the exclusion zone for it to be ignored, i.e. if 1 or more pixel is outside of the boundary, then it will *not* be ignored
* `Intersect`: Any part of the detected object must overlap with the exclusion zone for it to be ignored, i.e. if 1 or more pixel is within the boundary, then it *will* be ignored

#### Camera API

The following settings can be applied to the camera while SynoAI is running. All the following settings can be applied by performing a JSON POST to the respective Camera endpoint. Only the values that you wish to set need to be provided in the JSON body.

* Enabled [optional]: Whether the camera should be enabled or disabled.


As an example, to disable the camera with the name `Driveway` on the SynoAI URL `10.0.0.10` port `8080`, you `POST` to http://10.0.0.10:8080/Camera/Driveway with the following JSON body:

```json
{
"Enabled": false
}
```

To re-enable the camera, just POST again with `Enabled` set to `true`.

```json
{
"Enabled": true
}
```

### Development Config

Expand Down
23 changes: 23 additions & 0 deletions SynoAI/Controllers/CameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using SynoAI.Hubs;
using System.Drawing;
using System.Text;
using SynoAI.Models.DTOs;

namespace SynoAI.Controllers
{
Expand All @@ -36,6 +37,8 @@ public class CameraController : ControllerBase
private static ConcurrentDictionary<string, bool> _runningCameraChecks = new(StringComparer.OrdinalIgnoreCase);
private static ConcurrentDictionary<string, DateTime> _delayedCameraChecks = new(StringComparer.OrdinalIgnoreCase);

private static ConcurrentDictionary<string, bool> _enabledCameras = new(StringComparer.OrdinalIgnoreCase);

public CameraController(IAIService aiService, ISynologyService synologyService, ILogger<CameraController> logger, IHubContext<SynoAIHub> hubContext)
{
_hubContext = hubContext;
Expand All @@ -57,6 +60,16 @@ public async void Get(string id)
throw new ArgumentNullException(nameof(id));
}

if (_enabledCameras.TryGetValue(id, out bool enabled))
{
if (!enabled)
{
// The camera has been disabled, so don't process any requests
_logger.LogInformation($"{id}: Requests for this camera will not be processed as it is currently disabled.");
return;
}
}

// Fetch the camera
Camera camera = Config.Cameras.FirstOrDefault(x => x.Name.Equals(id, StringComparison.OrdinalIgnoreCase));
if (camera == null)
Expand Down Expand Up @@ -243,6 +256,16 @@ public async void Get(string id)
}
}
}

[HttpPost]
[Route("{id}")]
public void Post(string id, [FromBody]CameraOptionsDto options)
{
if (options.HasChanged(x=> x.Enabled))
{
_enabledCameras.AddOrUpdate(id, options.Enabled, (key, oldValue) => options.Enabled);
}
}

/// <summary>
/// Checks to ensure the prediction falls within the boundaries to be considered a valid prediction.
Expand Down
19 changes: 19 additions & 0 deletions SynoAI/Models/DTOs/CameraOptionsDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace SynoAI.Models.DTOs
{
public class CameraOptionsDto : UpdateDto<CameraOptionsDto>
{
public bool Enabled
{
get
{
return _enabled;
}
set
{
NotifyPropertyChange();
_enabled = value;
}
}
private bool _enabled;
}
}
31 changes: 31 additions & 0 deletions SynoAI/Models/DTOs/UpdateDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System;

namespace SynoAI.Models.DTOs
{
public abstract class UpdateDto<T>
{
public List<string> ChangedProperties = new List<string>();

protected void NotifyPropertyChange([CallerMemberName] string propertyName = "")
{
ChangedProperties.Add(propertyName);
}

public bool HasChanged(Expression<Func<T, object>> expression)
{
if (expression.Body is not MemberExpression body)
{
body = ((UnaryExpression)expression.Body).Operand as MemberExpression;
}
return HasChanged(body.Member.Name);
}

public bool HasChanged(string propertyName)
{
return ChangedProperties.Contains(propertyName);
}
}
}

0 comments on commit 8f92c0e

Please sign in to comment.