fix add and list telementry
This commit is contained in:
@@ -29,6 +29,9 @@ public sealed class TelemetryService : ITelemetryService
|
||||
dto.DeviceId = device.Id; // Update DTO for alert service
|
||||
}
|
||||
}
|
||||
// ذخیره زمان سرور در لحظه ثبت
|
||||
entity.ServerTimestampUtc = DateTime.Now;
|
||||
|
||||
var dt = dto.TimestampUtc;
|
||||
var py = PersianCalendar.GetYear(dt);
|
||||
var pm = PersianCalendar.GetMonth(dt);
|
||||
@@ -37,6 +40,16 @@ public sealed class TelemetryService : ITelemetryService
|
||||
entity.PersianMonth = pm;
|
||||
entity.PersianDate = $"{py:0000}/{pm:00}/{pd:00}";
|
||||
|
||||
if(entity.Lux < 0)
|
||||
{
|
||||
var lastItem = await dbContext.TelemetryRecords
|
||||
.Where(x => x.DeviceId == entity.DeviceId)
|
||||
.OrderByDescending(x => x.TimestampUtc)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
entity.Lux = lastItem?.Lux ?? 0;
|
||||
}
|
||||
|
||||
dbContext.TelemetryRecords.Add(entity);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
return entity.Id;
|
||||
@@ -51,24 +64,34 @@ public sealed class TelemetryService : ITelemetryService
|
||||
}
|
||||
if (filter.StartDateUtc.HasValue)
|
||||
{
|
||||
var start = filter.StartDateUtc.Value.Date.AddDays(1);
|
||||
query = query.Where(x => x.TimestampUtc >= start);
|
||||
//var start = filter.StartDateUtc.Value.Date.AddDays(1);
|
||||
query = query.Where(x => x.ServerTimestampUtc >= filter.StartDateUtc.Value);
|
||||
}
|
||||
|
||||
if (filter.EndDateUtc.HasValue)
|
||||
{
|
||||
var end = filter.EndDateUtc.Value.Date.AddDays(1);
|
||||
query = query.Where(x => x.TimestampUtc < end);
|
||||
query = query.Where(x => x.ServerTimestampUtc < filter.EndDateUtc.Value);
|
||||
}
|
||||
|
||||
if(filter.Page <= 0) filter.Page = 1;
|
||||
if(filter.PageSize <= 0) filter.PageSize = 1000000; // No limit
|
||||
var total = await query.CountAsync(cancellationToken);
|
||||
var skip = (filter.Page - 1) * filter.PageSize;
|
||||
var items = await query
|
||||
.OrderByDescending(x => x.TimestampUtc)
|
||||
.OrderByDescending(x => x.ServerTimestampUtc)
|
||||
.Skip(skip)
|
||||
.Take(filter.PageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if(items.Any(x => x.Lux < 0))
|
||||
{
|
||||
for (int i = 1; i < items.Count; i++)
|
||||
{
|
||||
if (items[i].Lux < 0)
|
||||
items[i].Lux = items[i - 1].Lux;
|
||||
}
|
||||
}
|
||||
|
||||
return new PagedResult<TelemetryDto>
|
||||
{
|
||||
Items = mapper.Map<IReadOnlyList<TelemetryDto>>(items),
|
||||
|
||||
Reference in New Issue
Block a user