feat: add device relay and telemetry support

This commit is contained in:
2026-09-03 07:49:10 +03:30
parent 9eaa5b70d1
commit 6297df7dfd
18 changed files with 2077 additions and 20 deletions

View File

@@ -123,24 +123,29 @@ public sealed class AlertConditionService : IAlertConditionService
public async Task<IReadOnlyList<RelayConditionDto>> GetRelayConditionsAsync(int deviceId, CancellationToken cancellationToken)
{
var conditions = await _dbContext.AlertConditions
var relays = await _dbContext.DeviceRelays
.Include(x => x.Rules)
.Where(x => x.DeviceId == deviceId && (int)x.NotificationType > 0)
.OrderByDescending(x => x.CreatedAt)
.Where(x => x.DeviceSettings.DeviceId == deviceId && x.IsEnabled)
.OrderBy(x => x.RelayIndex)
.AsNoTracking()
.ToListAsync(cancellationToken);
var result = conditions.Select(c => new RelayConditionDto
var result = relays.Select(relay => new RelayConditionDto
{
Enabled = c.IsEnabled ? 1 : 0,
TimeType = (int)c.TimeType,
Relay = "R" + (int)c.NotificationType,
Rules = c.Rules.OrderBy(r => r.Order).Select(r => new RelayRuleDto
Enabled = relay.IsEnabled ? 1 : 0,
TimeType = relay.Rules.Any()
? (int)relay.Rules.OrderBy(r => r.Order).First().TimeType
: (int)Domain.AlertTimeType.Always,
Relay = "R" + relay.RelayIndex,
Name = relay.Name,
Rules = relay.Rules.OrderBy(r => r.Order).Select(r => new RelayRuleDto
{
Sensor = ((int)r.SensorType).ToString(),
Op = ((int)r.ComparisonType).ToString(),
V1 = r.Value1,
V2 = r.Value2
V2 = r.Value2,
Action = (int)r.Action,
TimeType = (int)r.TimeType
}).ToList()
}).ToList();
@@ -152,8 +157,8 @@ public sealed class AlertConditionService : IAlertConditionService
if (lastUpdateDate == null)
return await GetRelayConditionsAsync(deviceId, cancellationToken);
var hasUpdates = await _dbContext.AlertConditions
.AnyAsync(x => x.DeviceId == deviceId && (int)x.NotificationType > 0 && x.UpdatedAt > lastUpdateDate, cancellationToken);
var hasUpdates = await _dbContext.DeviceRelays
.AnyAsync(x => x.DeviceSettings.DeviceId == deviceId && x.UpdatedAt > lastUpdateDate, cancellationToken);
if (!hasUpdates)
return Array.Empty<RelayConditionDto>();
@@ -165,7 +170,7 @@ public sealed class AlertConditionService : IAlertConditionService
{
if (lastUpdateDate == null) return true;
return await _dbContext.AlertConditions
.AnyAsync(x => x.DeviceId == deviceId && (int)x.NotificationType > 0 && x.UpdatedAt > lastUpdateDate, cancellationToken);
return await _dbContext.DeviceRelays
.AnyAsync(x => x.DeviceSettings.DeviceId == deviceId && x.UpdatedAt > lastUpdateDate, cancellationToken);
}
}