implementacion de get mqtt
parent
cfecf1424d
commit
45e2dfa286
|
|
@ -22,6 +22,9 @@ namespace Mqtt.App.Application.Interfaces
|
||||||
|
|
||||||
|
|
||||||
public Task<bool> AddMqtt(MqttEntity e);
|
public Task<bool> AddMqtt(MqttEntity e);
|
||||||
|
public Task<bool> AddMqtt(List<MqttEntity> e);
|
||||||
|
public Task<bool> UpdateOrInsertqtt(List<MqttEntity> lista);
|
||||||
|
public Task<List<MqttEntity>> getPendiente(string keytopic);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ namespace Mqtt.App.Application.configuration
|
||||||
public MapperProfile()
|
public MapperProfile()
|
||||||
{
|
{
|
||||||
CreateMap<MqttEntity, CreateMqttModel>().ReverseMap();
|
CreateMap<MqttEntity, CreateMqttModel>().ReverseMap();
|
||||||
|
CreateMap<MqttEntity, resultGetMqttModel>().ReverseMap();
|
||||||
|
|
||||||
|
|
||||||
CreateMap<UserEntity, CreateUserModel>().ReverseMap();
|
CreateMap<UserEntity, CreateUserModel>().ReverseMap();
|
||||||
CreateMap<UserEntity, UpdateUserModel>().ReverseMap();
|
CreateMap<UserEntity, UpdateUserModel>().ReverseMap();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using Mqtt.App.Application.mqtt.commands;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
@ -9,5 +10,7 @@ namespace Mqtt.App.Application.mqtt
|
||||||
public interface IMqttAppService
|
public interface IMqttAppService
|
||||||
{
|
{
|
||||||
Task<bool> Publica(mqttPublish data);
|
Task<bool> Publica(mqttPublish data);
|
||||||
|
Task<List<resultGetMqttModel>> get(GetMqttModel data);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
using Mqtt.App.Application.Interfaces.External;
|
using AutoMapper;
|
||||||
|
using Mqtt.App.Application.Interfaces;
|
||||||
|
using Mqtt.App.Application.Interfaces.External;
|
||||||
|
using Mqtt.App.Application.mqtt.commands;
|
||||||
|
using Mqtt.App.Domain.Entities.User;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
@ -10,9 +15,14 @@ namespace Mqtt.App.Application.mqtt
|
||||||
public class MqttAppService: IMqttAppService
|
public class MqttAppService: IMqttAppService
|
||||||
{
|
{
|
||||||
private readonly IMqttService _mqttService;
|
private readonly IMqttService _mqttService;
|
||||||
|
private readonly IPersistenciaService _persistenciaService;
|
||||||
public MqttAppService(IMqttService mqttService)
|
private IMapper _mapper;
|
||||||
|
public MqttAppService(IMqttService mqttService,
|
||||||
|
IPersistenciaService persistenciaService,
|
||||||
|
IMapper mapper)
|
||||||
{
|
{
|
||||||
|
_persistenciaService = persistenciaService;
|
||||||
|
_mapper = mapper;
|
||||||
_mqttService=mqttService;
|
_mqttService=mqttService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,6 +30,18 @@ namespace Mqtt.App.Application.mqtt
|
||||||
{
|
{
|
||||||
return CompruebaKey(data) && await _mqttService.publishAsync(data.topic, data.payload);
|
return CompruebaKey(data) && await _mqttService.publishAsync(data.topic, data.payload);
|
||||||
}
|
}
|
||||||
|
public async Task<List<resultGetMqttModel>> get(GetMqttModel data)
|
||||||
|
{
|
||||||
|
var res = new List<resultGetMqttModel> ();
|
||||||
|
if(data==null || String.IsNullOrWhiteSpace(data.topic) || !CompruebaKey(data))
|
||||||
|
return res;
|
||||||
|
var resBD = await _persistenciaService.getPendiente(data.topic);
|
||||||
|
res = _mapper.Map<List<resultGetMqttModel>>(resBD);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
internal bool CompruebaKey(MqttBase data)
|
internal bool CompruebaKey(MqttBase data)
|
||||||
{
|
{
|
||||||
return _mqttService.CompruebaKey(data.key);
|
return _mqttService.CompruebaKey(data.key);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Mqtt.App.Application.Interfaces;
|
using Mqtt.App.Application.Interfaces;
|
||||||
using Mqtt.App.Application.Interfaces.External;
|
using Mqtt.App.Application.Interfaces.External;
|
||||||
|
|
@ -19,34 +20,50 @@ namespace Mqtt.App.Application.mqtt
|
||||||
|
|
||||||
|
|
||||||
private readonly IMqttService _mqttService;
|
private readonly IMqttService _mqttService;
|
||||||
private readonly IPersistenciaService _persistenciaService;
|
private IPersistenciaService _persistenciaService;
|
||||||
private IMapper _mapper;
|
private IMapper _mapper;
|
||||||
public MqttBackgroundService(IPersistenciaService persistenciaService,
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
private List<MqttEntity> cola;
|
||||||
|
public MqttBackgroundService(IServiceScopeFactory scopeFactory,
|
||||||
IMqttService mqttService,
|
IMqttService mqttService,
|
||||||
IMapper mapper)
|
IMapper mapper)
|
||||||
{
|
{
|
||||||
_persistenciaService = persistenciaService;
|
_scopeFactory = scopeFactory;
|
||||||
_mqttService = mqttService;
|
_mqttService = mqttService;
|
||||||
_mapper = mapper;
|
_mapper = mapper;
|
||||||
|
cola = new List<MqttEntity>();
|
||||||
}
|
}
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
|
using var scope = _scopeFactory.CreateScope();
|
||||||
|
_persistenciaService = scope.ServiceProvider.GetRequiredService<IPersistenciaService>();
|
||||||
|
_mqttService.TastOnReceive(stoppingToken, this);
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
_mqttService.TastOnReceive(stoppingToken, this);
|
await Task.Delay(1000, stoppingToken);
|
||||||
|
await revisaCola();
|
||||||
}
|
}
|
||||||
|
await revisaCola();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public async Task revisaCola()
|
||||||
|
{
|
||||||
|
if(cola.Any())
|
||||||
|
{
|
||||||
|
|
||||||
|
var aux= new List<MqttEntity>();
|
||||||
|
var cola2 = cola;
|
||||||
|
cola = aux;
|
||||||
|
await _persistenciaService.UpdateOrInsertqtt(cola2);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
public void onReceived(string topic, string payload)
|
public void onReceived(string topic, string payload)
|
||||||
{
|
{
|
||||||
var model = new CreateMqttModel();
|
var model = new CreateMqttModel();
|
||||||
model.Procesa(topic, payload);
|
model.Procesa(topic, payload);
|
||||||
var entity = _mapper.Map<MqttEntity>(model);
|
var entity = _mapper.Map<MqttEntity>(model);
|
||||||
_persistenciaService.AddMqtt(entity);
|
cola.Add(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Mqtt.App.Application.mqtt.commands
|
||||||
|
{
|
||||||
|
public class GetMqttModel : MqttBase
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Mqtt.App.Application.mqtt.commands
|
||||||
|
{
|
||||||
|
public class resultGetMqttModel
|
||||||
|
{
|
||||||
|
public string payload { get; set; }
|
||||||
|
public string topic { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ namespace Mqtt.App.Domain.Entities.Mqtt
|
||||||
{
|
{
|
||||||
public class MqttEntity
|
public class MqttEntity
|
||||||
{
|
{
|
||||||
public Int64 Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Payload { get; set; }
|
public string Payload { get; set; }
|
||||||
public string Topic { get; set; }
|
public string Topic { get; set; }
|
||||||
public string KeyTopic { get; set; }
|
public string KeyTopic { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Mqtt.App.Application.Features;
|
using Mqtt.App.Application.Features;
|
||||||
using Mqtt.App.Application.mqtt;
|
using Mqtt.App.Application.mqtt;
|
||||||
|
using Mqtt.App.Application.mqtt.commands;
|
||||||
using Mqtt.App.Application.Persistencia.user.commands.CreateUser;
|
using Mqtt.App.Application.Persistencia.user.commands.CreateUser;
|
||||||
|
|
||||||
namespace Mqtt.App.api.Controllers
|
namespace Mqtt.App.api.Controllers
|
||||||
|
|
@ -22,5 +23,15 @@ namespace Mqtt.App.api.Controllers
|
||||||
var data = await mqttService.Publica(model);
|
var data = await mqttService.Publica(model);
|
||||||
return StatusCode(StatusCodes.Status201Created, ResponseApiService.Response(StatusCodes.Status201Created, data));
|
return StatusCode(StatusCodes.Status201Created, ResponseApiService.Response(StatusCodes.Status201Created, data));
|
||||||
}
|
}
|
||||||
|
[AllowAnonymous]
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> Create(
|
||||||
|
[FromBody] GetMqttModel model,
|
||||||
|
[FromServices] IMqttAppService mqttService
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var data = await mqttService.get(model);
|
||||||
|
return StatusCode(StatusCodes.Status201Created, ResponseApiService.Response(StatusCodes.Status201Created, data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"SqlConnectionString": "server=192.168.56.1,1433;user id=sa;password=pass1234.4321@#;initial catalog=AppPrueba; Trust Server Certificate=True",
|
"SqlConnectionString": "server=192.168.2.20,1433;user id=sa;password=Filoctetes.66337788;initial catalog=MqttApp; Trust Server Certificate=True",
|
||||||
"SecretKeyJwt": "NFAJJSAK3523dgsdf.dasfas.dsnfansnvDSFaskin908348",
|
"SecretKeyJwt": "NFAJJSAK3523dgsdf.dasfas.dsnfansnvDSFaskin908348",
|
||||||
"IssuerJWT": "MqttApp.com",
|
"IssuerJWT": "MqttApp.com",
|
||||||
"AudienceJwt": "MqttApp.com",
|
"AudienceJwt": "MqttApp.com",
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ namespace Mqtt.App.Persistencia.Configurations
|
||||||
public class UserConfiguration
|
public class UserConfiguration
|
||||||
{
|
{
|
||||||
public UserConfiguration( EntityTypeBuilder<UserEntity> entityBuilder) {
|
public UserConfiguration( EntityTypeBuilder<UserEntity> entityBuilder) {
|
||||||
entityBuilder.ToTable("UserEntity");
|
entityBuilder.ToTable("User");
|
||||||
entityBuilder.HasKey(x => x.Id);
|
entityBuilder.HasKey(x => x.Id);
|
||||||
entityBuilder.Property(x => x.UserName).IsRequired();
|
entityBuilder.Property(x => x.UserName).IsRequired();
|
||||||
entityBuilder.Property(x => x.LastName).IsRequired();
|
entityBuilder.Property(x => x.LastName).IsRequired();
|
||||||
|
|
@ -25,7 +25,7 @@ namespace Mqtt.App.Persistencia.Configurations
|
||||||
}
|
}
|
||||||
public UserConfiguration(EntityTypeBuilder<MqttEntity> entityBuilder)
|
public UserConfiguration(EntityTypeBuilder<MqttEntity> entityBuilder)
|
||||||
{
|
{
|
||||||
entityBuilder.ToTable("UserEntity");
|
entityBuilder.ToTable("Mqtt");
|
||||||
entityBuilder.HasKey(x => x.Id);
|
entityBuilder.HasKey(x => x.Id);
|
||||||
entityBuilder.Property(x => x.Payload).IsRequired();
|
entityBuilder.Property(x => x.Payload).IsRequired();
|
||||||
entityBuilder.Property(x => x.Topic).IsRequired();
|
entityBuilder.Property(x => x.Topic).IsRequired();
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ namespace Mqtt.App.Persistencia
|
||||||
services.AddDbContext<PersistenciaService>( options =>
|
services.AddDbContext<PersistenciaService>( options =>
|
||||||
options.UseSqlServer(config["SqlConnectionString"]));
|
options.UseSqlServer(config["SqlConnectionString"]));
|
||||||
|
|
||||||
services.AddScoped<IPersistenciaService, PersistenciaService>();
|
services.AddTransient<IPersistenciaService, PersistenciaService>();
|
||||||
|
|
||||||
|
|
||||||
//para cifrado de bbdd
|
//para cifrado de bbdd
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Mqtt.App.Domain.Entities.Mqtt;
|
using Mqtt.App.Domain.Entities.Mqtt;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace Mqtt.App.Persistencia.Services
|
namespace Mqtt.App.Persistencia.Services
|
||||||
{
|
{
|
||||||
|
|
@ -83,6 +84,59 @@ namespace Mqtt.App.Persistencia.Services
|
||||||
Mqtt.Add(e);
|
Mqtt.Add(e);
|
||||||
return await BoolAsync();
|
return await BoolAsync();
|
||||||
}
|
}
|
||||||
|
public async Task<bool> AddMqtt(List<MqttEntity> e)
|
||||||
|
{
|
||||||
|
Mqtt.AddRange(e);
|
||||||
|
return await BoolAsync();
|
||||||
|
}
|
||||||
|
public async Task<List<MqttEntity>> getPendiente(string keytopic)
|
||||||
|
{
|
||||||
|
using var transaction = await Database.BeginTransactionAsync();
|
||||||
|
var res = new List<MqttEntity>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
res = Mqtt.Where(e => !e.IsActive && e.KeyTopic.Equals(keytopic)).OrderBy(e => e.Fecha).ToList();
|
||||||
|
if (res == null)
|
||||||
|
res = new List<MqttEntity>();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (var e in res)
|
||||||
|
{
|
||||||
|
var ent = await Set<MqttEntity>().FindAsync(e.Id);
|
||||||
|
ent.IsActive = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await SaveChangesAsync();
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> UpdateOrInsertqtt(List<MqttEntity> lista)
|
||||||
|
{
|
||||||
|
foreach(var e in lista)
|
||||||
|
{
|
||||||
|
var eDB = Mqtt.FirstOrDefault(a => a.Topic.Equals(e.Topic));
|
||||||
|
if (eDB == null || eDB.Id<=0)
|
||||||
|
Mqtt.AddRange(e);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eDB.Payload = e.Payload;
|
||||||
|
eDB.Fecha = e.Fecha;
|
||||||
|
eDB.IsActive = e.IsActive;
|
||||||
|
Mqtt.Update(eDB);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return await BoolAsync();
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private async Task<bool> BoolAsync()
|
private async Task<bool> BoolAsync()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
USE [MqttApp]
|
||||||
|
GO
|
||||||
|
|
||||||
|
SET ANSI_NULLS ON
|
||||||
|
GO
|
||||||
|
|
||||||
|
SET QUOTED_IDENTIFIER ON
|
||||||
|
GO
|
||||||
|
--motivos cancelacion
|
||||||
|
-- DROP TABLE [MqttApp].[dbo].[User]
|
||||||
|
-- SELECT * [MqttApp].[dbo].[User]
|
||||||
|
CREATE TABLE [MqttApp].[dbo].[User](
|
||||||
|
[Id] [int] IDENTITY(1,1) NOT NULL,
|
||||||
|
[UserName] [nvarchar](125) NULL,
|
||||||
|
[FristName] [nvarchar](125) NULL,
|
||||||
|
[LastName] [nvarchar](125) NULL,
|
||||||
|
[Password] [nvarchar](256) NULL
|
||||||
|
PRIMARY KEY ([ID])
|
||||||
|
)
|
||||||
|
CREATE TABLE [MqttApp].[dbo].[Mqtt](
|
||||||
|
[Id] [int] IDENTITY(1,1) NOT NULL,
|
||||||
|
[Payload] [nvarchar](256) NULL,
|
||||||
|
[Topic] [nvarchar](125) NULL,
|
||||||
|
[KeyTopic] [nvarchar](125) NULL,
|
||||||
|
[TypeTopic] [nvarchar](32) NULL,
|
||||||
|
[Fecha] [date] NOT NULL,
|
||||||
|
IsActive bit not NULL
|
||||||
|
PRIMARY KEY ([Id])
|
||||||
|
)
|
||||||
Loading…
Reference in New Issue