Update service for myhomie App + room get main details

This commit is contained in:
Fransolet Thomas 2022-04-10 02:19:50 +02:00
parent cf85feb33f
commit b194a6e367
13 changed files with 221 additions and 20 deletions

View File

@ -23,8 +23,31 @@ namespace MyCore.Interfaces.DTO
public List<DeviceDetailDTO> Devices { get; set; }
}
public class RoomMainDetailDTO : RoomSummaryDTO
{
public DateTime CreatedDate { get; set; }
public DateTime UpdatedDate { get; set; }
public bool IsTemperature { get; set; }
public string Temperature { get; set; }
public bool IsHumidity { get; set; }
public string Humidity { get; set; }
public bool IsMotion { get; set; }
public bool? Motion { get; set; }
public bool IsDoor { get; set; }
public bool? Door { get; set; }
public List<DeviceDetailDTO> EnvironmentalDevices { get; set; } // for temp and humidity
public List<DeviceDetailDTO> SecurityDevices { get; set; } // for motion
}
public class RoomCreateOrUpdateDetailDTO : RoomSummaryDTO
{
public List<string> DeviceIds { get; set; }
public List<string> DeviceIds { get; set; } // TODO handle groupdIds
}
}

View File

@ -10,6 +10,8 @@ namespace MyCore.Interfaces.DTO
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Language { get; set; }
public List<string> HomeIds { get; set; }
// TODO

View File

@ -56,5 +56,30 @@ namespace MyCore.Interfaces.Models
Devices = devicesDetail
};
}
public RoomCreateOrUpdateDetailDTO ToCreateOrUpdateDetailDTO()
{
return new RoomCreateOrUpdateDetailDTO()
{
Id = Id,
HomeId = HomeId,
Name = Name,
DeviceIds = DevicesIds,
};
}
public RoomMainDetailDTO ToMainDetailsDTO(List<DeviceDetailDTO> devicesDetail)
{
return new RoomMainDetailDTO()
{
Id = Id,
HomeId = HomeId,
Name = Name,
CreatedDate = CreatedDate,
UpdatedDate = UpdatedDate,
EnvironmentalDevices = devicesDetail.Where(d => d.Type == DeviceType.Environment).ToList(),
SecurityDevices = devicesDetail.Where(d => d.Type == DeviceType.Motion || d.Type == DeviceType.Door).ToList(),
};
}
}
}

View File

@ -80,6 +80,8 @@ namespace MyCore.Interfaces.Models
Email = Email,
FirstName = FirstName,
LastName = LastName,
Language = Language,
HomeIds = HomeIds,
};
}

View File

