Skip to content

Commit

Permalink
add autofocus commands, more agent events
Browse files Browse the repository at this point in the history
  • Loading branch information
mlapaglia committed Nov 9, 2023
1 parent 582d099 commit 4b713bc
Show file tree
Hide file tree
Showing 26 changed files with 629 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,26 @@ await camera.SetZoomAndFocusAsync(
}
}

public async Task<bool> TriggerAutofocusAsync(
Guid cameraId,
CancellationToken cancellationToken)
{
using (var scope = _serviceProvider.CreateScope())
{
var processorContext = scope.ServiceProvider.GetRequiredService<ProcessorContext>();

var dbCamera = await processorContext.Cameras.FirstOrDefaultAsync(
x => x.Id == cameraId,
cancellationToken);

var camera = CameraFactory.Create(
dbCamera.Manufacturer,
dbCamera);

return await camera.TriggerAutoFocusAsync(cancellationToken);
}
}

private async Task ForceClearOverlaysAsync()
{
using (var scope = _serviceProvider.CreateScope())
Expand Down
16 changes: 15 additions & 1 deletion OpenAlprWebhookProcessor/Cameras/CameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@ public class CameraController : Controller

private readonly SetZoomAndFocusHandler _setZoomAndFocusHandler;

private readonly TriggerAutofocusHandler _triggerAutofocusHandler;

public CameraController(
GetCameraRequestHandler getCameraHandler,
DeleteCameraHandler deleteCameraHandler,
UpsertCameraHandler upsertCameraHandler,
TestCameraHandler testCameraHandler,
GetZoomAndFocusHandler getZoomAndFocusHandler,
SetZoomAndFocusHandler setZoomAndFocusHandler)
SetZoomAndFocusHandler setZoomAndFocusHandler,
TriggerAutofocusHandler triggerAutofocusHandler)
{
_getCameraHandler = getCameraHandler;
_deleteCameraHandler = deleteCameraHandler;
_upsertCameraHandler = upsertCameraHandler;
_testCameraHandler = testCameraHandler;
_getZoomAndFocusHandler = getZoomAndFocusHandler;
_setZoomAndFocusHandler = setZoomAndFocusHandler;
_triggerAutofocusHandler = triggerAutofocusHandler;
}

[HttpGet]
Expand Down Expand Up @@ -108,5 +112,15 @@ await _setZoomAndFocusHandler.HandleAsync(
zoomAndFocus,
cancellationToken);
}

[HttpPost("{cameraId}/triggerAutofocus")]
public async Task<bool> TriggerAutofocus(
Guid cameraId,
CancellationToken cancellationToken)
{
return await _triggerAutofocusHandler.HandleAsync(
cameraId,
cancellationToken);
}
}
}
34 changes: 31 additions & 3 deletions OpenAlprWebhookProcessor/Cameras/Dahua/DahuaCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace OpenAlprWebhookProcessor.Cameras
{
public class DahuaCamera : ICamera
public partial class DahuaCamera : ICamera
{
private readonly Data.Camera _camera;

Expand Down Expand Up @@ -129,9 +129,37 @@ public async Task<ZoomFocus> GetZoomAndFocusAsync(CancellationToken cancellation

return new ZoomFocus()
{
Focus = decimal.Parse(Regex.Match(response, "status\\.Focus=(.*)\\r").Groups[1].Value),
Zoom = decimal.Parse(Regex.Match(response, "status\\.Zoom=(.*)\\r").Groups[1].Value),
Focus = decimal.Parse(FocusRegex().Match(response).Groups[1].Value),
Zoom = decimal.Parse(ZoomRegex().Match(response).Groups[1].Value),
};
}

public async Task<bool> TriggerAutoFocusAsync(CancellationToken cancellationToken)
{
try
{
var result = await _httpClient.PostAsync(
$"http://{_camera.IpAddress}/cgi-bin/devVideoInput.cgi?action=autoFocus",
null,
cancellationToken);

var response = await result.Content.ReadAsStringAsync(cancellationToken);

return bool.Parse(SuccessRegex().Match(response).Groups[1].Value);
}
catch
{
return false;
}
}

[GeneratedRegex("result\":(.*?)\"")]
private static partial Regex SuccessRegex();

[GeneratedRegex("status\\.Focus=(.*)\\r")]
private static partial Regex FocusRegex();

[GeneratedRegex("status\\.Zoom=(.*)\\r")]
private static partial Regex ZoomRegex();
}
}
5 changes: 5 additions & 0 deletions OpenAlprWebhookProcessor/Cameras/Hikvision/HikvisionCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,10 @@ public Task<ZoomFocus> GetZoomAndFocusAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task<bool> TriggerAutoFocusAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}
2 changes: 2 additions & 0 deletions OpenAlprWebhookProcessor/Cameras/ICamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Task SetZoomAndFocusAsync(
ZoomFocus zoomAndFocus,
CancellationToken cancellationToken);

