add RelayConditions endpoint and RelayCount to DeviceSettings and bugfix
This commit is contained in:
@@ -9,11 +9,13 @@ public class TelemetryController : ControllerBase
|
||||
{
|
||||
private readonly ITelemetryService telemetryService;
|
||||
private readonly IAlertService alertService;
|
||||
private readonly IAlertConditionService alertConditionService;
|
||||
|
||||
public TelemetryController(ITelemetryService telemetryService, IAlertService alertService)
|
||||
public TelemetryController(ITelemetryService telemetryService, IAlertService alertService, IAlertConditionService alertConditionService)
|
||||
{
|
||||
this.telemetryService = telemetryService;
|
||||
this.alertService = alertService;
|
||||
this.alertConditionService = alertConditionService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -25,7 +27,7 @@ public class TelemetryController : ControllerBase
|
||||
}
|
||||
|
||||
[HttpGet("AddData")]
|
||||
public async Task<ActionResult<string>> Create([FromQuery] TelemetryDto telementry, CancellationToken cancellationToken)
|
||||
public async Task<ActionResult<string>> Create([FromQuery] TelemetryDto telementry, [FromQuery] string? lastUpdateDate, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
telementry.TimestampUtc = DateTime.UtcNow;
|
||||
@@ -43,28 +45,47 @@ public class TelemetryController : ControllerBase
|
||||
}
|
||||
}
|
||||
|
||||
// Check and send alerts if needed (fire and forget)
|
||||
// _ = Task.Run(async () =>
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// // Get deviceId from the saved telemetry record
|
||||
// var deviceId = telementry.DeviceId;
|
||||
// if (deviceId > 0)
|
||||
// {
|
||||
// await alertService.CheckAndSendAlertsAsync(deviceId, telementry, cancellationToken);
|
||||
// }
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// // Log error but don't fail the request
|
||||
// // Errors are logged in AlertService
|
||||
// }
|
||||
// }, cancellationToken);
|
||||
// Check if relay conditions have been updated since lastUpdateDate
|
||||
if (deviceId > 0 && !string.IsNullOrEmpty(lastUpdateDate))
|
||||
{
|
||||
var parsedDate = ParseLastUpdateDate(lastUpdateDate);
|
||||
// var relayConditions = await alertConditionService.GetUpdatedRelayConditionsAsync(deviceId, parsedDate, cancellationToken);
|
||||
// if (relayConditions.Count > 0)
|
||||
// {
|
||||
// return Ok(new { conditions = relayConditions });
|
||||
// }
|
||||
|
||||
var hasUpdates = await alertConditionService.HasUpdatedConditionsAsync(deviceId, parsedDate, cancellationToken);
|
||||
if(hasUpdates)
|
||||
return Ok("Relay");
|
||||
}
|
||||
|
||||
return Ok(id + "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse date string in format: yyyy/MM/dd-HH:mm
|
||||
/// </summary>
|
||||
private static DateTime? ParseLastUpdateDate(string dateStr)
|
||||
{
|
||||
if (string.IsNullOrEmpty(dateStr))
|
||||
return null;
|
||||
|
||||
// Try parsing as yyyy/MM/dd-HH:mm
|
||||
if (DateTime.TryParseExact(dateStr, "yyyy/MM/dd-HH:mm", null, System.Globalization.DateTimeStyles.None, out var result))
|
||||
{
|
||||
return DateTime.SpecifyKind(result, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
// Try parsing as yyyy-MM-dd HH:mm (ISO-like)
|
||||
if (DateTime.TryParseExact(dateStr, "yyyy-MM-dd HH:mm", null, System.Globalization.DateTimeStyles.None, out var result2))
|
||||
{
|
||||
return DateTime.SpecifyKind(result2, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[HttpGet("minmax")]
|
||||
public async Task<ActionResult<TelemetryMinMax>> MinMax([FromQuery] int deviceId, [FromQuery] DateTime? startUtc, [FromQuery] DateTime? endUtc, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user