mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
Add Provider controller.. Call a DBService in service is null.. Blocked.
This commit is contained in:
parent
8960953ccc
commit
f35c89f861
@ -114,13 +114,14 @@ namespace MyCore.Controllers.Devices
|
||||
{
|
||||
try
|
||||
{
|
||||
if (providerId == null)
|
||||
throw new KeyNotFoundException("Provider id is null");
|
||||
|
||||
if (userId == null && UserService.IsExist(userId))
|
||||
throw new KeyNotFoundException("User not found");
|
||||
|
||||
List<DeviceDetailDTO> devicesCreated = DeviceService.CreateFromProvider(providerId);
|
||||
Provider provider = ProviderService.GetProviderById(userId, providerId);
|
||||
if (provider != null)
|
||||
throw new KeyNotFoundException("Provider id is null");
|
||||
|
||||
List<DeviceDetailDTO> devicesCreated = DeviceService.CreateFromProvider(provider);
|
||||
|
||||
return new OkObjectResult(devicesCreated);
|
||||
|
||||
@ -147,7 +148,7 @@ namespace MyCore.Controllers.Devices
|
||||
try
|
||||
{
|
||||
if (!_DeviceDatabaseService.IsExist(deviceId))
|
||||
throw new KeyNotFoundException("Location does not exist");
|
||||
throw new KeyNotFoundException("Device does not exist");
|
||||
|
||||
DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(deviceDetailDTO, false);
|
||||
|
||||
|
||||
134
MyCore/Controllers/Devices/ProviderController.cs
Normal file
134
MyCore/Controllers/Devices/ProviderController.cs
Normal file
@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MongoDB.Bson;
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Models;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Services;
|
||||
using MyCore.Services.Devices;
|
||||
using MyCore.Services.MyControlPanel;
|
||||
|
||||
namespace MyCore.Controllers.Devices
|
||||
{
|
||||
//[Authorize(Roles = "Admin")]
|
||||
[Authorize]
|
||||
[Route("api/provider")]
|
||||
[ApiController]
|
||||
public class ProviderController : ControllerBase
|
||||
{
|
||||
|
||||
// GET: Devices
|
||||
/// <summary>
|
||||
/// Get all user providers
|
||||
/// </summary>
|
||||
[ProducesResponseType(typeof(List<ProviderDTO>), 200)]
|
||||
[HttpGet]
|
||||
public ObjectResult GetAll(string userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Provider> providers = ProviderService.GetAll(userId);
|
||||
|
||||
List<ProviderDTO> providersDTO = providers.Select(p => p.ToDTO()).ToList();
|
||||
|
||||
return new OkObjectResult(providersDTO);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a provider
|
||||
/// </summary>
|
||||
/// <param name="userId">User Id</param>
|
||||
/// <param name="providerDTO">Provider to create</param>
|
||||
[ProducesResponseType(typeof(ProviderDTO), 200)]
|
||||
[HttpPost]
|
||||
public ObjectResult Create(string userId, [FromBody] ProviderDTO providerDTO)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
if (userId == null && UserService.IsExist(userId))
|
||||
throw new KeyNotFoundException("User not found");
|
||||
|
||||
if (providerDTO == null)
|
||||
throw new KeyNotFoundException("Provider is null");
|
||||
|
||||
ProviderDTO providerCreated = ProviderService.CreateOrUpdate(userId, providerDTO, true);
|
||||
|
||||
return new OkObjectResult(providerCreated);
|
||||
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update a provider
|
||||
/// </summary>
|
||||
/// <param name="userId">User Id</param>
|
||||
/// <param name="providerDTO">Provider to update</param>
|
||||
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
|
||||
[HttpPut("{deviceId}")]
|
||||
public ObjectResult Update(string userId, [FromBody] ProviderDTO providerDTO)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (userId == null && UserService.IsExist(userId))
|
||||
throw new KeyNotFoundException("User not found");
|
||||
|
||||
if (!ProviderService.IsExist(userId, providerDTO.Id))
|
||||
throw new KeyNotFoundException("Provider does not exist");
|
||||
|
||||
ProviderDTO providerUpdated = ProviderService.CreateOrUpdate(userId, providerDTO, false);
|
||||
|
||||
return new OkObjectResult(providerUpdated);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete a provider
|
||||
/// </summary>
|
||||
/// <param name="providerId">Id of provider to delete</param>
|
||||
[HttpDelete("{providerId}")]
|
||||
public ObjectResult Delete(string providerId)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Check if exist
|
||||
// TODO
|
||||
// ProviderDatabaseService.Remove(providerId);
|
||||
|
||||
return new OkObjectResult(201);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ namespace MyCore.Controllers
|
||||
|
||||
if (IsValidUserAndPasswordCombination(tokenDTO.Email, tokenDTO.Password))
|
||||
{
|
||||
UserInfo user = _userService.GetUserByEmail(tokenDTO.Email);
|
||||
UserInfo user = _userService.GetByEmail(tokenDTO.Email);
|
||||
user.Token = _tokenService.GenerateToken(tokenDTO.Email).ToString();
|
||||
|
||||
return user;
|
||||
@ -49,7 +49,7 @@ namespace MyCore.Controllers
|
||||
private bool IsValidUserAndPasswordCombination(string email, string password)
|
||||
{
|
||||
// Test if is database and is correct
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
List<UserInfo> users = _userService.GetAll();
|
||||
|
||||
UserInfo user = users.Where(u => u.Email == email).FirstOrDefault();
|
||||
|
||||
|
||||
@ -49,14 +49,14 @@ namespace MyCore.Controllers
|
||||
{
|
||||
if (id != null)
|
||||
{
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
List<UserInfo> users = _userService.GetAll();
|
||||
|
||||
if (!users.Select(u => u.Id).Contains(id))
|
||||
{
|
||||
return Conflict("This user was not found");
|
||||
}
|
||||
|
||||
return _userService.GetUserById(id);
|
||||
return _userService.GetById(id);
|
||||
}
|
||||
return StatusCode(500);
|
||||
}
|
||||
@ -74,14 +74,14 @@ namespace MyCore.Controllers
|
||||
newUser.Token = _tokenService.GenerateToken(newUser.Email).ToString();
|
||||
newUser.DateCreation = DateTime.Now;
|
||||
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
List<UserInfo> users = _userService.GetAll();
|
||||
|
||||
if (users.Select(u => u.Email).Contains(newUser.Email))
|
||||
{
|
||||
return Conflict("This Email is already used");
|
||||
}
|
||||
|
||||
UserInfo userCreated = _userService.CreateUser(newUser);
|
||||
UserInfo userCreated = _userService.Create(newUser);
|
||||
|
||||
return userCreated;
|
||||
}
|
||||
@ -98,7 +98,7 @@ namespace MyCore.Controllers
|
||||
{
|
||||
if (updatedUser != null)
|
||||
{
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
List<UserInfo> users = _userService.GetAll();
|
||||
|
||||
if (!users.Select(u => u.Email).Contains(updatedUser.Email))
|
||||
{
|
||||
@ -119,7 +119,7 @@ namespace MyCore.Controllers
|
||||
{
|
||||
if (id != null)
|
||||
{
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
List<UserInfo> users = _userService.GetAll();
|
||||
|
||||
if (!users.Select(u => u.Id).Contains(id))
|
||||
{
|
||||
|
||||
@ -9,6 +9,7 @@ namespace MyCore.DTO.MyControlPanel
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string UserId { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; } // TODO ENCRYPTED
|
||||
public string ApiKey { get; set; } // TODO ENCRYPTED
|
||||
|
||||
@ -14,7 +14,6 @@ namespace MyCore.Models.MyControlPanel
|
||||
/// </summary>
|
||||
public class Device
|
||||
{
|
||||
private LocationDatabaseService _LocationDatabaseService;
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string Id { get; set; }
|
||||
@ -71,7 +70,7 @@ namespace MyCore.Models.MyControlPanel
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
ConnectionStatus = ConnectionStatus,
|
||||
Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
|
||||
//Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
|
||||
Battery = Battery,
|
||||
BatteryStatus = BatteryStatus
|
||||
};
|
||||
@ -84,7 +83,7 @@ namespace MyCore.Models.MyControlPanel
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
ConnectionStatus = ConnectionStatus,
|
||||
Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
|
||||
//Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
|
||||
MeansOfCommunications = MeansOfCommunications,
|
||||
CreatedDate = CreatedDate,
|
||||
UpdatedDate = UpdatedDate,
|
||||
@ -16,6 +16,10 @@ namespace MyCore.Models.MyControlPanel
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string Id { get; set; }
|
||||
|
||||
[BsonElement("UserId")]
|
||||
[BsonRequired]
|
||||
public string UserId { get; set; }
|
||||
|
||||
[BsonElement("Name")]
|
||||
[BsonRequired]
|
||||
public string Name { get; set; }
|
||||
@ -81,12 +81,11 @@ namespace MyCore.Models
|
||||
[BsonElement("Groups")]
|
||||
public Group[] Groups { get; set; }
|
||||
|
||||
// TODO
|
||||
/*[BsonElement("ScreenConfigurationIds")]
|
||||
[BsonElement("ScreenConfigurationIds")]
|
||||
public ScreenConfiguration[] ScreenConfigurationIds { get; set; }
|
||||
|
||||
[BsonElement("DeviceIds")]
|
||||
public ScreenDevice[] DeviceIds { get; set; }*/
|
||||
public ScreenDevice[] DeviceIds { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@ -16,6 +16,13 @@ namespace MyCore.Services.Devices
|
||||
private static ProviderDatabaseService _ProviderDatabaseService;
|
||||
private static LocationDatabaseService _LocationDatabaseService;
|
||||
|
||||
public DeviceService(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService)
|
||||
{
|
||||
_DeviceDatabaseService = DeviceDatabaseService;
|
||||
_ProviderDatabaseService = ProviderDatabaseService;
|
||||
_LocationDatabaseService = LocationDatabaseService;
|
||||
}
|
||||
|
||||
public static DeviceDetailDTO CreateOrUpdate(DeviceDetailDTO deviceDetailDTO, bool create)
|
||||
{
|
||||
Device device;
|
||||
@ -55,27 +62,24 @@ namespace MyCore.Services.Devices
|
||||
return _DeviceDatabaseService.Update(device.Id, device).ToDTO();
|
||||
}
|
||||
|
||||
public static List<DeviceDetailDTO> CreateFromProvider(string providerId)
|
||||
public static List<DeviceDetailDTO> CreateFromProvider(Provider provider)
|
||||
{
|
||||
// TODO !
|
||||
Device device = new Device();
|
||||
|
||||
Provider provider = new Provider();
|
||||
if (_ProviderDatabaseService.IsExist(providerId))
|
||||
provider = _ProviderDatabaseService.GetById(providerId);
|
||||
else
|
||||
throw new KeyNotFoundException("Provider does not exist");
|
||||
if (!ProviderService.IsProviderSupported(provider.Name))
|
||||
throw new KeyNotFoundException("Provider is not yet supported");
|
||||
|
||||
switch (provider.Name)
|
||||
{
|
||||
case "Arlo":
|
||||
ArloService arloService = new ArloService(provider.Username, provider.Password);
|
||||
break;
|
||||
case "Meross":
|
||||
|
||||
break;
|
||||
case "Yeelight":
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new KeyNotFoundException("Provider is not yet supported");
|
||||
}
|
||||
|
||||
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
|
||||
|
||||
@ -107,11 +107,13 @@ namespace MyCore.Services
|
||||
LIBRARY
|
||||
}
|
||||
|
||||
public ArloService()
|
||||
public ArloService(string username, string password)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
/*_email = username;
|
||||
_password = password;*/
|
||||
|
||||
// LOGIN
|
||||
var loginTask = Task.Run(() => RequestURI(new Uri(_loginUrl), RequestType.Post, Request.LOGIN));
|
||||
loginTask.Wait();
|
||||
@ -122,28 +124,6 @@ namespace MyCore.Services
|
||||
var data = ((JObject)JsonConvert.DeserializeObject(loginTask.Result))["data"];
|
||||
resultToken = JsonConvert.DeserializeObject<LoginResult>(data.ToString());
|
||||
|
||||
// GET DEVICE LIST
|
||||
var deviceTask = Task.Run(() => RequestURI(new Uri(_userDevicesUrl), RequestType.Get, Request.DEVICES));
|
||||
deviceTask.Wait();
|
||||
|
||||
if (deviceTask.Result != "")
|
||||
{
|
||||
data = ((JObject)JsonConvert.DeserializeObject(deviceTask.Result))["data"];
|
||||
// RETRIEVE ALL ARLO DEVICES
|
||||
allArloDevices = JsonConvert.DeserializeObject<List<ArloDevice>>(data.ToString());
|
||||
}
|
||||
|
||||
// GET USER LIBRARY
|
||||
var libraryTask = Task.Run(() => RequestURI(new Uri(_userLibraryUrl), RequestType.Post, Request.LIBRARY));
|
||||
libraryTask.Wait();
|
||||
|
||||
if (libraryTask.Result != "")
|
||||
{
|
||||
data = ((JObject)JsonConvert.DeserializeObject(libraryTask.Result))["data"];
|
||||
// RETRIEVE ALL DATA IN USER LIBRARY
|
||||
allUserMedias = JsonConvert.DeserializeObject<List<UserMedia>>(data.ToString());
|
||||
}
|
||||
|
||||
//SSE CONNEXION
|
||||
ConnexionToSSE();
|
||||
}
|
||||
@ -207,6 +187,36 @@ namespace MyCore.Services
|
||||
}
|
||||
}
|
||||
|
||||
public List<ArloDevice> GetAllDevices() {
|
||||
// GET DEVICE LIST
|
||||
var deviceTask = Task.Run(() => RequestURI(new Uri(_userDevicesUrl), RequestType.Get, Request.DEVICES));
|
||||
deviceTask.Wait();
|
||||
|
||||
if (deviceTask.Result != "")
|
||||
{
|
||||
var data = ((JObject)JsonConvert.DeserializeObject(deviceTask.Result))["data"];
|
||||
// RETRIEVE ALL ARLO DEVICES
|
||||
allArloDevices = JsonConvert.DeserializeObject<List<ArloDevice>>(data.ToString());
|
||||
}
|
||||
|
||||
return allArloDevices;
|
||||
}
|
||||
|
||||
public List<UserMedia> GetUserLibrary() {
|
||||
// GET USER LIBRARY
|
||||
var libraryTask = Task.Run(() => RequestURI(new Uri(_userLibraryUrl), RequestType.Post, Request.LIBRARY));
|
||||
libraryTask.Wait();
|
||||
|
||||
if (libraryTask.Result != "")
|
||||
{
|
||||
var data = ((JObject)JsonConvert.DeserializeObject(libraryTask.Result))["data"];
|
||||
// RETRIEVE ALL DATA IN USER LIBRARY
|
||||
allUserMedias = JsonConvert.DeserializeObject<List<UserMedia>>(data.ToString());
|
||||
}
|
||||
|
||||
return allUserMedias;
|
||||
}
|
||||
|
||||
public void ConnexionToSSE()
|
||||
{
|
||||
/*var sseClient = new ServerEventsClient($"{_clientSubscribeUrl}?token={resultToken.token}", new string[] { "EventStream" })
|
||||
@ -18,14 +18,14 @@ namespace MyCore.Services.MyControlPanel
|
||||
var database = client.GetDatabase("MyCoreDb");
|
||||
_Providers = database.GetCollection<Provider>("Providers");
|
||||
}
|
||||
public List<Provider> GetAll()
|
||||
public List<Provider> GetAll(string userId)
|
||||
{
|
||||
return _Providers.Find(p => true).ToList();
|
||||
return _Providers.Find(p => p.UserId == userId).ToList();
|
||||
}
|
||||
|
||||
public Provider GetById(string id)
|
||||
public Provider GetById(string userId, string id)
|
||||
{
|
||||
return _Providers.Find<Provider>(p => p.Id == id).FirstOrDefault();
|
||||
return _Providers.Find<Provider>(p => p.Id == id && p.UserId == userId).FirstOrDefault();
|
||||
}
|
||||
|
||||
public Provider GetByName(string name)
|
||||
|
||||
79
MyCore/Services/MyControlPanel/ProviderService.cs
Normal file
79
MyCore/Services/MyControlPanel/ProviderService.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Services.MyControlPanel
|
||||
{
|
||||
public class ProviderService
|
||||
{
|
||||
static List<string> supportedProviders = new List<string>() {
|
||||
"Arlo",
|
||||
"Meross",
|
||||
"Yeelight",
|
||||
"ZigBee"
|
||||
};
|
||||
|
||||
private readonly ProviderDatabaseService _ProviderDatabaseService;
|
||||
|
||||
public ProviderService(ProviderDatabaseService ProviderDatabaseService)
|
||||
{
|
||||
_ProviderDatabaseService = ProviderDatabaseService;
|
||||
}
|
||||
|
||||
public static bool IsExist(string userId, string providerId)
|
||||
{
|
||||
return _ProviderDatabaseService.GetById(userId, providerId) != null ? true : false;
|
||||
}
|
||||
|
||||
public static List<Provider> GetAll(string userId)
|
||||
{
|
||||
return _ProviderDatabaseService.GetAll(userId);
|
||||
}
|
||||
|
||||
public static ProviderDTO CreateOrUpdate(string userId, ProviderDTO providerDTO, bool create)
|
||||
{
|
||||
Provider provider;
|
||||
if (create)
|
||||
provider = new Provider();
|
||||
else
|
||||
{
|
||||
provider = _ProviderDatabaseService.GetById(userId, providerDTO.Id);
|
||||
}
|
||||
|
||||
if (!IsProviderSupported(providerDTO.Name))
|
||||
throw new KeyNotFoundException("Provider is not yet supported");
|
||||
|
||||
provider.Name = providerDTO.Name;
|
||||
provider.UserId = providerDTO.UserId;
|
||||
provider.Username = providerDTO.Username;
|
||||
provider.Password = providerDTO.Password;
|
||||
provider.ApiKey = providerDTO.ApiKey;
|
||||
|
||||
if (create)
|
||||
return _ProviderDatabaseService.Create(provider).ToDTO();
|
||||
else
|
||||
return _ProviderDatabaseService.Update(provider.Id, provider).ToDTO();
|
||||
}
|
||||
|
||||
public static Provider GetProviderById(string userId, string providerId)
|
||||
{
|
||||
return _ProviderDatabaseService.GetById(userId, providerId);
|
||||
}
|
||||
|
||||
// TODO Get supported services
|
||||
public static List<string> GetSupportedProvider()
|
||||
{
|
||||
return supportedProviders;
|
||||
}
|
||||
|
||||
public static bool IsProviderSupported(string providerName)
|
||||
{
|
||||
return supportedProviders.Contains(providerName) ? true : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user