Task<bool> TriggerAutoFocusAsync(CancellationToken cancellationToken);

Task<ZoomFocus> GetZoomAndFocusAsync(CancellationToken cancellationToken);

Task<Stream> GetSnapshotAsync(CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Threading.Tasks;
using System.Threading;
using System;

namespace OpenAlprWebhookProcessor.Cameras.ZoomAndFocus
{
public class TriggerAutofocusHandler
{
private readonly CameraUpdateService.CameraUpdateService _cameraUpdateService;

public TriggerAutofocusHandler(CameraUpdateService.CameraUpdateService cameraUpdateService)
{
_cameraUpdateService = cameraUpdateService;
}

public async Task<bool> HandleAsync(
Guid cameraId,
CancellationToken cancellationToken)
{
return await _cameraUpdateService.TriggerAutofocusAsync(
cameraId,
cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.EntityFrameworkCore;
using OpenAlprWebhookProcessor.Cameras;
using OpenAlprWebhookProcessor.Data;
using OpenAlprWebhookProcessor.WebhookProcessor.OpenAlprWebsocket;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace OpenAlprWebhookProcessor.ImageRelay
{
public class GetWebSocketSnapshotHandler
{
private readonly ProcessorContext _processorContext;

private readonly WebsocketClientOrganizer _websocketClientOrganizer;

public GetWebSocketSnapshotHandler(
ProcessorContext processorContext,
WebsocketClientOrganizer websocketClientOrganizer)
{
_processorContext = processorContext;
_websocketClientOrganizer = websocketClientOrganizer;
}

public async Task<Stream> GetSnapshotAsync(
string agentId,
Guid cameraId,
CancellationToken cancellationToken)
{
var openAlprCameraId = await _processorContext.Cameras
.AsNoTracking()
.Where(x => x.Id == cameraId)
.Select(x => x.OpenAlprCameraId)
.FirstOrDefaultAsync(cancellationToken);

var image = await _websocketClientOrganizer.GetCameraImageAsync(
agentId,
openAlprCameraId,
cancellationToken);

return image;
}
}
}
3 changes: 1 addition & 2 deletions OpenAlprWebhookProcessor/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
using OpenAlprWebhookProcessor.Settings.GetDebubPlateGroups;
using OpenAlprWebhookProcessor.WebPushSubscriptions;
using Lib.Net.Http.WebPush;
using OpenAlprWebhookProcessor.WebPushSubscriptions;
using OpenAlprWebhookProcessor.Alerts.WebPush;
using Microsoft.AspNetCore.Http;
using OpenAlprWebhookProcessor.WebhookProcessor.OpenAlprWebsocket;

namespace OpenAlprWebhookProcessor
Expand Down Expand Up @@ -180,6 +178,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<EnrichLicensePlateRequestHandler>();
services.AddScoped<GetDebugPlateGroupRequestHandler>();
services.AddScoped<DeleteDebugPlateGroupRequestHandler>();
services.AddScoped<TriggerAutofocusHandler>();

services.AddScoped<UpsertWebPushClientRequestHandler>();
services.AddScoped<GetWebPushClientRequestHandler>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
using OpenAlprWebhookProcessor.WebPushSubscriptions.VapidKeys;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using OpenAlprWebhookProcessor.Alerts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Flurl.Util;

namespace OpenAlprWebhookProcessor.WebPushSubscriptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Text.Json.Serialization;

namespace OpenAlprWebhookProcessor.WebhookProcessor.OpenAlprWebsocket
{
public class ConfigSaveMaskRequest
{
[JsonPropertyName("type")]
public string RequestType { get; set; }

[JsonPropertyName("transaction_id")]
public Guid TransactionId { get; set; }

[JsonPropertyName("stream_file")]
public string StreamFile { get; set; }

[JsonPropertyName("mask_image")]
public string MaskImage { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Text.Json.Serialization;

namespace OpenAlprWebhookProcessor.WebhookProcessor.OpenAlprWebsocket
{
public class ImageDownloadRequest
{
public ImageDownloadRequest() { }


[JsonPropertyName("type")]
public string RequestType { get; set; }

[JsonPropertyName("direction")]
public string Direction { get; set; }

[JsonPropertyName("camera_id")]
public long CameraId { get; set; }

[JsonPropertyName("transaction_id")]
public Guid TransactionId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Text.Json.Serialization;

namespace OpenAlprWebhookProcessor.WebhookProcessor.OpenAlprWebsocket
{
public class ImageDownloadResponse
{
[JsonPropertyName("image")]
public string Image { get; set; }

[JsonPropertyName("response_code")]
public string ResponseCode { get; set; }

[JsonPropertyName("transaction_id")]
public Guid TransactionId { get; set; }
}
}
Loading

0 comments on commit 4b713bc

Please sign in to comment.