mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
135 lines
6.1 KiB
C#
135 lines
6.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
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;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MyCore.Services.Devices
|
|
{
|
|
public class DeviceService
|
|
{
|
|
|
|
public static DeviceDetailDTO CreateOrUpdate(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, DeviceDetailDTO deviceDetailDTO, bool create)
|
|
{
|
|
Device device;
|
|
if (create)
|
|
device = new Device();
|
|
else
|
|
{
|
|
device = _DeviceDatabaseService.GetById(deviceDetailDTO.Id);
|
|
}
|
|
|
|
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 (device.LocationId == null || _LocationDatabaseService.IsExist(deviceDetailDTO.LocationId))
|
|
device.LocationId = deviceDetailDTO.LocationId;
|
|
else
|
|
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;
|
|
else
|
|
device.ConnectionStatus = deviceDetailDTO.ConnectionStatus;
|
|
device.Status = device.Status;
|
|
device.LocationId = device.LocationId;
|
|
device.CreatedDate = DateTime.Now;
|
|
device.UpdatedDate = DateTime.Now;
|
|
|
|
device.MeansOfCommunications = deviceDetailDTO.MeansOfCommunications;
|
|
device.IpAddress = deviceDetailDTO.IpAddress;
|
|
device.Battery = deviceDetailDTO.Battery;
|
|
device.BatteryStatus = deviceDetailDTO.BatteryStatus;
|
|
device.GroupIds = device.GroupIds;
|
|
// Todo structure Properties
|
|
device.Properties = device.Properties;
|
|
// Todo structure SupportedOperations
|
|
device.SupportedOperations = device.SupportedOperations;
|
|
|
|
if (create)
|
|
return _DeviceDatabaseService.Create(device).ToDTO();
|
|
else
|
|
return _DeviceDatabaseService.Update(device.Id, device).ToDTO();
|
|
}
|
|
|
|
public async static Task<List<DeviceDetailDTO>> CreateFromProvider(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, Provider provider)
|
|
{
|
|
if (!ProviderService.IsProviderSupported(provider.Name))
|
|
throw new KeyNotFoundException("Provider is not yet supported");
|
|
|
|
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
|
|
|
|
switch (provider.Name)
|
|
{
|
|
case "Arlo":
|
|
ArloService arloService = new ArloService(provider.Username, provider.Password);
|
|
break;
|
|
case "Meross":
|
|
|
|
break;
|
|
case "Yeelight":
|
|
List<YeelightAPI.Device> yeelightDevices = await new YeelightService().GetDevices();
|
|
createdDevice = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, yeelightDevices, provider);
|
|
break;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|