Pobieranie danych z sensorów ( urządzeń )
Dane są pobierane z poszczególnych sensorów które połączone przez wifi . Funkcje opisane tutaj. Dodany jest timer w celu pobierania danych co 1 minutę i wyświetlania aktualnych. Obsłużone też są zmiany ustawień
using HomeService;
using HomeService.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration;
using MierzwiceWeb.Data;
using Radzen;
using Radzen.Blazor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MierzwiceWeb.Area.Temperature
{
public partial class BakeSensorsComponent : ComponentBase
{
Timer timer;
[Inject]
private EspEasyDS18B20Service espservice { get; set; }
private SettingStove settingStove { get; set; } = new();
private SettingStove settingFroze { get; set; } = new();
private PressureData pressureData { get; set; } = new();
private PressureData termaData { get; set; } = new();
private SettingStove SavedSettigStove { get; set; } = new ();
private SettingStove SavedSettigFroze { get; set; } = new();
private SensorValue OutSideData { get; set; } = new SensorValue();
[Inject]
IConfiguration _configuration { get; set; }
[Inject]
NotificationService notificationService { get; set; }
protected override void OnInitialized()
{
settingStove = espservice.GetData();
settingFroze = espservice.GetDataFroze();
SavedSettigStove = settingStove;
SavedSettigFroze = settingFroze;
timer = new Timer( state =>
{
//pobieramy dane
settingStove = espservice.GetData();
settingFroze = espservice.GetDataFroze();
//przekazujemy do obiektow
pressureData.Pressure = HomeService.Data.SensorReandings.InSide.Pressure;
pressureData.Temperature = HomeService.Data.SensorReandings.InSide.Temperature;
termaData.Temperature = HomeService.Data.SensorReandings.Terma.Temperature;
OutSideData = HomeService.Data.SensorReandings.OutSide;
InvokeAsync(StateHasChanged);
}, null, 0, 1000);// co minute
}

Zmiany są realizowane przez zapis do bazy i ponowny odczyt parametrów .
public async Task OnClick(string buttonName)
{
int type = 0;
if ( buttonName=="Kuchnia")
{
type = 1;
}
var resul= await espservice.SaveBakeSettiings(type==0?SavedSettigStove.temperatureOFF:SavedSettigFroze.temperatureOFF, type==0?SavedSettigStove.temperatureON:SavedSettigFroze.temperatureON , type);
if (resul.IsValid)
{
DatabaseTools databaseTools = new(_configuration);
databaseTools.GetSettings();
settingStove = espservice.GetData();
NotificationMessage notification = new NotificationMessage
{
Detail = "sukcess" + resul.GetMessages(),
Duration = 400000,
Severity = NotificationSeverity.Success,
Summary = "ZAPIS",
// Style= "position: absolute; left: -1000px;"
};
notificationService.Notify(notification);
}
else
{
NotificationMessage notification = new NotificationMessage
{
Detail = resul.GetErrors(),
Duration = 40000,
Severity = NotificationSeverity.Error,
Summary = "ZAPIS",
Style = "position: absolute; left: -1000px;"
};
notificationService.Notify(notification);
}
}
Funkcja zapisująca dane do bazy danych LiteDB
public async Task<State> SaveBakeSettiings(int tempOFF , int tempOn , int type )
{
State state = new State();
try
{
string path = _configuration.GetValue<string>("Path:File");
if (path == "")
{
path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
}
path += @_configuration.GetValue<string>("Database:Name");
Console.WriteLine(path);
using (var db = new LiteDatabase(path))
{
var col = db.GetCollection<Settings>("Settings");
var tempoff = col.Query().Where(a => a.Type ==( type == 0 ? SettingsType.TempBakeOff : SettingsType.TempStoveOff)).FirstOrDefault();
if (tempoff != null)
{
tempoff.Value = tempOFF.ToString();
col.Update(tempoff);
}
else
{
var item = new Settings
{
Type = type == 0 ? SettingsType.TempBakeOff : SettingsType.TempStoveOff,
SaveType = SaveType.Int,
Value = tempOFF.ToString(),
};
col.Insert(item);
}
var tempon = col.Query().Where(a => a.Type == (type == 0 ? SettingsType.TempBakeOn : SettingsType.TempStoveOn)).FirstOrDefault();
if (tempon != null)
{
tempon.Value = tempOn.ToString();
col.Update(tempon);
}
else
{
var item2 = new Settings
{
Type = type == 0 ? SettingsType.TempBakeOn : SettingsType.TempStoveOn,
SaveType = SaveType.Int,
Value = tempOn.ToString(),
Id = 0
};
col.Insert(item2);
}
state.AddMessage("Zapisano");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
state.AddError(ex.ToString());
}
return state;
}
Funkcja wysyłająca komendę zmiany stanu przekaźnika na włączony lub wyłączony w sensorze . Wysyłanie przez sieć WIFI. Opis funkcji na stronie projektu ESP easy
public static async Task<State> Switch(bool stat, string requesturl)
{
State state = new State();
if (stat == true)
{
requesturl += "control?cmd=GPIO,12,1";
}
else
{
requesturl += "control?cmd=GPIO,12,0";
}
try
{
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage res = await client.GetAsync(requesturl))
{
using (HttpContent content = res.Content)
{
string data = await content.ReadAsStringAsync();
}
}
}
}
catch (Exception ex)
{
state.AddError(ex.Message);
}
finally
{
}
return state;
}
