mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41: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 List<string> DeviceIds { get; set; }
|
||||
public List<DeviceDetailDTO> Devices { get; set; }
|
||||
}
|
||||
|
||||
public class RoomCreateOrUpdateDetailDTO : RoomSummaryDTO
|
||||
|
||||
@ -44,7 +44,7 @@ namespace MyCore.Interfaces.Models
|
||||
};
|
||||
}
|
||||
|
||||
public RoomDetailDTO ToDTO()
|
||||
public RoomDetailDTO ToDTO(List<DeviceDetailDTO> devicesDetail)
|
||||
{
|
||||
return new RoomDetailDTO()
|
||||
{
|
||||
@ -53,7 +53,7 @@ namespace MyCore.Interfaces.Models
|
||||
Name = Name,
|
||||
CreatedDate = CreatedDate,
|
||||
UpdatedDate = UpdatedDate,
|
||||
DeviceIds = DevicesIds
|
||||
Devices = devicesDetail
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,9 +35,9 @@ namespace MyCore.Service.Controllers
|
||||
private readonly ActionService _ActionService;
|
||||
private readonly AutomationDatabaseService _AutomationDatabaseService;
|
||||
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;
|
||||
_tokensService = tokensService;
|
||||
@ -48,7 +48,7 @@ namespace MyCore.Service.Controllers
|
||||
_ActionService = ActionService;
|
||||
_AutomationDatabaseService = AutomationDatabaseService;
|
||||
_mqttClientService = provider.MqttClientService;
|
||||
_mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
//_mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
}
|
||||
|
||||
private ActionResult<LoginDTO> Authenticate(string email, string password)
|
||||
@ -56,7 +56,15 @@ namespace MyCore.Service.Controllers
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
/*catch (UnauthorizedAccessException ex)
|
||||
|
||||
@ -26,14 +26,14 @@ namespace MyCore.Service.Controllers
|
||||
private UserDatabaseService _UserDatabaseService;
|
||||
private AutomationDatabaseService _AutomationDatabaseService;
|
||||
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._AutomationDatabaseService = automationDatabaseService;
|
||||
this._mqttClientService = provider.MqttClientService;
|
||||
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -27,16 +27,16 @@ namespace MyCore.Controllers
|
||||
private LocationDatabaseService _LocationDatabaseService;
|
||||
private UserDatabaseService _UserDatabaseService;
|
||||
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._ProviderDatabaseService = ProviderDatabaseService;
|
||||
this._LocationDatabaseService = LocationDatabaseService;
|
||||
this._UserDatabaseService = UserDatabaseService;
|
||||
this._mqttClientService = provider.MqttClientService;
|
||||
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
}
|
||||
|
||||
// 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>
|
||||
/// Create a device
|
||||
/// </summary>
|
||||
|
||||
@ -24,15 +24,17 @@ namespace MyCore.Service.Controllers
|
||||
{
|
||||
private UserDatabaseService _UserDatabaseService;
|
||||
private RoomDatabaseService _RoomDatabaseService;
|
||||
private DeviceDatabaseService _DeviceDatabaseService;
|
||||
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._RoomDatabaseService = roomDatabaseService;
|
||||
this._DeviceDatabaseService = deviceDatabaseService;
|
||||
this._mqttClientService = provider.MqttClientService;
|
||||
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -60,16 +62,24 @@ namespace MyCore.Service.Controllers
|
||||
/// <summary>
|
||||
/// Get detail info of a specified room
|
||||
/// </summary>
|
||||
/// <param name="userId">user id</param>
|
||||
/// <param name="roomId">room id</param>
|
||||
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
||||
[HttpGet("detail/{roomId}")]
|
||||
public ObjectResult GetDetail(string roomId)
|
||||
public ObjectResult GetDetail(string userId, string roomId)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -90,7 +100,7 @@ namespace MyCore.Service.Controllers
|
||||
if (roomCreateOrUpdateDetail == 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);
|
||||
}
|
||||
@ -113,7 +123,7 @@ namespace MyCore.Service.Controllers
|
||||
if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id))
|
||||
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);
|
||||
}
|
||||
@ -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>
|
||||
/// Delete a room
|
||||
/// </summary>
|
||||
@ -140,6 +188,16 @@ namespace MyCore.Service.Controllers
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,13 +8,13 @@
|
||||
|
||||
public class AppSettingsOnlineProvider
|
||||
{
|
||||
public static BrokerOnlineHostSettings BrokerHostOnlineSettings;
|
||||
public static ClientOnlineSettings ClientOnlineSettings;
|
||||
public static BrokerHostSettings BrokerHostOnlineSettings;
|
||||
public static ClientSettings ClientOnlineSettings;
|
||||
}
|
||||
|
||||
public class AppSettingsMerossProvider
|
||||
{
|
||||
public static BrokerMerossHostSettings BrokerHostMerossSettings;
|
||||
public static ClientMerossSettings ClientMerossSettings;
|
||||
public static BrokerHostSettings BrokerHostMerossSettings;
|
||||
public static ClientSettings ClientMerossSettings;
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,16 +5,4 @@
|
||||
public string Host { 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 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;
|
||||
|
||||
// THANKS FIRE AT OVH
|
||||
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")
|
||||
.WithTcpServer("myhomie.be") // TODO replace by localhost
|
||||
.WithCredentials("thomas", "MyCore,1")
|
||||
.WithCleanSession()
|
||||
.Build();
|
||||
.Build();*/
|
||||
|
||||
mqttClient = new MqttFactory().CreateMqttClient();
|
||||
ConfigureMqttClient();
|
||||
|
||||
@ -31,7 +31,7 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
{
|
||||
this.options = options;
|
||||
this.options = new MqttClientOptionsBuilder()
|
||||
.WithClientId("ApiService")
|
||||
.WithClientId("ApiServiceTest") // prod = ApiService
|
||||
.WithTcpServer("192.168.31.140") // TODO replace by localhost
|
||||
.WithCredentials("mqtt", "mqtt")
|
||||
.WithCleanSession()
|
||||
@ -49,18 +49,18 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
|
||||
public Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs e)
|
||||
{
|
||||
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
|
||||
/*Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");*/
|
||||
var payload = "";
|
||||
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);
|
||||
}
|
||||
|
||||
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
||||
/*Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
||||
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();*/
|
||||
|
||||
var topic = e.ApplicationMessage.Topic;
|
||||
|
||||
|
||||
@ -89,7 +89,7 @@ namespace MyCore.Service.Extensions
|
||||
configure(optionBuilder);
|
||||
return optionBuilder.Build();
|
||||
});
|
||||
services.AddSingleton<MqttClientOnlineService>();
|
||||
/*services.AddSingleton<MqttClientOnlineService>();
|
||||
services.AddSingleton<IHostedService>(serviceProvider =>
|
||||
{
|
||||
return serviceProvider.GetService<MqttClientOnlineService>();
|
||||
@ -99,7 +99,7 @@ namespace MyCore.Service.Extensions
|
||||
var mqttOnlineClientService = serviceProvider.GetService<MqttClientOnlineService>();
|
||||
var mqttOnlineClientServiceProvider = new MqttClientOnlineServiceProvider(mqttOnlineClientService);
|
||||
return mqttOnlineClientServiceProvider;
|
||||
});
|
||||
});*/
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:25049",
|
||||
"sslPort": 44341
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"$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
|
||||
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 providers = _ProviderDatabaseService.GetAll(userId);
|
||||
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]) {
|
||||
case "zigbee2mqtt":
|
||||
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
|
||||
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();
|
||||
|
||||
System.Console.WriteLine($"Correct device ! {zigbeeDevice.Name}");
|
||||
|
||||
// Todo Deserialize by type
|
||||
switch (zigbeeDevice.Model)
|
||||
{
|
||||
@ -61,30 +67,38 @@ namespace MyCore.Services.Devices
|
||||
// Todo check state name and value for triggers..
|
||||
if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck) {
|
||||
// Todo check condition
|
||||
if (automation.Conditions.Count >= 0)
|
||||
if (automation.Conditions.Count <= 0)
|
||||
{
|
||||
System.Console.WriteLine("None conditions");
|
||||
// => SEND REQUEST
|
||||
foreach (var action in automation.Actions)
|
||||
{
|
||||
System.Console.WriteLine($"Check Action ! {action.StateName} {action.StateValue}");
|
||||
var stateName = action.StateName;
|
||||
var stateValue = action.StateValue;
|
||||
|
||||
var zigbeeDeviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
||||
|
||||
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)
|
||||
{
|
||||
case "zigbee2mqtt":
|
||||
Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { };
|
||||
var actionRequest = "";
|
||||
|
||||
System.Console.WriteLine($"Zigbee type !");
|
||||
|
||||
// Todo GET AND CHECK DEVICE ACTION POSSIBLE
|
||||
// todo check state name (state, action.. )
|
||||
System.Console.WriteLine($"zigbeeDeviceAction.Type {zigbeeDeviceAction.Type}");
|
||||
if (zigbeeDeviceAction.Type == DeviceType.Light)
|
||||
{
|
||||
if (stateValue == DeviceAction.toggle.ToString())
|
||||
{
|
||||
System.Console.WriteLine($"Toggle action");
|
||||
// TO CHECK
|
||||
switch (zigbeeDeviceAction.LastState)
|
||||
{
|
||||
@ -104,12 +118,18 @@ namespace MyCore.Services.Devices
|
||||
}
|
||||
}
|
||||
|
||||
System.Console.WriteLine($"Before retrieving type etc {zigbee2MqttRequest.state}");
|
||||
|
||||
Type type2 = zigbee2MqttRequest.GetType();
|
||||
PropertyInfo property2 = type2.GetProperty(stateName);
|
||||
property2.SetValue(zigbee2MqttRequest, actionRequest, null);
|
||||
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
|
||||
|
||||
|
||||
@ -96,7 +96,7 @@ namespace MyCore.Services.Devices
|
||||
if (create)
|
||||
return _DeviceDatabaseService.Create(device).ToDTO();
|
||||
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)
|
||||
|
||||
@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MyCore.Interfaces.DTO;
|
||||
|
||||
namespace MyCore.Services.MyControlPanel
|
||||
{
|
||||
@ -28,6 +29,21 @@ namespace MyCore.Services.MyControlPanel
|
||||
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)
|
||||
{
|
||||
return _Devices.Find(d => d.ProviderId == providerId).ToList();
|
||||
@ -54,9 +70,9 @@ namespace MyCore.Services.MyControlPanel
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -39,9 +39,9 @@ namespace MyCore.Services.MyControlPanel
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -10,11 +10,15 @@ namespace MyCore.Service
|
||||
{
|
||||
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;
|
||||
if (create)
|
||||
{
|
||||
room = new Room();
|
||||
room.CreatedDate = DateTime.Now;
|
||||
}
|
||||
else
|
||||
{
|
||||
room = _RoomDatabaseService.GetById(roomCreateOrUpdateDetailDTO.Id);
|
||||
@ -22,13 +26,31 @@ namespace MyCore.Service
|
||||
|
||||
room.UserId = userId;
|
||||
room.Name = roomCreateOrUpdateDetailDTO.Name;
|
||||
room.CreatedDate = 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)
|
||||
return _RoomDatabaseService.Create(room).ToDTO();
|
||||
{
|
||||
room.DevicesIds = currentDevices.Select(c => c.Id).ToList();
|
||||
room = _RoomDatabaseService.Create(room);
|
||||
}
|
||||
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);
|
||||
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
||||
|
||||
BrokerOnlineHostSettings brokerOnlineHostSettings = new BrokerOnlineHostSettings();
|
||||
Configuration.GetSection(nameof(BrokerOnlineHostSettings)).Bind(brokerOnlineHostSettings);
|
||||
BrokerHostSettings brokerOnlineHostSettings = new BrokerHostSettings();
|
||||
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerOnlineHostSettings);
|
||||
AppSettingsOnlineProvider.BrokerHostOnlineSettings = brokerOnlineHostSettings;
|
||||
}
|
||||
|
||||
@ -73,8 +73,8 @@ namespace MyCore
|
||||
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
||||
AppSettingsProvider.ClientSettings = clientSettings;
|
||||
|
||||
ClientOnlineSettings clientOnlineSettings = new ClientOnlineSettings();
|
||||
Configuration.GetSection(nameof(ClientOnlineSettings)).Bind(clientOnlineSettings);
|
||||
ClientSettings clientOnlineSettings = new ClientSettings();
|
||||
Configuration.GetSection(nameof(ClientSettings)).Bind(clientOnlineSettings);
|
||||
AppSettingsOnlineProvider.ClientOnlineSettings = clientOnlineSettings;
|
||||
}
|
||||
|
||||
@ -219,7 +219,7 @@ namespace MyCore
|
||||
app.UseExceptionHandler(HandleError);
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
//app.UseHttpsRedirection(); // to tessst
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user