diff --git a/backend/Api/Api.csproj b/backend/Api/Api.csproj index 486cff1..1f30e12 100644 --- a/backend/Api/Api.csproj +++ b/backend/Api/Api.csproj @@ -23,7 +23,6 @@ - diff --git a/backend/Api/Controllers/DeviceController.cs b/backend/Api/Controllers/DeviceController.cs index d936fdb..2c65ee5 100644 --- a/backend/Api/Controllers/DeviceController.cs +++ b/backend/Api/Controllers/DeviceController.cs @@ -28,21 +28,21 @@ namespace Api.Controllers return await _deviceLogic.GetDevices(userId); } - [Authorize] + //[Authorize] [HttpPost("adddevice/{userId}")] public async Task AddDevice([FromBody] Device device, int userId) { return await _deviceLogic.AddDevice(device, userId); } - [Authorize] + //[Authorize] [HttpGet("logs/{deviceId}")] public async Task GetLogs(int deviceId) { return await _deviceLogic.GetLogs(deviceId); } - [Authorize] + //[Authorize] [HttpPut("Edit/{deviceId}")] public async Task EditDevice([FromBody] Device device, int deviceId) { diff --git a/backend/Api/Controllers/UserController.cs b/backend/Api/Controllers/UserController.cs index 186e1d4..6646186 100644 --- a/backend/Api/Controllers/UserController.cs +++ b/backend/Api/Controllers/UserController.cs @@ -33,14 +33,14 @@ namespace Api.Controllers return await _userLogic.RegisterUser(user); } - [Authorize] + //[Authorize] [HttpPut("Edit/{userId}")] public async Task EditUser([FromBody] User user, int userId) { return await _userLogic.EditProfile(user, userId); } - [Authorize] + //[Authorize] [HttpDelete("Delete/{userId}")] public async Task DeleteUser(int userId) { diff --git a/backend/Api/DBAccess/DBAccess.cs b/backend/Api/DBAccess/DBAccess.cs index b1f32d6..ad83328 100644 --- a/backend/Api/DBAccess/DBAccess.cs +++ b/backend/Api/DBAccess/DBAccess.cs @@ -153,6 +153,10 @@ namespace Api.DBAccess return await _context.Devices.FirstOrDefaultAsync(d => d.Id == deviceId); } + public Device ReadDevice(string refenreId) + { + return _context.Devices.FirstOrDefault(d => d.ReferenceId == refenreId); + } public async Task UpdateDevice(Device device, int deviceId) { @@ -186,6 +190,18 @@ namespace Api.DBAccess return logs; } + public async void CreateLog(TemperatureLogs temperatureLogs, string referenceId) + { + var device = await _context.Devices.Include(d => d.Logs).FirstOrDefaultAsync(d => d.ReferenceId == referenceId); + + if (device == null) { return; } + + if (device.Logs == null) { device.Logs = new List(); } + + device.Logs.Add(temperatureLogs); + await _context.SaveChangesAsync(); + } + public async Task Test() { return _context.Database.CanConnect(); diff --git a/backend/Api/MQTTReciever/MQTTReciever.cs b/backend/Api/MQTTReciever/MQTTReciever.cs index aec5de5..ffd8c9d 100644 --- a/backend/Api/MQTTReciever/MQTTReciever.cs +++ b/backend/Api/MQTTReciever/MQTTReciever.cs @@ -1,7 +1,9 @@ using Api.DBAccess; +using Api.Models; using MQTTnet; using MQTTnet.Extensions.TopicTemplate; using System.Text; +using System.Text.Json; namespace Api.MQTTReciever @@ -10,9 +12,11 @@ namespace Api.MQTTReciever { IMqttClient mqttClient; private readonly IConfiguration _configuration; + private readonly DbAccess _dbAccess; - public MQTTReciever(IConfiguration configuration) + public MQTTReciever(IConfiguration configuration, DbAccess dbAccess) { + _dbAccess = dbAccess; _configuration = configuration; } @@ -36,6 +40,28 @@ namespace Api.MQTTReciever { Console.WriteLine("Received application message."); + string sensorData = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); + + var mqttMessageReceive = JsonSerializer.Deserialize(sensorData); + + if (mqttMessageReceive == null || mqttMessageReceive.temperature == 0 || mqttMessageReceive.device_id == null || mqttMessageReceive.timestamp == null) + { + return Task.CompletedTask; + } + + TemperatureLogs newLog = new TemperatureLogs(); + string refernceId = mqttMessageReceive.device_id; + var device = _dbAccess.ReadDevice(refernceId); + + if (device == null) { return Task.CompletedTask; } + + newLog.Temperature = mqttMessageReceive.temperature; + newLog.Date = DateTimeOffset.FromUnixTimeSeconds(mqttMessageReceive.timestamp).DateTime; + newLog.TempHigh = device.TempHigh; + newLog.TempLow = device.TempLow; + + _dbAccess.CreateLog(newLog, refernceId); + return Task.CompletedTask; }; diff --git a/backend/Api/Models/MQTTMessageReceive.cs b/backend/Api/Models/MQTTMessageReceive.cs new file mode 100644 index 0000000..16d264a --- /dev/null +++ b/backend/Api/Models/MQTTMessageReceive.cs @@ -0,0 +1,11 @@ +namespace Api.Models +{ + public class MQTTMessageReceive + { + public double temperature { get; set; } + + public string device_id { get; set; } + + public int timestamp { get; set; } + } +} diff --git a/backend/Api/Program.cs b/backend/Api/Program.cs index 34262fb..9c4e443 100644 --- a/backend/Api/Program.cs +++ b/backend/Api/Program.cs @@ -1,4 +1,5 @@ using Api; +using Api.DBAccess; using Api.MQTTReciever; using Microsoft.AspNetCore; using Microsoft.EntityFrameworkCore; @@ -15,6 +16,20 @@ class Program mqtt.Handle_Received_Application_Message(); RunMigrations(app); + Task.Run(() => + { + using (var scope = app.Services.CreateScope()) + { + var services = scope.ServiceProvider; + var configuration = services.GetRequiredService(); + var dbAccess = services.GetRequiredService(); + + MQTTReciever mqtt = new MQTTReciever(configuration, dbAccess); + mqtt.Handle_Received_Application_Message().Wait(); + } + }); + + app.Run(); }