mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
MC Room controller + fix fire at OVH + small changes
This commit is contained in:
parent
f6d6bd4012
commit
cb4ab29e40
@ -20,7 +20,7 @@ namespace MyCore.Interfaces.DTO
|
|||||||
|
|
||||||
public DateTime UpdatedDate { get; set; }
|
public DateTime UpdatedDate { get; set; }
|
||||||
|
|
||||||
public List<string> DeviceIds { get; set; }
|
public List<DeviceDetailDTO> Devices { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RoomCreateOrUpdateDetailDTO : RoomSummaryDTO
|
public class RoomCreateOrUpdateDetailDTO : RoomSummaryDTO
|
||||||
|
|||||||
@ -44,7 +44,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public RoomDetailDTO ToDTO()
|
public RoomDetailDTO ToDTO(List<DeviceDetailDTO> devicesDetail)
|
||||||
{
|
{
|
||||||
return new RoomDetailDTO()
|
return new RoomDetailDTO()
|
||||||
{
|
{
|
||||||
@ -53,7 +53,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
Name = Name,
|
Name = Name,
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
DeviceIds = DevicesIds
|
Devices = devicesDetail
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,9 +35,9 @@ namespace MyCore.Service.Controllers
|
|||||||
private readonly ActionService _ActionService;
|
private readonly ActionService _ActionService;
|
||||||
private readonly AutomationDatabaseService _AutomationDatabaseService;
|
private readonly AutomationDatabaseService _AutomationDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public AuthenticationController(ILogger<AuthenticationController> logger, TokensService tokensService, DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, ActionService ActionService, AutomationDatabaseService AutomationDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
public AuthenticationController(ILogger<AuthenticationController> logger, TokensService tokensService, DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, ActionService ActionService, AutomationDatabaseService AutomationDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_tokensService = tokensService;
|
_tokensService = tokensService;
|
||||||
@ -48,7 +48,7 @@ namespace MyCore.Service.Controllers
|
|||||||
_ActionService = ActionService;
|
_ActionService = ActionService;
|
||||||
_AutomationDatabaseService = AutomationDatabaseService;
|
_AutomationDatabaseService = AutomationDatabaseService;
|
||||||
_mqttClientService = provider.MqttClientService;
|
_mqttClientService = provider.MqttClientService;
|
||||||
_mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
//_mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ActionResult<LoginDTO> Authenticate(string email, string password)
|
private ActionResult<LoginDTO> Authenticate(string email, string password)
|
||||||
@ -56,7 +56,15 @@ namespace MyCore.Service.Controllers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var token = _tokensService.Authenticate(email.ToLower(), password);
|
var token = _tokensService.Authenticate(email.ToLower(), password);
|
||||||
MqttClientService.SetServices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, _ActionService, _AutomationDatabaseService, "5fef55bae30e1016d4776bfe"); // TODO Get userId when connected !!
|
|
||||||
|
// Set user token ?
|
||||||
|
var user = _UserDatabaseService.GetByEmail(email.ToLower());
|
||||||
|
|
||||||
|
if (user != null) {
|
||||||
|
System.Console.WriteLine($"Init userId for MqqClientService ! {user.Email}");
|
||||||
|
MqttClientService.SetServices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, _ActionService, _AutomationDatabaseService, user.Id);
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(token);
|
return Ok(token);
|
||||||
}
|
}
|
||||||
/*catch (UnauthorizedAccessException ex)
|
/*catch (UnauthorizedAccessException ex)
|
||||||
|
|||||||
@ -26,14 +26,14 @@ namespace MyCore.Service.Controllers
|
|||||||
private UserDatabaseService _UserDatabaseService;
|
private UserDatabaseService _UserDatabaseService;
|
||||||
private AutomationDatabaseService _AutomationDatabaseService;
|
private AutomationDatabaseService _AutomationDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public AutomationController(UserDatabaseService userDatabaseService, AutomationDatabaseService automationDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
public AutomationController(UserDatabaseService userDatabaseService, AutomationDatabaseService automationDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
this._UserDatabaseService = userDatabaseService;
|
this._UserDatabaseService = userDatabaseService;
|
||||||
this._AutomationDatabaseService = automationDatabaseService;
|
this._AutomationDatabaseService = automationDatabaseService;
|
||||||
this._mqttClientService = provider.MqttClientService;
|
this._mqttClientService = provider.MqttClientService;
|
||||||
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -27,16 +27,16 @@ namespace MyCore.Controllers
|
|||||||
private LocationDatabaseService _LocationDatabaseService;
|
private LocationDatabaseService _LocationDatabaseService;
|
||||||
private UserDatabaseService _UserDatabaseService;
|
private UserDatabaseService _UserDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
this._DeviceDatabaseService = DeviceDatabaseService;
|
this._DeviceDatabaseService = DeviceDatabaseService;
|
||||||
this._ProviderDatabaseService = ProviderDatabaseService;
|
this._ProviderDatabaseService = ProviderDatabaseService;
|
||||||
this._LocationDatabaseService = LocationDatabaseService;
|
this._LocationDatabaseService = LocationDatabaseService;
|
||||||
this._UserDatabaseService = UserDatabaseService;
|
this._UserDatabaseService = UserDatabaseService;
|
||||||
this._mqttClientService = provider.MqttClientService;
|
this._mqttClientService = provider.MqttClientService;
|
||||||
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: Devices
|
// GET: Devices
|
||||||
@ -87,6 +87,33 @@ namespace MyCore.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get list of devices from a type
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">user Id</param>
|
||||||
|
/// <param name="type">device type</param>
|
||||||
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
||||||
|
[HttpGet("{userId}/type/{type}")]
|
||||||
|
public ObjectResult GetDevicesByType(string userId, DeviceType type)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (userId != null)
|
||||||
|
{
|
||||||
|
List<Device> devices = _DeviceDatabaseService.GetByType(userId, type);
|
||||||
|
|
||||||
|
return new OkObjectResult(devices.Select(d => d.ToDTO()));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return new ObjectResult("Invalid parameters") { StatusCode = 400 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a device
|
/// Create a device
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -24,15 +24,17 @@ namespace MyCore.Service.Controllers
|
|||||||
{
|
{
|
||||||
private UserDatabaseService _UserDatabaseService;
|
private UserDatabaseService _UserDatabaseService;
|
||||||
private RoomDatabaseService _RoomDatabaseService;
|
private RoomDatabaseService _RoomDatabaseService;
|
||||||
|
private DeviceDatabaseService _DeviceDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public RoomController(UserDatabaseService userDatabaseService, RoomDatabaseService roomDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
public RoomController(UserDatabaseService userDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
this._UserDatabaseService = userDatabaseService;
|
this._UserDatabaseService = userDatabaseService;
|
||||||
this._RoomDatabaseService = roomDatabaseService;
|
this._RoomDatabaseService = roomDatabaseService;
|
||||||
|
this._DeviceDatabaseService = deviceDatabaseService;
|
||||||
this._mqttClientService = provider.MqttClientService;
|
this._mqttClientService = provider.MqttClientService;
|
||||||
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -56,20 +58,28 @@ namespace MyCore.Service.Controllers
|
|||||||
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get detail info of a specified room
|
/// Get detail info of a specified room
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="userId">user id</param>
|
||||||
/// <param name="roomId">room id</param>
|
/// <param name="roomId">room id</param>
|
||||||
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
||||||
[HttpGet("detail/{roomId}")]
|
[HttpGet("detail/{roomId}")]
|
||||||
public ObjectResult GetDetail(string roomId)
|
public ObjectResult GetDetail(string userId, string roomId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Room room = _RoomDatabaseService.GetById(roomId);
|
if (userId != null && roomId != null)
|
||||||
|
{
|
||||||
|
Room room = _RoomDatabaseService.GetById(roomId);
|
||||||
|
List<Device> devices = _DeviceDatabaseService.GetByLocation(room.UserId, roomId);
|
||||||
|
|
||||||
return new OkObjectResult(room.ToDTO());
|
return new OkObjectResult(room.ToDTO(devices.Select(d => d.ToDTO()).ToList()));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return new ObjectResult("Invalid parameters") { StatusCode = 400 };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -90,7 +100,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (roomCreateOrUpdateDetail == null)
|
if (roomCreateOrUpdateDetail == null)
|
||||||
throw new KeyNotFoundException("Room is null");
|
throw new KeyNotFoundException("Room is null");
|
||||||
|
|
||||||
RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, true);
|
RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, true);
|
||||||
|
|
||||||
return new OkObjectResult(roomCreated);
|
return new OkObjectResult(roomCreated);
|
||||||
}
|
}
|
||||||
@ -113,7 +123,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id))
|
if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id))
|
||||||
throw new KeyNotFoundException("Room does not exist");
|
throw new KeyNotFoundException("Room does not exist");
|
||||||
|
|
||||||
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, false);
|
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, false);
|
||||||
|
|
||||||
return new OkObjectResult(roomUpdated);
|
return new OkObjectResult(roomUpdated);
|
||||||
}
|
}
|
||||||
@ -127,6 +137,44 @@ namespace MyCore.Service.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Delete device from a room
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="deviceId">Id of device to delete from the room</param>
|
||||||
|
/// <param name="roomId">Id of room </param>
|
||||||
|
[HttpDelete("{roomId}/device/{deviceId}")]
|
||||||
|
public ObjectResult Delete(string deviceId, string roomId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (roomId != null && deviceId != null)
|
||||||
|
{
|
||||||
|
if (_RoomDatabaseService.IsExist(roomId))
|
||||||
|
{
|
||||||
|
// Update room
|
||||||
|
Room room = _RoomDatabaseService.GetById(roomId);
|
||||||
|
room.DevicesIds = room.DevicesIds.Where(d => d != deviceId).ToList();
|
||||||
|
room.UpdatedDate = DateTime.Now;
|
||||||
|
_RoomDatabaseService.Update(room);
|
||||||
|
|
||||||
|
// Update device
|
||||||
|
Device device = _DeviceDatabaseService.GetById(deviceId);
|
||||||
|
if (device != null) {
|
||||||
|
device.LocationId = null;
|
||||||
|
device.UpdatedDate = DateTime.Now;
|
||||||
|
_DeviceDatabaseService.Update(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new OkObjectResult(201);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete a room
|
/// Delete a room
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -140,6 +188,16 @@ namespace MyCore.Service.Controllers
|
|||||||
{
|
{
|
||||||
if (_RoomDatabaseService.IsExist(roomId))
|
if (_RoomDatabaseService.IsExist(roomId))
|
||||||
{
|
{
|
||||||
|
Room room = _RoomDatabaseService.GetById(roomId);
|
||||||
|
// Delete location from all devices
|
||||||
|
List<Device> devices = _DeviceDatabaseService.GetByLocation(room.UserId, roomId);
|
||||||
|
foreach (var device in devices)
|
||||||
|
{
|
||||||
|
device.LocationId = null;
|
||||||
|
device.UpdatedDate = DateTime.Now;
|
||||||
|
_DeviceDatabaseService.Update(device);
|
||||||
|
}
|
||||||
|
// Delete room
|
||||||
_RoomDatabaseService.Remove(roomId);
|
_RoomDatabaseService.Remove(roomId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,13 +8,13 @@
|
|||||||
|
|
||||||
public class AppSettingsOnlineProvider
|
public class AppSettingsOnlineProvider
|
||||||
{
|
{
|
||||||
public static BrokerOnlineHostSettings BrokerHostOnlineSettings;
|
public static BrokerHostSettings BrokerHostOnlineSettings;
|
||||||
public static ClientOnlineSettings ClientOnlineSettings;
|
public static ClientSettings ClientOnlineSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AppSettingsMerossProvider
|
public class AppSettingsMerossProvider
|
||||||
{
|
{
|
||||||
public static BrokerMerossHostSettings BrokerHostMerossSettings;
|
public static BrokerHostSettings BrokerHostMerossSettings;
|
||||||
public static ClientMerossSettings ClientMerossSettings;
|
public static ClientSettings ClientMerossSettings;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,16 +5,4 @@
|
|||||||
public string Host { set; get; }
|
public string Host { set; get; }
|
||||||
public int Port { set; get; }
|
public int Port { set; get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BrokerOnlineHostSettings
|
|
||||||
{
|
|
||||||
public string Host { set; get; }
|
|
||||||
public int Port { set; get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class BrokerMerossHostSettings
|
|
||||||
{
|
|
||||||
public string Host { set; get; }
|
|
||||||
public int Port { set; get; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,18 +6,4 @@
|
|||||||
public string UserName { set; get; }
|
public string UserName { set; get; }
|
||||||
public string Password { set; get; }
|
public string Password { set; get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ClientOnlineSettings
|
|
||||||
{
|
|
||||||
public string Id { set; get; }
|
|
||||||
public string UserName { set; get; }
|
|
||||||
public string Password { set; get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ClientMerossSettings
|
|
||||||
{
|
|
||||||
public string Id { set; get; }
|
|
||||||
public string UserName { set; get; }
|
|
||||||
public string Password { set; get; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,12 +23,19 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
{
|
{
|
||||||
this.onlineOptions = options;
|
this.onlineOptions = options;
|
||||||
|
|
||||||
|
// THANKS FIRE AT OVH
|
||||||
this.onlineOptions = new MqttClientOptionsBuilder()
|
this.onlineOptions = new MqttClientOptionsBuilder()
|
||||||
|
.WithClientId("ApiService")
|
||||||
|
.WithTcpServer("192.168.31.140") // TODO replace by localhost
|
||||||
|
.WithCredentials("mqtt", "mqtt")
|
||||||
|
.WithCleanSession()
|
||||||
|
.Build();
|
||||||
|
/*this.onlineOptions = new MqttClientOptionsBuilder()
|
||||||
.WithClientId("ApiService")
|
.WithClientId("ApiService")
|
||||||
.WithTcpServer("myhomie.be") // TODO replace by localhost
|
.WithTcpServer("myhomie.be") // TODO replace by localhost
|
||||||
.WithCredentials("thomas", "MyCore,1")
|
.WithCredentials("thomas", "MyCore,1")
|
||||||
.WithCleanSession()
|
.WithCleanSession()
|
||||||
.Build();
|
.Build();*/
|
||||||
|
|
||||||
mqttClient = new MqttFactory().CreateMqttClient();
|
mqttClient = new MqttFactory().CreateMqttClient();
|
||||||
ConfigureMqttClient();
|
ConfigureMqttClient();
|
||||||
|
|||||||
@ -31,7 +31,7 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
{
|
{
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.options = new MqttClientOptionsBuilder()
|
this.options = new MqttClientOptionsBuilder()
|
||||||
.WithClientId("ApiService")
|
.WithClientId("ApiServiceTest") // prod = ApiService
|
||||||
.WithTcpServer("192.168.31.140") // TODO replace by localhost
|
.WithTcpServer("192.168.31.140") // TODO replace by localhost
|
||||||
.WithCredentials("mqtt", "mqtt")
|
.WithCredentials("mqtt", "mqtt")
|
||||||
.WithCleanSession()
|
.WithCleanSession()
|
||||||
@ -49,18 +49,18 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
|
|
||||||
public Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs e)
|
public Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs e)
|
||||||
{
|
{
|
||||||
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
/*Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||||
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
|
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");*/
|
||||||
var payload = "";
|
var payload = "";
|
||||||
if (e.ApplicationMessage.Payload != null)
|
if (e.ApplicationMessage.Payload != null)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
|
//Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
|
||||||
payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
/*Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
||||||
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
|
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
|
||||||
Console.WriteLine();
|
Console.WriteLine();*/
|
||||||
|
|
||||||
var topic = e.ApplicationMessage.Topic;
|
var topic = e.ApplicationMessage.Topic;
|
||||||
|
|
||||||
|
|||||||
@ -89,7 +89,7 @@ namespace MyCore.Service.Extensions
|
|||||||
configure(optionBuilder);
|
configure(optionBuilder);
|
||||||
return optionBuilder.Build();
|
return optionBuilder.Build();
|
||||||
});
|
});
|
||||||
services.AddSingleton<MqttClientOnlineService>();
|
/*services.AddSingleton<MqttClientOnlineService>();
|
||||||
services.AddSingleton<IHostedService>(serviceProvider =>
|
services.AddSingleton<IHostedService>(serviceProvider =>
|
||||||
{
|
{
|
||||||
return serviceProvider.GetService<MqttClientOnlineService>();
|
return serviceProvider.GetService<MqttClientOnlineService>();
|
||||||
@ -99,7 +99,7 @@ namespace MyCore.Service.Extensions
|
|||||||
var mqttOnlineClientService = serviceProvider.GetService<MqttClientOnlineService>();
|
var mqttOnlineClientService = serviceProvider.GetService<MqttClientOnlineService>();
|
||||||
var mqttOnlineClientServiceProvider = new MqttClientOnlineServiceProvider(mqttOnlineClientService);
|
var mqttOnlineClientServiceProvider = new MqttClientOnlineServiceProvider(mqttOnlineClientService);
|
||||||
return mqttOnlineClientServiceProvider;
|
return mqttOnlineClientServiceProvider;
|
||||||
});
|
});*/
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
"anonymousAuthentication": true,
|
"anonymousAuthentication": true,
|
||||||
"iisExpress": {
|
"iisExpress": {
|
||||||
"applicationUrl": "http://localhost:25049",
|
"applicationUrl": "http://localhost:25049",
|
||||||
"sslPort": 44341
|
"sslPort": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
|||||||
@ -24,11 +24,14 @@ namespace MyCore.Services.Devices
|
|||||||
// TODO it's here that action are thrown.. Call from Mqtt Or other service like controller if from RpiServices
|
// TODO it's here that action are thrown.. Call from Mqtt Or other service like controller if from RpiServices
|
||||||
public static void HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, AutomationDatabaseService _AutomationDatabaseService, string userId)
|
public static void HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, AutomationDatabaseService _AutomationDatabaseService, string userId)
|
||||||
{
|
{
|
||||||
// TODO Check if last action is not too close for each action
|
// TODO Check if two action from the same device ar not too closed (!! motion (F => T) and switch (action and click = same)
|
||||||
var actionTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
var actionTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||||
|
|
||||||
var providers = _ProviderDatabaseService.GetAll(userId);
|
var providers = _ProviderDatabaseService.GetAll(userId);
|
||||||
string[] topicSplit = topic.Split('/');
|
string[] topicSplit = topic.Split('/');
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Received message {message}");
|
||||||
|
// TODO => Check the diff between <name> and <name>/set as the first, we receive the state and the second is only the request (we are no sure it has been accepted by device)
|
||||||
switch (topicSplit[0]) {
|
switch (topicSplit[0]) {
|
||||||
case "zigbee2mqtt":
|
case "zigbee2mqtt":
|
||||||
var test0 = _ProviderDatabaseService.GetByType(topicSplit[0]).Id;
|
var test0 = _ProviderDatabaseService.GetByType(topicSplit[0]).Id;
|
||||||
@ -41,8 +44,11 @@ namespace MyCore.Services.Devices
|
|||||||
|
|
||||||
// todo check if not null and if more than one element
|
// todo check if not null and if more than one element
|
||||||
if (zigbeeDevice != null && automation.Triggers.Any(t => t.DeviceId == zigbeeDevice.Id)) {
|
if (zigbeeDevice != null && automation.Triggers.Any(t => t.DeviceId == zigbeeDevice.Id)) {
|
||||||
|
System.Console.WriteLine($"Open automation {automation.Name}");
|
||||||
var automationTrigger = automation.Triggers.Where(t => t.DeviceId == zigbeeDevice.Id).FirstOrDefault();
|
var automationTrigger = automation.Triggers.Where(t => t.DeviceId == zigbeeDevice.Id).FirstOrDefault();
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Correct device ! {zigbeeDevice.Name}");
|
||||||
|
|
||||||
// Todo Deserialize by type
|
// Todo Deserialize by type
|
||||||
switch (zigbeeDevice.Model)
|
switch (zigbeeDevice.Model)
|
||||||
{
|
{
|
||||||
@ -61,30 +67,38 @@ namespace MyCore.Services.Devices
|
|||||||
// Todo check state name and value for triggers..
|
// Todo check state name and value for triggers..
|
||||||
if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck) {
|
if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck) {
|
||||||
// Todo check condition
|
// Todo check condition
|
||||||
if (automation.Conditions.Count >= 0)
|
if (automation.Conditions.Count <= 0)
|
||||||
{
|
{
|
||||||
|
System.Console.WriteLine("None conditions");
|
||||||
// => SEND REQUEST
|
// => SEND REQUEST
|
||||||
foreach (var action in automation.Actions)
|
foreach (var action in automation.Actions)
|
||||||
{
|
{
|
||||||
|
System.Console.WriteLine($"Check Action ! {action.StateName} {action.StateValue}");
|
||||||
var stateName = action.StateName;
|
var stateName = action.StateName;
|
||||||
var stateValue = action.StateValue;
|
var stateValue = action.StateValue;
|
||||||
|
|
||||||
var zigbeeDeviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
var zigbeeDeviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
||||||
|
|
||||||
var providerAction = _ProviderDatabaseService.GetById(userId, zigbeeDeviceAction.ProviderId);
|
var providerAction = _ProviderDatabaseService.GetById(userId, zigbeeDeviceAction.ProviderId);
|
||||||
|
|
||||||
|
System.Console.WriteLine($"We get a zigbeeDeviceAction ! Name={zigbeeDeviceAction.Name} Type={zigbeeDeviceAction.Type}");
|
||||||
|
System.Console.WriteLine($"Check action provider type ! Type={providerAction.Type} Name={providerAction.Name}");
|
||||||
|
|
||||||
switch (providerAction.Type)
|
switch (providerAction.Type)
|
||||||
{
|
{
|
||||||
case "zigbee2mqtt":
|
case "zigbee2mqtt":
|
||||||
Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { };
|
Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { };
|
||||||
var actionRequest = "";
|
var actionRequest = "";
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Zigbee type !");
|
||||||
|
|
||||||
// Todo GET AND CHECK DEVICE ACTION POSSIBLE
|
// Todo GET AND CHECK DEVICE ACTION POSSIBLE
|
||||||
// todo check state name (state, action.. )
|
// todo check state name (state, action.. )
|
||||||
|
System.Console.WriteLine($"zigbeeDeviceAction.Type {zigbeeDeviceAction.Type}");
|
||||||
if (zigbeeDeviceAction.Type == DeviceType.Light)
|
if (zigbeeDeviceAction.Type == DeviceType.Light)
|
||||||
{
|
{
|
||||||
if (stateValue == DeviceAction.toggle.ToString())
|
if (stateValue == DeviceAction.toggle.ToString())
|
||||||
{
|
{
|
||||||
|
System.Console.WriteLine($"Toggle action");
|
||||||
// TO CHECK
|
// TO CHECK
|
||||||
switch (zigbeeDeviceAction.LastState)
|
switch (zigbeeDeviceAction.LastState)
|
||||||
{
|
{
|
||||||
@ -104,12 +118,18 @@ namespace MyCore.Services.Devices
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Before retrieving type etc {zigbee2MqttRequest.state}");
|
||||||
|
|
||||||
Type type2 = zigbee2MqttRequest.GetType();
|
Type type2 = zigbee2MqttRequest.GetType();
|
||||||
PropertyInfo property2 = type2.GetProperty(stateName);
|
PropertyInfo property2 = type2.GetProperty(stateName);
|
||||||
property2.SetValue(zigbee2MqttRequest, actionRequest, null);
|
property2.SetValue(zigbee2MqttRequest, actionRequest, null);
|
||||||
var request = JsonConvert.SerializeObject(zigbee2MqttRequest);
|
var request = JsonConvert.SerializeObject(zigbee2MqttRequest);
|
||||||
|
|
||||||
MqttClientService.PublishMessage("zigbee2mqtt/" + zigbeeDeviceAction.Name.Substring(0, zigbeeDeviceAction.Name.Length-1) + "/set", request);
|
var name = zigbeeDeviceAction.Name.Substring(0, zigbeeDeviceAction.Name.Length - 1); // TODO CHANGE !!!!
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Send request ! zigbee2mqtt/{name}/set/{request}");
|
||||||
|
|
||||||
|
MqttClientService.PublishMessage("zigbee2mqtt/" + name + "/set", request);
|
||||||
|
|
||||||
// Save laststate
|
// Save laststate
|
||||||
|
|
||||||
|
|||||||
@ -96,7 +96,7 @@ namespace MyCore.Services.Devices
|
|||||||
if (create)
|
if (create)
|
||||||
return _DeviceDatabaseService.Create(device).ToDTO();
|
return _DeviceDatabaseService.Create(device).ToDTO();
|
||||||
else
|
else
|
||||||
return _DeviceDatabaseService.Update(device.Id, device).ToDTO();
|
return _DeviceDatabaseService.Update(device).ToDTO();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async static Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromProvider(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, Provider provider)
|
public async static Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromProvider(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, Provider provider)
|
||||||
|
|||||||
@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
|||||||
using MyCore.Interfaces.Models;
|
using MyCore.Interfaces.Models;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using MyCore.Interfaces.DTO;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services.MyControlPanel
|
||||||
{
|
{
|
||||||
@ -28,6 +29,21 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault();
|
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Device> GetByIds(List<string> ids)
|
||||||
|
{
|
||||||
|
return _Devices.Find<Device>(d => ids.Contains(d.Id)).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Device> GetByLocation(string userId, string locationId)
|
||||||
|
{
|
||||||
|
return _Devices.Find<Device>(d => d.UserId == userId && d.LocationId == locationId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Device> GetByType(string userId, DeviceType type)
|
||||||
|
{
|
||||||
|
return _Devices.Find<Device>(d => d.UserId == userId && d.Type == type).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public List<Device> GetByProviderId(string providerId)
|
public List<Device> GetByProviderId(string providerId)
|
||||||
{
|
{
|
||||||
return _Devices.Find(d => d.ProviderId == providerId).ToList();
|
return _Devices.Find(d => d.ProviderId == providerId).ToList();
|
||||||
@ -54,9 +70,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Device Update(string id, Device deviceIn)
|
public Device Update(Device deviceIn)
|
||||||
{
|
{
|
||||||
_Devices.ReplaceOne(device => device.Id == id, deviceIn);
|
_Devices.ReplaceOne(device => device.Id == deviceIn.Id, deviceIn);
|
||||||
return deviceIn;
|
return deviceIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -39,9 +39,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return room;
|
return room;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Room Update(string id, Room roomIn)
|
public Room Update(Room roomIn)
|
||||||
{
|
{
|
||||||
_Rooms.ReplaceOne(room => room.Id == id, roomIn);
|
_Rooms.ReplaceOne(room => room.Id == roomIn.Id, roomIn);
|
||||||
return roomIn;
|
return roomIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,11 +10,15 @@ namespace MyCore.Service
|
|||||||
{
|
{
|
||||||
public class RoomService
|
public class RoomService
|
||||||
{
|
{
|
||||||
public static RoomDetailDTO CreateOrUpdate(RoomDatabaseService _RoomDatabaseService, string userId, RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO, bool create)
|
public static RoomDetailDTO CreateOrUpdate(RoomDatabaseService _RoomDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string userId, RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO, bool create)
|
||||||
{
|
{
|
||||||
|
List<Device> devices = new List<Device>();
|
||||||
Room room;
|
Room room;
|
||||||
if (create)
|
if (create)
|
||||||
|
{
|
||||||
room = new Room();
|
room = new Room();
|
||||||
|
room.CreatedDate = DateTime.Now;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
room = _RoomDatabaseService.GetById(roomCreateOrUpdateDetailDTO.Id);
|
room = _RoomDatabaseService.GetById(roomCreateOrUpdateDetailDTO.Id);
|
||||||
@ -22,13 +26,31 @@ namespace MyCore.Service
|
|||||||
|
|
||||||
room.UserId = userId;
|
room.UserId = userId;
|
||||||
room.Name = roomCreateOrUpdateDetailDTO.Name;
|
room.Name = roomCreateOrUpdateDetailDTO.Name;
|
||||||
room.CreatedDate = DateTime.Now;
|
|
||||||
room.UpdatedDate = DateTime.Now;
|
room.UpdatedDate = DateTime.Now;
|
||||||
|
|
||||||
|
List<Device> currentDevices = _DeviceDatabaseService.GetByIds(roomCreateOrUpdateDetailDTO.DeviceIds); // A device cannot have multiple rooms. Filter list
|
||||||
|
currentDevices = currentDevices.Where(c => c.LocationId == null).ToList(); // TODO add exception or something
|
||||||
|
|
||||||
if (create)
|
if (create)
|
||||||
return _RoomDatabaseService.Create(room).ToDTO();
|
{
|
||||||
|
room.DevicesIds = currentDevices.Select(c => c.Id).ToList();
|
||||||
|
room = _RoomDatabaseService.Create(room);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
return _RoomDatabaseService.Update(room.Id, room).ToDTO();
|
{
|
||||||
|
room.DevicesIds.AddRange(currentDevices.Select(c => c.Id).ToList());
|
||||||
|
room = _RoomDatabaseService.Update(room);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var deviceId in roomCreateOrUpdateDetailDTO.DeviceIds)
|
||||||
|
{
|
||||||
|
Device device = _DeviceDatabaseService.GetById(deviceId);
|
||||||
|
devices.Add(device);
|
||||||
|
device.LocationId = room.Id;
|
||||||
|
_DeviceDatabaseService.Update(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
return room.ToDTO(devices.Select(d => d.ToDTO()).ToList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -62,8 +62,8 @@ namespace MyCore
|
|||||||
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
|
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
|
||||||
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
||||||
|
|
||||||
BrokerOnlineHostSettings brokerOnlineHostSettings = new BrokerOnlineHostSettings();
|
BrokerHostSettings brokerOnlineHostSettings = new BrokerHostSettings();
|
||||||
Configuration.GetSection(nameof(BrokerOnlineHostSettings)).Bind(brokerOnlineHostSettings);
|
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerOnlineHostSettings);
|
||||||
AppSettingsOnlineProvider.BrokerHostOnlineSettings = brokerOnlineHostSettings;
|
AppSettingsOnlineProvider.BrokerHostOnlineSettings = brokerOnlineHostSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,8 +73,8 @@ namespace MyCore
|
|||||||
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
||||||
AppSettingsProvider.ClientSettings = clientSettings;
|
AppSettingsProvider.ClientSettings = clientSettings;
|
||||||
|
|
||||||
ClientOnlineSettings clientOnlineSettings = new ClientOnlineSettings();
|
ClientSettings clientOnlineSettings = new ClientSettings();
|
||||||
Configuration.GetSection(nameof(ClientOnlineSettings)).Bind(clientOnlineSettings);
|
Configuration.GetSection(nameof(ClientSettings)).Bind(clientOnlineSettings);
|
||||||
AppSettingsOnlineProvider.ClientOnlineSettings = clientOnlineSettings;
|
AppSettingsOnlineProvider.ClientOnlineSettings = clientOnlineSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,7 +219,7 @@ namespace MyCore
|
|||||||
app.UseExceptionHandler(HandleError);
|
app.UseExceptionHandler(HandleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
//app.UseHttpsRedirection(); // to tessst
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"BookstoreDb": "mongodb://admin:MioTech4ever!@localhost:27017",
|
"BookstoreDb": "mongodb://admin:MioTech4ever!@localhost:27017",
|
||||||
"MyCoreDb": "mongodb://admin:MioTech4ever!@localhost:27017"
|
"MyCoreDb": "mongodb://admin:MioTech4ever!@localhost:27017"
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user