Skip to content

Commit

Permalink
Extract notification JSON creation to NotifierBase
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrhoyt committed Sep 29, 2022
1 parent c394e07 commit a1da6ff
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 44 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ The following is example data for when ```SendImage``` is ```false```.
}
],
"message": "Motion detected on Driveway\n\nDetected 1 objects:\nCar",
"image": null
}
```

Expand Down
33 changes: 1 addition & 32 deletions SynoAI/Notifiers/Mqtt/Mqtt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
using MQTTnet;
using System.Threading;
using System;
using System.Text;
using Newtonsoft.Json;
using System.IO;

namespace SynoAI.Notifiers.Mqtt
{
Expand Down Expand Up @@ -66,7 +63,7 @@ public override async Task SendAsync(Camera camera, Notification notification, I

MqttApplicationMessageBuilder messageBuilder = new MqttApplicationMessageBuilder()
.WithTopic($"{BaseTopic}/{camera.Name}/notification")
.WithPayload(GeneratePayload(camera, notification));
.WithPayload(GenerateJSON(camera, notification, SendImage));

await _client.PublishAsync(messageBuilder.Build());
}
Expand Down Expand Up @@ -132,34 +129,6 @@ private async Task DisconnectAsync(ILogger logger)
}
}

/// <summary>
/// Generates the message payload as a JSON string encoded as UTF-8
/// </summary>
private byte[] GeneratePayload(Camera camera, Notification notification)
{
var request = new
{
camera = camera.Name,
foundTypes = notification.FoundTypes,
predictions = notification.ValidPredictions,
message = GetMessage(camera, notification.FoundTypes),
image = SendImage ? ToBase64String(notification.ProcessedImage.GetReadonlyStream()) : null
};

return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));
}

/// <summary>
/// Returns FileStream data as a base64-encoded string
/// </summary>
private string ToBase64String(FileStream fileStream)
{
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);

return Convert.ToBase64String(buffer);
}

public override Task CleanupAsync(ILogger logger)
{
return DisconnectAsync(logger);
Expand Down
46 changes: 46 additions & 0 deletions SynoAI/Notifiers/NotifierBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
Expand Down Expand Up @@ -59,6 +61,50 @@ protected string GetMessage(Camera camera, IEnumerable<string> foundTypes, strin
return result;
}

protected string GetImageUrl(Camera camera, Notification notification)
{
if (Config.SynoAIUrL == null)
{
return null;
}

UriBuilder builder = new UriBuilder(Config.SynoAIUrL);
builder.Path += $"/{camera.Name}/{notification.ProcessedImage.FileName}";

return builder.Uri.ToString();
}

/// <summary>
/// Generates a JSON representation of the notification.
/// </summary>
protected string GenerateJSON(Camera camera, Notification notification, bool sendImage)
{
dynamic jsonObject = new ExpandoObject();

jsonObject.camera = camera.Name;
jsonObject.foundTypes = notification.FoundTypes;
jsonObject.predictions = notification.ValidPredictions;
jsonObject.message = GetMessage(camera, notification.FoundTypes);

if (sendImage)
{
jsonObject.image = ToBase64String(notification.ProcessedImage.GetReadonlyStream());
}

return JsonConvert.SerializeObject(jsonObject);
}

/// <summary>
/// Returns FileStream data as a base64-encoded string
/// </summary>
private string ToBase64String(FileStream fileStream)
{
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);

return Convert.ToBase64String(buffer);
}

/// <summary>
/// Fetches the response content and parses it as the specified type.
/// </summary>
Expand Down
12 changes: 1 addition & 11 deletions SynoAI/Notifiers/Webhook/Webhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -102,16 +101,7 @@ public override async Task SendAsync(Camera camera, Notification notification, I
else
{
// Otherwise we can just use a simple JSON object
var request = new
{
camera = camera.Name,
foundTypes = foundTypes,
predictions = notification.ValidPredictions,
message = message
};

string requestJson = JsonConvert.SerializeObject(request);
content = new StringContent(requestJson, null, "application/json");
content = new StringContent(GenerateJSON(camera, notification, false), null, "application/json");
}

logger.LogInformation($"{camera.Name}: Webhook: Calling {Method}.");
Expand Down

0 comments on commit a1da6ff

Please sign in to comment.