Delete config link in devices if used + mqtt client not retained message
This commit is contained in:
parent
d6fd248b4f
commit
16d4d6335d
@ -23,14 +23,16 @@ namespace ManagerService.Controllers
|
|||||||
private ConfigurationDatabaseService _configurationService;
|
private ConfigurationDatabaseService _configurationService;
|
||||||
private SectionDatabaseService _sectionService;
|
private SectionDatabaseService _sectionService;
|
||||||
private ResourceDatabaseService _resourceService;
|
private ResourceDatabaseService _resourceService;
|
||||||
|
private DeviceDatabaseService _deviceService;
|
||||||
private readonly ILogger<ConfigurationController> _logger;
|
private readonly ILogger<ConfigurationController> _logger;
|
||||||
|
|
||||||
public ConfigurationController(ILogger<ConfigurationController> logger, ConfigurationDatabaseService configurationService, SectionDatabaseService sectionService, ResourceDatabaseService resourceService)
|
public ConfigurationController(ILogger<ConfigurationController> logger, ConfigurationDatabaseService configurationService, SectionDatabaseService sectionService, ResourceDatabaseService resourceService, DeviceDatabaseService deviceService)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_configurationService = configurationService;
|
_configurationService = configurationService;
|
||||||
_sectionService = sectionService;
|
_sectionService = sectionService;
|
||||||
_resourceService = resourceService;
|
_resourceService = resourceService;
|
||||||
|
_deviceService = deviceService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -197,6 +199,16 @@ namespace ManagerService.Controllers
|
|||||||
|
|
||||||
_configurationService.Remove(id);
|
_configurationService.Remove(id);
|
||||||
|
|
||||||
|
// Delete config for all devices
|
||||||
|
List<Device> devices = _deviceService.GetAllWithConfig(id);
|
||||||
|
|
||||||
|
foreach (var device in devices)
|
||||||
|
{
|
||||||
|
device.Configuration = null;
|
||||||
|
device.ConfigurationId = null;
|
||||||
|
_deviceService.Update(device.Id, device);
|
||||||
|
}
|
||||||
|
|
||||||
MqttClientService.PublishMessage($"config/{id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true, isDeleted = true }));
|
MqttClientService.PublishMessage($"config/{id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true, isDeleted = true }));
|
||||||
|
|
||||||
return new ObjectResult("The configuration has been deleted") { StatusCode = 202 };
|
return new ObjectResult("The configuration has been deleted") { StatusCode = 202 };
|
||||||
|
|||||||
@ -65,10 +65,15 @@ namespace ManagerService.Controllers
|
|||||||
if (id == null)
|
if (id == null)
|
||||||
throw new ArgumentNullException("Param is null");
|
throw new ArgumentNullException("Param is null");
|
||||||
|
|
||||||
|
if (_configurationService.IsExist(id))
|
||||||
|
{
|
||||||
List<Section> sections = _sectionService.GetAllFromConfiguration(id);
|
List<Section> sections = _sectionService.GetAllFromConfiguration(id);
|
||||||
|
|
||||||
return new OkObjectResult(sections.Select(r => r.ToDTO()));
|
return new OkObjectResult(sections.Select(r => r.ToDTO()));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
return new NotFoundObjectResult("Configuration not found");
|
||||||
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
return new BadRequestObjectResult(ex.Message) { };
|
return new BadRequestObjectResult(ex.Message) { };
|
||||||
|
|||||||
@ -5,6 +5,7 @@ using MQTTnet.Client;
|
|||||||
using MQTTnet.Client.Connecting;
|
using MQTTnet.Client.Connecting;
|
||||||
using MQTTnet.Client.Disconnecting;
|
using MQTTnet.Client.Disconnecting;
|
||||||
using MQTTnet.Client.Options;
|
using MQTTnet.Client.Options;
|
||||||
|
using MQTTnet.Protocol;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -135,8 +136,9 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
var mqttMessage = new MqttApplicationMessageBuilder()
|
var mqttMessage = new MqttApplicationMessageBuilder()
|
||||||
.WithTopic(topic)
|
.WithTopic(topic)
|
||||||
.WithPayload(message)
|
.WithPayload(message)
|
||||||
.WithExactlyOnceQoS()
|
.WithAtMostOnceQoS()
|
||||||
.WithRetainFlag()
|
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce)
|
||||||
|
.WithRetainFlag(false)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (mqttClient.IsConnected)
|
if (mqttClient.IsConnected)
|
||||||
|
|||||||
@ -29,6 +29,11 @@ namespace Manager.Services
|
|||||||
return _Devices.Find(d => d.Connected).ToList();
|
return _Devices.Find(d => d.Connected).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Device> GetAllWithConfig(string configId)
|
||||||
|
{
|
||||||
|
return _Devices.Find(d => d.ConfigurationId == configId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public Device GetById(string id)
|
public Device GetById(string id)
|
||||||
{
|
{
|
||||||
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault();
|
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user