Compare commits
No commits in common. "1f5515ac8800b3d30900c1ad338bc2f9ce8d9274" and "eade0f6d694853dc3d49b7122201bf7ced22a195" have entirely different histories.
1f5515ac88
...
eade0f6d69
@ -23,6 +23,7 @@
|
|||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
|
||||||
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
|
<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" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -28,21 +28,21 @@ namespace Api.Controllers
|
|||||||
return await _deviceLogic.GetDevices(userId);
|
return await _deviceLogic.GetDevices(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
//[Authorize]
|
[Authorize]
|
||||||
[HttpPost("adddevice/{userId}")]
|
[HttpPost("adddevice/{userId}")]
|
||||||
public async Task<IActionResult> AddDevice([FromBody] Device device, int userId)
|
public async Task<IActionResult> AddDevice([FromBody] Device device, int userId)
|
||||||
{
|
{
|
||||||
return await _deviceLogic.AddDevice(device, userId);
|
return await _deviceLogic.AddDevice(device, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
//[Authorize]
|
[Authorize]
|
||||||
[HttpGet("logs/{deviceId}")]
|
[HttpGet("logs/{deviceId}")]
|
||||||
public async Task<IActionResult> GetLogs(int deviceId)
|
public async Task<IActionResult> GetLogs(int deviceId)
|
||||||
{
|
{
|
||||||
return await _deviceLogic.GetLogs(deviceId);
|
return await _deviceLogic.GetLogs(deviceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
//[Authorize]
|
[Authorize]
|
||||||
[HttpPut("Edit/{deviceId}")]
|
[HttpPut("Edit/{deviceId}")]
|
||||||
public async Task<IActionResult> EditDevice([FromBody] Device device, int deviceId)
|
public async Task<IActionResult> EditDevice([FromBody] Device device, int deviceId)
|
||||||
{
|
{
|
||||||
|
@ -33,14 +33,14 @@ namespace Api.Controllers
|
|||||||
return await _userLogic.RegisterUser(user);
|
return await _userLogic.RegisterUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
//[Authorize]
|
[Authorize]
|
||||||
[HttpPut("Edit/{userId}")]
|
[HttpPut("Edit/{userId}")]
|
||||||
public async Task<IActionResult> EditUser([FromBody] User user, int userId)
|
public async Task<IActionResult> EditUser([FromBody] User user, int userId)
|
||||||
{
|
{
|
||||||
return await _userLogic.EditProfile(user, userId);
|
return await _userLogic.EditProfile(user, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
//[Authorize]
|
[Authorize]
|
||||||
[HttpDelete("Delete/{userId}")]
|
[HttpDelete("Delete/{userId}")]
|
||||||
public async Task<IActionResult> DeleteUser(int userId)
|
public async Task<IActionResult> DeleteUser(int userId)
|
||||||
{
|
{
|
||||||
|
@ -153,10 +153,6 @@ namespace Api.DBAccess
|
|||||||
return await _context.Devices.FirstOrDefaultAsync(d => d.Id == deviceId);
|
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)
|
public async Task<IActionResult> UpdateDevice(Device device, int deviceId)
|
||||||
{
|
{
|
||||||
@ -190,18 +186,6 @@ namespace Api.DBAccess
|
|||||||
return logs;
|
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()
|
public async Task<bool> Test()
|
||||||
{
|
{
|
||||||
return _context.Database.CanConnect();
|
return _context.Database.CanConnect();
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
using Api.DBAccess;
|
using Api.DBAccess;
|
||||||
using Api.Models;
|
|
||||||
using MQTTnet;
|
using MQTTnet;
|
||||||
using MQTTnet.Extensions.TopicTemplate;
|
using MQTTnet.Extensions.TopicTemplate;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
|
|
||||||
namespace Api.MQTTReciever
|
namespace Api.MQTTReciever
|
||||||
@ -12,11 +10,9 @@ namespace Api.MQTTReciever
|
|||||||
{
|
{
|
||||||
IMqttClient mqttClient;
|
IMqttClient mqttClient;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
private readonly DbAccess _dbAccess;
|
|
||||||
|
|
||||||
public MQTTReciever(IConfiguration configuration, DbAccess dbAccess)
|
public MQTTReciever(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_dbAccess = dbAccess;
|
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,28 +36,6 @@ namespace Api.MQTTReciever
|
|||||||
{
|
{
|
||||||
Console.WriteLine("Received application message.");
|
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;
|
return Task.CompletedTask;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
using Api;
|
using Api;
|
||||||
using Api.DBAccess;
|
|
||||||
using Api.MQTTReciever;
|
using Api.MQTTReciever;
|
||||||
using Microsoft.AspNetCore;
|
using Microsoft.AspNetCore;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@ -16,20 +15,6 @@ class Program
|
|||||||
mqtt.Handle_Received_Application_Message();
|
mqtt.Handle_Received_Application_Message();
|
||||||
RunMigrations(app);
|
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();
|
app.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user