mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
122 lines
4.6 KiB
C#
122 lines
4.6 KiB
C#
using MyCore.Interfaces.DTO;
|
|
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;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MyCore.Service
|
|
{
|
|
public class RoomService
|
|
{
|
|
public static RoomDetailDTO CreateOrUpdate(RoomDatabaseService _RoomDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string homeId, RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO, bool create)
|
|
{
|
|
List<Device> devices = new List<Device>();
|
|
Room room;
|
|
if (create)
|
|
{
|
|
room = new Room();
|
|
room.CreatedDate = DateTime.Now;
|
|
}
|
|
else
|
|
{
|
|
room = _RoomDatabaseService.GetById(roomCreateOrUpdateDetailDTO.Id);
|
|
}
|
|
|
|
room.HomeId = homeId;
|
|
room.Name = roomCreateOrUpdateDetailDTO.Name;
|
|
room.UpdatedDate = DateTime.Now;
|
|
|
|
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)
|
|
{
|
|
room.DevicesIds = currentDevices.Select(c => c.Id).ToList();
|
|
room = _RoomDatabaseService.Create(room);
|
|
}
|
|
else
|
|
{
|
|
room.DevicesIds.AddRange(currentDevices.Select(c => c.Id).ToList());
|
|
room = _RoomDatabaseService.Update(room);
|
|
}
|
|
|
|
foreach (var deviceId in roomCreateOrUpdateDetailDTO.DeviceIds)
|
|
{
|
|
Device device = _DeviceDatabaseService.GetById(deviceId);
|
|
devices.Add(device);
|
|
device.RoomId = room.Id;
|
|
_DeviceDatabaseService.Update(device);
|
|
}
|
|
|
|
return room.ToDTO(devices.Select(d => d.ToDTO()).ToList());
|
|
}
|
|
|
|
public static dynamic GetValueFromDeviceType(List<DeviceDetailDTO> devicesDetail, RoomMainInfo roomMainInfo)
|
|
{
|
|
foreach (var device in devicesDetail)
|
|
{
|
|
switch (roomMainInfo)
|
|
{
|
|
case RoomMainInfo.Temperature:
|
|
if (device.IsTemperature && device.Temperature != null && device.Temperature != 0)
|
|
{
|
|
return device.Temperature.ToString();
|
|
}
|
|
break;
|
|
case RoomMainInfo.Humidity:
|
|
if (device.IsHumidity && device.Humidity != null && device.Humidity != 0)
|
|
{
|
|
return device.Humidity.ToString();
|
|
}
|
|
break;
|
|
case RoomMainInfo.Door:
|
|
if (device.IsContact)
|
|
{
|
|
return device.Contact;
|
|
}
|
|
break;
|
|
case RoomMainInfo.Motion:
|
|
if (device.IsOccupation)
|
|
{
|
|
return device.Occupation;
|
|
}
|
|
break;
|
|
case RoomMainInfo.Illuminance:
|
|
if (device.IsIlluminance && device.Illuminance != null && device.Illuminance != 0)
|
|
{
|
|
return device.Illuminance.ToString();
|
|
}
|
|
break;
|
|
case RoomMainInfo.Pressure:
|
|
if (device.IsPressure && device.Pressure != null && device.Pressure != 0)
|
|
{
|
|
return device.Pressure.ToString();
|
|
}
|
|
break;
|
|
}
|
|
/*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];
|
|
}
|
|
}
|
|
}*/
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|