MC support Yeelight devices creation from provider (automatic)

This commit is contained in:
Thomas Fransolet 2020-03-30 17:58:52 +02:00
parent 96b552f884
commit 7e1365c277
24 changed files with 156 additions and 96 deletions

View File

@ -10,6 +10,7 @@ using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using MyCore.Models;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
using MyCore.Services;
using MyCore.Services.Devices;
using MyCore.Services.MyControlPanel;
@ -95,6 +96,10 @@ namespace MyCore.Controllers.Devices
return new OkObjectResult(deviceCreated);
}
catch (DuplicateWaitObjectException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
}
catch (KeyNotFoundException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
@ -112,7 +117,7 @@ namespace MyCore.Controllers.Devices
/// <param name="providerId">Id of Provider</param>
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
[HttpPost("fromProvider/{userId}")]
public ObjectResult CreateDevicesFromProvider(string userId, [FromBody] string providerId)
public async Task<ObjectResult> CreateDevicesFromProvider(string userId, string providerId)
{
try
{
@ -123,13 +128,12 @@ namespace MyCore.Controllers.Devices
throw new KeyNotFoundException("User not found");
Provider provider = ProviderService.GetProviderById(this._ProviderDatabaseService, userId, providerId);
if (provider != null)
if (provider == null)
throw new KeyNotFoundException("Provider id is null");
List<DeviceDetailDTO> devicesCreated = DeviceService.CreateFromProvider(provider);
List<DeviceDetailDTO> devicesCreated = await DeviceService.CreateFromProvider(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, provider);
return new OkObjectResult(devicesCreated);
}
catch (InvalidOperationException ex)
{

View File

@ -10,6 +10,7 @@ using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using MyCore.Models;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
using MyCore.Services;
using MyCore.Services.Devices;
using MyCore.Services.MyControlPanel;

View File

@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using MyCore.DTO;
using MyCore.Models;
using MyCore.Models.MyControlPanel.Database;
using MyCore.Services;
namespace MyCore.Controllers

View File

@ -8,6 +8,7 @@ using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Server;
using MyCore.Models;
using MyCore.Models.MyControlPanel.Database;
using MyCore.Services;
namespace MyCore.Controllers

View File

@ -13,8 +13,14 @@ namespace MyCore.DTO.MyControlPanel
public string Name { get; set; }
public string Model { get; set; }
public bool Status { get; set; }
public ConnectionStatus ConnectionStatus { get; set; }
public string LocationId { get; set; }
public LocationDTO Location { get; set; }
public bool Battery { get; set; }
@ -28,8 +34,16 @@ namespace MyCore.DTO.MyControlPanel
public string Name { get; set; }
public string Model { get; set; }
public string FirmwareVersion { get; set; }
public int Port { get; set; }
public ConnectionStatus ConnectionStatus { get; set; }
public bool Status { get; set; }
public string LocationId { get; set; }
public LocationDTO Location { get; set; }
@ -54,6 +68,8 @@ namespace MyCore.DTO.MyControlPanel
public List<string> Groups { get; set; }
public List<InformationDTO> Informations { get; set; }
public Dictionary<string, object> Properties { get; set; }
public List<string> SupportedOperations { get; set; }
}
}

View File

@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO.MyControlPanel
{
public class InformationDTO
{
public string Id { get; set; }
public string Name { get; set; }
public object Value { get; set; }
}
}

View File

@ -13,5 +13,6 @@ namespace MyCore.DTO.MyControlPanel
public string Username { get; set; }
public string Password { get; set; } // TODO ENCRYPTED
public string ApiKey { get; set; } // TODO ENCRYPTED
public bool Active { get; set; }
}
}

View File

