68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using ManagementPage.Database;
|
|
using ManagementPage.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
|
|
namespace ManagementPage.Pages
|
|
{
|
|
[Authorize]
|
|
public class DevicePageModel : PageModel
|
|
{
|
|
public List<EnvironmentData> data;
|
|
public List<DataSet> humidity;
|
|
public List<DataSet> temperature;
|
|
public DeviceData device;
|
|
|
|
private readonly MongoDbClient _dbClient;
|
|
|
|
public DevicePageModel(MongoDbClient dbClient)
|
|
{
|
|
data = new List<EnvironmentData>();
|
|
humidity = new List<DataSet>();
|
|
temperature = new List<DataSet>();
|
|
_dbClient = dbClient;
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetAsync()
|
|
{
|
|
var deviceId = 0l ;
|
|
try
|
|
{
|
|
var idString = (string?)RouteData.Values.Where(k => k.Key == "id")?.First().Value;
|
|
if (idString == null)
|
|
{
|
|
throw new NullReferenceException();
|
|
}
|
|
|
|
deviceId = (Int64)UInt64.Parse(idString);
|
|
}
|
|
catch
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var filter = new BsonDocument("ApiID", deviceId);
|
|
device = await _dbClient.DeviceCollection.Find(filter).FirstOrDefaultAsync();
|
|
|
|
FindOptions<EnvironmentData> options = new FindOptions<EnvironmentData>
|
|
{
|
|
Limit = 100,
|
|
NoCursorTimeout = false
|
|
};
|
|
|
|
using var cursor = await _dbClient.Collection.FindAsync(filter, options);
|
|
data.AddRange(await cursor.ToListAsync());
|
|
foreach (var item in data)
|
|
{
|
|
humidity.Add(new DataSet() { x = item.Time, y = item.Humidity });
|
|
temperature.Add(new DataSet() { x = item.Time, y = item.Temperature });
|
|
}
|
|
|
|
return Page();
|
|
}
|
|
}
|
|
}
|