mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
MC support Yeelight devices creation from provider (automatic)
This commit is contained in:
parent
96b552f884
commit
7e1365c277
@ -10,6 +10,7 @@ using MyCore.DTO.Common;
|
|||||||
using MyCore.DTO.MyControlPanel;
|
using MyCore.DTO.MyControlPanel;
|
||||||
using MyCore.Models;
|
using MyCore.Models;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
using MyCore.Services;
|
using MyCore.Services;
|
||||||
using MyCore.Services.Devices;
|
using MyCore.Services.Devices;
|
||||||
using MyCore.Services.MyControlPanel;
|
using MyCore.Services.MyControlPanel;
|
||||||
@ -95,6 +96,10 @@ namespace MyCore.Controllers.Devices
|
|||||||
return new OkObjectResult(deviceCreated);
|
return new OkObjectResult(deviceCreated);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
catch (DuplicateWaitObjectException ex)
|
||||||
|
{
|
||||||
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
|
||||||
|
}
|
||||||
catch (KeyNotFoundException ex)
|
catch (KeyNotFoundException ex)
|
||||||
{
|
{
|
||||||
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
||||||
@ -112,7 +117,7 @@ namespace MyCore.Controllers.Devices
|
|||||||
/// <param name="providerId">Id of Provider</param>
|
/// <param name="providerId">Id of Provider</param>
|
||||||
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
||||||
[HttpPost("fromProvider/{userId}")]
|
[HttpPost("fromProvider/{userId}")]
|
||||||
public ObjectResult CreateDevicesFromProvider(string userId, [FromBody] string providerId)
|
public async Task<ObjectResult> CreateDevicesFromProvider(string userId, string providerId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -123,13 +128,12 @@ namespace MyCore.Controllers.Devices
|
|||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("User not found");
|
||||||
|
|
||||||
Provider provider = ProviderService.GetProviderById(this._ProviderDatabaseService, userId, providerId);
|
Provider provider = ProviderService.GetProviderById(this._ProviderDatabaseService, userId, providerId);
|
||||||
if (provider != null)
|
if (provider == null)
|
||||||
throw new KeyNotFoundException("Provider id is 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);
|
return new OkObjectResult(devicesCreated);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException ex)
|
catch (InvalidOperationException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -10,6 +10,7 @@ using MyCore.DTO.Common;
|
|||||||
using MyCore.DTO.MyControlPanel;
|
using MyCore.DTO.MyControlPanel;
|
||||||
using MyCore.Models;
|
using MyCore.Models;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
using MyCore.Services;
|
using MyCore.Services;
|
||||||
using MyCore.Services.Devices;
|
using MyCore.Services.Devices;
|
||||||
using MyCore.Services.MyControlPanel;
|
using MyCore.Services.MyControlPanel;
|
||||||
|
|||||||
@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using MyCore.DTO;
|
using MyCore.DTO;
|
||||||
using MyCore.Models;
|
using MyCore.Models;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
using MyCore.Services;
|
using MyCore.Services;
|
||||||
|
|
||||||
namespace MyCore.Controllers
|
namespace MyCore.Controllers
|
||||||
|
|||||||
@ -8,6 +8,7 @@ using MQTTnet;
|
|||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
using MQTTnet.Server;
|
using MQTTnet.Server;
|
||||||
using MyCore.Models;
|
using MyCore.Models;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
using MyCore.Services;
|
using MyCore.Services;
|
||||||
|
|
||||||
namespace MyCore.Controllers
|
namespace MyCore.Controllers
|
||||||
|
|||||||
@ -13,8 +13,14 @@ namespace MyCore.DTO.MyControlPanel
|
|||||||
|
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public string Model { get; set; }
|
||||||
|
|
||||||
|
public bool Status { get; set; }
|
||||||
|
|
||||||
public ConnectionStatus ConnectionStatus { get; set; }
|
public ConnectionStatus ConnectionStatus { get; set; }
|
||||||
|
|
||||||
|
public string LocationId { get; set; }
|
||||||
|
|
||||||
public LocationDTO Location { get; set; }
|
public LocationDTO Location { get; set; }
|
||||||
|
|
||||||
public bool Battery { get; set; }
|
public bool Battery { get; set; }
|
||||||
@ -28,8 +34,16 @@ namespace MyCore.DTO.MyControlPanel
|
|||||||
|
|
||||||
public string Name { get; set; }
|
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 ConnectionStatus ConnectionStatus { get; set; }
|
||||||
|
|
||||||
|
public bool Status { get; set; }
|
||||||
|
|
||||||
public string LocationId { get; set; }
|
public string LocationId { get; set; }
|
||||||
|
|
||||||
public LocationDTO Location { get; set; }
|
public LocationDTO Location { get; set; }
|
||||||
@ -54,6 +68,8 @@ namespace MyCore.DTO.MyControlPanel
|
|||||||
|
|
||||||
public List<string> Groups { get; set; }
|
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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -13,5 +13,6 @@ namespace MyCore.DTO.MyControlPanel
|
|||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
public string Password { get; set; } // TODO ENCRYPTED
|
public string Password { get; set; } // TODO ENCRYPTED
|
||||||
public string ApiKey { get; set; } // TODO ENCRYPTED
|
public string ApiKey { get; set; } // TODO ENCRYPTED
|
||||||
|
public bool Active { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ using MyCore.DTO.MyControlPanel;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MyCore.Models
|
namespace MyCore.Models.MyControlPanel.Database
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Automation
|
/// Automation
|
||||||
|
|||||||
@ -2,12 +2,13 @@
|
|||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
using MyCore.DTO.Common;
|
using MyCore.DTO.Common;
|
||||||
using MyCore.DTO.MyControlPanel;
|
using MyCore.DTO.MyControlPanel;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
using MyCore.Services.MyControlPanel;
|
using MyCore.Services.MyControlPanel;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace MyCore.Models.MyControlPanel
|
namespace MyCore.Models.MyControlPanel.Database
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Group of devices
|
/// Group of devices
|
||||||
@ -22,11 +23,25 @@ namespace MyCore.Models.MyControlPanel
|
|||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string Name { get; set; }
|
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")]
|
[BsonElement("ConnectionStatus")]
|
||||||
public ConnectionStatus ConnectionStatus { get; set; }
|
public ConnectionStatus ConnectionStatus { get; set; }
|
||||||
|
|
||||||
[BsonElement("LocationId")]
|
[BsonElement("LocationId")]
|
||||||
[BsonRequired]
|
|
||||||
public string LocationId { get; set; }
|
public string LocationId { get; set; }
|
||||||
|
|
||||||
[BsonElement("MeansOfCommunications")]
|
[BsonElement("MeansOfCommunications")]
|
||||||
@ -60,8 +75,11 @@ namespace MyCore.Models.MyControlPanel
|
|||||||
[BsonElement("GroupIds")]
|
[BsonElement("GroupIds")]
|
||||||
public List<string> GroupIds { get; set; }
|
public List<string> GroupIds { get; set; }
|
||||||
|
|
||||||
[BsonElement("Informations")]
|
[BsonElement("Properties")]
|
||||||
public List<Information> Information { get; set; }
|
public Dictionary<string, object> Properties { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("SupportedOperations")]
|
||||||
|
public List<string> SupportedOperations { get; set; }
|
||||||
|
|
||||||
public DeviceSummaryDTO ToSummaryDTO()
|
public DeviceSummaryDTO ToSummaryDTO()
|
||||||
{
|
{
|
||||||
@ -69,8 +87,11 @@ namespace MyCore.Models.MyControlPanel
|
|||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
|
Model = Model,
|
||||||
|
Status = Status,
|
||||||
ConnectionStatus = ConnectionStatus,
|
ConnectionStatus = ConnectionStatus,
|
||||||
//Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
|
LocationId = LocationId, // Check if correct way
|
||||||
|
// Location =
|
||||||
Battery = Battery,
|
Battery = Battery,
|
||||||
BatteryStatus = BatteryStatus
|
BatteryStatus = BatteryStatus
|
||||||
};
|
};
|
||||||
@ -82,17 +103,23 @@ namespace MyCore.Models.MyControlPanel
|
|||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
|
Model = Model,
|
||||||
|
Status = Status,
|
||||||
|
Port = Port,
|
||||||
|
FirmwareVersion = FirmwareVersion,
|
||||||
ConnectionStatus = ConnectionStatus,
|
ConnectionStatus = ConnectionStatus,
|
||||||
//Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
|
LocationId = LocationId, // Check if correct way
|
||||||
|
// Location =
|
||||||
MeansOfCommunications = MeansOfCommunications,
|
MeansOfCommunications = MeansOfCommunications,
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
LastMessage = LastMessage,
|
LastMessage = LastMessage,
|
||||||
LastMessageDate = LastMessageDate,
|
LastMessageDate = LastMessageDate,
|
||||||
IpAddress = IpAddress,
|
IpAddress = IpAddress,
|
||||||
//Provider = Provider.ToDTO(device.Provider),
|
ProviderId = ProviderId,
|
||||||
//Groups = device.Groups.Select(i => i.ToDTO(i)).ToList(),
|
Groups = GroupIds,
|
||||||
//Informations = device.Informations.Select(i => i.ToDTO(i)).ToList(),
|
Properties = Properties,
|
||||||
|
SupportedOperations = SupportedOperations,
|
||||||
Battery = Battery,
|
Battery = Battery,
|
||||||
BatteryStatus = BatteryStatus
|
BatteryStatus = BatteryStatus
|
||||||
};
|
};
|
||||||
|
|||||||
@ -4,7 +4,7 @@ using MyCore.DTO.Common;
|
|||||||
using MyCore.DTO.MyControlPanel;
|
using MyCore.DTO.MyControlPanel;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
namespace MyCore.Models.MyControlPanel
|
namespace MyCore.Models.MyControlPanel.Database
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Group of devices
|
/// Group of devices
|
||||||
|
|||||||
@ -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
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,7 +2,7 @@
|
|||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
using MyCore.DTO.MyControlPanel;
|
using MyCore.DTO.MyControlPanel;
|
||||||
|
|
||||||
namespace MyCore.Models.MyControlPanel
|
namespace MyCore.Models.MyControlPanel.Database
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Location of a device (Room name, garden, ..)
|
/// Location of a device (Room name, garden, ..)
|
||||||
|
|||||||
@ -5,7 +5,7 @@ using MyCore.DTO.MyControlPanel;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MyCore.Models.MyControlPanel
|
namespace MyCore.Models.MyControlPanel.Database
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provider of a device (provider of informations) - e.g. : Meross, Arlo, IoThomas, ...
|
/// Provider of a device (provider of informations) - e.g. : Meross, Arlo, IoThomas, ...
|
||||||
@ -35,12 +35,17 @@ namespace MyCore.Models.MyControlPanel
|
|||||||
[BsonElement("ApiKey")]
|
[BsonElement("ApiKey")]
|
||||||
public string ApiKey { get; set; } // TODO ENCRYPTED
|
public string ApiKey { get; set; } // TODO ENCRYPTED
|
||||||
|
|
||||||
|
[BsonElement("Active")]
|
||||||
|
[BsonRequired]
|
||||||
|
public bool Active { get; set; }
|
||||||
|
|
||||||
public ProviderDTO ToDTO()
|
public ProviderDTO ToDTO()
|
||||||
{
|
{
|
||||||
return new ProviderDTO()
|
return new ProviderDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
Name = Name
|
Name = Name,
|
||||||
|
Active = Active
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ using MongoDB.Bson;
|
|||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel;
|
||||||
|
|
||||||
namespace MyCore.Models
|
namespace MyCore.Models.MyControlPanel.Database
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// User Information
|
/// User Information
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
using MyCore.DTO.Common;
|
using MyCore.DTO.Common;
|
||||||
using MyCore.DTO.MyControlPanel;
|
using MyCore.DTO.MyControlPanel;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
using MyCore.Services.MyControlPanel;
|
using MyCore.Services.MyControlPanel;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -23,18 +24,32 @@ namespace MyCore.Services.Devices
|
|||||||
device = _DeviceDatabaseService.GetById(deviceDetailDTO.Id);
|
device = _DeviceDatabaseService.GetById(deviceDetailDTO.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_DeviceDatabaseService.IsAlreadyHere(deviceDetailDTO.IpAddress, deviceDetailDTO.Port) && create)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
device.Name = deviceDetailDTO.Name;
|
device.Name = deviceDetailDTO.Name;
|
||||||
if (_ProviderDatabaseService.IsExist(deviceDetailDTO.ProviderId))
|
if (_ProviderDatabaseService.IsExist(deviceDetailDTO.ProviderId))
|
||||||
device.ProviderId = deviceDetailDTO.ProviderId;
|
device.ProviderId = deviceDetailDTO.ProviderId;
|
||||||
else
|
else
|
||||||
throw new KeyNotFoundException("Provider does not exist");
|
throw new KeyNotFoundException("Provider does not exist");
|
||||||
|
|
||||||
if (_LocationDatabaseService.IsExist(deviceDetailDTO.LocationId))
|
if (device.LocationId == null || _LocationDatabaseService.IsExist(deviceDetailDTO.LocationId))
|
||||||
device.LocationId = deviceDetailDTO.LocationId;
|
device.LocationId = deviceDetailDTO.LocationId;
|
||||||
else
|
else
|
||||||
throw new KeyNotFoundException("Location does not exist");
|
throw new KeyNotFoundException("Location does not exist");
|
||||||
|
|
||||||
|
device.Port = deviceDetailDTO.Port;
|
||||||
|
device.Model = deviceDetailDTO.Model;
|
||||||
|
device.FirmwareVersion = deviceDetailDTO.FirmwareVersion;
|
||||||
|
device.Status = deviceDetailDTO.Status;
|
||||||
|
if (create)
|
||||||
device.ConnectionStatus = ConnectionStatus.Unknown;
|
device.ConnectionStatus = ConnectionStatus.Unknown;
|
||||||
|
else
|
||||||
|
device.ConnectionStatus = deviceDetailDTO.ConnectionStatus;
|
||||||
|
device.Status = device.Status;
|
||||||
|
device.LocationId = device.LocationId;
|
||||||
device.CreatedDate = DateTime.Now;
|
device.CreatedDate = DateTime.Now;
|
||||||
device.UpdatedDate = DateTime.Now;
|
device.UpdatedDate = DateTime.Now;
|
||||||
|
|
||||||
@ -43,8 +58,10 @@ namespace MyCore.Services.Devices
|
|||||||
device.Battery = deviceDetailDTO.Battery;
|
device.Battery = deviceDetailDTO.Battery;
|
||||||
device.BatteryStatus = deviceDetailDTO.BatteryStatus;
|
device.BatteryStatus = deviceDetailDTO.BatteryStatus;
|
||||||
device.GroupIds = device.GroupIds;
|
device.GroupIds = device.GroupIds;
|
||||||
// Todo structure informations
|
// Todo structure Properties
|
||||||
device.Information = device.Information;
|
device.Properties = device.Properties;
|
||||||
|
// Todo structure SupportedOperations
|
||||||
|
device.SupportedOperations = device.SupportedOperations;
|
||||||
|
|
||||||
if (create)
|
if (create)
|
||||||
return _DeviceDatabaseService.Create(device).ToDTO();
|
return _DeviceDatabaseService.Create(device).ToDTO();
|
||||||
@ -52,13 +69,13 @@ namespace MyCore.Services.Devices
|
|||||||
return _DeviceDatabaseService.Update(device.Id, device).ToDTO();
|
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))
|
if (!ProviderService.IsProviderSupported(provider.Name))
|
||||||
throw new KeyNotFoundException("Provider is not yet supported");
|
throw new KeyNotFoundException("Provider is not yet supported");
|
||||||
|
|
||||||
|
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
|
||||||
|
|
||||||
switch (provider.Name)
|
switch (provider.Name)
|
||||||
{
|
{
|
||||||
case "Arlo":
|
case "Arlo":
|
||||||
@ -68,14 +85,50 @@ namespace MyCore.Services.Devices
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case "Yeelight":
|
case "Yeelight":
|
||||||
|
List<YeelightAPI.Device> yeelightDevices = await new YeelightService().GetDevices();
|
||||||
|
createdDevice = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, yeelightDevices, provider);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
|
|
||||||
|
|
||||||
|
|
||||||
return createdDevice;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,16 +33,6 @@ namespace MyCore.Services
|
|||||||
|
|
||||||
public MQTTService()
|
public MQTTService()
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
yeelightService.GetDevices();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Create a new MQTT client.
|
// Create a new MQTT client.
|
||||||
|
|||||||
@ -10,7 +10,7 @@ namespace MyCore.Services
|
|||||||
{
|
{
|
||||||
public List<Device> devices = new List<Device>();
|
public List<Device> devices = new List<Device>();
|
||||||
|
|
||||||
public async Task<List<Device>> GetDevices()
|
public async Task<List<YeelightAPI.Device>> GetDevices()
|
||||||
{
|
{
|
||||||
devices = await DeviceLocator.Discover();
|
devices = await DeviceLocator.Discover();
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@ using MyCore.Models;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services.MyControlPanel
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,6 +6,7 @@ using MyCore.Models;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services.MyControlPanel
|
||||||
{
|
{
|
||||||
@ -34,6 +35,11 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault() != null ? true : false;
|
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)
|
public Device Create(Device device)
|
||||||
{
|
{
|
||||||
_Devices.InsertOne(device);
|
_Devices.InsertOne(device);
|
||||||
|
|||||||
@ -6,6 +6,7 @@ using MyCore.Models;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services.MyControlPanel
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,6 +6,7 @@ using MyCore.Models;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services.MyControlPanel
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,7 +5,8 @@ using System.Threading.Tasks;
|
|||||||
using MyCore.Models;
|
using MyCore.Models;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services.MyControlPanel
|
||||||
{
|
{
|
||||||
public class ProviderDatabaseService
|
public class ProviderDatabaseService
|
||||||
|
|||||||
@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
|||||||
using MyCore.Models;
|
using MyCore.Models;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
|
|
||||||
namespace MyCore.Services
|
namespace MyCore.Services
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MyCore.DTO.MyControlPanel;
|
using MyCore.DTO.MyControlPanel;
|
||||||
using MyCore.Models.MyControlPanel;
|
using MyCore.Models.MyControlPanel.Database;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -45,6 +45,7 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
provider.Username = providerDTO.Username;
|
provider.Username = providerDTO.Username;
|
||||||
provider.Password = providerDTO.Password;
|
provider.Password = providerDTO.Password;
|
||||||
provider.ApiKey = providerDTO.ApiKey;
|
provider.ApiKey = providerDTO.ApiKey;
|
||||||
|
provider.Active = true;
|
||||||
|
|
||||||
if (create)
|
if (create)
|
||||||
return _ProviderDatabaseService.Create(provider).ToDTO();
|
return _ProviderDatabaseService.Create(provider).ToDTO();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user