@ -5,7 +5,7 @@ using MyCore.DTO.MyControlPanel;
using System;
using System.Collections.Generic;
namespace MyCore.Models
namespace MyCore.Models.MyControlPanel.Database
{
/// <summary>
/// Automation

View File

@ -2,12 +2,13 @@
using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
using MyCore.Services.MyControlPanel;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyCore.Models.MyControlPanel
namespace MyCore.Models.MyControlPanel.Database
{
/// <summary>
/// Group of devices
@ -22,11 +23,25 @@ namespace MyCore.Models.MyControlPanel
[BsonRequired]
public string Name { get; set; }
[BsonElement("Model")]
[BsonRequired]
public string Model { get; set; }
[BsonElement("Port")]
[BsonRequired]
public int Port { get; set; }
[BsonElement("FirmwareVersion")]
[BsonRequired]
public string FirmwareVersion { get; set; }
[BsonElement("Status")]
public bool Status { get; set; }
[BsonElement("ConnectionStatus")]
public ConnectionStatus ConnectionStatus { get; set; }
[BsonElement("LocationId")]
[BsonRequired]
public string LocationId { get; set; }
[BsonElement("MeansOfCommunications")]
@ -60,8 +75,11 @@ namespace MyCore.Models.MyControlPanel
[BsonElement("GroupIds")]
public List<string> GroupIds { get; set; }
[BsonElement("Informations")]
public List<Information> Information { get; set; }
[BsonElement("Properties")]
public Dictionary<string, object> Properties { get; set; }
[BsonElement("SupportedOperations")]
public List<string> SupportedOperations { get; set; }
public DeviceSummaryDTO ToSummaryDTO()
{
@ -69,8 +87,11 @@ namespace MyCore.Models.MyControlPanel
{
Id = Id,
Name = Name,
Model = Model,
Status = Status,
ConnectionStatus = ConnectionStatus,
//Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
LocationId = LocationId, // Check if correct way
// Location =
Battery = Battery,
BatteryStatus = BatteryStatus
};
@ -82,17 +103,23 @@ namespace MyCore.Models.MyControlPanel
{
Id = Id,
Name = Name,
Model = Model,
Status = Status,
Port = Port,
FirmwareVersion = FirmwareVersion,
ConnectionStatus = ConnectionStatus,
//Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
LocationId = LocationId, // Check if correct way
// Location =
MeansOfCommunications = MeansOfCommunications,
CreatedDate = CreatedDate,
UpdatedDate = UpdatedDate,
LastMessage = LastMessage,
LastMessageDate = LastMessageDate,
IpAddress = IpAddress,
//Provider = Provider.ToDTO(device.Provider),
//Groups = device.Groups.Select(i => i.ToDTO(i)).ToList(),
//Informations = device.Informations.Select(i => i.ToDTO(i)).ToList(),
ProviderId = ProviderId,
Groups = GroupIds,
Properties = Properties,
SupportedOperations = SupportedOperations,
Battery = Battery,
BatteryStatus = BatteryStatus
};

View File

@ -4,7 +4,7 @@ using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System;
using System.Collections.Generic;
namespace MyCore.Models.MyControlPanel
namespace MyCore.Models.MyControlPanel.Database
{
/// <summary>
/// Group of devices

View File

@ -1,37 +0,0 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System;
using System.Collections.Generic;
namespace MyCore.Models.MyControlPanel
{
/// <summary>
/// Information contains in device message
/// </summary>
public class Information
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
[BsonRequired]
public string Name { get; set; }
[BsonElement("Value")]
[BsonRequired]
public object Value { get; set; }
public InformationDTO ToDTO()
{
return new InformationDTO()
{
Id = Id,
Name = Name,
Value = Value
};
}
}
}

View File

@ -2,7 +2,7 @@
using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.MyControlPanel;
namespace MyCore.Models.MyControlPanel
namespace MyCore.Models.MyControlPanel.Database
{
/// <summary>
/// Location of a device (Room name, garden, ..)

View File

@ -5,7 +5,7 @@ using MyCore.DTO.MyControlPanel;
using System;
using System.Collections.Generic;
namespace MyCore.Models.MyControlPanel
namespace MyCore.Models.MyControlPanel.Database
{
/// <summary>
/// Provider of a device (provider of informations) - e.g. : Meross, Arlo, IoThomas, ...
@ -35,12 +35,17 @@ namespace MyCore.Models.MyControlPanel
[BsonElement("ApiKey")]
public string ApiKey { get; set; } // TODO ENCRYPTED
[BsonElement("Active")]
[BsonRequired]
public bool Active { get; set; }
public ProviderDTO ToDTO()
{
return new ProviderDTO()
{
Id = Id,
Name = Name
Name = Name,
Active = Active
};
}
}

View File

@ -7,7 +7,7 @@ using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MyCore.Models.MyControlPanel;
namespace MyCore.Models
namespace MyCore.Models.MyControlPanel.Database
{
/// <summary>
/// User Information

View File

@ -2,6 +2,7 @@
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
using MyCore.Services.MyControlPanel;
using System;
using System.Collections.Generic;
@ -22,19 +23,33 @@ namespace MyCore.Services.Devices
{
device = _DeviceDatabaseService.GetById(deviceDetailDTO.Id);
}
device.Name = deviceDetailDTO.Name;
if (_DeviceDatabaseService.IsAlreadyHere(deviceDetailDTO.IpAddress, deviceDetailDTO.Port) && create)
{
return null;
}
device.Name = deviceDetailDTO.Name;
if (_ProviderDatabaseService.IsExist(deviceDetailDTO.ProviderId))
device.ProviderId = deviceDetailDTO.ProviderId;
else
throw new KeyNotFoundException("Provider does not exist");
if (_LocationDatabaseService.IsExist(deviceDetailDTO.LocationId))
if (device.LocationId == null || _LocationDatabaseService.IsExist(deviceDetailDTO.LocationId))
device.LocationId = deviceDetailDTO.LocationId;
else
throw new KeyNotFoundException("Location does not exist");
device.ConnectionStatus = ConnectionStatus.Unknown;
device.Port = deviceDetailDTO.Port;
device.Model = deviceDetailDTO.Model;
device.FirmwareVersion = deviceDetailDTO.FirmwareVersion;
device.Status = deviceDetailDTO.Status;
if (create)
device.ConnectionStatus = ConnectionStatus.Unknown;
else
device.ConnectionStatus = deviceDetailDTO.ConnectionStatus;
device.Status = device.Status;
device.LocationId = device.LocationId;
device.CreatedDate = DateTime.Now;
device.UpdatedDate = DateTime.Now;
@ -43,8 +58,10 @@ namespace MyCore.Services.Devices
device.Battery = deviceDetailDTO.Battery;
device.BatteryStatus = deviceDetailDTO.BatteryStatus;
device.GroupIds = device.GroupIds;
// Todo structure informations
device.Information = device.Information;
// Todo structure Properties
device.Properties = device.Properties;
// Todo structure SupportedOperations
device.SupportedOperations = device.SupportedOperations;
if (create)
return _DeviceDatabaseService.Create(device).ToDTO();
@ -52,13 +69,13 @@ namespace MyCore.Services.Devices
return _DeviceDatabaseService.Update(device.Id, device).ToDTO();
}
public static List<DeviceDetailDTO> CreateFromProvider(Provider provider)
public async static Task<List<DeviceDetailDTO>> CreateFromProvider(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, Provider provider)
{
Device device = new Device();
if (!ProviderService.IsProviderSupported(provider.Name))
throw new KeyNotFoundException("Provider is not yet supported");
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
switch (provider.Name)
{
case "Arlo":
@ -68,14 +85,50 @@ namespace MyCore.Services.Devices
break;
case "Yeelight":
List<YeelightAPI.Device> yeelightDevices = await new YeelightService().GetDevices();
createdDevice = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, yeelightDevices, provider);
break;
}
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
return createdDevice;
}
public static List<DeviceDetailDTO> CreateYeelightDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, List<YeelightAPI.Device> yeelightDevices, Provider provider)
{
List<DeviceDetailDTO> createdYeelightDevices = new List<DeviceDetailDTO>();
foreach (var light in yeelightDevices)
{
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
deviceDetailDTO.Name = light.Name;
deviceDetailDTO.IpAddress = light.Hostname;
deviceDetailDTO.ProviderId = provider.Id;
deviceDetailDTO.ConnectionStatus = ConnectionStatus.Connected;
deviceDetailDTO.Status = false;
deviceDetailDTO.Model = light.Model.ToString();
deviceDetailDTO.Port = light.Port;
deviceDetailDTO.FirmwareVersion = light.FirmwareVersion;
Dictionary<string, object> properties = new Dictionary<string, object>();
foreach (var property in light.Properties)
{
properties.Add(property.Key, property.Value);
}
deviceDetailDTO.Properties = properties;
List<string> supportedOperationsDTO = new List<string>();
foreach (var supportedOperation in light.SupportedOperations)
{
supportedOperationsDTO.Add(supportedOperation.ToString());
}
deviceDetailDTO.SupportedOperations = supportedOperationsDTO;
deviceDetailDTO.MeansOfCommunications = new List<MeansOfCommunication>();
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi);
deviceDetailDTO.CreatedDate = DateTime.Now;
deviceDetailDTO.UpdatedDate = DateTime.Now;
createdYeelightDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, deviceDetailDTO, true));
}
return createdYeelightDevices;
}
}
}

