Compare commits

..

No commits in common. "1f5515ac8800b3d30900c1ad338bc2f9ce8d9274" and "eade0f6d694853dc3d49b7122201bf7ced22a195" have entirely different histories.

7 changed files with 7 additions and 74 deletions

View File

@ -23,6 +23,7 @@
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
<PackageReference Include="MQTTnet.Extensions.TopicTemplate" Version="5.0.1.1416" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -28,21 +28,21 @@ namespace Api.Controllers
return await _deviceLogic.GetDevices(userId);
}
//[Authorize]
[Authorize]
[HttpPost("adddevice/{userId}")]
public async Task<IActionResult> AddDevice([FromBody] Device device, int userId)
{
return await _deviceLogic.AddDevice(device, userId);
}
//[Authorize]
[Authorize]
[HttpGet("logs/{deviceId}")]
public async Task<IActionResult> GetLogs(int deviceId)
{
return await _deviceLogic.GetLogs(deviceId);
}
//[Authorize]
[Authorize]
[HttpPut("Edit/{deviceId}")]
public async Task<IActionResult> EditDevice([FromBody] Device device, int deviceId)
{

View File

@ -33,14 +33,14 @@ namespace Api.Controllers
return await _userLogic.RegisterUser(user);
}
//[Authorize]
[Authorize]
[HttpPut("Edit/{userId}")]
public async Task<IActionResult> EditUser([FromBody] User user, int userId)
{
return await _userLogic.EditProfile(user, userId);
}
//[Authorize]
[Authorize]
[HttpDelete("Delete/{userId}")]
public async Task<IActionResult> DeleteUser(int userId)
{

View File

@ -153,10 +153,6 @@ 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<IActionResult> UpdateDevice(Device device, int deviceId)
{
@ -190,18 +186,6 @@ 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<TemperatureLogs>(); }
device.Logs.Add(temperatureLogs);
await _context.SaveChangesAsync();
}
public async Task<bool> Test()
{
return _context.Database.CanConnect();

View File

@ -1,9 +1,7 @@
using Api.DBAccess;
using Api.Models;
using MQTTnet;
using MQTTnet.Extensions.TopicTemplate;
using System.Text;
using System.Text.Json;
namespace Api.MQTTReciever
@ -12,11 +10,9 @@ namespace Api.MQTTReciever
{
IMqttClient mqttClient;
private readonly IConfiguration _configuration;
private readonly DbAccess _dbAccess;
public MQTTReciever(IConfiguration configuration, DbAccess dbAccess)
public MQTTReciever(IConfiguration configuration)
{
_dbAccess = dbAccess;
_configuration = configuration;
}
@ -40,28 +36,6 @@ namespace Api.MQTTReciever
{
Console.WriteLine("Received application message.");
string sensorData = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
var mqttMessageReceive = JsonSerializer.Deserialize<MQTTMessageReceive>(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;
};

View File

@ -1,11 +0,0 @@
namespace Api.Models
{
public class MQTTMessageReceive
{
public double temperature { get; set; }
public string device_id { get; set; }
public int timestamp { get; set; }
}
}

View File

@ -1,5 +1,4 @@
using Api;
using Api.DBAccess;
using Api.MQTTReciever;
using Microsoft.AspNetCore;
using Microsoft.EntityFrameworkCore;
@ -16,20 +15,6 @@ 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<IConfiguration>();
var dbAccess = services.GetRequiredService<DbAccess>();
MQTTReciever mqtt = new MQTTReciever(configuration, dbAccess);
mqtt.Handle_Received_Application_Message().Wait();
}
});
app.Run();
}