@ -91,7 +91,7 @@ namespace MyCore.Service.Controllers
if (group == null)
throw new KeyNotFoundException("Group not found");
List<Device> devices = _DeviceDatabaseService.GetByRoom(group.HomeId, groupId);
List<Device> devices = _DeviceDatabaseService.GetByGroup(groupId);
return new OkObjectResult(group.ToDTO(devices.Select(d => d.ToDTO()).ToList()));
@ -360,7 +360,7 @@ namespace MyCore.Service.Controllers
Group group = _GroupDatabaseService.GetById(groupId);
// Delete group from all devices
List<Device> devices = _DeviceDatabaseService.GetByRoom(group.HomeId, groupId);
List<Device> devices = _DeviceDatabaseService.GetByGroup(groupId);
foreach (var device in devices)
{
device.GroupIds = device.GroupIds.Where(g => g != groupId).ToList();

View File

@ -49,7 +49,6 @@ namespace MyCore.Controllers
}
}
/// <summary>
/// Get a specific user
/// </summary>
@ -79,6 +78,35 @@ namespace MyCore.Controllers
}
}
/// <summary>
/// Get a specific user by email
/// </summary>
/// <param name="email">user email</param>
[ProducesResponseType(typeof(UserInfoDetailDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("email/{email}")]
public ObjectResult GetByEmail(string email)
{
try
{
UserInfo user = _userService.GetByEmail(email);
if (user == null)
throw new KeyNotFoundException("This user was not found");
return new OkObjectResult(user.ToDTO());
}
catch (KeyNotFoundException ex)
{
return new NotFoundObjectResult(ex.Message) { };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Create an user
/// </summary>

View File

@ -25,14 +25,16 @@ namespace MyCore.Service.Controllers
private HomeDatabaseService _HomeDatabaseService;
private RoomDatabaseService _RoomDatabaseService;
private DeviceDatabaseService _DeviceDatabaseService;
private RoomService _RoomService;
private readonly IMqttClientService _mqttClientService;
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
public RoomController(HomeDatabaseService homeDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
public RoomController(HomeDatabaseService homeDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, RoomService roomService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
{
this._HomeDatabaseService = homeDatabaseService;
this._RoomDatabaseService = roomDatabaseService;
this._DeviceDatabaseService = deviceDatabaseService;
this._RoomService = roomService;
this._mqttClientService = provider.MqttClientService;
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
}
@ -60,6 +62,41 @@ namespace MyCore.Service.Controllers
}
}
/// <summary>
/// Get all rooms main details for the specified home
/// </summary>
/// <param name="homeId">Home Id</param>
[ProducesResponseType(typeof(List<RoomMainDetailDTO>), 200)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{homeId}/details")]
public ObjectResult GetAllWithMainDetails(string homeId)
{
try
{
List<Room> rooms = _RoomDatabaseService.GetAll(homeId);
List<RoomMainDetailDTO> roomMainDetailDTOs = new List<RoomMainDetailDTO>();
foreach (var room in rooms)
{
RoomMainDetailDTO roomMainDetailDTO = room.ToMainDetailsDTO(_DeviceDatabaseService.GetByRoom(room.Id).Select(d => d.ToDTO()).ToList());
roomMainDetailDTO.Temperature = (string) RoomService.GetValueFromJson(roomMainDetailDTO.EnvironmentalDevices, DeviceType.Environment, "temperature", false); // Todo take the one selected by user as default if more than one
roomMainDetailDTO.IsTemperature = roomMainDetailDTO.Temperature != null;
roomMainDetailDTO.Humidity = (string) RoomService.GetValueFromJson(roomMainDetailDTO.EnvironmentalDevices, DeviceType.Environment, "humidity", false); // Todo take the one selected by user as default if more than one
roomMainDetailDTO.IsHumidity = roomMainDetailDTO.Humidity != null;
roomMainDetailDTO.Motion = (bool?) RoomService.GetValueFromJson(roomMainDetailDTO.SecurityDevices, DeviceType.Motion, "motion", true);
roomMainDetailDTO.IsMotion = roomMainDetailDTO.SecurityDevices.Any(sd => sd.Type == DeviceType.Motion) ? roomMainDetailDTO.Motion != null : false;
roomMainDetailDTO.Door = (bool?) RoomService.GetValueFromJson(roomMainDetailDTO.SecurityDevices, DeviceType.Door, "contact", true); // Todo handle more than just by string
roomMainDetailDTO.IsDoor = roomMainDetailDTO.SecurityDevices.Any(sd => sd.Type == DeviceType.Door) ? roomMainDetailDTO.Door != null : false;
roomMainDetailDTOs.Add(roomMainDetailDTO);
}
return new OkObjectResult(roomMainDetailDTOs);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Get detail info of a specified room
/// </summary>
@ -79,10 +116,9 @@ namespace MyCore.Service.Controllers
Room room = _RoomDatabaseService.GetById(roomId);
if (room == null)
throw new KeyNotFoundException("Room does not exist");
List<Device> devices = _DeviceDatabaseService.GetByRoom(room.HomeId, roomId);
List<Device> devices = _DeviceDatabaseService.GetByRoom(roomId);
return new OkObjectResult(room.ToDTO(devices.Select(d => d.ToDTO()).ToList()));
}
catch (ArgumentNullException ex)
{
@ -106,7 +142,7 @@ namespace MyCore.Service.Controllers
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost]
public ObjectResult Create([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail)
public ObjectResult Create([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail) // TODO handle groupdIds
{
try
{
@ -156,6 +192,58 @@ namespace MyCore.Service.Controllers
}
}
/// <summary>
/// Add devices in the specified room
/// </summary>
/// <param name="roomId">Room Id</param>
/// <param name="deviceIds">Device Ids</param>
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut("{roomId}")]
public ObjectResult AddDeviceToRoom(string roomId, [FromBody] List<string> deviceIds) // TODO handle groupdIds
{
try
{
if (roomId == null || deviceIds.Count == 0)
{
throw new ArgumentNullException("Incorrect parameters");
}
if (!_RoomDatabaseService.IsExist(roomId))
throw new KeyNotFoundException("Room does not exist");
Room room = _RoomDatabaseService.GetById(roomId);
List<string> deviceIdsToTake = new List<string>();
foreach (var deviceId in deviceIds) {
if (!_DeviceDatabaseService.IsExist(deviceId))
break;
deviceIdsToTake.Add(deviceId);
}
RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO = room.ToCreateOrUpdateDetailDTO();
roomCreateOrUpdateDetailDTO.DeviceIds.AddRange(deviceIdsToTake);
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, room.HomeId, roomCreateOrUpdateDetailDTO, false);
return new OkObjectResult(roomUpdated);
}
catch (ArgumentNullException ex)
{
return new BadRequestObjectResult(ex.Message) { };
}
catch (KeyNotFoundException ex)
{
return new NotFoundObjectResult(ex.Message) { };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Delete device from a room
/// </summary>
@ -230,7 +318,7 @@ namespace MyCore.Service.Controllers
Room room = _RoomDatabaseService.GetById(roomId);
// Delete location from all devices
List<Device> devices = _DeviceDatabaseService.GetByRoom(room.HomeId, roomId);
List<Device> devices = _DeviceDatabaseService.GetByRoom(roomId);
foreach (var device in devices)
{
device.RoomId = null;

View File

@ -20,7 +20,6 @@
<ItemGroup>
<PackageReference Include="3v.EvtSource" Version="2.0.0" />
<PackageReference Include="AspNetCore.Security.Jwt" Version="1.6.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.2105168" />

View File

@ -46,7 +46,6 @@ namespace MyCore.Services.Devices
if (currentProvider != null)
{
deviceTrigger = _DeviceDatabaseService.GetByName(deviceServiceName).FirstOrDefault();
if (deviceTrigger != null)

View File

@ -94,12 +94,17 @@ namespace MyCore.Services.Devices
device.ConnectionStatus = ConnectionStatus.Unknown;
else
device.ConnectionStatus = deviceDetailDTO.ConnectionStatus;
device.Status = deviceDetailDTO.Status;
device.RoomId = deviceDetailDTO.RoomId;
device.CreatedDate = DateTime.Now;
device.UpdatedDate = DateTime.Now;
device.LastState = deviceDetailDTO.LastState;
device.LastStateDate = deviceDetailDTO.LastStateDate;
if (create)
{
device.CreatedDate = DateTime.Now;
}
else {
device.UpdatedDate = DateTime.Now;
device.LastState = deviceDetailDTO.LastState;
device.LastStateDate = deviceDetailDTO.LastStateDate;
device.Status = deviceDetailDTO.Status;
}
device.MeansOfCommunications = deviceDetailDTO.MeansOfCommunications;
device.IpAddress = deviceDetailDTO.IpAddress;

View File

@ -44,9 +44,9 @@ namespace MyCore.Services
return _Devices.Find<Device>(h => h.HomeId == homeId).ToList();
}
public List<Device> GetByRoom(string homeId, string roomId)
public List<Device> GetByRoom(string roomId)
{
return _Devices.Find<Device>(d => d.HomeId == homeId && d.RoomId == roomId).ToList();
return _Devices.Find<Device>(d => d.RoomId == roomId).ToList();
}
public List<Device> GetByType(string homeId, DeviceType type)

View File

@ -2,6 +2,8 @@
using MyCore.Interfaces.Models;
using MyCore.Services;
using MyCore.Services.MyControlPanel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@ -29,7 +31,7 @@ namespace MyCore.Service
room.Name = roomCreateOrUpdateDetailDTO.Name;
room.UpdatedDate = DateTime.Now;
List<Device> currentDevices = _DeviceDatabaseService.GetByIds(roomCreateOrUpdateDetailDTO.DeviceIds); // A device cannot have multiple rooms. Filter list
List<Device> currentDevices = _DeviceDatabaseService.GetByIds(roomCreateOrUpdateDetailDTO.DeviceIds.Distinct().ToList()); // A device cannot have multiple rooms. Filter list
currentDevices = currentDevices.Where(c => c.RoomId == null).ToList(); // TODO add exception or something
if (create)
@ -53,5 +55,32 @@ namespace MyCore.Service
return room.ToDTO(devices.Select(d => d.ToDTO()).ToList());
}
public static dynamic GetValueFromJson(List<DeviceDetailDTO> devicesDetail, DeviceType deviceType, string field, bool ifAny)
{
var isAny = false;
foreach (var device in devicesDetail)
{
if (device.LastState != null)
{
var lastState = JsonConvert.DeserializeObject<JObject>(device.LastState);
if (lastState[field] != null && deviceType == device.Type)
{
if (ifAny)
{
isAny = (bool) lastState[field] ? true : isAny;
}
else
{
return lastState[field];
}
}
}
}
if (ifAny)
return isAny;
else
return null;
}
}
}

View File

@ -144,7 +144,7 @@ namespace MyCore
//services.AddMqttClientOnlineHostedService();
// Comment this line when dev
//services.AddMerossClientHostedService(); // Todo client files (a lot are useless)
services.AddMerossClientHostedService(); // Todo client files (a lot are useless)
services.AddScoped<YeelightService>(); // To clarify if needed.. ?
@ -161,6 +161,7 @@ namespace MyCore
services.AddScoped<RoomDatabaseService>();
services.AddScoped<AutomationDatabaseService>();
services.AddScoped<ActionService>();
services.AddScoped<RoomService>();
services.AddScoped<AutomationService>();
services.AddScoped<AlarmDatabaseService>();
services.AddScoped<EventDatabaseService>();