View File

@ -33,16 +33,6 @@ namespace MyCore.Services
public MQTTService()
{
try
{
yeelightService.GetDevices();
}
catch (Exception ex)
{
}
try
{
// Create a new MQTT client.

View File

@ -10,7 +10,7 @@ namespace MyCore.Services
{
public List<Device> devices = new List<Device>();
public async Task<List<Device>> GetDevices()
public async Task<List<YeelightAPI.Device>> GetDevices()
{
devices = await DeviceLocator.Discover();

View File

@ -6,6 +6,7 @@ using MyCore.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
namespace MyCore.Services.MyControlPanel
{

View File

@ -6,6 +6,7 @@ using MyCore.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
namespace MyCore.Services.MyControlPanel
{
@ -34,6 +35,11 @@ namespace MyCore.Services.MyControlPanel
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault() != null ? true : false;
}
public bool IsAlreadyHere(string ipAddress, int port)
{
return _Devices.Find<Device>(d => d.IpAddress == ipAddress && d.Port == port).FirstOrDefault() != null ? true : false;
}
public Device Create(Device device)
{
_Devices.InsertOne(device);

View File

@ -6,6 +6,7 @@ using MyCore.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
namespace MyCore.Services.MyControlPanel
{

View File

@ -6,6 +6,7 @@ using MyCore.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
namespace MyCore.Services.MyControlPanel
{

View File

@ -5,7 +5,8 @@ using System.Threading.Tasks;
using MyCore.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
namespace MyCore.Services.MyControlPanel
{
public class ProviderDatabaseService

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using MyCore.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using MyCore.Models.MyControlPanel.Database;
namespace MyCore.Services
{

View File

@ -1,6 +1,6 @@
using Microsoft.Extensions.Configuration;
using MyCore.DTO.MyControlPanel;
using MyCore.Models.MyControlPanel;
using MyCore.Models.MyControlPanel.Database;
using System;
using System.Collections.Generic;
using System.Linq;
@ -45,6 +45,7 @@ namespace MyCore.Services.MyControlPanel
provider.Username = providerDTO.Username;
provider.Password = providerDTO.Password;
provider.ApiKey = providerDTO.ApiKey;
provider.Active = true;
if (create)
return _ProviderDatabaseService.Create(provider).ToDTO();