mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
Updated version - include scoped services in mqtt service + fix bugs for group (groupIds update for device) + add Home Id + Home controller + Alarm controller (wip) + remove location and place => Room (and RoomId for device)
This commit is contained in:
parent
20f91757ac
commit
479ef5784d
21
MyCore.Interfaces/DTO/MyControlPanel/AlarmModeDTO.cs
Normal file
21
MyCore.Interfaces/DTO/MyControlPanel/AlarmModeDTO.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MyCore.Interfaces.DTO
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Alarm mode DTO
|
||||||
|
/// </summary>
|
||||||
|
public class AlarmModeDTO
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string HomeId { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public bool IsDefault { get; set; }
|
||||||
|
public DateTime CreatedDate { get; set; }
|
||||||
|
public DateTime UpdatedDate { get; set; }
|
||||||
|
public List<string> DevicesIds { get; set; } // Check if ok
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -11,7 +11,7 @@ namespace MyCore.Interfaces.DTO
|
|||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public bool Active { get; set; }
|
public bool Active { get; set; }
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
public DateTime CreatedDate { get; set; }
|
public DateTime CreatedDate { get; set; }
|
||||||
public DateTime UpdatedDate { get; set; }
|
public DateTime UpdatedDate { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ namespace MyCore.Interfaces.DTO
|
|||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
|
|
||||||
@ -23,14 +23,12 @@ namespace MyCore.Interfaces.DTO
|
|||||||
|
|
||||||
public ConnectionStatus ConnectionStatus { get; set; }
|
public ConnectionStatus ConnectionStatus { get; set; }
|
||||||
|
|
||||||
public string LocationId { get; set; }
|
public string RoomId { get; set; }
|
||||||
|
|
||||||
public string ProviderId { get; set; }
|
public string ProviderId { get; set; }
|
||||||
|
|
||||||
public string ProviderName { get; set; }
|
public string ProviderName { get; set; }
|
||||||
|
|
||||||
public PlaceDTO Location { get; set; }
|
|
||||||
|
|
||||||
public DateTime LastStateDate { get; set; }
|
public DateTime LastStateDate { get; set; }
|
||||||
|
|
||||||
public bool Battery { get; set; }
|
public bool Battery { get; set; }
|
||||||
|
|||||||
@ -8,7 +8,7 @@ namespace MyCore.Interfaces.DTO
|
|||||||
public class GroupSummaryDTO
|
public class GroupSummaryDTO
|
||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Type { get; set; }
|
public string Type { get; set; }
|
||||||
public bool IsAlarm { get; set; }
|
public bool IsAlarm { get; set; }
|
||||||
|
|||||||
40
MyCore.Interfaces/DTO/MyControlPanel/HomeDTO.cs
Normal file
40
MyCore.Interfaces/DTO/MyControlPanel/HomeDTO.cs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
using MyCore.Interfaces.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MyCore.Interfaces.DTO
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Home DTO
|
||||||
|
/// </summary>
|
||||||
|
public class HomeDTO
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public bool IsAlarm { get; set; }
|
||||||
|
public bool IsDefault { get; set; }
|
||||||
|
public AlarmModeDTO CurrentAlarmMode { get; set; }
|
||||||
|
public DateTime CreatedDate { get; set; }
|
||||||
|
public DateTime UpdatedDate { get; set; }
|
||||||
|
public List<string> UsersIds { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class HomeDetailDTO : HomeDTO
|
||||||
|
{
|
||||||
|
|
||||||
|
public List<UserInfoDetailDTO> Users { get; set; }
|
||||||
|
public List<DeviceSummaryDTO> Devices { get; set; }
|
||||||
|
public List<AutomationDTO> Automations { get; set; }
|
||||||
|
public List<ProviderDTO> Providers { get; set; }
|
||||||
|
public List<GroupSummaryDTO> Groups { get; set; }
|
||||||
|
//public List<ScreenConfigurationDTO> ScreenConfigurations { get; set; } TODO
|
||||||
|
//public List<ScreenDeviceDTO> ScreenDevices { get; set; } TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CreateOrUpdateHomeDTO : HomeDTO
|
||||||
|
{
|
||||||
|
public List<string> UsersIds { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MyCore.Interfaces.DTO
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Place DTO
|
|
||||||
/// </summary>
|
|
||||||
public class PlaceDTO
|
|
||||||
{
|
|
||||||
public string Id { get; set; }
|
|
||||||
public string Name { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using MyCore.Interfaces.Models;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -9,8 +10,8 @@ namespace MyCore.Interfaces.DTO
|
|||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Type { get; set; }
|
public ProviderType Type { get; set; }
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
public string Endpoint { get; set; }
|
public string Endpoint { get; set; }
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
public string Password { get; set; } // TODO ENCRYPTED
|
public string Password { get; set; } // TODO ENCRYPTED
|
||||||
|
|||||||
@ -9,7 +9,7 @@ namespace MyCore.Interfaces.DTO
|
|||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,55 @@
|
|||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
|
using MyCore.Interfaces.DTO;
|
||||||
|
using MyCore.Interfaces.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace MyCore.Interfaces.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Group of alarms
|
||||||
|
/// </summary>
|
||||||
|
public class AlarmMode
|
||||||
|
{
|
||||||
|
[BsonId]
|
||||||
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("HomeId")]
|
||||||
|
[BsonRequired]
|
||||||
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Name")]
|
||||||
|
[BsonRequired]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("IsDefault")]
|
||||||
|
[BsonRequired]
|
||||||
|
public bool IsDefault { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("CreatedDate")]
|
||||||
|
public DateTime CreatedDate { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("UpdatedDate")]
|
||||||
|
public DateTime UpdatedDate { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("DevicesIds")]
|
||||||
|
public List<string> DevicesIds { get; set; }
|
||||||
|
|
||||||
|
public AlarmModeDTO ToDTO()
|
||||||
|
{
|
||||||
|
return new AlarmModeDTO()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
HomeId = HomeId,
|
||||||
|
Name = Name,
|
||||||
|
IsDefault = IsDefault,
|
||||||
|
CreatedDate = CreatedDate,
|
||||||
|
UpdatedDate = UpdatedDate,
|
||||||
|
DevicesIds = DevicesIds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -15,9 +15,9 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
[BsonElement("UserId")]
|
[BsonElement("HomeId")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
[BsonElement("Name")]
|
[BsonElement("Name")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
@ -52,7 +52,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
Id = Id,
|
Id = Id,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
Active = Active,
|
Active = Active,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
//Triggers = Triggers
|
//Triggers = Triggers
|
||||||
@ -68,7 +68,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
Id = Id,
|
Id = Id,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
Active = Active,
|
Active = Active,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
Triggers = Triggers,
|
Triggers = Triggers,
|
||||||
@ -129,6 +129,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
public string RawRequest { get; set; } // http, mqtt
|
public string RawRequest { get; set; } // http, mqtt
|
||||||
public string ProviderId { get; set; }
|
public string ProviderId { get; set; }
|
||||||
public ActionType Type { get; set; }
|
public ActionType Type { get; set; }
|
||||||
|
public bool IsForce { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RawRequestMQTT{
|
public class RawRequestMQTT{
|
||||||
|
|||||||
@ -17,9 +17,9 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
[BsonElement("UserId")]
|
[BsonElement("HomeId")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
[BsonElement("Name")]
|
[BsonElement("Name")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
@ -52,8 +52,8 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonElement("ConnectionStatus")]
|
[BsonElement("ConnectionStatus")]
|
||||||
public ConnectionStatus ConnectionStatus { get; set; }
|
public ConnectionStatus ConnectionStatus { get; set; }
|
||||||
|
|
||||||
[BsonElement("LocationId")]
|
[BsonElement("RoomId")]
|
||||||
public string LocationId { get; set; }
|
public string RoomId { get; set; }
|
||||||
|
|
||||||
[BsonElement("MeansOfCommunications")]
|
[BsonElement("MeansOfCommunications")]
|
||||||
public List<MeansOfCommunication> MeansOfCommunications { get; set; }
|
public List<MeansOfCommunication> MeansOfCommunications { get; set; }
|
||||||
@ -104,14 +104,14 @@ namespace MyCore.Interfaces.Models
|
|||||||
return new DeviceSummaryDTO()
|
return new DeviceSummaryDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
Model = Model,
|
Model = Model,
|
||||||
Type = Type,
|
Type = Type,
|
||||||
Status = Status,
|
Status = Status,
|
||||||
ConnectionStatus = ConnectionStatus,
|
ConnectionStatus = ConnectionStatus,
|
||||||
ProviderId = ProviderId,
|
ProviderId = ProviderId,
|
||||||
LocationId = LocationId, // Include room id
|
RoomId = RoomId, // Include room id
|
||||||
LastStateDate = LastStateDate,
|
LastStateDate = LastStateDate,
|
||||||
Battery = Battery,
|
Battery = Battery,
|
||||||
BatteryStatus = BatteryStatus
|
BatteryStatus = BatteryStatus
|
||||||
@ -123,7 +123,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
return new DeviceDetailDTO()
|
return new DeviceDetailDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
Model = Model,
|
Model = Model,
|
||||||
Type = Type,
|
Type = Type,
|
||||||
@ -131,7 +131,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
Port = Port,
|
Port = Port,
|
||||||
FirmwareVersion = FirmwareVersion,
|
FirmwareVersion = FirmwareVersion,
|
||||||
ConnectionStatus = ConnectionStatus,
|
ConnectionStatus = ConnectionStatus,
|
||||||
LocationId = LocationId, // Include room id
|
RoomId = RoomId, // Include room id
|
||||||
MeansOfCommunications = MeansOfCommunications,
|
MeansOfCommunications = MeansOfCommunications,
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
|
|||||||
@ -14,9 +14,9 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
[BsonElement("UserId")]
|
[BsonElement("HomeId")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
[BsonElement("Name")]
|
[BsonElement("Name")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
@ -49,10 +49,9 @@ namespace MyCore.Interfaces.Models
|
|||||||
return new GroupSummaryDTO()
|
return new GroupSummaryDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
Type = Type,
|
Type = Type
|
||||||
IsAlarm = IsAlarm
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,10 +60,9 @@ namespace MyCore.Interfaces.Models
|
|||||||
return new GroupDetailDTO()
|
return new GroupDetailDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
Type = Type,
|
Type = Type,
|
||||||
IsAlarm = IsAlarm,
|
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
Devices = devicesDetail
|
Devices = devicesDetail
|
||||||
|
|||||||
94
MyCore.Interfaces/Models/MyControlPanel/Database/Home.cs
Normal file
94
MyCore.Interfaces/Models/MyControlPanel/Database/Home.cs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
|
using MyCore.Interfaces.DTO;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace MyCore.Interfaces.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Home (list of devices, list of users, main element of a house)
|
||||||
|
/// </summary>
|
||||||
|
public class Home
|
||||||
|
{
|
||||||
|
[BsonId]
|
||||||
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Name")]
|
||||||
|
[BsonRequired]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("IsDefault")]
|
||||||
|
[BsonRequired]
|
||||||
|
public bool IsDefault { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("IsAlarm")]
|
||||||
|
public bool IsAlarm { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("CurrentAlarmMode")]
|
||||||
|
public AlarmMode CurrentAlarmMode { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("CreatedDate")]
|
||||||
|
public DateTime CreatedDate { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("UpdatedDate")]
|
||||||
|
public DateTime UpdatedDate { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("UsersIds")]
|
||||||
|
public string[] UsersIds { get; set; }
|
||||||
|
|
||||||
|
/*[BsonElement("Automations")]
|
||||||
|
public Automation[] Automations { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Devices")]
|
||||||
|
public Device[] Devices { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Providers")]
|
||||||
|
public Provider[] Providers { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Groups")]
|
||||||
|
public Group[] Groups { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("ScreenConfigurationIds")]
|
||||||
|
public ScreenConfiguration[] ScreenConfigurations { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("DeviceIds")]
|
||||||
|
public ScreenDevice[] DeviceIds { get; set; }*/
|
||||||
|
|
||||||
|
public HomeDTO ToDTO()
|
||||||
|
{
|
||||||
|
return new HomeDTO()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
Name = Name,
|
||||||
|
IsAlarm = IsAlarm,
|
||||||
|
IsDefault = IsDefault,
|
||||||
|
CurrentAlarmMode = CurrentAlarmMode != null ? CurrentAlarmMode.ToDTO(): null,
|
||||||
|
UsersIds = UsersIds.ToList(),
|
||||||
|
CreatedDate = CreatedDate,
|
||||||
|
UpdatedDate = UpdatedDate
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public HomeDetailDTO ToDetailDTO(List<UserInfo> users, List<Device> devices, List<Automation> automations, List<Provider> providers, List<Group> groups)
|
||||||
|
{
|
||||||
|
return new HomeDetailDTO()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
Name = Name,
|
||||||
|
Users = users.Select(u => u.ToDTO()).ToList(),
|
||||||
|
IsAlarm = IsAlarm,
|
||||||
|
IsDefault = IsDefault,
|
||||||
|
CurrentAlarmMode = CurrentAlarmMode.ToDTO(),
|
||||||
|
CreatedDate = CreatedDate,
|
||||||
|
UpdatedDate = UpdatedDate,
|
||||||
|
Devices = devices.Select(d => d.ToSummaryDTO()).ToList(),
|
||||||
|
Automations = automations.Select(a => a.ToDTO()).ToList(),
|
||||||
|
Providers = providers.Select(p => p.ToDTO()).ToList(),
|
||||||
|
Groups = groups.Select(g => g.ToSummaryDTO()).ToList(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,29 +0,0 @@
|
|||||||
using MongoDB.Bson;
|
|
||||||
using MongoDB.Bson.Serialization.Attributes;
|
|
||||||
using MyCore.Interfaces.DTO;
|
|
||||||
|
|
||||||
namespace MyCore.Interfaces.Models
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Location of a device (Room name, garden, ..)
|
|
||||||
/// </summary>
|
|
||||||
public class Place
|
|
||||||
{
|
|
||||||
[BsonId]
|
|
||||||
[BsonRepresentation(BsonType.ObjectId)]
|
|
||||||
public string Id { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("Name")]
|
|
||||||
[BsonRequired]
|
|
||||||
public string Name { get; set; }
|
|
||||||
|
|
||||||
public PlaceDTO ToDTO()
|
|
||||||
{
|
|
||||||
return new PlaceDTO()
|
|
||||||
{
|
|
||||||
Id = Id,
|
|
||||||
Name = Name
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -15,9 +15,9 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
[BsonElement("UserId")]
|
[BsonElement("HomeId")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
[BsonElement("Name")]
|
[BsonElement("Name")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
@ -25,7 +25,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
|
|
||||||
[BsonElement("Type")]
|
[BsonElement("Type")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string Type { get; set; }
|
public ProviderType Type { get; set; }
|
||||||
|
|
||||||
[BsonElement("Username")]
|
[BsonElement("Username")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
@ -57,7 +57,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
Id = Id,
|
Id = Id,
|
||||||
Type = Type,
|
Type = Type,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
Endpoint = Endpoint,
|
Endpoint = Endpoint,
|
||||||
/*Username = Username,
|
/*Username = Username,
|
||||||
Password = Password,
|
Password = Password,
|
||||||
@ -66,4 +66,12 @@ namespace MyCore.Interfaces.Models
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum ProviderType
|
||||||
|
{
|
||||||
|
arlo,
|
||||||
|
meross,
|
||||||
|
yeelight,
|
||||||
|
zigbee2mqtt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,9 +17,9 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
[BsonElement("UserId")]
|
[BsonElement("HomeId")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
[BsonElement("Name")]
|
[BsonElement("Name")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
@ -39,7 +39,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
return new RoomSummaryDTO()
|
return new RoomSummaryDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
Name = Name
|
Name = Name
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
return new RoomDetailDTO()
|
return new RoomDetailDTO()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
UserId = UserId,
|
HomeId = HomeId,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
|
|||||||
@ -44,6 +44,9 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonElement("Birthday")]
|
[BsonElement("Birthday")]
|
||||||
public DateTime Birthday { get; set; }
|
public DateTime Birthday { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("HomeIds")]
|
||||||
|
public List<string> HomeIds { get; set; }
|
||||||
|
|
||||||
[BsonElement("DateCreation")]
|
[BsonElement("DateCreation")]
|
||||||
public DateTime DateCreation { get; set; }
|
public DateTime DateCreation { get; set; }
|
||||||
|
|
||||||
@ -69,24 +72,6 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonElement("PostalCode")]
|
[BsonElement("PostalCode")]
|
||||||
public int PostalCode { get; set; }
|
public int PostalCode { get; set; }
|
||||||
|
|
||||||
[BsonElement("Automations")]
|
|
||||||
public Automation[] Automations { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("Devices")]
|
|
||||||
public Device[] Devices { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("Providers")]
|
|
||||||
public Provider[] Providers { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("Groups")]
|
|
||||||
public Group[] Groups { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("ScreenConfigurationIds")]
|
|
||||||
public ScreenConfiguration[] ScreenConfigurationIds { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("DeviceIds")]
|
|
||||||
public ScreenDevice[] DeviceIds { get; set; }
|
|
||||||
|
|
||||||
public UserInfoDetailDTO ToDTO()
|
public UserInfoDetailDTO ToDTO()
|
||||||
{
|
{
|
||||||
return new UserInfoDetailDTO()
|
return new UserInfoDetailDTO()
|
||||||
|
|||||||
@ -16,8 +16,8 @@ namespace MyCore.Interfaces.Models
|
|||||||
[BsonElement("DeviceId")]
|
[BsonElement("DeviceId")]
|
||||||
public string DeviceId { get; set; }
|
public string DeviceId { get; set; }
|
||||||
|
|
||||||
[BsonElement("UserId")]
|
[BsonElement("HomeId")]
|
||||||
public string UserId { get; set; }
|
public string HomeId { get; set; }
|
||||||
|
|
||||||
[BsonElement("Watt")]
|
[BsonElement("Watt")]
|
||||||
public double Watt { get; set; }
|
public double Watt { get; set; }
|
||||||
|
|||||||
@ -24,6 +24,22 @@ namespace MyCore.Interfaces.Models
|
|||||||
public string dateCode { get; set; }
|
public string dateCode { get; set; }
|
||||||
public long lastSeen { get; set; }
|
public long lastSeen { get; set; }
|
||||||
}*/
|
}*/
|
||||||
|
public class Zigbee2MqttDeviceConfig
|
||||||
|
{
|
||||||
|
public string description { get; set; }
|
||||||
|
public string dateCode { get; set; }
|
||||||
|
public string friendly_name { get; set; }
|
||||||
|
public string ieeeAddr { get; set; }
|
||||||
|
public long lastSeen { get; set; }
|
||||||
|
public string manufacturerID { get; set; }
|
||||||
|
public string manufacturerName { get; set; }
|
||||||
|
public string model { get; set; }
|
||||||
|
public string modelID { get; set; }
|
||||||
|
public string type { get; set; }
|
||||||
|
public int networkAddress { get; set; }
|
||||||
|
public string powerSource { get; set; }
|
||||||
|
public string vendor { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public class Zigbee2MqttDevice
|
public class Zigbee2MqttDevice
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="DevExpress.Xpo" Version="20.1.8" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.8.0" />
|
<PackageReference Include="MongoDB.Driver" Version="2.8.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
40
MyCore/Controllers/AlarmController.cs
Normal file
40
MyCore/Controllers/AlarmController.cs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Security.Authentication;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using Mqtt.Client.AspNetCore.Services;
|
||||||
|
using MyCore.Interfaces.DTO;
|
||||||
|
using MyCore.Interfaces.Models;
|
||||||
|
using MyCore.Services;
|
||||||
|
using MyCore.Services.Devices;
|
||||||
|
using MyCore.Services.MyControlPanel;
|
||||||
|
|
||||||
|
namespace MyCore.Service.Controllers
|
||||||
|
{
|
||||||
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||||
|
[Route("api/alarm")]
|
||||||
|
[ApiController]
|
||||||
|
public class AlarmController : ControllerBase
|
||||||
|
{
|
||||||
|
private UserDatabaseService _UserDatabaseService;
|
||||||
|
private RoomDatabaseService _RoomDatabaseService;
|
||||||
|
private DeviceDatabaseService _DeviceDatabaseService;
|
||||||
|
private readonly IMqttClientService _mqttClientService;
|
||||||
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
|
public AlarmController(UserDatabaseService userDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
|
{
|
||||||
|
this._UserDatabaseService = userDatabaseService;
|
||||||
|
this._RoomDatabaseService = roomDatabaseService;
|
||||||
|
this._DeviceDatabaseService = deviceDatabaseService;
|
||||||
|
this._mqttClientService = provider.MqttClientService;
|
||||||
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -31,21 +31,21 @@ namespace MyCore.Service.Controllers
|
|||||||
private readonly DeviceDatabaseService _DeviceDatabaseService;
|
private readonly DeviceDatabaseService _DeviceDatabaseService;
|
||||||
private readonly GroupDatabaseService _GroupDatabaseService;
|
private readonly GroupDatabaseService _GroupDatabaseService;
|
||||||
private readonly ProviderDatabaseService _ProviderDatabaseService;
|
private readonly ProviderDatabaseService _ProviderDatabaseService;
|
||||||
private readonly LocationDatabaseService _LocationDatabaseService;
|
private readonly RoomDatabaseService _RoomDatabaseService;
|
||||||
private readonly UserDatabaseService _UserDatabaseService;
|
private readonly UserDatabaseService _UserDatabaseService;
|
||||||
private readonly ActionService _ActionService;
|
private readonly ActionService _ActionService;
|
||||||
private readonly AutomationDatabaseService _AutomationDatabaseService;
|
private readonly AutomationDatabaseService _AutomationDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public AuthenticationController(ILogger<AuthenticationController> logger, TokensService tokensService, DeviceDatabaseService DeviceDatabaseService, GroupDatabaseService GroupDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, ActionService ActionService, AutomationDatabaseService AutomationDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
public AuthenticationController(ILogger<AuthenticationController> logger, TokensService tokensService, DeviceDatabaseService DeviceDatabaseService, GroupDatabaseService GroupDatabaseService, ProviderDatabaseService ProviderDatabaseService, RoomDatabaseService RoomDatabaseService, UserDatabaseService UserDatabaseService, ActionService ActionService, AutomationDatabaseService AutomationDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_tokensService = tokensService;
|
_tokensService = tokensService;
|
||||||
_DeviceDatabaseService = DeviceDatabaseService;
|
_DeviceDatabaseService = DeviceDatabaseService;
|
||||||
_GroupDatabaseService = GroupDatabaseService;
|
_GroupDatabaseService = GroupDatabaseService;
|
||||||
_ProviderDatabaseService = ProviderDatabaseService;
|
_ProviderDatabaseService = ProviderDatabaseService;
|
||||||
_LocationDatabaseService = LocationDatabaseService;
|
_RoomDatabaseService = RoomDatabaseService;
|
||||||
_UserDatabaseService = UserDatabaseService;
|
_UserDatabaseService = UserDatabaseService;
|
||||||
_ActionService = ActionService;
|
_ActionService = ActionService;
|
||||||
_AutomationDatabaseService = AutomationDatabaseService;
|
_AutomationDatabaseService = AutomationDatabaseService;
|
||||||
@ -74,8 +74,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (user == null)
|
if (user == null)
|
||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("User not found");
|
||||||
|
|
||||||
System.Console.WriteLine($"Init userId for MqttClientService ! {user.Email}");
|
System.Console.WriteLine($"{user.Email} is connected");
|
||||||
MqttClientService.SetServices(_DeviceDatabaseService, _GroupDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, _ActionService, _AutomationDatabaseService, user.Id);
|
|
||||||
|
|
||||||
return new OkObjectResult(token);
|
return new OkObjectResult(token);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,31 +23,31 @@ namespace MyCore.Service.Controllers
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
public class AutomationController : ControllerBase
|
public class AutomationController : ControllerBase
|
||||||
{
|
{
|
||||||
private UserDatabaseService _UserDatabaseService;
|
private HomeDatabaseService _HomeDatabaseService;
|
||||||
private AutomationDatabaseService _AutomationDatabaseService;
|
private AutomationDatabaseService _AutomationDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public AutomationController(UserDatabaseService userDatabaseService, AutomationDatabaseService automationDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
public AutomationController(HomeDatabaseService homeDatabaseService, AutomationDatabaseService automationDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
this._UserDatabaseService = userDatabaseService;
|
this._HomeDatabaseService = homeDatabaseService;
|
||||||
this._AutomationDatabaseService = automationDatabaseService;
|
this._AutomationDatabaseService = automationDatabaseService;
|
||||||
this._mqttClientService = provider.MqttClientService;
|
this._mqttClientService = provider.MqttClientService;
|
||||||
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all automations for the specified user
|
/// Get all automations for the specified home
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(List<AutomationDTO>), 200)]
|
[ProducesResponseType(typeof(List<AutomationDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}")]
|
[HttpGet("{homeId}")]
|
||||||
public ObjectResult GetAll(string userId)
|
public ObjectResult GetAll(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
List<Automation> Automations = _AutomationDatabaseService.GetAll(userId);
|
List<Automation> Automations = _AutomationDatabaseService.GetAll(homeId);
|
||||||
|
|
||||||
List<AutomationDTO> automationDTO = Automations.Select(d => d.ToDTO()).ToList();
|
List<AutomationDTO> automationDTO = Automations.Select(d => d.ToDTO()).ToList();
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (automationCreateOrUpdateDetail == null)
|
if (automationCreateOrUpdateDetail == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
AutomationDTO automationCreated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.UserId, automationCreateOrUpdateDetail, true);
|
AutomationDTO automationCreated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.HomeId, automationCreateOrUpdateDetail, true);
|
||||||
|
|
||||||
return new OkObjectResult(automationCreated);
|
return new OkObjectResult(automationCreated);
|
||||||
}
|
}
|
||||||
@ -146,7 +146,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (!_AutomationDatabaseService.IsExist(automationCreateOrUpdateDetail.Id))
|
if (!_AutomationDatabaseService.IsExist(automationCreateOrUpdateDetail.Id))
|
||||||
throw new KeyNotFoundException("Automation does not exist");
|
throw new KeyNotFoundException("Automation does not exist");
|
||||||
|
|
||||||
AutomationDTO automationUpdated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.UserId, automationCreateOrUpdateDetail, false);
|
AutomationDTO automationUpdated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.HomeId, automationCreateOrUpdateDetail, false);
|
||||||
|
|
||||||
return new OkObjectResult(automationUpdated);
|
return new OkObjectResult(automationUpdated);
|
||||||
}
|
}
|
||||||
@ -202,27 +202,27 @@ namespace MyCore.Service.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete all automation for a specified
|
/// Delete all automation for a specified home
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(string), 202)]
|
[ProducesResponseType(typeof(string), 202)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpDelete("user/{userId}")]
|
[HttpDelete("home/{homeId}")]
|
||||||
public ObjectResult DeleteAllForUser(string userId)
|
public ObjectResult DeleteAllForHome(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
if (!_UserDatabaseService.IsExist(userId))
|
if (!_HomeDatabaseService.IsExist(homeId))
|
||||||
throw new KeyNotFoundException("User does not exist");
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
_AutomationDatabaseService.RemoveForUser(userId);
|
_AutomationDatabaseService.RemoveForHome(homeId);
|
||||||
|
|
||||||
return new ObjectResult("All automation linked with specified user has been successfully deleted") { StatusCode = 202 };
|
return new ObjectResult("All automation linked with specified home has been successfully deleted") { StatusCode = 202 };
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -24,17 +24,17 @@ namespace MyCore.Controllers
|
|||||||
{
|
{
|
||||||
private DeviceDatabaseService _DeviceDatabaseService;
|
private DeviceDatabaseService _DeviceDatabaseService;
|
||||||
private ProviderDatabaseService _ProviderDatabaseService;
|
private ProviderDatabaseService _ProviderDatabaseService;
|
||||||
private LocationDatabaseService _LocationDatabaseService;
|
private RoomDatabaseService _RoomDatabaseService;
|
||||||
private UserDatabaseService _UserDatabaseService;
|
private HomeDatabaseService _HomeDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, RoomDatabaseService RoomDatabaseService, HomeDatabaseService HomeDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
this._DeviceDatabaseService = DeviceDatabaseService;
|
this._DeviceDatabaseService = DeviceDatabaseService;
|
||||||
this._ProviderDatabaseService = ProviderDatabaseService;
|
this._ProviderDatabaseService = ProviderDatabaseService;
|
||||||
this._LocationDatabaseService = LocationDatabaseService;
|
this._RoomDatabaseService = RoomDatabaseService;
|
||||||
this._UserDatabaseService = UserDatabaseService;
|
this._HomeDatabaseService = HomeDatabaseService;
|
||||||
this._mqttClientService = provider.MqttClientService;
|
this._mqttClientService = provider.MqttClientService;
|
||||||
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
}
|
}
|
||||||
@ -42,21 +42,21 @@ namespace MyCore.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all devices summary
|
/// Get all devices summary
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(List<DeviceSummaryDTO>), 200)]
|
[ProducesResponseType(typeof(List<DeviceSummaryDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}")]
|
[HttpGet("{homeId}")]
|
||||||
public ObjectResult GetAll(string userId)
|
public ObjectResult GetAll(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
List<Device> Devices = _DeviceDatabaseService.GetAll(userId);
|
List<Device> Devices = _DeviceDatabaseService.GetAll(homeId);
|
||||||
|
|
||||||
List<DeviceSummaryDTO> devicesSummaryDTO = Devices.Select(d => d.ToSummaryDTO()).ToList();
|
List<DeviceSummaryDTO> devicesSummaryDTO = Devices.Select(d => d.ToSummaryDTO()).ToList();
|
||||||
|
|
||||||
foreach (var device in devicesSummaryDTO)
|
foreach (var device in devicesSummaryDTO)
|
||||||
{
|
{
|
||||||
device.ProviderName = _ProviderDatabaseService.GetById(userId, device.ProviderId).Name;
|
device.ProviderName = _ProviderDatabaseService.GetById(homeId, device.ProviderId).Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OkObjectResult(devicesSummaryDTO);
|
return new OkObjectResult(devicesSummaryDTO);
|
||||||
@ -107,20 +107,20 @@ namespace MyCore.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get list of devices from a type
|
/// Get list of devices from a type
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">user Id</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
/// <param name="type">device type</param>
|
/// <param name="type">device type</param>
|
||||||
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}/type/{type}")]
|
[HttpGet("{homeId}/type/{type}")]
|
||||||
public ObjectResult GetDevicesByType(string userId, DeviceType type)
|
public ObjectResult GetDevicesByType(string homeId, DeviceType type)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
List<Device> devices = _DeviceDatabaseService.GetByType(userId, type);
|
List<Device> devices = _DeviceDatabaseService.GetByType(homeId, type);
|
||||||
|
|
||||||
return new OkObjectResult(devices.Select(d => d.ToDTO()));
|
return new OkObjectResult(devices.Select(d => d.ToDTO()));
|
||||||
|
|
||||||
@ -152,7 +152,7 @@ namespace MyCore.Controllers
|
|||||||
if (deviceDetailDTO == null)
|
if (deviceDetailDTO == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
DeviceDetailDTO deviceCreated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, deviceDetailDTO.UserId, deviceDetailDTO, true);
|
DeviceDetailDTO deviceCreated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._RoomDatabaseService, deviceDetailDTO.HomeId, deviceDetailDTO, true);
|
||||||
|
|
||||||
return new OkObjectResult(deviceCreated);
|
return new OkObjectResult(deviceCreated);
|
||||||
|
|
||||||
@ -178,7 +178,7 @@ namespace MyCore.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create devices from provider
|
/// Create devices from provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">User Id</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
/// <param name="providerId">Id of Provider</param>
|
/// <param name="providerId">Id of Provider</param>
|
||||||
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
@ -186,22 +186,22 @@ namespace MyCore.Controllers
|
|||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 421)]
|
[ProducesResponseType(typeof(string), 421)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpPost("{userId}/fromProvider/{providerId}")]
|
[HttpPost("{homeId}/fromProvider/{providerId}")]
|
||||||
public async Task<ObjectResult> CreateDevicesFromProvider(string userId, string providerId)
|
public async Task<ObjectResult> CreateDevicesFromProvider(string homeId, string providerId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Invalid paramaters");
|
throw new ArgumentNullException("Invalid paramaters");
|
||||||
|
|
||||||
if (!UserService.IsExist(_UserDatabaseService, userId))
|
if (!HomeService.IsExist(_HomeDatabaseService, homeId))
|
||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("Home not found");
|
||||||
|
|
||||||
Provider provider = ProviderService.GetProviderById(this._ProviderDatabaseService, userId, providerId);
|
Provider provider = ProviderService.GetProviderById(this._ProviderDatabaseService, homeId, providerId);
|
||||||
if (provider == null)
|
if (provider == null)
|
||||||
throw new KeyNotFoundException("Provider not found");
|
throw new KeyNotFoundException("Provider not found");
|
||||||
|
|
||||||
Dictionary<string, List<DeviceDetailDTO>> devices = await DeviceService.CreateFromProvider(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, userId, provider);
|
Dictionary<string, List<DeviceDetailDTO>> devices = await DeviceService.CreateFromProvider(this._DeviceDatabaseService, this._ProviderDatabaseService, this._RoomDatabaseService, homeId, provider);
|
||||||
|
|
||||||
return new OkObjectResult(devices);
|
return new OkObjectResult(devices);
|
||||||
}
|
}
|
||||||
@ -230,21 +230,21 @@ namespace MyCore.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all zigbee2Mqtt devices
|
/// Get all zigbee2Mqtt devices
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">User Id</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("zigbee2Mqtt/{userId}")]
|
[HttpGet("zigbee2Mqtt/{homeId}")]
|
||||||
public ObjectResult GetDevicesFromZigbee2Mqtt(string userId)
|
public ObjectResult GetDevicesFromZigbee2Mqtt(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Invalid paramaters");
|
throw new ArgumentNullException("Invalid paramaters");
|
||||||
|
|
||||||
if (!UserService.IsExist(_UserDatabaseService, userId))
|
if (!HomeService.IsExist(_HomeDatabaseService, homeId))
|
||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("Home not found");
|
||||||
|
|
||||||
// GET ALL LOCAL DEVICES
|
// GET ALL LOCAL DEVICES
|
||||||
var devices = MqttClientService.devices; // Be carefull, we only got the exact result after each connection
|
var devices = MqttClientService.devices; // Be carefull, we only got the exact result after each connection
|
||||||
@ -268,22 +268,22 @@ namespace MyCore.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get devices from provider
|
/// Get devices from provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">User Id</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
/// <param name="providerId">Id of Provider</param>
|
/// <param name="providerId">Id of Provider</param>
|
||||||
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}/fromProvider/{providerId}")]
|
[HttpGet("{homeId}/fromProvider/{providerId}")]
|
||||||
public ObjectResult GetDevicesFromProvider(string userId, string providerId)
|
public ObjectResult GetDevicesFromProvider(string homeId, string providerId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null || providerId == null)
|
if (homeId == null || providerId == null)
|
||||||
throw new ArgumentNullException("Invalid paramaters");
|
throw new ArgumentNullException("Invalid paramaters");
|
||||||
|
|
||||||
if (!_UserDatabaseService.IsExist(userId))
|
if (!_HomeDatabaseService.IsExist(homeId))
|
||||||
throw new KeyNotFoundException("User does not exist");
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
if (!_ProviderDatabaseService.IsExist(providerId))
|
if (!_ProviderDatabaseService.IsExist(providerId))
|
||||||
throw new KeyNotFoundException("Provider does not exist");
|
throw new KeyNotFoundException("Provider does not exist");
|
||||||
@ -322,7 +322,7 @@ namespace MyCore.Controllers
|
|||||||
if (!_DeviceDatabaseService.IsExist(deviceDetailDTO.Id))
|
if (!_DeviceDatabaseService.IsExist(deviceDetailDTO.Id))
|
||||||
throw new KeyNotFoundException("Device does not exist");
|
throw new KeyNotFoundException("Device does not exist");
|
||||||
|
|
||||||
DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, deviceDetailDTO.UserId, deviceDetailDTO, false);
|
DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._RoomDatabaseService, deviceDetailDTO.HomeId, deviceDetailDTO, false);
|
||||||
|
|
||||||
return new OkObjectResult(deviceUpdated);
|
return new OkObjectResult(deviceUpdated);
|
||||||
}
|
}
|
||||||
@ -378,22 +378,22 @@ namespace MyCore.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete devices from provider
|
/// Delete devices from provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">User Id</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
/// <param name="providerId">Id of Provider</param>
|
/// <param name="providerId">Id of Provider</param>
|
||||||
[ProducesResponseType(typeof(string), 202)]
|
[ProducesResponseType(typeof(string), 202)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpDelete("{userId}/fromProvider/{providerId}")]
|
[HttpDelete("{homeId}/fromProvider/{providerId}")]
|
||||||
public ObjectResult DeleteDevicesFromProvider(string userId, string providerId)
|
public ObjectResult DeleteDevicesFromProvider(string homeId, string providerId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null || providerId == null)
|
if (homeId == null || providerId == null)
|
||||||
throw new ArgumentNullException("Invalid paramaters");
|
throw new ArgumentNullException("Invalid paramaters");
|
||||||
|
|
||||||
if (!_UserDatabaseService.IsExist(userId))
|
if (!_HomeDatabaseService.IsExist(homeId))
|
||||||
throw new KeyNotFoundException("User does not exist");
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
if (!_ProviderDatabaseService.IsExist(providerId))
|
if (!_ProviderDatabaseService.IsExist(providerId))
|
||||||
throw new KeyNotFoundException("Provider does not exist");
|
throw new KeyNotFoundException("Provider does not exist");
|
||||||
@ -417,27 +417,27 @@ namespace MyCore.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete all device for a specified user
|
/// Delete all device for a specified home
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Id of home</param>
|
||||||
[ProducesResponseType(typeof(string), 202)]
|
[ProducesResponseType(typeof(string), 202)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpDelete("user/{userId}")]
|
[HttpDelete("home/{homeId}")]
|
||||||
public ObjectResult DeleteAllForUser(string userId)
|
public ObjectResult DeleteAllForHome(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Invalid paramaters");
|
throw new ArgumentNullException("Invalid paramaters");
|
||||||
|
|
||||||
if (!_UserDatabaseService.IsExist(userId))
|
if (!_HomeDatabaseService.IsExist(homeId))
|
||||||
throw new KeyNotFoundException("User does not exist");
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
// TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ?
|
// TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ?
|
||||||
_DeviceDatabaseService.RemoveForUser(userId);
|
_DeviceDatabaseService.RemoveForHome(homeId);
|
||||||
return new ObjectResult("User devices has been successfully deleted") { StatusCode = 202 };
|
return new ObjectResult("Home devices has been successfully deleted") { StatusCode = 202 };
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -32,20 +32,20 @@ namespace MyCore.Controllers
|
|||||||
/// Get summary production of Kwh/Year
|
/// Get summary production of Kwh/Year
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[HttpGet("electricity")]
|
[HttpGet("electricity")]
|
||||||
public ActionResult<List<ElectricityProduction>> GetElectricityProduction(string userId, ViewBy viewBy)
|
public ActionResult<List<ElectricityProduction>> GetElectricityProduction(string homeId, ViewBy viewBy)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
switch (viewBy)
|
switch (viewBy)
|
||||||
{
|
{
|
||||||
case ViewBy.Year:
|
case ViewBy.Year:
|
||||||
return _EnergyService.GetElectricityProductionForSpecifiedYear(userId, DateTime.Now.Year);
|
return _EnergyService.GetElectricityProductionForSpecifiedYear(homeId, DateTime.Now.Year);
|
||||||
break;
|
break;
|
||||||
case ViewBy.Month:
|
case ViewBy.Month:
|
||||||
return _EnergyService.GetElectricityProductionForSpecifiedMonth(userId, DateTime.Now.Month);
|
return _EnergyService.GetElectricityProductionForSpecifiedMonth(homeId, DateTime.Now.Month);
|
||||||
break;
|
break;
|
||||||
case ViewBy.Day:
|
case ViewBy.Day:
|
||||||
return _EnergyService.GetElectricityProductionForSpecifiedDay(userId, DateTime.Now.DayOfYear);
|
return _EnergyService.GetElectricityProductionForSpecifiedDay(homeId, DateTime.Now.DayOfYear);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return new ObjectResult("Error - ViewBy incorrect") { StatusCode = 500 };
|
return new ObjectResult("Error - ViewBy incorrect") { StatusCode = 500 };
|
||||||
|
|||||||
@ -22,37 +22,37 @@ namespace MyCore.Controllers
|
|||||||
{
|
{
|
||||||
private DeviceDatabaseService _DeviceDatabaseService;
|
private DeviceDatabaseService _DeviceDatabaseService;
|
||||||
private ProviderDatabaseService _ProviderDatabaseService;
|
private ProviderDatabaseService _ProviderDatabaseService;
|
||||||
private LocationDatabaseService _LocationDatabaseService;
|
private RoomDatabaseService _RoomDatabaseService;
|
||||||
private UserDatabaseService _UserDatabaseService;
|
private HomeDatabaseService _HomeDatabaseService;
|
||||||
|
|
||||||
public ProviderController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService)
|
public ProviderController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, RoomDatabaseService RoomDatabaseService, HomeDatabaseService HomeDatabaseService)
|
||||||
{
|
{
|
||||||
this._DeviceDatabaseService = DeviceDatabaseService;
|
this._DeviceDatabaseService = DeviceDatabaseService;
|
||||||
this._ProviderDatabaseService = ProviderDatabaseService;
|
this._ProviderDatabaseService = ProviderDatabaseService;
|
||||||
this._LocationDatabaseService = LocationDatabaseService;
|
this._RoomDatabaseService = RoomDatabaseService;
|
||||||
this._UserDatabaseService = UserDatabaseService;
|
this._HomeDatabaseService = HomeDatabaseService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all user providers
|
/// Get all home providers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(List<ProviderDTO>), 200)]
|
[ProducesResponseType(typeof(List<ProviderDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}")]
|
[HttpGet("{homeId}")]
|
||||||
public ObjectResult GetAll(string userId)
|
public ObjectResult GetAll(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
if (!UserService.IsExist(_UserDatabaseService, userId))
|
if (!HomeService.IsExist(_HomeDatabaseService, homeId))
|
||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("Home not found");
|
||||||
|
|
||||||
List<Provider> providers = ProviderService.GetAll(this._ProviderDatabaseService, userId);
|
List<Provider> providers = ProviderService.GetAll(this._ProviderDatabaseService, homeId);
|
||||||
|
|
||||||
List<ProviderDTO> providersDTO = providers.Select(p => p.ToDTO()).ToList();
|
List<ProviderDTO> providersDTO = providers.Select(p => p.ToDTO()).ToList();
|
||||||
|
|
||||||
@ -90,16 +90,16 @@ namespace MyCore.Controllers
|
|||||||
if (providerDTO == null)
|
if (providerDTO == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
if (providerDTO.UserId == null)
|
if (providerDTO.HomeId == null)
|
||||||
throw new ArgumentNullException("User not found");
|
throw new ArgumentNullException("Home not found");
|
||||||
|
|
||||||
if (!UserService.IsExist(_UserDatabaseService, providerDTO.UserId))
|
if (!HomeService.IsExist(_HomeDatabaseService, providerDTO.HomeId))
|
||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("Home not found");
|
||||||
|
|
||||||
if (_ProviderDatabaseService.AlreadyExistForUser(providerDTO.UserId, providerDTO.Name))
|
if (_ProviderDatabaseService.AlreadyExistForHome(providerDTO.HomeId, providerDTO.Name))
|
||||||
throw new InvalidOperationException("Provider already exists");
|
throw new InvalidOperationException("Provider already exists");
|
||||||
|
|
||||||
ProviderDTO providerCreated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, providerDTO.UserId, providerDTO, true);
|
ProviderDTO providerCreated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, providerDTO.HomeId, providerDTO, true);
|
||||||
|
|
||||||
return new OkObjectResult(providerCreated);
|
return new OkObjectResult(providerCreated);
|
||||||
|
|
||||||
@ -138,16 +138,16 @@ namespace MyCore.Controllers
|
|||||||
if (providerDTO == null)
|
if (providerDTO == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
if (providerDTO.UserId == null)
|
if (providerDTO.HomeId == null)
|
||||||
throw new ArgumentNullException("User not found");
|
throw new ArgumentNullException("Home not found");
|
||||||
|
|
||||||
if (!UserService.IsExist(_UserDatabaseService, providerDTO.UserId))
|
if (!HomeService.IsExist(_HomeDatabaseService, providerDTO.HomeId))
|
||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("Home not found");
|
||||||
|
|
||||||
if (!ProviderService.IsExist(this._ProviderDatabaseService, providerDTO.UserId, providerDTO.Id))
|
if (!ProviderService.IsExist(this._ProviderDatabaseService, providerDTO.HomeId, providerDTO.Id))
|
||||||
throw new KeyNotFoundException("Provider does not exist");
|
throw new KeyNotFoundException("Provider does not exist");
|
||||||
|
|
||||||
ProviderDTO providerUpdated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, providerDTO.UserId, providerDTO, false);
|
ProviderDTO providerUpdated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, providerDTO.HomeId, providerDTO, false);
|
||||||
|
|
||||||
return new OkObjectResult(providerUpdated);
|
return new OkObjectResult(providerUpdated);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,16 +23,16 @@ namespace MyCore.Service.Controllers
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
public class GroupController : ControllerBase
|
public class GroupController : ControllerBase
|
||||||
{
|
{
|
||||||
private UserDatabaseService _UserDatabaseService;
|
private HomeDatabaseService _HomeDatabaseService;
|
||||||
private GroupDatabaseService _GroupDatabaseService;
|
private GroupDatabaseService _GroupDatabaseService;
|
||||||
private DeviceDatabaseService _DeviceDatabaseService;
|
private DeviceDatabaseService _DeviceDatabaseService;
|
||||||
private ProviderDatabaseService _ProviderDatabaseService;
|
private ProviderDatabaseService _ProviderDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public GroupController(UserDatabaseService userDatabaseService, GroupDatabaseService groupDatabaseService, DeviceDatabaseService deviceDatabaseService, ProviderDatabaseService providerDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
public GroupController(HomeDatabaseService homeDatabaseService, GroupDatabaseService groupDatabaseService, DeviceDatabaseService deviceDatabaseService, ProviderDatabaseService providerDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
this._UserDatabaseService = userDatabaseService;
|
this._HomeDatabaseService = homeDatabaseService;
|
||||||
this._GroupDatabaseService = groupDatabaseService;
|
this._GroupDatabaseService = groupDatabaseService;
|
||||||
this._DeviceDatabaseService = deviceDatabaseService;
|
this._DeviceDatabaseService = deviceDatabaseService;
|
||||||
this._ProviderDatabaseService = providerDatabaseService;
|
this._ProviderDatabaseService = providerDatabaseService;
|
||||||
@ -41,21 +41,21 @@ namespace MyCore.Service.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all groups for the specified user
|
/// Get all groups for the specified home
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(List<GroupSummaryDTO>), 200)]
|
[ProducesResponseType(typeof(List<GroupSummaryDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}")]
|
[HttpGet("{homeId}")]
|
||||||
public ObjectResult GetAll(string userId)
|
public ObjectResult GetAll(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
List<Group> Groups = _GroupDatabaseService.GetAll(userId);
|
List<Group> Groups = _GroupDatabaseService.GetAll(homeId);
|
||||||
|
|
||||||
List<GroupSummaryDTO> groupsSummaryDTO = Groups.Select(d => d.ToSummaryDTO()).ToList();
|
List<GroupSummaryDTO> groupsSummaryDTO = Groups.Select(d => d.ToSummaryDTO()).ToList();
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (group == null)
|
if (group == null)
|
||||||
throw new KeyNotFoundException("Group not found");
|
throw new KeyNotFoundException("Group not found");
|
||||||
|
|
||||||
List<Device> devices = _DeviceDatabaseService.GetByLocation(group.UserId, groupId);
|
List<Device> devices = _DeviceDatabaseService.GetByLocation(group.HomeId, groupId);
|
||||||
|
|
||||||
return new OkObjectResult(group.ToDTO(devices.Select(d => d.ToDTO()).ToList()));
|
return new OkObjectResult(group.ToDTO(devices.Select(d => d.ToDTO()).ToList()));
|
||||||
|
|
||||||
@ -113,20 +113,20 @@ namespace MyCore.Service.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get list of group from a type
|
/// Get list of group from a type
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">user Id</param>
|
/// <param name="homeId">home Id</param>
|
||||||
/// <param name="type">group type</param>
|
/// <param name="type">group type</param>
|
||||||
[ProducesResponseType(typeof(List<GroupSummaryDTO>), 200)]
|
[ProducesResponseType(typeof(List<GroupSummaryDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}/type/{type}")]
|
[HttpGet("{homeId}/type/{type}")]
|
||||||
public ObjectResult GetGroupsByType(string userId, string type) // Change string type to enum ? Lights etc ?
|
public ObjectResult GetGroupsByType(string homeId, string type) // Change string type to enum ? Lights etc ?
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null || type == null)
|
if (homeId == null || type == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
List<Group> groups = _GroupDatabaseService.GetByType(userId, type);
|
List<Group> groups = _GroupDatabaseService.GetByType(homeId, type);
|
||||||
|
|
||||||
return new OkObjectResult(groups.Select(d => d.ToSummaryDTO()));
|
return new OkObjectResult(groups.Select(d => d.ToSummaryDTO()));
|
||||||
}
|
}
|
||||||
@ -143,21 +143,21 @@ namespace MyCore.Service.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all zigbee2Mqtt groups
|
/// Get all zigbee2Mqtt groups
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">User Id</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(List<GroupDetailDTO>), 200)]
|
[ProducesResponseType(typeof(List<GroupDetailDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("zigbee2Mqtt/{userId}")]
|
[HttpGet("zigbee2Mqtt/{homeId}")]
|
||||||
public ObjectResult GetGroupsFromZigbee2Mqtt(string userId)
|
public ObjectResult GetGroupsFromZigbee2Mqtt(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
if (!UserService.IsExist(_UserDatabaseService, userId))
|
if (!HomeService.IsExist(_HomeDatabaseService, homeId))
|
||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("Home not found");
|
||||||
|
|
||||||
// GET ALL LOCAL GROUPS
|
// GET ALL LOCAL GROUPS
|
||||||
var groups = MqttClientService.groups; // Be carefull, we only got the exact result after each connection
|
var groups = MqttClientService.groups; // Be carefull, we only got the exact result after each connection
|
||||||
@ -193,7 +193,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (groupCreateOrUpdateDetail == null)
|
if (groupCreateOrUpdateDetail == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
GroupDetailDTO groupCreated = GroupService.CreateOrUpdate(this._GroupDatabaseService, this._DeviceDatabaseService, groupCreateOrUpdateDetail.UserId, groupCreateOrUpdateDetail, true);
|
GroupDetailDTO groupCreated = GroupService.CreateOrUpdate(this._GroupDatabaseService, this._DeviceDatabaseService, groupCreateOrUpdateDetail.HomeId, groupCreateOrUpdateDetail, true);
|
||||||
|
|
||||||
return new OkObjectResult(groupCreated);
|
return new OkObjectResult(groupCreated);
|
||||||
}
|
}
|
||||||
@ -210,30 +210,30 @@ namespace MyCore.Service.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create groups from provider
|
/// Create groups from provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">User Id</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(List<GroupDetailDTO>), 200)]
|
[ProducesResponseType(typeof(List<GroupDetailDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpPost("{userId}/fromZigbee")]
|
[HttpPost("{homeId}/fromZigbee")]
|
||||||
public ObjectResult CreateDevicesFromZigbee2Mqtt(string userId)
|
public ObjectResult CreateDevicesFromZigbee2Mqtt(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
if (!UserService.IsExist(_UserDatabaseService, userId))
|
if (!HomeService.IsExist(_HomeDatabaseService, homeId))
|
||||||
throw new KeyNotFoundException("User not found");
|
throw new KeyNotFoundException("Home not found");
|
||||||
|
|
||||||
Provider provider = _ProviderDatabaseService.GetByType("zigbee2mqtt");
|
Provider provider = _ProviderDatabaseService.GetByType(ProviderType.zigbee2mqtt);
|
||||||
if (provider == null)
|
if (provider == null)
|
||||||
throw new KeyNotFoundException("Zigbee2mqtt provider not found");
|
throw new KeyNotFoundException("Zigbee2mqtt provider not found");
|
||||||
|
|
||||||
// GET ALL LOCAL GROUPS
|
// GET ALL LOCAL GROUPS
|
||||||
var groups = MqttClientService.groups; // Be carefull, we only got the exact result after each connection
|
var groups = MqttClientService.groups; // Be carefull, we only got the exact result after each connection
|
||||||
|
|
||||||
List<GroupDetailDTO> groupsDetail= GroupService.CreateFromZigbeeAsync(_GroupDatabaseService, _DeviceDatabaseService, userId, groups);
|
List<GroupDetailDTO> groupsDetail= GroupService.CreateFromZigbeeAsync(_GroupDatabaseService, _DeviceDatabaseService, homeId, groups);
|
||||||
|
|
||||||
return new OkObjectResult(groupsDetail);
|
return new OkObjectResult(groupsDetail);
|
||||||
}
|
}
|
||||||
@ -270,7 +270,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (!_GroupDatabaseService.IsExist(groupCreateOrUpdateDetail.Id))
|
if (!_GroupDatabaseService.IsExist(groupCreateOrUpdateDetail.Id))
|
||||||
throw new KeyNotFoundException("Group does not exist");
|
throw new KeyNotFoundException("Group does not exist");
|
||||||
|
|
||||||
GroupDetailDTO groupUpdated = GroupService.CreateOrUpdate(this._GroupDatabaseService, this._DeviceDatabaseService, groupCreateOrUpdateDetail.UserId, groupCreateOrUpdateDetail, false);
|
GroupDetailDTO groupUpdated = GroupService.CreateOrUpdate(this._GroupDatabaseService, this._DeviceDatabaseService, groupCreateOrUpdateDetail.HomeId, groupCreateOrUpdateDetail, false);
|
||||||
|
|
||||||
return new OkObjectResult(groupUpdated);
|
return new OkObjectResult(groupUpdated);
|
||||||
}
|
}
|
||||||
@ -360,7 +360,7 @@ namespace MyCore.Service.Controllers
|
|||||||
|
|
||||||
Group group = _GroupDatabaseService.GetById(groupId);
|
Group group = _GroupDatabaseService.GetById(groupId);
|
||||||
// Delete group from all devices
|
// Delete group from all devices
|
||||||
List<Device> devices = _DeviceDatabaseService.GetByLocation(group.UserId, groupId);
|
List<Device> devices = _DeviceDatabaseService.GetByLocation(group.HomeId, groupId);
|
||||||
foreach (var device in devices)
|
foreach (var device in devices)
|
||||||
{
|
{
|
||||||
device.GroupIds = device.GroupIds.Where(g => g != groupId).ToList();
|
device.GroupIds = device.GroupIds.Where(g => g != groupId).ToList();
|
||||||
@ -387,27 +387,27 @@ namespace MyCore.Service.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete all group for a specified
|
/// Delete all group for a specified home
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(string), 202)]
|
[ProducesResponseType(typeof(string), 202)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpDelete("user/{userId}")]
|
[HttpDelete("home/{homeId}")]
|
||||||
public ObjectResult DeleteAllForUser(string userId)
|
public ObjectResult DeleteAllForHome(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
if (!_UserDatabaseService.IsExist(userId))
|
if (!_HomeDatabaseService.IsExist(homeId))
|
||||||
throw new KeyNotFoundException("User does not exist");
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
_GroupDatabaseService.RemoveForUser(userId);
|
_GroupDatabaseService.RemoveForHome(homeId);
|
||||||
|
|
||||||
return new ObjectResult("All group linked with specified user has been successfully deleted") { StatusCode = 202 };
|
return new ObjectResult("All group linked with specified home has been successfully deleted") { StatusCode = 202 };
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
251
MyCore/Controllers/HomeController.cs
Normal file
251
MyCore/Controllers/HomeController.cs
Normal file
@ -0,0 +1,251 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Security.Authentication;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using Mqtt.Client.AspNetCore.Services;
|
||||||
|
using MyCore.Interfaces.DTO;
|
||||||
|
using MyCore.Interfaces.Models;
|
||||||
|
using MyCore.Services;
|
||||||
|
using MyCore.Services.Devices;
|
||||||
|
using MyCore.Services.MyControlPanel;
|
||||||
|
|
||||||
|
namespace MyCore.Service.Controllers
|
||||||
|
{
|
||||||
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||||
|
[Route("api/home")]
|
||||||
|
[ApiController]
|
||||||
|
public class HomeController : ControllerBase
|
||||||
|
{
|
||||||
|
private HomeDatabaseService _HomeDatabaseService;
|
||||||
|
private AlarmDatabaseService _AlarmDatabaseService;
|
||||||
|
private RoomDatabaseService _RoomDatabaseService;
|
||||||
|
private DeviceDatabaseService _DeviceDatabaseService;
|
||||||
|
private ProviderDatabaseService _ProviderDatabaseService;
|
||||||
|
private UserDatabaseService _UserDatabaseService;
|
||||||
|
private GroupDatabaseService _GroupDatabaseService;
|
||||||
|
private AutomationDatabaseService _AutomationDatabaseService;
|
||||||
|
private readonly IMqttClientService _mqttClientService;
|
||||||
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
|
public HomeController(HomeDatabaseService homeDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, ProviderDatabaseService providerDatabaseService, UserDatabaseService userDatabaseService, AutomationDatabaseService automationDatabaseService, GroupDatabaseService groupDatabaseService, AlarmDatabaseService alarmDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
|
{
|
||||||
|
this._HomeDatabaseService = homeDatabaseService;
|
||||||
|
this._RoomDatabaseService = roomDatabaseService;
|
||||||
|
this._DeviceDatabaseService = deviceDatabaseService;
|
||||||
|
this._ProviderDatabaseService = providerDatabaseService;
|
||||||
|
this._UserDatabaseService = userDatabaseService;
|
||||||
|
this._AutomationDatabaseService = automationDatabaseService;
|
||||||
|
this._GroupDatabaseService = groupDatabaseService;
|
||||||
|
this._AlarmDatabaseService = alarmDatabaseService;
|
||||||
|
this._mqttClientService = provider.MqttClientService;
|
||||||
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get all home for specified user
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">User Id</param>
|
||||||
|
[ProducesResponseType(typeof(List<HomeDTO>), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpGet("{userId}")]
|
||||||
|
public ObjectResult GetAll(string userId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (userId == null)
|
||||||
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
|
UserInfo user = _UserDatabaseService.GetById(userId);
|
||||||
|
if (user == null)
|
||||||
|
throw new KeyNotFoundException("User does not exist");
|
||||||
|
|
||||||
|
List<HomeDTO> HomesResult = new List<HomeDTO>();
|
||||||
|
foreach (var homeId in user.HomeIds)
|
||||||
|
{
|
||||||
|
Home home = _HomeDatabaseService.GetById(homeId);
|
||||||
|
HomesResult.Add(home.ToDTO());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new OkObjectResult(HomesResult);
|
||||||
|
}
|
||||||
|
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>
|
||||||
|
/// Get detail info of a specified home
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="homeId">home id</param>
|
||||||
|
[ProducesResponseType(typeof(HomeDetailDTO), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpGet("detail/{homeId}")]
|
||||||
|
public ObjectResult GetDetail(string homeId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (homeId == null)
|
||||||
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
|
Home home = _HomeDatabaseService.GetById(homeId);
|
||||||
|
if (home == null)
|
||||||
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
|
List<UserInfo> users = _UserDatabaseService.GetByHomeId(home.Id);
|
||||||
|
List<Device> devices = _DeviceDatabaseService.GetByHomeId(home.Id);
|
||||||
|
List<Automation> automations = _AutomationDatabaseService.GetByHomeId(home.Id);
|
||||||
|
List<Provider> providers = _ProviderDatabaseService.GetByHomeId(home.Id);
|
||||||
|
List<Group> groups = _GroupDatabaseService.GetByHomeId(home.Id);
|
||||||
|
|
||||||
|
return new OkObjectResult(home.ToDetailDTO(users, devices, automations, providers, groups));
|
||||||
|
}
|
||||||
|
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>
|
||||||
|
/// Create a home
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="createOrUpdateHomeDTO">Home to create</param>
|
||||||
|
[ProducesResponseType(typeof(HomeDTO), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpPost]
|
||||||
|
public ObjectResult Create([FromBody] CreateOrUpdateHomeDTO createOrUpdateHomeDTO)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (createOrUpdateHomeDTO == null)
|
||||||
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
|
if (createOrUpdateHomeDTO.UsersIds.Count <= 0 && _UserDatabaseService.IsExistMultiple(createOrUpdateHomeDTO.UsersIds))
|
||||||
|
throw new ArgumentNullException("To create an home, you need at least one user");
|
||||||
|
|
||||||
|
Home homeCreated = HomeService.CreateOrUpdate(this._HomeDatabaseService, this._AlarmDatabaseService, createOrUpdateHomeDTO, true);
|
||||||
|
|
||||||
|
foreach (var userId in createOrUpdateHomeDTO.UsersIds)
|
||||||
|
{
|
||||||
|
UserInfo user = _UserDatabaseService.GetById(userId);
|
||||||
|
if (user.HomeIds == null)
|
||||||
|
user.HomeIds = new List<string>();
|
||||||
|
|
||||||
|
if (!user.HomeIds.Contains(homeCreated.Id))
|
||||||
|
user.HomeIds.Add(homeCreated.Id);
|
||||||
|
_UserDatabaseService.Update(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new OkObjectResult(homeCreated.ToDTO());
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ex)
|
||||||
|
{
|
||||||
|
return new BadRequestObjectResult(ex.Message) { };
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update a home
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="createOrUpdateHomeDTO">Home to update</param>
|
||||||
|
[ProducesResponseType(typeof(HomeDTO), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpPut]
|
||||||
|
public ObjectResult Update([FromBody] CreateOrUpdateHomeDTO createOrUpdateHomeDTO)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!_HomeDatabaseService.IsExist(createOrUpdateHomeDTO.Id))
|
||||||
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
|
if (createOrUpdateHomeDTO.UsersIds.Count <= 0)
|
||||||
|
throw new ArgumentNullException("To create an home, you need at least one user");
|
||||||
|
|
||||||
|
Home homeUpdated = HomeService.CreateOrUpdate(this._HomeDatabaseService, this._AlarmDatabaseService, createOrUpdateHomeDTO, false);
|
||||||
|
|
||||||
|
return new OkObjectResult(homeUpdated.ToDTO());
|
||||||
|
}
|
||||||
|
catch (KeyNotFoundException ex)
|
||||||
|
{
|
||||||
|
return new NotFoundObjectResult(ex.Message) {};
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Delete a home
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="homeId">Id of home to delete</param>
|
||||||
|
[ProducesResponseType(typeof(string), 202)]
|
||||||
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
|
[HttpDelete("{homeId}")]
|
||||||
|
public ObjectResult Delete(string homeId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (homeId == null)
|
||||||
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
|
if (!_HomeDatabaseService.IsExist(homeId))
|
||||||
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
|
Home home = _HomeDatabaseService.GetById(homeId);
|
||||||
|
// Check if something needs to be done.. (delete all devices related to home, all automations, all groups, etc)
|
||||||
|
// Delete home
|
||||||
|
_HomeDatabaseService.Remove(homeId);
|
||||||
|
|
||||||
|
return new OkObjectResult("Home has been successfully deleted") { StatusCode = 202 };
|
||||||
|
}
|
||||||
|
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 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -93,11 +93,16 @@ namespace MyCore.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// TODO Add check for password
|
||||||
if (newUser == null)
|
if (newUser == null)
|
||||||
throw new ArgumentNullException("User param is null");
|
throw new ArgumentNullException("User param is null");
|
||||||
|
|
||||||
|
if (newUser.Password == null || newUser.Email == null)
|
||||||
|
throw new ArgumentNullException("You need to specified email and password at least");
|
||||||
|
|
||||||
newUser.Token = _tokenService.GenerateToken(newUser.Email).ToString();
|
newUser.Token = _tokenService.GenerateToken(newUser.Email).ToString();
|
||||||
newUser.DateCreation = DateTime.Now;
|
newUser.DateCreation = DateTime.Now;
|
||||||
|
newUser.HomeIds = new List<string>();
|
||||||
|
|
||||||
List<UserInfo> users = _userService.GetAll();
|
List<UserInfo> users = _userService.GetAll();
|
||||||
|
|
||||||
@ -144,7 +149,7 @@ namespace MyCore.Controllers
|
|||||||
if (user == null)
|
if (user == null)
|
||||||
throw new KeyNotFoundException("User does not exist");
|
throw new KeyNotFoundException("User does not exist");
|
||||||
|
|
||||||
UserInfo userModified = _userService.Update(updatedUser.Id, updatedUser);
|
UserInfo userModified = _userService.Update(updatedUser);
|
||||||
|
|
||||||
return new OkObjectResult(userModified.ToDTO());
|
return new OkObjectResult(userModified.ToDTO());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,15 +22,15 @@ namespace MyCore.Service.Controllers
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
public class RoomController : ControllerBase
|
public class RoomController : ControllerBase
|
||||||
{
|
{
|
||||||
private UserDatabaseService _UserDatabaseService;
|
private HomeDatabaseService _HomeDatabaseService;
|
||||||
private RoomDatabaseService _RoomDatabaseService;
|
private RoomDatabaseService _RoomDatabaseService;
|
||||||
private DeviceDatabaseService _DeviceDatabaseService;
|
private DeviceDatabaseService _DeviceDatabaseService;
|
||||||
private readonly IMqttClientService _mqttClientService;
|
private readonly IMqttClientService _mqttClientService;
|
||||||
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||||
|
|
||||||
public RoomController(UserDatabaseService userDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
public RoomController(HomeDatabaseService homeDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
||||||
{
|
{
|
||||||
this._UserDatabaseService = userDatabaseService;
|
this._HomeDatabaseService = homeDatabaseService;
|
||||||
this._RoomDatabaseService = roomDatabaseService;
|
this._RoomDatabaseService = roomDatabaseService;
|
||||||
this._DeviceDatabaseService = deviceDatabaseService;
|
this._DeviceDatabaseService = deviceDatabaseService;
|
||||||
this._mqttClientService = provider.MqttClientService;
|
this._mqttClientService = provider.MqttClientService;
|
||||||
@ -38,17 +38,17 @@ namespace MyCore.Service.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all rooms for the specified user
|
/// Get all rooms for the specified home
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(List<RoomSummaryDTO>), 200)]
|
[ProducesResponseType(typeof(List<RoomSummaryDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}")]
|
[HttpGet("{homeId}")]
|
||||||
public ObjectResult GetAll(string userId)
|
public ObjectResult GetAll(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
List<Room> Rooms = _RoomDatabaseService.GetAll(userId);
|
List<Room> Rooms = _RoomDatabaseService.GetAll(homeId);
|
||||||
|
|
||||||
List<RoomSummaryDTO> roomsSummaryDTO = Rooms.Select(d => d.ToSummaryDTO()).ToList();
|
List<RoomSummaryDTO> roomsSummaryDTO = Rooms.Select(d => d.ToSummaryDTO()).ToList();
|
||||||
|
|
||||||
@ -63,24 +63,23 @@ namespace MyCore.Service.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get detail info of a specified room
|
/// Get detail info of a specified room
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">user id</param>
|
|
||||||
/// <param name="roomId">room id</param>
|
/// <param name="roomId">room id</param>
|
||||||
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("detail/{roomId}")]
|
[HttpGet("detail/{roomId}")]
|
||||||
public ObjectResult GetDetail(string userId, string roomId)
|
public ObjectResult GetDetail(string roomId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null || roomId == null)
|
if (roomId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
Room room = _RoomDatabaseService.GetById(roomId);
|
Room room = _RoomDatabaseService.GetById(roomId);
|
||||||
if (room == null)
|
if (room == null)
|
||||||
throw new KeyNotFoundException("Room does not exist");
|
throw new KeyNotFoundException("Room does not exist");
|
||||||
List<Device> devices = _DeviceDatabaseService.GetByLocation(room.UserId, roomId);
|
List<Device> devices = _DeviceDatabaseService.GetByLocation(room.HomeId, roomId);
|
||||||
|
|
||||||
return new OkObjectResult(room.ToDTO(devices.Select(d => d.ToDTO()).ToList()));
|
return new OkObjectResult(room.ToDTO(devices.Select(d => d.ToDTO()).ToList()));
|
||||||
|
|
||||||
@ -114,7 +113,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (roomCreateOrUpdateDetail == null)
|
if (roomCreateOrUpdateDetail == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, true);
|
RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.HomeId, roomCreateOrUpdateDetail, true);
|
||||||
|
|
||||||
return new OkObjectResult(roomCreated);
|
return new OkObjectResult(roomCreated);
|
||||||
}
|
}
|
||||||
@ -143,7 +142,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id))
|
if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id))
|
||||||
throw new KeyNotFoundException("Room does not exist");
|
throw new KeyNotFoundException("Room does not exist");
|
||||||
|
|
||||||
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, false);
|
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.HomeId, roomCreateOrUpdateDetail, false);
|
||||||
|
|
||||||
return new OkObjectResult(roomUpdated);
|
return new OkObjectResult(roomUpdated);
|
||||||
}
|
}
|
||||||
@ -189,7 +188,7 @@ namespace MyCore.Service.Controllers
|
|||||||
if (device == null)
|
if (device == null)
|
||||||
throw new KeyNotFoundException("Device does not exist");
|
throw new KeyNotFoundException("Device does not exist");
|
||||||
|
|
||||||
device.LocationId = null;
|
device.RoomId = null;
|
||||||
device.UpdatedDate = DateTime.Now;
|
device.UpdatedDate = DateTime.Now;
|
||||||
_DeviceDatabaseService.Update(device);
|
_DeviceDatabaseService.Update(device);
|
||||||
|
|
||||||
@ -231,10 +230,10 @@ namespace MyCore.Service.Controllers
|
|||||||
|
|
||||||
Room room = _RoomDatabaseService.GetById(roomId);
|
Room room = _RoomDatabaseService.GetById(roomId);
|
||||||
// Delete location from all devices
|
// Delete location from all devices
|
||||||
List<Device> devices = _DeviceDatabaseService.GetByLocation(room.UserId, roomId);
|
List<Device> devices = _DeviceDatabaseService.GetByLocation(room.HomeId, roomId);
|
||||||
foreach (var device in devices)
|
foreach (var device in devices)
|
||||||
{
|
{
|
||||||
device.LocationId = null;
|
device.RoomId = null;
|
||||||
device.UpdatedDate = DateTime.Now;
|
device.UpdatedDate = DateTime.Now;
|
||||||
_DeviceDatabaseService.Update(device);
|
_DeviceDatabaseService.Update(device);
|
||||||
}
|
}
|
||||||
@ -258,27 +257,27 @@ namespace MyCore.Service.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete all room for a specified user
|
/// Delete all room for a specified home
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="homeId">Home Id</param>
|
||||||
[ProducesResponseType(typeof(string), 202)]
|
[ProducesResponseType(typeof(string), 202)]
|
||||||
[ProducesResponseType(typeof(string), 400)]
|
[ProducesResponseType(typeof(string), 400)]
|
||||||
[ProducesResponseType(typeof(string), 404)]
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpDelete("user/{userId}")]
|
[HttpDelete("home/{homeId}")]
|
||||||
public ObjectResult DeleteAllForUser(string userId)
|
public ObjectResult DeleteAllForHome(string homeId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (userId == null)
|
if (homeId == null)
|
||||||
throw new ArgumentNullException("Incorrect parameters");
|
throw new ArgumentNullException("Incorrect parameters");
|
||||||
|
|
||||||
if (!_UserDatabaseService.IsExist(userId))
|
if (!_HomeDatabaseService.IsExist(homeId))
|
||||||
throw new KeyNotFoundException("User does not exist");
|
throw new KeyNotFoundException("Home does not exist");
|
||||||
|
|
||||||
_RoomDatabaseService.RemoveForUser(userId);
|
_RoomDatabaseService.RemoveForHome(homeId);
|
||||||
|
|
||||||
return new OkObjectResult("All room associated to specified user has been removed") { StatusCode = 202 };
|
return new OkObjectResult("All room associated to specified home has been removed") { StatusCode = 202 };
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -3,12 +3,17 @@ using MQTTnet.Client;
|
|||||||
using MQTTnet.Client.Connecting;
|
using MQTTnet.Client.Connecting;
|
||||||
using MQTTnet.Client.Disconnecting;
|
using MQTTnet.Client.Disconnecting;
|
||||||
using MQTTnet.Client.Options;
|
using MQTTnet.Client.Options;
|
||||||
|
using MyCore.Interfaces.DTO;
|
||||||
using MyCore.Interfaces.Models;
|
using MyCore.Interfaces.Models;
|
||||||
|
using MyCore.Interfaces.Models.Providers.Zigbee.Zigbee2Mqtt;
|
||||||
|
using MyCore.Service.Services;
|
||||||
|
using MyCore.Services;
|
||||||
using MyCore.Services.Devices;
|
using MyCore.Services.Devices;
|
||||||
using MyCore.Services.MyControlPanel;
|
using MyCore.Services.MyControlPanel;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -19,29 +24,44 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
{
|
{
|
||||||
private static IMqttClient mqttClient;
|
private static IMqttClient mqttClient;
|
||||||
private IMqttClientOptions options;
|
private IMqttClientOptions options;
|
||||||
public static List<Zigbee2MqttDevice> devices = new List<Zigbee2MqttDevice>();
|
public static List<Zigbee2MqttDeviceConfig> devices = new List<Zigbee2MqttDeviceConfig>();
|
||||||
public static List<Zigbee2MqttDevice> devicesNew = new List<Zigbee2MqttDevice>();
|
public static List<Zigbee2MqttDevice> devicesNew = new List<Zigbee2MqttDevice>();
|
||||||
public static List<Zigbee2MqttGroup> groups = new List<Zigbee2MqttGroup>();
|
public static List<Zigbee2MqttGroup> groups = new List<Zigbee2MqttGroup>();
|
||||||
public static string userId;
|
public static string homeId;
|
||||||
static DeviceDatabaseService _deviceDatabaseService;
|
private DeviceDatabaseService _deviceDatabaseService;
|
||||||
static GroupDatabaseService _groupDatabaseService;
|
private GroupDatabaseService _groupDatabaseService;
|
||||||
static ProviderDatabaseService _providerDatabaseService;
|
private ProviderDatabaseService _providerDatabaseService;
|
||||||
static LocationDatabaseService _locationDatabaseService;
|
private RoomDatabaseService _roomDatabaseService;
|
||||||
static AutomationDatabaseService _automationDatabaseService;
|
private AutomationDatabaseService _automationDatabaseService;
|
||||||
static ActionService _actionService;
|
private ActionService _actionService;
|
||||||
|
private HomeDatabaseService _homeDatabaseService;
|
||||||
|
|
||||||
public static string lastTopic;
|
public static string lastTopic;
|
||||||
public static long lastTimeTopic;
|
public static long lastTimeTopic;
|
||||||
|
|
||||||
public MqttClientService(IMqttClientOptions options)
|
public MqttClientService(IMqttClientOptions options, HomeDatabaseService homeDatabaseService, DeviceDatabaseService deviceDatabaseService, GroupDatabaseService groupDatabaseService, ProviderDatabaseService providerDatabaseService, RoomDatabaseService roomDatabaseService, ActionService actionService, AutomationDatabaseService automationDatabaseService)
|
||||||
{
|
{
|
||||||
|
this._homeDatabaseService = homeDatabaseService;
|
||||||
|
this._deviceDatabaseService = deviceDatabaseService;
|
||||||
|
this._groupDatabaseService = groupDatabaseService;
|
||||||
|
this._providerDatabaseService = providerDatabaseService;
|
||||||
|
this._roomDatabaseService = roomDatabaseService;
|
||||||
|
this._automationDatabaseService = automationDatabaseService;
|
||||||
|
this._actionService = actionService;
|
||||||
|
|
||||||
|
Home home = this._homeDatabaseService.GetAll().ToList().Where(h => h.IsDefault).FirstOrDefault();
|
||||||
|
if (home != null)
|
||||||
|
homeId = home.Id;
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Home Id in MQTT client service : {homeId}");
|
||||||
|
|
||||||
var server = "localhost";
|
var server = "localhost";
|
||||||
var clientId = "ApiService";
|
var clientId = "ApiService";
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
server = "192.168.31.140";
|
server = "192.168.31.140";
|
||||||
clientId = "ApiServiceTest";
|
clientId = "ApiServiceTest";
|
||||||
#endif
|
#endif
|
||||||
this.options = options;
|
//this.options = options;
|
||||||
this.options = new MqttClientOptionsBuilder()
|
this.options = new MqttClientOptionsBuilder()
|
||||||
.WithClientId(clientId)
|
.WithClientId(clientId)
|
||||||
.WithTcpServer(server)
|
.WithTcpServer(server)
|
||||||
@ -93,9 +113,9 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
// Less than one second between two messages from a same device
|
// Less than one second between two messages from a same device
|
||||||
if (!(lastTopic == topic && test <= 500) || configMessage)
|
if (!(lastTopic == topic && test <= 500) || configMessage)
|
||||||
{
|
{
|
||||||
if (_actionService != null)
|
if (_actionService != null && homeId != null)
|
||||||
{
|
{
|
||||||
ActionService.HandleActionFromMQTTAsync(topic, payload, _deviceDatabaseService, _groupDatabaseService, _providerDatabaseService, _locationDatabaseService, _automationDatabaseService, userId);
|
ActionService.HandleActionFromMQTTAsync(topic, payload, _deviceDatabaseService, _groupDatabaseService, _providerDatabaseService, _roomDatabaseService, _automationDatabaseService, homeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -104,12 +124,14 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
System.Console.WriteLine($"Drop message - spam from {topic}");
|
System.Console.WriteLine($"Drop message - spam from {topic}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UpdateZigbee2MqttConfigOrStateAsync(topic, payload, homeId, _deviceDatabaseService, _groupDatabaseService, _providerDatabaseService, _roomDatabaseService);
|
||||||
|
|
||||||
switch (topic)
|
switch (topic)
|
||||||
{
|
{
|
||||||
case "zigbee2mqtt/bridge/config/devices":
|
case "zigbee2mqtt/bridge/config/devices":
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var devicesConvert = JsonConvert.DeserializeObject<List<Zigbee2MqttDevice>>(payload);
|
var devicesConvert = JsonConvert.DeserializeObject<List<Zigbee2MqttDeviceConfig>>(payload);
|
||||||
devices = devicesConvert;
|
devices = devicesConvert;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -187,28 +209,141 @@ namespace Mqtt.Client.AspNetCore.Services
|
|||||||
await mqttClient.DisconnectAsync();
|
await mqttClient.DisconnectAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task PublishMessage(string topic, string message)
|
public static async Task PublishMessage(string topic, string message, bool retain = false)
|
||||||
{
|
{
|
||||||
var mqttMessage = new MqttApplicationMessageBuilder()
|
var mqttMessage = new MqttApplicationMessageBuilder()
|
||||||
.WithTopic(topic)
|
.WithTopic(topic)
|
||||||
.WithPayload(message)
|
.WithPayload(message)
|
||||||
.WithExactlyOnceQoS()
|
.WithExactlyOnceQoS()
|
||||||
.WithRetainFlag(false)
|
.WithRetainFlag(retain)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (mqttClient.IsConnected)
|
if (mqttClient.IsConnected)
|
||||||
await mqttClient.PublishAsync(mqttMessage);
|
await mqttClient.PublishAsync(mqttMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetServices(DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, ActionService _ActionService, AutomationDatabaseService _AutomationDatabaseService, string UserId)
|
/*public static void SetServices(DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, ActionService _ActionService, AutomationDatabaseService _AutomationDatabaseService, string HomeId)
|
||||||
{
|
{
|
||||||
_deviceDatabaseService = _DeviceDatabaseService;
|
_deviceDatabaseService = _DeviceDatabaseService;
|
||||||
_groupDatabaseService = _GroupDatabaseService;
|
_groupDatabaseService = _GroupDatabaseService;
|
||||||
_providerDatabaseService = _ProviderDatabaseService;
|
_providerDatabaseService = _ProviderDatabaseService;
|
||||||
_locationDatabaseService = _LocationDatabaseService;
|
_roomDatabaseService = _RoomDatabaseService;
|
||||||
_automationDatabaseService = _AutomationDatabaseService;
|
_automationDatabaseService = _AutomationDatabaseService;
|
||||||
_actionService = _ActionService;
|
_actionService = _ActionService;
|
||||||
userId = UserId;
|
homeId = HomeId;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public static async Task UpdateZigbee2MqttConfigOrStateAsync(string topic, string message, string homeId, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService)
|
||||||
|
{
|
||||||
|
// update zigbee2mqqtt config
|
||||||
|
switch (topic)
|
||||||
|
{
|
||||||
|
case "zigbee2mqtt/bridge/config/devices":
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var devices = JsonConvert.DeserializeObject<List<Zigbee2MqttDeviceConfig>>(message);
|
||||||
|
var zigbee2mqttProvider = _ProviderDatabaseService.GetByType(ProviderType.zigbee2mqtt);
|
||||||
|
|
||||||
|
if (zigbee2mqttProvider != null)
|
||||||
|
{
|
||||||
|
// Retrieve existing devices
|
||||||
|
List<Device> existingDevices = _DeviceDatabaseService.GetByProviderId(zigbee2mqttProvider.Id);
|
||||||
|
var existingDevicesAddresses = existingDevices.Select(ed => ed.ServiceIdentification);
|
||||||
|
|
||||||
|
// Filter devices and check if something new
|
||||||
|
var filteredDevices = devices.Where(d => !existingDevicesAddresses.Contains(d.ieeeAddr)).ToList();
|
||||||
|
// Add new devices
|
||||||
|
Dictionary<string, List<DeviceDetailDTO>> createdDevices = await DeviceService.CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, filteredDevices, zigbee2mqttProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Devices updated for home {homeId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error during retrieving devices ! Exception: {ex}");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "zigbee2mqtt/bridge/devices":
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var devices = JsonConvert.DeserializeObject<List<Zigbee2MqttDevice>>(message);
|
||||||
|
var zigbee2mqttProvider = _ProviderDatabaseService.GetByType(ProviderType.zigbee2mqtt);
|
||||||
|
|
||||||
|
if (zigbee2mqttProvider != null)
|
||||||
|
{
|
||||||
|
// Retrieve existing devices
|
||||||
|
/*List<Device> existingDevices = _DeviceDatabaseService.GetByProviderId(zigbee2mqttProvider.Id);
|
||||||
|
var existingDevicesAddresses = existingDevices.Select(ed => ed.ServiceIdentification);*/
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Nbr of devices : {devices.Count}");
|
||||||
|
|
||||||
|
// Update supported operation
|
||||||
|
Dictionary<string, List<DeviceDetailDTO>> createdDevices = await DeviceService.UpdateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, devices, zigbee2mqttProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.Console.WriteLine($"Devices updated for home {homeId} - more information");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error during retrieving devices ! Exception: {ex}");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "zigbee2mqtt/bridge/groups":
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var groupsConvert = JsonConvert.DeserializeObject<List<Zigbee2MqttGroup>>(message);
|
||||||
|
|
||||||
|
var zigbee2mqttProvider = _ProviderDatabaseService.GetByType(ProviderType.zigbee2mqtt);
|
||||||
|
|
||||||
|
if (zigbee2mqttProvider != null)
|
||||||
|
{
|
||||||
|
var groups = _GroupDatabaseService.GetByType(homeId, "zigbee2mqtt");
|
||||||
|
// Compare the groups from MyCore and the group we received, if something diff in one group => Hard refresh (delete, new)
|
||||||
|
|
||||||
|
// TODO : Test with new devices !! => Id always change BUUUUUUG !!
|
||||||
|
GroupService.CompareGroupsFromZigbee2Mqtt(homeId, groups, groupsConvert, _DeviceDatabaseService, _GroupDatabaseService);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error during retrieving groups ! Exception: {ex}");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "zigbee2mqtt/bridge/event":
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var eventConvert = JsonConvert.DeserializeObject<Zigbee2MqttEvent>(message);
|
||||||
|
|
||||||
|
if (eventConvert.data?.ieee_address != null)
|
||||||
|
{
|
||||||
|
var zigbeeDevice = _DeviceDatabaseService.GetByServiceIdentification(eventConvert.data.ieee_address);
|
||||||
|
|
||||||
|
if (zigbeeDevice != null && eventConvert.type == "device_announce") // Check if we can not hardcode that..
|
||||||
|
{
|
||||||
|
var status = new List<AutomationState>();
|
||||||
|
var state = new AutomationState();
|
||||||
|
state.Name = "state";
|
||||||
|
state.Value = "on";
|
||||||
|
status.Add(state);
|
||||||
|
|
||||||
|
var buildRequest = new Dictionary<string, object>();
|
||||||
|
buildRequest.Add(state.Name, state.Value);
|
||||||
|
|
||||||
|
// Update device state !
|
||||||
|
zigbeeDevice.LastState = JsonConvert.SerializeObject(buildRequest);
|
||||||
|
zigbeeDevice.LastStateDate = DateTime.Now;
|
||||||
|
_DeviceDatabaseService.Update(zigbeeDevice);
|
||||||
|
|
||||||
|
// Check if all group has the same state // Not needed as we test the first device of a group
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error during retrieving event ! Exception: {ex}");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ using Mqtt.Client.AspNetCore.Services;
|
|||||||
using Mqtt.Client.AspNetCore.Settings;
|
using Mqtt.Client.AspNetCore.Settings;
|
||||||
using MQTTnet.Client.Options;
|
using MQTTnet.Client.Options;
|
||||||
using MyCore.Services;
|
using MyCore.Services;
|
||||||
|
using MyCore.Services.Devices;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace MyCore.Service.Extensions
|
namespace MyCore.Service.Extensions
|
||||||
@ -61,12 +62,19 @@ namespace MyCore.Service.Extensions
|
|||||||
private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action<AspCoreMqttClientOptionBuilder> configure)
|
private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action<AspCoreMqttClientOptionBuilder> configure)
|
||||||
{
|
{
|
||||||
// No need as we implement options in service (localhost)
|
// No need as we implement options in service (localhost)
|
||||||
/*services.AddSingleton<IMqttClientOptions>(serviceProvider =>
|
services.AddSingleton<IMqttClientOptions>(serviceProvider =>
|
||||||
{
|
{
|
||||||
var optionBuilder = new AspCoreMqttClientOptionBuilder(serviceProvider);
|
var optionBuilder = new AspCoreMqttClientOptionBuilder(serviceProvider);
|
||||||
configure(optionBuilder);
|
configure(optionBuilder);
|
||||||
return optionBuilder.Build();
|
return optionBuilder.Build();
|
||||||
});*/
|
});
|
||||||
|
services.AddSingleton<HomeDatabaseService>();
|
||||||
|
services.AddSingleton<DeviceDatabaseService>();
|
||||||
|
services.AddSingleton<GroupDatabaseService>();
|
||||||
|
services.AddSingleton<ProviderDatabaseService>();
|
||||||
|
services.AddSingleton<RoomDatabaseService>();
|
||||||
|
services.AddSingleton<AutomationDatabaseService>();
|
||||||
|
services.AddSingleton<ActionService>();
|
||||||
services.AddSingleton<MqttClientService>();
|
services.AddSingleton<MqttClientService>();
|
||||||
services.AddSingleton<IHostedService>(serviceProvider =>
|
services.AddSingleton<IHostedService>(serviceProvider =>
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
using MyCore.Interfaces.DTO;
|
using MyCore.Interfaces.DTO;
|
||||||
using MyCore.Interfaces.Models;
|
using MyCore.Interfaces.Models;
|
||||||
|
using MyCore.Services;
|
||||||
using MyCore.Services.MyControlPanel;
|
using MyCore.Services.MyControlPanel;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -10,7 +11,7 @@ namespace MyCore.Service.Services
|
|||||||
{
|
{
|
||||||
public class AutomationService
|
public class AutomationService
|
||||||
{
|
{
|
||||||
public static AutomationDTO CreateOrUpdate(AutomationDatabaseService _AutomationDatabaseService, string userId, AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetailDTO, bool create)
|
public static AutomationDTO CreateOrUpdate(AutomationDatabaseService _AutomationDatabaseService, string homeId, AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetailDTO, bool create)
|
||||||
{
|
{
|
||||||
Automation automation;
|
Automation automation;
|
||||||
if (create)
|
if (create)
|
||||||
@ -20,7 +21,7 @@ namespace MyCore.Service.Services
|
|||||||
automation = _AutomationDatabaseService.GetById(automationCreateOrUpdateDetailDTO.Id);
|
automation = _AutomationDatabaseService.GetById(automationCreateOrUpdateDetailDTO.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
automation.UserId = userId;
|
automation.HomeId = homeId;
|
||||||
automation.Name = automationCreateOrUpdateDetailDTO.Name;
|
automation.Name = automationCreateOrUpdateDetailDTO.Name;
|
||||||
automation.Active = automationCreateOrUpdateDetailDTO.Active;
|
automation.Active = automationCreateOrUpdateDetailDTO.Active;
|
||||||
automation.CreatedDate = create ? DateTime.Now : automation.CreatedDate;
|
automation.CreatedDate = create ? DateTime.Now : automation.CreatedDate;
|
||||||
|
|||||||
@ -20,7 +20,7 @@ namespace MyCore.Services.Devices
|
|||||||
private static Device deviceTrigger;
|
private static Device deviceTrigger;
|
||||||
private static string providerFromTopic;
|
private static string providerFromTopic;
|
||||||
private static string deviceServiceName;
|
private static string deviceServiceName;
|
||||||
public static async Task HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, AutomationDatabaseService _AutomationDatabaseService, string userId)
|
public static async Task HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, AutomationDatabaseService _AutomationDatabaseService, string homeId)
|
||||||
{
|
{
|
||||||
System.Console.WriteLine($"Received message {message}");
|
System.Console.WriteLine($"Received message {message}");
|
||||||
|
|
||||||
@ -34,13 +34,13 @@ namespace MyCore.Services.Devices
|
|||||||
|
|
||||||
switch (providerFromTopic) {
|
switch (providerFromTopic) {
|
||||||
case "zigbee2mqtt":
|
case "zigbee2mqtt":
|
||||||
UpdateZigbee2MqttConfigOrStateAsync(topic, message, userId, _DeviceDatabaseService, _GroupDatabaseService, _ProviderDatabaseService, _LocationDatabaseService);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
currentProvider = _ProviderDatabaseService.GetByType(providerFromTopic);
|
currentProvider = Enum.IsDefined(typeof(ProviderType), providerFromTopic) ? _ProviderDatabaseService.GetByType((ProviderType) Enum.Parse(typeof(ProviderType), providerFromTopic.ToLower())) : null;
|
||||||
|
|
||||||
if (currentProvider != null)
|
if (currentProvider != null)
|
||||||
{
|
{
|
||||||
@ -107,7 +107,7 @@ namespace MyCore.Services.Devices
|
|||||||
{
|
{
|
||||||
case ActionType.DEVICE:
|
case ActionType.DEVICE:
|
||||||
var deviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
var deviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
||||||
var providerActionTest = _ProviderDatabaseService.GetById(userId, action.ProviderId);
|
var providerActionTest = _ProviderDatabaseService.GetById(homeId, action.ProviderId);
|
||||||
|
|
||||||
DeviceNameForAction = deviceAction.Name;
|
DeviceNameForAction = deviceAction.Name;
|
||||||
actionDeviceToTest = deviceAction;
|
actionDeviceToTest = deviceAction;
|
||||||
@ -140,14 +140,14 @@ namespace MyCore.Services.Devices
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var providerAction = _ProviderDatabaseService.GetById(userId, action.ProviderId);
|
var providerAction = _ProviderDatabaseService.GetById(homeId, action.ProviderId);
|
||||||
|
|
||||||
// Check if device exist
|
// Check if device exist
|
||||||
if (actionDeviceToTest != null && providerAction != null)
|
if (actionDeviceToTest != null && providerAction != null)
|
||||||
{
|
{
|
||||||
switch (providerAction.Type)
|
switch (providerAction.Type)
|
||||||
{
|
{
|
||||||
case "zigbee2mqtt":
|
case ProviderType.zigbee2mqtt:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ActionOnZigbee2Mqtt(actionDeviceToTest, action, DeviceNameForAction);
|
ActionOnZigbee2Mqtt(actionDeviceToTest, action, DeviceNameForAction);
|
||||||
@ -157,7 +157,7 @@ namespace MyCore.Services.Devices
|
|||||||
System.Console.WriteLine($"ActionOnZigbee2Mqtt result in error: {ex}");
|
System.Console.WriteLine($"ActionOnZigbee2Mqtt result in error: {ex}");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "meross":
|
case ProviderType.meross:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ActionOnMeross(_DeviceDatabaseService, actionDeviceToTest, action, DeviceNameForAction);
|
ActionOnMeross(_DeviceDatabaseService, actionDeviceToTest, action, DeviceNameForAction);
|
||||||
@ -167,7 +167,7 @@ namespace MyCore.Services.Devices
|
|||||||
System.Console.WriteLine($"ActionOnMeross result in error: {ex}");
|
System.Console.WriteLine($"ActionOnMeross result in error: {ex}");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "yeelight":
|
case ProviderType.yeelight:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ActionOnYeelight(_DeviceDatabaseService, actionDeviceToTest, action);
|
ActionOnYeelight(_DeviceDatabaseService, actionDeviceToTest, action);
|
||||||
@ -414,13 +414,15 @@ namespace MyCore.Services.Devices
|
|||||||
// Comment this for test (ensure the request will be sent)
|
// Comment this for test (ensure the request will be sent)
|
||||||
/*if (dic.Count > 0)
|
/*if (dic.Count > 0)
|
||||||
{
|
{
|
||||||
if (dic["state"].ToString().ToLower() == state.Value.ToLower() && action.States.Count <= 1) // workaround if brightness not the same
|
//if (device.Type == DeviceType.Light) {}
|
||||||
|
var test = dic["state"].ToString().ToLower();
|
||||||
|
var test0 = state.Value.ToLower();
|
||||||
|
if (dic["state"].ToString().ToLower() == state.Value.ToLower() && action.States.Count <= 1) // workaround if brightness not the same => For switch or light without brightness
|
||||||
{
|
{
|
||||||
throw new Exception($"Action device is already at the good state : {state.Name} {state.Value}");
|
throw new Exception($"Action device is already at the good state : {state.Name} {state.Value}");
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
// TODO clean this
|
|
||||||
if (state.Name == "brightness")
|
if (state.Name == "brightness")
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -445,6 +447,60 @@ namespace MyCore.Services.Devices
|
|||||||
throw new Exception($"Action device light|switch does not have expose of type {state.Name}");
|
throw new Exception($"Action device light|switch does not have expose of type {state.Name}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If any action ask for state update, check if device is already at the asked state
|
||||||
|
if (action.States.Any(s => s.Name == "state") && !action.IsForce)
|
||||||
|
{
|
||||||
|
bool sendRequest = true;
|
||||||
|
|
||||||
|
// If action doesn't ask for brightness just test if device is already at the asked state
|
||||||
|
if (action.States.Any(s => s.Name == "brightness"))
|
||||||
|
{
|
||||||
|
// Check state and brightness
|
||||||
|
// if we got state value (current value)
|
||||||
|
if (dic.Any(d => d.Key == "state"))
|
||||||
|
{
|
||||||
|
var currentValue = dic["state"].ToString().ToLower();
|
||||||
|
var askedValue = action.States.FirstOrDefault(s => s.Name == "state").Value.ToLower();
|
||||||
|
if (currentValue == askedValue)
|
||||||
|
{
|
||||||
|
// Check brightness difference
|
||||||
|
if (dic.Any(d => d.Key == "brightness"))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var currentValueBrightness = int.Parse(dic["brightness"].ToString().ToLower());
|
||||||
|
var askedValueBrightness = int.Parse(action.States.FirstOrDefault(s => s.Name == "brightness").Value.ToLower());
|
||||||
|
if (Math.Abs(currentValueBrightness - askedValueBrightness) <= 5) // brightness diff is lower than 5 => don't change brightness
|
||||||
|
sendRequest = false;
|
||||||
|
else
|
||||||
|
sendRequest = true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
sendRequest = false;
|
||||||
|
System.Console.WriteLine($"int parse error in brightness check", ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Check only state value
|
||||||
|
// if we got state value (current value)
|
||||||
|
if (dic.Any(d => d.Key == "state"))
|
||||||
|
{
|
||||||
|
var currentValue = dic["state"].ToString().ToLower();
|
||||||
|
var askedValue = action.States.FirstOrDefault(s => s.Name == "state").Value.ToLower();
|
||||||
|
if (currentValue == askedValue)
|
||||||
|
sendRequest = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sendRequest)
|
||||||
|
throw new Exception($"Action device is already at the good state");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw new Exception("Action device does not have expose of type light|switch");
|
throw new Exception("Action device does not have expose of type light|switch");
|
||||||
@ -465,7 +521,7 @@ namespace MyCore.Services.Devices
|
|||||||
request = JsonConvert.SerializeObject(buildRequest);
|
request = JsonConvert.SerializeObject(buildRequest);
|
||||||
|
|
||||||
// SEND REQUEST
|
// SEND REQUEST
|
||||||
var requestToSend = $"Send request ! zigbee2mqtt/{deviceNameForAction}/set/{request}";
|
//var requestToSend = $"Send request ! zigbee2mqtt/{deviceNameForAction}/set/{request}";
|
||||||
System.Console.WriteLine($"Send request ! zigbee2mqtt/{deviceNameForAction}/set/{request}");
|
System.Console.WriteLine($"Send request ! zigbee2mqtt/{deviceNameForAction}/set/{request}");
|
||||||
|
|
||||||
MqttClientService.PublishMessage("zigbee2mqtt/" + deviceNameForAction + "/set", request);
|
MqttClientService.PublishMessage("zigbee2mqtt/" + deviceNameForAction + "/set", request);
|
||||||
@ -573,90 +629,5 @@ namespace MyCore.Services.Devices
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task UpdateZigbee2MqttConfigOrStateAsync(string topic, string message, string userId, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService) {
|
|
||||||
// update zigbee2mqqtt config
|
|
||||||
switch (topic)
|
|
||||||
{
|
|
||||||
case "zigbee2mqtt/bridge/config/devices":
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var devices = JsonConvert.DeserializeObject<List<Zigbee2MqttDevice>>(message);
|
|
||||||
var zigbee2mqttProvider = _ProviderDatabaseService.GetByType("zigbee2mqtt");
|
|
||||||
|
|
||||||
if (zigbee2mqttProvider != null)
|
|
||||||
{
|
|
||||||
// Retrieve existing devices
|
|
||||||
List<Device> existingDevices = _DeviceDatabaseService.GetByProviderId(zigbee2mqttProvider.Id);
|
|
||||||
var existingDevicesAddresses = existingDevices.Select(ed => ed.ServiceIdentification);
|
|
||||||
|
|
||||||
// Filter devices and check if something new
|
|
||||||
var filteredDevices = devices.Where(d => !existingDevicesAddresses.Contains(d.ieee_address)).ToList();
|
|
||||||
// Add new devices
|
|
||||||
Dictionary<string, List<DeviceDetailDTO>> createdDevices = await DeviceService.CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, filteredDevices, zigbee2mqttProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.Console.WriteLine($"Devices updated for user {userId}");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error during retrieving devices ! Exception: {ex}");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "zigbee2mqtt/bridge/groups":
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var groupsConvert = JsonConvert.DeserializeObject<List<Zigbee2MqttGroup>>(message);
|
|
||||||
|
|
||||||
var zigbee2mqttProvider = _ProviderDatabaseService.GetByType("zigbee2mqtt");
|
|
||||||
|
|
||||||
if (zigbee2mqttProvider != null)
|
|
||||||
{
|
|
||||||
var groups = _GroupDatabaseService.GetByType(userId, "zigbee2mqtt");
|
|
||||||
// Compare the groups from MyCore and the group we received, if something diff in one group => Hard refresh (delete, new)
|
|
||||||
// TODO : Test with new devices
|
|
||||||
GroupService.CompareGroupsFromZigbee2Mqtt(userId, groups, groupsConvert, _DeviceDatabaseService, _GroupDatabaseService);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error during retrieving groups ! Exception: {ex}");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "zigbee2mqtt/bridge/event":
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var eventConvert = JsonConvert.DeserializeObject<Zigbee2MqttEvent>(message);
|
|
||||||
|
|
||||||
if (eventConvert.data?.ieee_address != null) {
|
|
||||||
var zigbeeDevice = _DeviceDatabaseService.GetByServiceIdentification(eventConvert.data.ieee_address);
|
|
||||||
|
|
||||||
if (zigbeeDevice != null && eventConvert.type == "device_announce") // Check if we can not hardcode that..
|
|
||||||
{
|
|
||||||
var status = new List<AutomationState>();
|
|
||||||
var state = new AutomationState();
|
|
||||||
state.Name = "state";
|
|
||||||
state.Value = "on";
|
|
||||||
status.Add(state);
|
|
||||||
|
|
||||||
var buildRequest = new Dictionary<string, object>();
|
|
||||||
buildRequest.Add(state.Name, state.Value);
|
|
||||||
|
|
||||||
// Update device state !
|
|
||||||
zigbeeDevice.LastState = JsonConvert.SerializeObject(buildRequest);
|
|
||||||
zigbeeDevice.LastStateDate = DateTime.Now;
|
|
||||||
_DeviceDatabaseService.Update(zigbeeDevice);
|
|
||||||
|
|
||||||
// Check if all group has the same state // Not needed as we test the first device of a group
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error during retrieving event ! Exception: {ex}");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,22 +38,32 @@ namespace MyCore.Services.Devices
|
|||||||
new SupportedDevice { Manufacturer = "SONOFF", Model = "BASICZBR3", Description = "Zigbee smart switch", DeviceType = DeviceType.Switch },
|
new SupportedDevice { Manufacturer = "SONOFF", Model = "BASICZBR3", Description = "Zigbee smart switch", DeviceType = DeviceType.Switch },
|
||||||
};
|
};
|
||||||
|
|
||||||
public static DeviceDetailDTO CreateOrUpdate(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, DeviceDetailDTO deviceDetailDTO, bool create)
|
public static DeviceDetailDTO CreateOrUpdate(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, string homeId, DeviceDetailDTO deviceDetailDTO, bool create, bool isZigbee = false)
|
||||||
{
|
{
|
||||||
Device device;
|
Device device;
|
||||||
|
|
||||||
|
if (isZigbee)
|
||||||
|
{
|
||||||
|
device = _DeviceDatabaseService.GetByServiceIdentification(deviceDetailDTO.ServiceIdentification);
|
||||||
|
if (device == null)
|
||||||
|
device = new Device();
|
||||||
|
}
|
||||||
|
else {
|
||||||
if (create)
|
if (create)
|
||||||
device = new Device();
|
device = new Device();
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
device = _DeviceDatabaseService.GetById(deviceDetailDTO.Id);
|
device = _DeviceDatabaseService.GetById(deviceDetailDTO.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_DeviceDatabaseService.IsAlreadyHere(userId, deviceDetailDTO.ServiceIdentification, deviceDetailDTO.Model) && create)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
device.UserId = userId;
|
/*// What's the purpose here ?
|
||||||
|
if (_DeviceDatabaseService.IsAlreadyHere(homeId, deviceDetailDTO.ServiceIdentification, deviceDetailDTO.Model) && create)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
device.HomeId = homeId;
|
||||||
device.Name = deviceDetailDTO.Name;
|
device.Name = deviceDetailDTO.Name;
|
||||||
device.Description = deviceDetailDTO.Description;
|
device.Description = deviceDetailDTO.Description;
|
||||||
device.ManufacturerName = deviceDetailDTO.ManufacturerName;
|
device.ManufacturerName = deviceDetailDTO.ManufacturerName;
|
||||||
@ -63,8 +73,8 @@ namespace MyCore.Services.Devices
|
|||||||
else
|
else
|
||||||
throw new KeyNotFoundException("Provider does not exist");
|
throw new KeyNotFoundException("Provider does not exist");
|
||||||
|
|
||||||
if (device.LocationId == null || _LocationDatabaseService.IsExist(deviceDetailDTO.LocationId))
|
if (device.RoomId == null || _RoomDatabaseService.IsExist(deviceDetailDTO.RoomId))
|
||||||
device.LocationId = deviceDetailDTO.LocationId;
|
device.RoomId = deviceDetailDTO.RoomId;
|
||||||
else
|
else
|
||||||
throw new KeyNotFoundException("Location does not exist");
|
throw new KeyNotFoundException("Location does not exist");
|
||||||
|
|
||||||
@ -79,7 +89,7 @@ namespace MyCore.Services.Devices
|
|||||||
else
|
else
|
||||||
device.ConnectionStatus = deviceDetailDTO.ConnectionStatus;
|
device.ConnectionStatus = deviceDetailDTO.ConnectionStatus;
|
||||||
device.Status = deviceDetailDTO.Status;
|
device.Status = deviceDetailDTO.Status;
|
||||||
device.LocationId = deviceDetailDTO.LocationId;
|
device.RoomId = deviceDetailDTO.RoomId;
|
||||||
device.CreatedDate = DateTime.Now;
|
device.CreatedDate = DateTime.Now;
|
||||||
device.UpdatedDate = DateTime.Now;
|
device.UpdatedDate = DateTime.Now;
|
||||||
device.LastState = deviceDetailDTO.LastState;
|
device.LastState = deviceDetailDTO.LastState;
|
||||||
@ -102,33 +112,38 @@ namespace MyCore.Services.Devices
|
|||||||
return _DeviceDatabaseService.Update(device).ToDTO();
|
return _DeviceDatabaseService.Update(device).ToDTO();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async static Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromProvider(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, Provider provider)
|
public async static Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromProvider(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, string homeId, Provider provider)
|
||||||
{
|
{
|
||||||
if (!ProviderService.IsProviderSupported(provider.Type))
|
|
||||||
throw new KeyNotFoundException("Provider is not yet supported");
|
|
||||||
|
|
||||||
Dictionary<string, List<DeviceDetailDTO>> devices = new Dictionary<string, List<DeviceDetailDTO>>();
|
Dictionary<string, List<DeviceDetailDTO>> devices = new Dictionary<string, List<DeviceDetailDTO>>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
switch (provider.Type)
|
switch (provider.Type)
|
||||||
{
|
{
|
||||||
case "arlo":
|
case ProviderType.arlo:
|
||||||
List<ArloDevice> arloDevices = new ArloService(provider.Username, provider.Password).GetAllDevices();
|
List<ArloDevice> arloDevices = new ArloService(provider.Username, provider.Password).GetAllDevices();
|
||||||
devices = CreateArloDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, arloDevices, provider);
|
devices = CreateArloDevices(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, arloDevices, provider);
|
||||||
break;
|
break;
|
||||||
case "meross":
|
case ProviderType.meross:
|
||||||
List<MerossDevice> merossDevices = MqttClientMerossService.GetMerossDevices(); // TO TEST IF IT WORKS
|
List<MerossDevice> merossDevices = MqttClientMerossService.GetMerossDevices(); // TO TEST IF IT WORKS
|
||||||
devices = CreateMerossDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, merossDevices, provider);
|
devices = CreateMerossDevices(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, merossDevices, provider);
|
||||||
break;
|
break;
|
||||||
case "yeelight":
|
case ProviderType.yeelight:
|
||||||
List<YeelightAPI.Device> yeelightDevices = await YeelightService.GetDevices();
|
List<YeelightAPI.Device> yeelightDevices = await YeelightService.GetDevices();
|
||||||
devices = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, yeelightDevices, provider);
|
devices = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, yeelightDevices, provider);
|
||||||
break;
|
break;
|
||||||
case "zigbee2mqtt":
|
case ProviderType.zigbee2mqtt:
|
||||||
|
if (MqttClientService.devices.Count > 0)
|
||||||
|
{
|
||||||
|
// Simple list mode
|
||||||
|
List<Zigbee2MqttDeviceConfig> zigbee2MqttDevices = MqttClientService.devices;
|
||||||
|
devices = await CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, zigbee2MqttDevices, provider);
|
||||||
|
}
|
||||||
|
if (MqttClientService.devicesNew.Count > 0)
|
||||||
|
{
|
||||||
|
// Complex list mode
|
||||||
List<Zigbee2MqttDevice> zigbee2MqttDevices = MqttClientService.devicesNew;
|
List<Zigbee2MqttDevice> zigbee2MqttDevices = MqttClientService.devicesNew;
|
||||||
devices = await CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, zigbee2MqttDevices, provider);
|
devices = await UpdateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, zigbee2MqttDevices, provider);
|
||||||
/*List<Zigbee2MqttDevice> zigbee2MqttDevices = MqttClientService.devices;
|
}
|
||||||
devices = await CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, zigbee2MqttDevices, provider);*/
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -145,7 +160,7 @@ namespace MyCore.Services.Devices
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Old way
|
// Old way
|
||||||
/*public static async Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Zigbee2MqttDevice> zigbee2MqttDevices, Provider provider)
|
/*public static async Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string homeId, List<Zigbee2MqttDevice> zigbee2MqttDevices, Provider provider)
|
||||||
{
|
{
|
||||||
List<DeviceDetailDTO> createdZigbeeDevices = new List<DeviceDetailDTO>();
|
List<DeviceDetailDTO> createdZigbeeDevices = new List<DeviceDetailDTO>();
|
||||||
List<DeviceDetailDTO> notSupportedZigbeeDevices = new List<DeviceDetailDTO>();
|
List<DeviceDetailDTO> notSupportedZigbeeDevices = new List<DeviceDetailDTO>();
|
||||||
@ -186,7 +201,7 @@ namespace MyCore.Services.Devices
|
|||||||
if (deviceDetailDTO.Type != DeviceType.Unknown)
|
if (deviceDetailDTO.Type != DeviceType.Unknown)
|
||||||
{
|
{
|
||||||
// Supoorted device !
|
// Supoorted device !
|
||||||
createdZigbeeDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, deviceDetailDTO, true));
|
createdZigbeeDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, homeId, deviceDetailDTO, true));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Not yet supported !
|
// Not yet supported !
|
||||||
@ -200,7 +215,7 @@ namespace MyCore.Services.Devices
|
|||||||
};
|
};
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
public static async Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Zigbee2MqttDevice> zigbee2MqttDevices, Provider provider)
|
public static async Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, string homeId, List<Zigbee2MqttDeviceConfig> zigbee2MqttDevices, Provider provider)
|
||||||
{
|
{
|
||||||
List<DeviceDetailDTO> createdZigbeeDevices = new List<DeviceDetailDTO>();
|
List<DeviceDetailDTO> createdZigbeeDevices = new List<DeviceDetailDTO>();
|
||||||
List<DeviceDetailDTO> notSupportedZigbeeDevices = new List<DeviceDetailDTO>();
|
List<DeviceDetailDTO> notSupportedZigbeeDevices = new List<DeviceDetailDTO>();
|
||||||
@ -213,23 +228,23 @@ namespace MyCore.Services.Devices
|
|||||||
//zigbee2MqttDevices = await MqttClientService.AskDevicesAsync();
|
//zigbee2MqttDevices = await MqttClientService.AskDevicesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
zigbee2MqttDevices = zigbee2MqttDevices.Where(yd => !existingDevices.Select(ed => ed.ServiceIdentification).ToList().Contains(yd.ieee_address)).ToList();
|
zigbee2MqttDevices = zigbee2MqttDevices.Where(yd => !existingDevices.Select(ed => ed.ServiceIdentification).ToList().Contains(yd.ieeeAddr)).ToList();
|
||||||
|
|
||||||
foreach (var zigbee2MqttDevice in zigbee2MqttDevices)
|
foreach (var zigbee2MqttDevice in zigbee2MqttDevices)
|
||||||
{
|
{
|
||||||
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
||||||
deviceDetailDTO.Name = zigbee2MqttDevice.friendly_name;
|
deviceDetailDTO.Name = zigbee2MqttDevice.friendly_name;
|
||||||
deviceDetailDTO.ServiceIdentification = zigbee2MqttDevice.ieee_address;
|
deviceDetailDTO.ServiceIdentification = zigbee2MqttDevice.ieeeAddr;
|
||||||
deviceDetailDTO.ProviderId = provider.Id;
|
deviceDetailDTO.ProviderId = provider.Id;
|
||||||
deviceDetailDTO.ProviderName = provider.Name;
|
deviceDetailDTO.ProviderName = provider.Name;
|
||||||
|
|
||||||
deviceDetailDTO.Description = zigbee2MqttDevice.type == "Coordinator" ? "Coordinator" : zigbee2MqttDevice.definition.description;
|
deviceDetailDTO.Description = zigbee2MqttDevice.type == "Coordinator" ? "Coordinator" : zigbee2MqttDevice.description;
|
||||||
|
|
||||||
deviceDetailDTO.Model = zigbee2MqttDevice.type == "Coordinator" ? "Coordinator" : zigbee2MqttDevice.definition.model; // Is the base to understand incoming messages !
|
deviceDetailDTO.Model = zigbee2MqttDevice.type == "Coordinator" ? "Coordinator" : zigbee2MqttDevice.model; // Is the base to understand incoming messages !
|
||||||
deviceDetailDTO.FirmwareVersion = zigbee2MqttDevice.software_build_id;
|
//deviceDetailDTO.FirmwareVersion = zigbee2MqttDevice.software_build_id;
|
||||||
|
|
||||||
deviceDetailDTO.Battery = zigbee2MqttDevice.power_source == null ? false : zigbee2MqttDevice.power_source.Contains("Battery");
|
deviceDetailDTO.Battery = zigbee2MqttDevice.powerSource == null ? false : zigbee2MqttDevice.powerSource.Contains("Battery");
|
||||||
deviceDetailDTO.ManufacturerName = zigbee2MqttDevice.definition?.vendor == null ? provider.Type : zigbee2MqttDevice.definition?.vendor.ToLower();
|
deviceDetailDTO.ManufacturerName = zigbee2MqttDevice.vendor == null ? provider.Type.ToString() : zigbee2MqttDevice.vendor.ToLower();
|
||||||
deviceDetailDTO.MeansOfCommunications = new List<MeansOfCommunication>();
|
deviceDetailDTO.MeansOfCommunications = new List<MeansOfCommunication>();
|
||||||
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Zigbee);
|
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Zigbee);
|
||||||
|
|
||||||
@ -240,22 +255,22 @@ namespace MyCore.Services.Devices
|
|||||||
{
|
{
|
||||||
// EXPOSES !
|
// EXPOSES !
|
||||||
List<string> supportedOperationsDTO = new List<string>();
|
List<string> supportedOperationsDTO = new List<string>();
|
||||||
foreach (var supportedOperation in zigbee2MqttDevice.definition?.exposes)
|
/*foreach (var supportedOperation in zigbee2MqttDevice.definition?.exposes)
|
||||||
{
|
{
|
||||||
supportedOperationsDTO.Add(JsonConvert.SerializeObject(supportedOperation));
|
supportedOperationsDTO.Add(JsonConvert.SerializeObject(supportedOperation));
|
||||||
}
|
}
|
||||||
deviceDetailDTO.SupportedOperations = supportedOperationsDTO;
|
deviceDetailDTO.SupportedOperations = supportedOperationsDTO;
|
||||||
|
|
||||||
deviceDetailDTO.Type = GetDeviceTypeFromZigbeeModel(zigbee2MqttDevice.definition.model);
|
deviceDetailDTO.Type = GetDeviceTypeFromZigbeeModel(zigbee2MqttDevice.definition.model);*/
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
deviceDetailDTO.Type = DeviceType.Gateway;
|
deviceDetailDTO.Type = DeviceType.Gateway;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zigbee2MqttDevice.supported || deviceDetailDTO.Type == DeviceType.Gateway)
|
if (true) //zigbee2MqttDevice.supported // TODO QUID
|
||||||
{
|
{
|
||||||
// Supported device !
|
// Supported device !
|
||||||
createdZigbeeDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, deviceDetailDTO, true));
|
createdZigbeeDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, deviceDetailDTO, true));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -270,6 +285,83 @@ namespace MyCore.Services.Devices
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async Task<Dictionary<string, List<DeviceDetailDTO>>> UpdateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, string homeId, List<Zigbee2MqttDevice> zigbee2MqttDevices, Provider provider)
|
||||||
|
{
|
||||||
|
List<DeviceDetailDTO> updatedZigbeeDevices = new List<DeviceDetailDTO>();
|
||||||
|
List<DeviceDetailDTO> notSupportedZigbeeDevices = new List<DeviceDetailDTO>();
|
||||||
|
|
||||||
|
List<Device> existingDevices = _DeviceDatabaseService.GetByProviderId(provider.Id);
|
||||||
|
|
||||||
|
// Not necessarry
|
||||||
|
if (zigbee2MqttDevices.Count <= 0)
|
||||||
|
{
|
||||||
|
//zigbee2MqttDevices = await MqttClientService.AskDevicesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
//zigbee2MqttDevices = zigbee2MqttDevices.Where(yd => !existingDevices.Select(ed => ed.ServiceIdentification).ToList().Contains(yd.ieee_address)).ToList();
|
||||||
|
|
||||||
|
foreach (var zigbee2MqttDevice in zigbee2MqttDevices)
|
||||||
|
{
|
||||||
|
var existingDevice = _DeviceDatabaseService.GetByServiceIdentification(zigbee2MqttDevice.ieee_address);
|
||||||
|
|
||||||
|
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
||||||
|
deviceDetailDTO.Name = zigbee2MqttDevice.friendly_name;
|
||||||
|
deviceDetailDTO.ServiceIdentification = zigbee2MqttDevice.ieee_address;
|
||||||
|
deviceDetailDTO.ProviderId = provider.Id;
|
||||||
|
deviceDetailDTO.ProviderName = provider.Name;
|
||||||
|
|
||||||
|
deviceDetailDTO.Description = zigbee2MqttDevice.type == "Coordinator" ? "Coordinator" : zigbee2MqttDevice.definition.description;
|
||||||
|
|
||||||
|
deviceDetailDTO.Model = zigbee2MqttDevice.type == "Coordinator" ? "Coordinator" : zigbee2MqttDevice.definition.model; // Is the base to understand incoming messages !
|
||||||
|
deviceDetailDTO.FirmwareVersion = zigbee2MqttDevice.software_build_id;
|
||||||
|
|
||||||
|
deviceDetailDTO.Battery = zigbee2MqttDevice.power_source == null ? false : zigbee2MqttDevice.power_source.Contains("Battery");
|
||||||
|
deviceDetailDTO.ManufacturerName = zigbee2MqttDevice.definition?.vendor == null ? provider.Type.ToString() : zigbee2MqttDevice.definition?.vendor.ToLower();
|
||||||
|
deviceDetailDTO.MeansOfCommunications = new List<MeansOfCommunication>();
|
||||||
|
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Zigbee);
|
||||||
|
|
||||||
|
deviceDetailDTO.GroupIds = existingDevice != null ? existingDevice.GroupIds : null;
|
||||||
|
deviceDetailDTO.Properties = existingDevice != null ? existingDevice.Properties : null;
|
||||||
|
deviceDetailDTO.RoomId = existingDevice != null ? existingDevice.RoomId : null;
|
||||||
|
|
||||||
|
deviceDetailDTO.CreatedDate = existingDevice != null ? existingDevice.CreatedDate : DateTime.Now;
|
||||||
|
deviceDetailDTO.UpdatedDate = DateTime.Now;
|
||||||
|
|
||||||
|
if (zigbee2MqttDevice.type != "Coordinator")
|
||||||
|
{
|
||||||
|
// EXPOSES !
|
||||||
|
List<string> supportedOperationsDTO = new List<string>();
|
||||||
|
foreach (var supportedOperation in zigbee2MqttDevice.definition?.exposes)
|
||||||
|
{
|
||||||
|
supportedOperationsDTO.Add(JsonConvert.SerializeObject(supportedOperation));
|
||||||
|
}
|
||||||
|
deviceDetailDTO.SupportedOperations = supportedOperationsDTO;
|
||||||
|
|
||||||
|
deviceDetailDTO.Type = GetDeviceTypeFromZigbeeModel(zigbee2MqttDevice.definition.model);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
deviceDetailDTO.Type = DeviceType.Gateway;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (zigbee2MqttDevice.supported || deviceDetailDTO.Type == DeviceType.Gateway)
|
||||||
|
{
|
||||||
|
// Supported device !
|
||||||
|
updatedZigbeeDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, deviceDetailDTO, false, true));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Not yet supported !
|
||||||
|
notSupportedZigbeeDevices.Add(deviceDetailDTO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Dictionary<string, List<DeviceDetailDTO>>() {
|
||||||
|
{ "updatedZigbeeDevices", updatedZigbeeDevices },
|
||||||
|
{ "notSupportedDevices", notSupportedZigbeeDevices }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private static DeviceType GetDeviceTypeFromZigbeeModel(string zigbeeModel)
|
private static DeviceType GetDeviceTypeFromZigbeeModel(string zigbeeModel)
|
||||||
{
|
{
|
||||||
return supportedDevices.Any(sd => sd.Model == zigbeeModel) ?
|
return supportedDevices.Any(sd => sd.Model == zigbeeModel) ?
|
||||||
@ -279,7 +371,7 @@ namespace MyCore.Services.Devices
|
|||||||
DeviceType.Unknown;
|
DeviceType.Unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<string, List<DeviceDetailDTO>> CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<ArloDevice> arloDevices, Provider provider)
|
public static Dictionary<string, List<DeviceDetailDTO>> CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, string homeId, List<ArloDevice> arloDevices, Provider provider)
|
||||||
{
|
{
|
||||||
List<DeviceDetailDTO> createdArloDevices = new List<DeviceDetailDTO>();
|
List<DeviceDetailDTO> createdArloDevices = new List<DeviceDetailDTO>();
|
||||||
|
|
||||||
@ -290,7 +382,7 @@ namespace MyCore.Services.Devices
|
|||||||
foreach (var arlo in arloDevices)
|
foreach (var arlo in arloDevices)
|
||||||
{
|
{
|
||||||
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
||||||
deviceDetailDTO.ManufacturerName = provider.Type;
|
deviceDetailDTO.ManufacturerName = provider.Type.ToString();
|
||||||
deviceDetailDTO.Name = arlo.deviceName;
|
deviceDetailDTO.Name = arlo.deviceName;
|
||||||
deviceDetailDTO.Description = arlo.deviceName; // As description not exist, put name in description
|
deviceDetailDTO.Description = arlo.deviceName; // As description not exist, put name in description
|
||||||
deviceDetailDTO.ServiceIdentification = arlo.deviceId;
|
deviceDetailDTO.ServiceIdentification = arlo.deviceId;
|
||||||
@ -330,13 +422,13 @@ namespace MyCore.Services.Devices
|
|||||||
|
|
||||||
deviceDetailDTO.CreatedDate = DateTime.Now;
|
deviceDetailDTO.CreatedDate = DateTime.Now;
|
||||||
deviceDetailDTO.UpdatedDate = DateTime.Now;
|
deviceDetailDTO.UpdatedDate = DateTime.Now;
|
||||||
createdArloDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, deviceDetailDTO, true));
|
createdArloDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, deviceDetailDTO, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Dictionary<string, List<DeviceDetailDTO>>() { { "createdDevices", createdArloDevices } }; // TODO Check if exist not supported devices
|
return new Dictionary<string, List<DeviceDetailDTO>>() { { "createdDevices", createdArloDevices } }; // TODO Check if exist not supported devices
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<string, List<DeviceDetailDTO>> CreateMerossDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<MerossDevice> merossDevices, Provider provider)
|
public static Dictionary<string, List<DeviceDetailDTO>> CreateMerossDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, string homeId, List<MerossDevice> merossDevices, Provider provider)
|
||||||
{
|
{
|
||||||
List<DeviceDetailDTO> createdMerossDevices = new List<DeviceDetailDTO>();
|
List<DeviceDetailDTO> createdMerossDevices = new List<DeviceDetailDTO>();
|
||||||
|
|
||||||
@ -347,7 +439,7 @@ namespace MyCore.Services.Devices
|
|||||||
foreach (var meross in merossDevices)
|
foreach (var meross in merossDevices)
|
||||||
{
|
{
|
||||||
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
||||||
deviceDetailDTO.ManufacturerName = provider.Type;
|
deviceDetailDTO.ManufacturerName = provider.Type.ToString();
|
||||||
deviceDetailDTO.HardwareVersion = meross.hdwareVersion;
|
deviceDetailDTO.HardwareVersion = meross.hdwareVersion;
|
||||||
deviceDetailDTO.Name = meross.devName;
|
deviceDetailDTO.Name = meross.devName;
|
||||||
deviceDetailDTO.Description = meross.devName; // As description not exist, put name in description
|
deviceDetailDTO.Description = meross.devName; // As description not exist, put name in description
|
||||||
@ -385,13 +477,13 @@ namespace MyCore.Services.Devices
|
|||||||
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi); // TO CHECK
|
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi); // TO CHECK
|
||||||
deviceDetailDTO.CreatedDate = DateTime.Now;
|
deviceDetailDTO.CreatedDate = DateTime.Now;
|
||||||
deviceDetailDTO.UpdatedDate = DateTime.Now;
|
deviceDetailDTO.UpdatedDate = DateTime.Now;
|
||||||
createdMerossDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, deviceDetailDTO, true));
|
createdMerossDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, deviceDetailDTO, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Dictionary<string, List<DeviceDetailDTO>>() { { "createdDevices", createdMerossDevices } }; // TODO Check if exist not supported devices
|
return new Dictionary<string, List<DeviceDetailDTO>>() { { "createdDevices", createdMerossDevices } }; // TODO Check if exist not supported devices
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<string, List<DeviceDetailDTO>> CreateYeelightDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<YeelightAPI.Device> yeelightDevices, Provider provider)
|
public static Dictionary<string, List<DeviceDetailDTO>> CreateYeelightDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, string homeId, List<YeelightAPI.Device> yeelightDevices, Provider provider)
|
||||||
{
|
{
|
||||||
List<DeviceDetailDTO> createdYeelightDevices = new List<DeviceDetailDTO>();
|
List<DeviceDetailDTO> createdYeelightDevices = new List<DeviceDetailDTO>();
|
||||||
|
|
||||||
@ -402,7 +494,7 @@ namespace MyCore.Services.Devices
|
|||||||
foreach (var light in yeelightDevices)
|
foreach (var light in yeelightDevices)
|
||||||
{
|
{
|
||||||
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
|
||||||
deviceDetailDTO.ManufacturerName = provider.Type;
|
deviceDetailDTO.ManufacturerName = provider.Type.ToString();
|
||||||
deviceDetailDTO.Name = light.Name;
|
deviceDetailDTO.Name = light.Name;
|
||||||
deviceDetailDTO.Description = light.Name; // As description not exist, put name in description
|
deviceDetailDTO.Description = light.Name; // As description not exist, put name in description
|
||||||
deviceDetailDTO.IpAddress = light.Hostname;
|
deviceDetailDTO.IpAddress = light.Hostname;
|
||||||
@ -431,7 +523,7 @@ namespace MyCore.Services.Devices
|
|||||||
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi);
|
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi);
|
||||||
deviceDetailDTO.CreatedDate = DateTime.Now;
|
deviceDetailDTO.CreatedDate = DateTime.Now;
|
||||||
deviceDetailDTO.UpdatedDate = DateTime.Now;
|
deviceDetailDTO.UpdatedDate = DateTime.Now;
|
||||||
createdYeelightDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, deviceDetailDTO, true));
|
createdYeelightDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _RoomDatabaseService, homeId, deviceDetailDTO, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Dictionary<string, List<DeviceDetailDTO>>() { { "createdDevices", createdYeelightDevices } }; // TODO Check if exist not supported devices
|
return new Dictionary<string, List<DeviceDetailDTO>>() { { "createdDevices", createdYeelightDevices } }; // TODO Check if exist not supported devices
|
||||||
|
|||||||
@ -24,25 +24,25 @@ namespace MyCore.Services
|
|||||||
return _electricityProductionData.Find(m => true).ToList();
|
return _electricityProductionData.Find(m => true).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ElectricityProduction> GetElectricityProductionForSpecifiedYear(string userId, int year)
|
public List<ElectricityProduction> GetElectricityProductionForSpecifiedYear(string homeId, int year)
|
||||||
{
|
{
|
||||||
return _electricityProductionData.Find<ElectricityProduction>(m => m.UserId == userId && m.Timestamp.Year == year).ToList();
|
return _electricityProductionData.Find<ElectricityProduction>(m => m.HomeId == homeId && m.Timestamp.Year == year).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ElectricityProduction> GetElectricityProductionForSpecifiedMonth(string userId, int month)
|
public List<ElectricityProduction> GetElectricityProductionForSpecifiedMonth(string homeId, int month)
|
||||||
{
|
{
|
||||||
return _electricityProductionData.Find<ElectricityProduction>(m => m.UserId == userId && m.Timestamp.Month == month).ToList();
|
return _electricityProductionData.Find<ElectricityProduction>(m => m.HomeId == homeId && m.Timestamp.Month == month).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ElectricityProduction> GetElectricityProductionForSpecifiedDay(string userId, int dayOfYear)
|
public List<ElectricityProduction> GetElectricityProductionForSpecifiedDay(string homeId, int dayOfYear)
|
||||||
{
|
{
|
||||||
return _electricityProductionData.Find<ElectricityProduction>(m => m.UserId == userId && m.Timestamp.DayOfYear == dayOfYear).ToList();
|
return _electricityProductionData.Find<ElectricityProduction>(m => m.HomeId == homeId && m.Timestamp.DayOfYear == dayOfYear).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if necessary
|
// Check if necessary
|
||||||
public ElectricityProduction GetElectricityProductionForUser(string userId)
|
public ElectricityProduction GetElectricityProductionForHome(string homeId)
|
||||||
{
|
{
|
||||||
return _electricityProductionData.Find<ElectricityProduction>(m => m.UserId == userId).FirstOrDefault();
|
return _electricityProductionData.Find<ElectricityProduction>(m => m.HomeId == homeId).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ElectricityProduction InsertData(ElectricityProduction data)
|
public ElectricityProduction InsertData(ElectricityProduction data)
|
||||||
@ -70,15 +70,15 @@ namespace MyCore.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if necessary
|
// Check if necessary
|
||||||
public void RemoveByDeviceId(string userId, string id)
|
public void RemoveByDeviceId(string homeId, string id)
|
||||||
{
|
{
|
||||||
_electricityProductionData.DeleteOne(data => data.UserId == userId && data.DeviceId == id);
|
_electricityProductionData.DeleteOne(data => data.HomeId == homeId && data.DeviceId == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if necessary
|
// Check if necessary
|
||||||
public void RemoveByDeviceIdBeforeDate(string userId, DateTime dateTime)
|
public void RemoveByDeviceIdBeforeDate(string homeId, DateTime dateTime)
|
||||||
{
|
{
|
||||||
_electricityProductionData.DeleteOne(data => data.UserId == userId && data.Timestamp < dateTime);
|
_electricityProductionData.DeleteOne(data => data.HomeId == homeId && data.Timestamp < dateTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
using MyCore.Interfaces.DTO;
|
using MyCore.Interfaces.DTO;
|
||||||
using MyCore.Interfaces.Models;
|
using MyCore.Interfaces.Models;
|
||||||
|
using MyCore.Services;
|
||||||
using MyCore.Services.MyControlPanel;
|
using MyCore.Services.MyControlPanel;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -10,7 +11,7 @@ namespace MyCore.Service.Services
|
|||||||
{
|
{
|
||||||
public class GroupService
|
public class GroupService
|
||||||
{
|
{
|
||||||
public static GroupDetailDTO CreateOrUpdate(GroupDatabaseService _GroupDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string userId, GroupCreateOrUpdateDetailDTO groupCreateOrUpdateDetailDTO, bool create)
|
public static GroupDetailDTO CreateOrUpdate(GroupDatabaseService _GroupDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string homeId, GroupCreateOrUpdateDetailDTO groupCreateOrUpdateDetailDTO, bool create)
|
||||||
{
|
{
|
||||||
List<Device> devices = new List<Device>();
|
List<Device> devices = new List<Device>();
|
||||||
Group group;
|
Group group;
|
||||||
@ -25,7 +26,7 @@ namespace MyCore.Service.Services
|
|||||||
group = _GroupDatabaseService.GetById(groupCreateOrUpdateDetailDTO.Id);
|
group = _GroupDatabaseService.GetById(groupCreateOrUpdateDetailDTO.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
group.UserId = userId;
|
group.HomeId = homeId;
|
||||||
group.Name = groupCreateOrUpdateDetailDTO.Name;
|
group.Name = groupCreateOrUpdateDetailDTO.Name;
|
||||||
group.Type = group.Type; // TODO
|
group.Type = group.Type; // TODO
|
||||||
group.UpdatedDate = DateTime.Now;
|
group.UpdatedDate = DateTime.Now;
|
||||||
@ -51,12 +52,12 @@ namespace MyCore.Service.Services
|
|||||||
return group.ToDTO(devices.Select(d => d.ToDTO()).ToList());
|
return group.ToDTO(devices.Select(d => d.ToDTO()).ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<GroupDetailDTO> CreateFromZigbeeAsync(GroupDatabaseService _GroupDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string userId, List<Zigbee2MqttGroup> zigbee2MqttGroups)
|
public static List<GroupDetailDTO> CreateFromZigbeeAsync(GroupDatabaseService _GroupDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string homeId, List<Zigbee2MqttGroup> zigbee2MqttGroups)
|
||||||
{
|
{
|
||||||
List<GroupDetailDTO> groups = new List<GroupDetailDTO>();
|
List<GroupDetailDTO> groups = new List<GroupDetailDTO>();
|
||||||
|
|
||||||
// Get zigbee groups
|
// Get zigbee groups
|
||||||
List<Group> existingGroups = _GroupDatabaseService.GetByType(userId, "zigbee2mqtt");
|
List<Group> existingGroups = _GroupDatabaseService.GetByType(homeId, "zigbee2mqtt");
|
||||||
|
|
||||||
foreach (var zigbee2MqttGroup in zigbee2MqttGroups.Where(z => z.members.Count > 0)) // Only take group with members
|
foreach (var zigbee2MqttGroup in zigbee2MqttGroups.Where(z => z.members.Count > 0)) // Only take group with members
|
||||||
{
|
{
|
||||||
@ -66,13 +67,12 @@ namespace MyCore.Service.Services
|
|||||||
var existingGroup = existingGroups.Where(eg => eg.ServiceIdentification == zigbee2MqttGroup.id).FirstOrDefault();
|
var existingGroup = existingGroups.Where(eg => eg.ServiceIdentification == zigbee2MqttGroup.id).FirstOrDefault();
|
||||||
|
|
||||||
GroupCreateOrUpdateDetailDTO groupToUpdate = new GroupCreateOrUpdateDetailDTO();
|
GroupCreateOrUpdateDetailDTO groupToUpdate = new GroupCreateOrUpdateDetailDTO();
|
||||||
groupToUpdate.UserId = existingGroup.UserId;
|
groupToUpdate.HomeId = existingGroup.HomeId;
|
||||||
groupToUpdate.Name = existingGroup.Name;
|
groupToUpdate.Name = existingGroup.Name;
|
||||||
groupToUpdate.Id = existingGroup.Id;
|
groupToUpdate.Id = existingGroup.Id;
|
||||||
groupToUpdate.IsAlarm = existingGroup.IsAlarm;
|
|
||||||
groupToUpdate.DeviceIds = existingGroup.DevicesIds;
|
groupToUpdate.DeviceIds = existingGroup.DevicesIds;
|
||||||
// Hard refresh
|
// Hard refresh
|
||||||
CreateOrUpdate(_GroupDatabaseService, _DeviceDatabaseService, userId, groupToUpdate, false);
|
CreateOrUpdate(_GroupDatabaseService, _DeviceDatabaseService, homeId, groupToUpdate, false);
|
||||||
create = false;
|
create = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ namespace MyCore.Service.Services
|
|||||||
group.CreatedDate = DateTime.Now;
|
group.CreatedDate = DateTime.Now;
|
||||||
group.DevicesIds = new List<string>();
|
group.DevicesIds = new List<string>();
|
||||||
group.ServiceIdentification = zigbee2MqttGroup.id;
|
group.ServiceIdentification = zigbee2MqttGroup.id;
|
||||||
group.UserId = userId;
|
group.HomeId = homeId;
|
||||||
group.Name = zigbee2MqttGroup.friendly_name;
|
group.Name = zigbee2MqttGroup.friendly_name;
|
||||||
group.Type = "zigbee2mqtt";
|
group.Type = "zigbee2mqtt";
|
||||||
group.UpdatedDate = DateTime.Now;
|
group.UpdatedDate = DateTime.Now;
|
||||||
@ -118,7 +118,7 @@ namespace MyCore.Service.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We only go in this method if the group config change during operation
|
// We only go in this method if the group config change during operation
|
||||||
public static void CompareGroupsFromZigbee2Mqtt(string userId, List<Group> groups, List<Zigbee2MqttGroup> zigbee2MqttGroups, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService)
|
public static void CompareGroupsFromZigbee2Mqtt(string homeId, List<Group> groups, List<Zigbee2MqttGroup> zigbee2MqttGroups, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService)
|
||||||
{
|
{
|
||||||
// We do not refresh all list as we don't want to create plenty of new groupId and have to delete from device param everytime
|
// We do not refresh all list as we don't want to create plenty of new groupId and have to delete from device param everytime
|
||||||
List<Zigbee2MqttGroup> existingGroups = new List<Zigbee2MqttGroup>();
|
List<Zigbee2MqttGroup> existingGroups = new List<Zigbee2MqttGroup>();
|
||||||
@ -136,7 +136,7 @@ namespace MyCore.Service.Services
|
|||||||
if (existingGroupWithThisId == null)
|
if (existingGroupWithThisId == null)
|
||||||
{
|
{
|
||||||
// New group -> Create it !
|
// New group -> Create it !
|
||||||
CreateFromZigbeeAsync(_GroupDatabaseService, _DeviceDatabaseService, userId, zigbee2MqttGroups.Where(z => z.id == zigbee2MqttGroup.id).ToList());
|
CreateFromZigbeeAsync(_GroupDatabaseService, _DeviceDatabaseService, homeId, zigbee2MqttGroups.Where(z => z.id == zigbee2MqttGroup.id).ToList());
|
||||||
} else {
|
} else {
|
||||||
if (!Enumerable.SequenceEqual(zigbee2MqttGroup.members.Select(m => m.ieee_address).OrderBy(t => t), existingGroupWithThisId.members.Select(m => m.ieee_address).OrderBy(t => t)))
|
if (!Enumerable.SequenceEqual(zigbee2MqttGroup.members.Select(m => m.ieee_address).OrderBy(t => t), existingGroupWithThisId.members.Select(m => m.ieee_address).OrderBy(t => t)))
|
||||||
{
|
{
|
||||||
@ -157,7 +157,7 @@ namespace MyCore.Service.Services
|
|||||||
_GroupDatabaseService.Remove(groupToDelete.Id);
|
_GroupDatabaseService.Remove(groupToDelete.Id);
|
||||||
|
|
||||||
// Create new group
|
// Create new group
|
||||||
CreateFromZigbeeAsync(_GroupDatabaseService, _DeviceDatabaseService, userId, new List<Zigbee2MqttGroup>() { zigbee2MqttGroup });
|
CreateFromZigbeeAsync(_GroupDatabaseService, _DeviceDatabaseService, homeId, new List<Zigbee2MqttGroup>() { zigbee2MqttGroup });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// else do nothing
|
// else do nothing
|
||||||
|
|||||||
@ -0,0 +1,58 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MyCore.Interfaces.Models;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace MyCore.Services
|
||||||
|
{
|
||||||
|
public class AlarmDatabaseService
|
||||||
|
{
|
||||||
|
private readonly IMongoCollection<AlarmMode> _Alarms;
|
||||||
|
|
||||||
|
public AlarmDatabaseService(IConfiguration config)
|
||||||
|
{
|
||||||
|
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
|
||||||
|
var database = client.GetDatabase("MyCoreDb");
|
||||||
|
_Alarms = database.GetCollection<AlarmMode>("AlarmModes");
|
||||||
|
}
|
||||||
|
public List<AlarmMode> GetAll(string homeId)
|
||||||
|
{
|
||||||
|
return _Alarms.Find(alarm => alarm.HomeId == homeId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AlarmMode GetById(string id)
|
||||||
|
{
|
||||||
|
return _Alarms.Find<AlarmMode>(alarm => alarm.Id == id).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsExist(string id)
|
||||||
|
{
|
||||||
|
return _Alarms.Find<AlarmMode>(alarm => alarm.Id == id).FirstOrDefault() != null ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AlarmMode Create(AlarmMode alarm)
|
||||||
|
{
|
||||||
|
_Alarms.InsertOne(alarm);
|
||||||
|
return alarm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AlarmMode Update(AlarmMode alarmIn)
|
||||||
|
{
|
||||||
|
_Alarms.ReplaceOne(alarm => alarm.Id == alarmIn.Id, alarmIn);
|
||||||
|
return alarmIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(string id)
|
||||||
|
{
|
||||||
|
_Alarms.DeleteOne(alarm => alarm.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveForHome(string homeId)
|
||||||
|
{
|
||||||
|
_Alarms.DeleteMany(alarm => alarm.HomeId== homeId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,7 +6,7 @@ using MyCore.Interfaces.Models;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services
|
||||||
{
|
{
|
||||||
public class AutomationDatabaseService
|
public class AutomationDatabaseService
|
||||||
{
|
{
|
||||||
@ -18,9 +18,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
var database = client.GetDatabase("MyCoreDb");
|
var database = client.GetDatabase("MyCoreDb");
|
||||||
_Automations = database.GetCollection<Automation>("Automations");
|
_Automations = database.GetCollection<Automation>("Automations");
|
||||||
}
|
}
|
||||||
public List<Automation> GetAll(string userId)
|
public List<Automation> GetAll(string homeId)
|
||||||
{
|
{
|
||||||
return _Automations.Find(a => a.UserId == userId).ToList();
|
return _Automations.Find(a => a.HomeId == homeId).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Automation GetById(string id)
|
public Automation GetById(string id)
|
||||||
@ -28,6 +28,11 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return _Automations.Find<Automation>(a => a.Id == id).FirstOrDefault();
|
return _Automations.Find<Automation>(a => a.Id == id).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Automation> GetByHomeId(string homeId)
|
||||||
|
{
|
||||||
|
return _Automations.Find<Automation>(a => a.HomeId == homeId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public List<Automation> GetByProvider(string id)
|
public List<Automation> GetByProvider(string id)
|
||||||
{
|
{
|
||||||
return _Automations.Find<Automation>(a => a.Triggers.Any(t => t.ProviderId == id)).ToList();
|
return _Automations.Find<Automation>(a => a.Triggers.Any(t => t.ProviderId == id)).ToList();
|
||||||
@ -60,9 +65,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
_Automations.DeleteOne(automation => automation.Id == id);
|
_Automations.DeleteOne(automation => automation.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveForUser(string userId)
|
public void RemoveForHome(string homeId)
|
||||||
{
|
{
|
||||||
_Automations.DeleteMany(automation => automation.UserId == userId);
|
_Automations.DeleteMany(automation => automation.HomeId == homeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ using Microsoft.Extensions.Configuration;
|
|||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using MyCore.Interfaces.DTO;
|
using MyCore.Interfaces.DTO;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services
|
||||||
{
|
{
|
||||||
public class DeviceDatabaseService
|
public class DeviceDatabaseService
|
||||||
{
|
{
|
||||||
@ -19,9 +19,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
var database = client.GetDatabase("MyCoreDb");
|
var database = client.GetDatabase("MyCoreDb");
|
||||||
_Devices = database.GetCollection<Device>("Devices");
|
_Devices = database.GetCollection<Device>("Devices");
|
||||||
}
|
}
|
||||||
public List<Device> GetAll(string userId)
|
public List<Device> GetAll(string homeId)
|
||||||
{
|
{
|
||||||
return _Devices.Find(d => d.UserId == userId).ToList();
|
return _Devices.Find(d => d.HomeId == homeId).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Device GetById(string id)
|
public Device GetById(string id)
|
||||||
@ -39,14 +39,19 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return _Devices.Find<Device>(d => ids.Contains(d.Id)).ToList();
|
return _Devices.Find<Device>(d => ids.Contains(d.Id)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Device> GetByLocation(string userId, string locationId)
|
public List<Device> GetByHomeId(string homeId)
|
||||||
{
|
{
|
||||||
return _Devices.Find<Device>(d => d.UserId == userId && d.LocationId == locationId).ToList();
|
return _Devices.Find<Device>(h => h.HomeId == homeId).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Device> GetByType(string userId, DeviceType type)
|
public List<Device> GetByLocation(string homeId, string roomId)
|
||||||
{
|
{
|
||||||
return _Devices.Find<Device>(d => d.UserId == userId && d.Type == type).ToList();
|
return _Devices.Find<Device>(d => d.HomeId == homeId && d.RoomId == roomId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Device> GetByType(string homeId, DeviceType type)
|
||||||
|
{
|
||||||
|
return _Devices.Find<Device>(d => d.HomeId == homeId && d.Type == type).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Device> GetByProviderId(string providerId)
|
public List<Device> GetByProviderId(string providerId)
|
||||||
@ -69,9 +74,9 @@ 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 userId, string serviceIdentification, string model)
|
public bool IsAlreadyHere(string homeId, string serviceIdentification, string model)
|
||||||
{
|
{
|
||||||
return _Devices.Find<Device>(d => d.UserId == userId && d.ServiceIdentification == serviceIdentification && d.Model == model).FirstOrDefault() != null ? true : false;
|
return _Devices.Find<Device>(d => d.HomeId == homeId && d.ServiceIdentification == serviceIdentification && d.Model == model).FirstOrDefault() != null ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Device Create(Device device)
|
public Device Create(Device device)
|
||||||
@ -96,9 +101,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
_Devices.DeleteMany(device => device.ProviderId == providerId);
|
_Devices.DeleteMany(device => device.ProviderId == providerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveForUser(string userId)
|
public void RemoveForHome(string homeId)
|
||||||
{
|
{
|
||||||
_Devices.DeleteMany(device => device.UserId == userId);
|
_Devices.DeleteMany(device => device.HomeId == homeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ using MyCore.Interfaces.Models;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services
|
||||||
{
|
{
|
||||||
public class GroupDatabaseService
|
public class GroupDatabaseService
|
||||||
{
|
{
|
||||||
@ -19,9 +19,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
_Groups = database.GetCollection<Group>("Groups");
|
_Groups = database.GetCollection<Group>("Groups");
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Group> GetAll(string userId)
|
public List<Group> GetAll(string homeId)
|
||||||
{
|
{
|
||||||
return _Groups.Find(d => d.UserId == userId).ToList();
|
return _Groups.Find(d => d.HomeId == homeId).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Group GetById(string id)
|
public Group GetById(string id)
|
||||||
@ -29,9 +29,14 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return _Groups.Find<Group>(d => d.Id == id).FirstOrDefault();
|
return _Groups.Find<Group>(d => d.Id == id).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Group> GetByType(string userId, string type)
|
public List<Group> GetByHomeId(string homeId)
|
||||||
{
|
{
|
||||||
return _Groups.Find(d => d.UserId == userId && d.Type == type).ToList();
|
return _Groups.Find<Group>(g => g.HomeId == homeId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Group> GetByType(string homeId, string type)
|
||||||
|
{
|
||||||
|
return _Groups.Find(d => d.HomeId == homeId && d.Type == type).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsExist(string id)
|
public bool IsExist(string id)
|
||||||
@ -56,9 +61,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
_Groups.DeleteOne(group => group.Id == id);
|
_Groups.DeleteOne(group => group.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveForUser(string userId)
|
public void RemoveForHome(string homeId)
|
||||||
{
|
{
|
||||||
_Groups.DeleteMany(group => group.UserId == userId);
|
_Groups.DeleteMany(group => group.HomeId == homeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MyCore.Interfaces.Models;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace MyCore.Services
|
||||||
|
{
|
||||||
|
public class HomeDatabaseService
|
||||||
|
{
|
||||||
|
private readonly IMongoCollection<Home> _Homes;
|
||||||
|
|
||||||
|
public HomeDatabaseService(IConfiguration config)
|
||||||
|
{
|
||||||
|
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
|
||||||
|
var database = client.GetDatabase("MyCoreDb");
|
||||||
|
_Homes = database.GetCollection<Home>("Homes");
|
||||||
|
}
|
||||||
|
public List<Home> GetAll()
|
||||||
|
{
|
||||||
|
return _Homes.Find(h => true).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Home GetById(string id)
|
||||||
|
{
|
||||||
|
return _Homes.Find<Home>(m => m.Id == id).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsExist(string id)
|
||||||
|
{
|
||||||
|
return _Homes.Find<Home>(d => d.Id == id).FirstOrDefault() != null ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Home Create(Home home)
|
||||||
|
{
|
||||||
|
_Homes.InsertOne(home);
|
||||||
|
return home;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Home Update(string id, Home homeIn)
|
||||||
|
{
|
||||||
|
_Homes.ReplaceOne(home => home.Id == id, homeIn);
|
||||||
|
return homeIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(string id)
|
||||||
|
{
|
||||||
|
_Homes.DeleteOne(home => home.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,53 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using MyCore.Interfaces.Models;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using MongoDB.Driver;
|
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
|
||||||
{
|
|
||||||
public class LocationDatabaseService
|
|
||||||
{
|
|
||||||
private readonly IMongoCollection<Place> _Locations;
|
|
||||||
|
|
||||||
public LocationDatabaseService(IConfiguration config)
|
|
||||||
{
|
|
||||||
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
|
|
||||||
var database = client.GetDatabase("MyCoreDb");
|
|
||||||
_Locations = database.GetCollection<Place>("Locations");
|
|
||||||
}
|
|
||||||
public List<Place> GetAll()
|
|
||||||
{
|
|
||||||
return _Locations.Find(l => true).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Place GetById(string id)
|
|
||||||
{
|
|
||||||
return _Locations.Find<Place>(l => l.Id == id).FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsExist(string id)
|
|
||||||
{
|
|
||||||
return _Locations.Find<Place>(p => p.Id == id).FirstOrDefault() != null ? true : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Place Create(Place location)
|
|
||||||
{
|
|
||||||
_Locations.InsertOne(location);
|
|
||||||
return location;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Place Update(string id, Place locationIn)
|
|
||||||
{
|
|
||||||
_Locations.ReplaceOne(location => location.Id == id, locationIn);
|
|
||||||
return locationIn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove(string id)
|
|
||||||
{
|
|
||||||
_Locations.DeleteOne(location => location.Id == id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -6,7 +6,7 @@ using MyCore.Interfaces.Models;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services
|
||||||
{
|
{
|
||||||
public class ProviderDatabaseService
|
public class ProviderDatabaseService
|
||||||
{
|
{
|
||||||
@ -18,19 +18,24 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
var database = client.GetDatabase("MyCoreDb");
|
var database = client.GetDatabase("MyCoreDb");
|
||||||
_Providers = database.GetCollection<Provider>("Providers");
|
_Providers = database.GetCollection<Provider>("Providers");
|
||||||
}
|
}
|
||||||
public List<Provider> GetAll(string userId)
|
public List<Provider> GetAll(string homeId)
|
||||||
{
|
{
|
||||||
return _Providers.Find(p => p.UserId == userId).ToList();
|
return _Providers.Find(p => p.HomeId == homeId).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Provider GetById(string userId, string id)
|
public Provider GetById(string homeId, string id)
|
||||||
{
|
{
|
||||||
return _Providers.Find<Provider>(p => p.Id == id && p.UserId == userId).FirstOrDefault();
|
return _Providers.Find<Provider>(p => p.Id == id && p.HomeId == homeId).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool AlreadyExistForUser(string userId, string name)
|
public List<Provider> GetByHomeId(string homeId)
|
||||||
{
|
{
|
||||||
return _Providers.Find<Provider>(p => p.UserId == userId && p.Name == name).FirstOrDefault() != null ? true : false; ;
|
return _Providers.Find<Provider>(p => p.HomeId == homeId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool AlreadyExistForHome(string homeId, string name)
|
||||||
|
{
|
||||||
|
return _Providers.Find<Provider>(p => p.HomeId == homeId && p.Name == name).FirstOrDefault() != null ? true : false; ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Provider GetByName(string name)
|
public Provider GetByName(string name)
|
||||||
@ -38,7 +43,7 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return _Providers.Find<Provider>(p => p.Name == name).FirstOrDefault();
|
return _Providers.Find<Provider>(p => p.Name == name).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Provider GetByType(string type)
|
public Provider GetByType(ProviderType type)
|
||||||
{
|
{
|
||||||
return _Providers.Find<Provider>(p => p.Type == type).FirstOrDefault();
|
return _Providers.Find<Provider>(p => p.Type == type).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ using MyCore.Interfaces.Models;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
namespace MyCore.Services
|
||||||
{
|
{
|
||||||
public class RoomDatabaseService
|
public class RoomDatabaseService
|
||||||
{
|
{
|
||||||
@ -18,9 +18,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
var database = client.GetDatabase("MyCoreDb");
|
var database = client.GetDatabase("MyCoreDb");
|
||||||
_Rooms = database.GetCollection<Room>("Rooms");
|
_Rooms = database.GetCollection<Room>("Rooms");
|
||||||
}
|
}
|
||||||
public List<Room> GetAll(string userId)
|
public List<Room> GetAll(string homeId)
|
||||||
{
|
{
|
||||||
return _Rooms.Find(d => d.UserId == userId).ToList();
|
return _Rooms.Find(d => d.HomeId == homeId).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Room GetById(string id)
|
public Room GetById(string id)
|
||||||
@ -50,9 +50,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
_Rooms.DeleteOne(room => room.Id == id);
|
_Rooms.DeleteOne(room => room.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveForUser(string userId)
|
public void RemoveForHome(string homeId)
|
||||||
{
|
{
|
||||||
_Rooms.DeleteMany(room => room.UserId == userId);
|
_Rooms.DeleteMany(room => room.HomeId == homeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,15 +38,31 @@ namespace MyCore.Services
|
|||||||
return _Users.Find<UserInfo>(d => d.Id == id).FirstOrDefault() != null ? true : false;
|
return _Users.Find<UserInfo>(d => d.Id == id).FirstOrDefault() != null ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsExistMultiple(List<string> ids)
|
||||||
|
{
|
||||||
|
foreach (var id in ids)
|
||||||
|
{
|
||||||
|
if (_Users.Find<UserInfo>(d => d.Id == id).FirstOrDefault() == null)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public UserInfo Create(UserInfo user)
|
public UserInfo Create(UserInfo user)
|
||||||
{
|
{
|
||||||
_Users.InsertOne(user);
|
_Users.InsertOne(user);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserInfo Update(string id, UserInfo userIn)
|
public List<UserInfo> GetByHomeId(string homeId)
|
||||||
{
|
{
|
||||||
_Users.ReplaceOne(user => user.Id == id, userIn);
|
return _Users.Find<UserInfo>(h => h.HomeIds.Contains(homeId)).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserInfo Update(UserInfo userIn)
|
||||||
|
{
|
||||||
|
_Users.ReplaceOne(user => user.Id == userIn.Id, userIn);
|
||||||
return userIn;
|
return userIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
46
MyCore/Services/MyControlPanel/HomeService.cs
Normal file
46
MyCore/Services/MyControlPanel/HomeService.cs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
using MyCore.Interfaces.DTO;
|
||||||
|
using MyCore.Interfaces.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MyCore.Services
|
||||||
|
{
|
||||||
|
public class HomeService
|
||||||
|
{
|
||||||
|
public static bool IsExist(HomeDatabaseService _HomeDatabaseService, string homeId)
|
||||||
|
{
|
||||||
|
return _HomeDatabaseService.GetById(homeId) != null ? true : false ;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Home CreateOrUpdate(HomeDatabaseService _HomeDatabaseService, AlarmDatabaseService _AlarmDatabaseService, CreateOrUpdateHomeDTO createOrUpdateHomeDTO, bool create)
|
||||||
|
{
|
||||||
|
Home home;
|
||||||
|
if (create)
|
||||||
|
home = new Home();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
home = _HomeDatabaseService.GetById(createOrUpdateHomeDTO.Id);
|
||||||
|
home.CurrentAlarmMode = _AlarmDatabaseService.IsExist(createOrUpdateHomeDTO.CurrentAlarmMode.Id) ? _AlarmDatabaseService.GetById(createOrUpdateHomeDTO.CurrentAlarmMode.Id) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
home.Name = createOrUpdateHomeDTO.Name;
|
||||||
|
home.CreatedDate = create ? DateTime.Now : createOrUpdateHomeDTO.CreatedDate;
|
||||||
|
home.UpdatedDate = DateTime.Now;
|
||||||
|
home.IsAlarm = createOrUpdateHomeDTO.IsAlarm;
|
||||||
|
home.UsersIds = createOrUpdateHomeDTO.UsersIds.ToArray();
|
||||||
|
// Todo : check if necessary
|
||||||
|
/*home.Users = homeDetail.Users;
|
||||||
|
home.Automations = homeDetail.Automations;
|
||||||
|
home.Devices = homeDetail.Devices;
|
||||||
|
home.Providers = homeDetail.Providers;*/
|
||||||
|
|
||||||
|
if (create)
|
||||||
|
return _HomeDatabaseService.Create(home);
|
||||||
|
else
|
||||||
|
return _HomeDatabaseService.Update(home.Id, home);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -10,40 +10,30 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
{
|
{
|
||||||
public class ProviderService
|
public class ProviderService
|
||||||
{
|
{
|
||||||
static List<string> supportedProviders = new List<string>() {
|
public static bool IsExist(ProviderDatabaseService _ProviderDatabaseService, string homeId, string providerId)
|
||||||
"arlo",
|
|
||||||
"meross",
|
|
||||||
"yeelight",
|
|
||||||
"zigbee2mqtt"
|
|
||||||
};
|
|
||||||
|
|
||||||
public static bool IsExist(ProviderDatabaseService _ProviderDatabaseService, string userId, string providerId)
|
|
||||||
{
|
{
|
||||||
return _ProviderDatabaseService.GetById(userId, providerId) != null ? true : false;
|
return _ProviderDatabaseService.GetById(homeId, providerId) != null ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Provider> GetAll(ProviderDatabaseService _ProviderDatabaseService, string userId)
|
public static List<Provider> GetAll(ProviderDatabaseService _ProviderDatabaseService, string homeId)
|
||||||
{
|
{
|
||||||
return _ProviderDatabaseService.GetAll(userId);
|
return _ProviderDatabaseService.GetAll(homeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ProviderDTO CreateOrUpdate(ProviderDatabaseService _ProviderDatabaseService, string userId, ProviderDTO providerDTO, bool create)
|
public static ProviderDTO CreateOrUpdate(ProviderDatabaseService _ProviderDatabaseService, string homeId, ProviderDTO providerDTO, bool create)
|
||||||
{
|
{
|
||||||
Provider provider;
|
Provider provider;
|
||||||
if (create)
|
if (create)
|
||||||
provider = new Provider();
|
provider = new Provider();
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
provider = _ProviderDatabaseService.GetById(userId, providerDTO.Id);
|
provider = _ProviderDatabaseService.GetById(homeId, providerDTO.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsProviderSupported(providerDTO.Type))
|
|
||||||
throw new KeyNotFoundException("Provider is not yet supported");
|
|
||||||
|
|
||||||
provider.Type = providerDTO.Type;
|
provider.Type = providerDTO.Type;
|
||||||
provider.Name = providerDTO.Name;
|
provider.Name = providerDTO.Name;
|
||||||
provider.Endpoint = providerDTO.Endpoint;
|
provider.Endpoint = providerDTO.Endpoint;
|
||||||
provider.UserId = providerDTO.UserId;
|
provider.HomeId = providerDTO.HomeId;
|
||||||
provider.Username = providerDTO.Username;
|
provider.Username = providerDTO.Username;
|
||||||
provider.Password = providerDTO.Password;
|
provider.Password = providerDTO.Password;
|
||||||
provider.ApiKey = providerDTO.ApiKey;
|
provider.ApiKey = providerDTO.ApiKey;
|
||||||
@ -55,20 +45,9 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return _ProviderDatabaseService.Update(provider.Id, provider).ToDTO();
|
return _ProviderDatabaseService.Update(provider.Id, provider).ToDTO();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Provider GetProviderById(ProviderDatabaseService _ProviderDatabaseService, string userId, string providerId)
|
public static Provider GetProviderById(ProviderDatabaseService _ProviderDatabaseService, string homeId, string providerId)
|
||||||
{
|
{
|
||||||
return _ProviderDatabaseService.GetById(userId, providerId);
|
return _ProviderDatabaseService.GetById(homeId, providerId);
|
||||||
}
|
|
||||||
|
|
||||||
// TODO Get supported services
|
|
||||||
public static List<string> GetSupportedProvider()
|
|
||||||
{
|
|
||||||
return supportedProviders;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool IsProviderSupported(string providerName)
|
|
||||||
{
|
|
||||||
return supportedProviders.Contains(providerName) ? true : false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MyCore.Services.MyControlPanel
|
|
||||||
{
|
|
||||||
public class UserService
|
|
||||||
{
|
|
||||||
public static bool IsExist(UserDatabaseService _UserDatabaseService, string userId)
|
|
||||||
{
|
|
||||||
return _UserDatabaseService.GetById(userId) != null ? true : false ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,5 +1,6 @@
|
|||||||
using MyCore.Interfaces.DTO;
|
using MyCore.Interfaces.DTO;
|
||||||
using MyCore.Interfaces.Models;
|
using MyCore.Interfaces.Models;
|
||||||
|
using MyCore.Services;
|
||||||
using MyCore.Services.MyControlPanel;
|
using MyCore.Services.MyControlPanel;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -10,7 +11,7 @@ namespace MyCore.Service
|
|||||||
{
|
{
|
||||||
public class RoomService
|
public class RoomService
|
||||||
{
|
{
|
||||||
public static RoomDetailDTO CreateOrUpdate(RoomDatabaseService _RoomDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string userId, RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO, bool create)
|
public static RoomDetailDTO CreateOrUpdate(RoomDatabaseService _RoomDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string homeId, RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO, bool create)
|
||||||
{
|
{
|
||||||
List<Device> devices = new List<Device>();
|
List<Device> devices = new List<Device>();
|
||||||
Room room;
|
Room room;
|
||||||
@ -24,12 +25,12 @@ namespace MyCore.Service
|
|||||||
room = _RoomDatabaseService.GetById(roomCreateOrUpdateDetailDTO.Id);
|
room = _RoomDatabaseService.GetById(roomCreateOrUpdateDetailDTO.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
room.UserId = userId;
|
room.HomeId = homeId;
|
||||||
room.Name = roomCreateOrUpdateDetailDTO.Name;
|
room.Name = roomCreateOrUpdateDetailDTO.Name;
|
||||||
room.UpdatedDate = DateTime.Now;
|
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); // A device cannot have multiple rooms. Filter list
|
||||||
currentDevices = currentDevices.Where(c => c.LocationId == null).ToList(); // TODO add exception or something
|
currentDevices = currentDevices.Where(c => c.RoomId == null).ToList(); // TODO add exception or something
|
||||||
|
|
||||||
if (create)
|
if (create)
|
||||||
{
|
{
|
||||||
@ -46,7 +47,7 @@ namespace MyCore.Service
|
|||||||
{
|
{
|
||||||
Device device = _DeviceDatabaseService.GetById(deviceId);
|
Device device = _DeviceDatabaseService.GetById(deviceId);
|
||||||
devices.Add(device);
|
devices.Add(device);
|
||||||
device.LocationId = room.Id;
|
device.RoomId = room.Id;
|
||||||
_DeviceDatabaseService.Update(device);
|
_DeviceDatabaseService.Update(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -62,9 +62,9 @@ namespace MyCore
|
|||||||
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
|
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
|
||||||
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
||||||
|
|
||||||
BrokerHostSettings brokerOnlineHostSettings = new BrokerHostSettings();
|
/*BrokerHostSettings brokerOnlineHostSettings = new BrokerHostSettings();
|
||||||
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerOnlineHostSettings);
|
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerOnlineHostSettings);
|
||||||
AppSettingsOnlineProvider.BrokerHostOnlineSettings = brokerOnlineHostSettings;
|
AppSettingsOnlineProvider.BrokerHostOnlineSettings = brokerOnlineHostSettings;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MapClientSettings()
|
private void MapClientSettings()
|
||||||
@ -73,9 +73,9 @@ namespace MyCore
|
|||||||
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
||||||
AppSettingsProvider.ClientSettings = clientSettings;
|
AppSettingsProvider.ClientSettings = clientSettings;
|
||||||
|
|
||||||
ClientSettings clientOnlineSettings = new ClientSettings();
|
/*ClientSettings clientOnlineSettings = new ClientSettings();
|
||||||
Configuration.GetSection(nameof(ClientSettings)).Bind(clientOnlineSettings);
|
Configuration.GetSection(nameof(ClientSettings)).Bind(clientOnlineSettings);
|
||||||
AppSettingsOnlineProvider.ClientOnlineSettings = clientOnlineSettings;
|
AppSettingsOnlineProvider.ClientOnlineSettings = clientOnlineSettings;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
@ -140,10 +140,11 @@ namespace MyCore
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddMqttClientHostedService(); // Todo client files (a lot are useless)
|
|
||||||
services.AddMqttClientOnlineHostedService();
|
|
||||||
|
|
||||||
services.AddMerossClientHostedService(); // Todo client files (a lot are useless)
|
//services.AddMqttClientOnlineHostedService();
|
||||||
|
|
||||||
|
// Comment this line when dev
|
||||||
|
//services.AddMerossClientHostedService(); // Todo client files (a lot are useless)
|
||||||
|
|
||||||
services.AddScoped<YeelightService>(); // To clarify if needed.. ?
|
services.AddScoped<YeelightService>(); // To clarify if needed.. ?
|
||||||
|
|
||||||
@ -153,13 +154,16 @@ namespace MyCore
|
|||||||
services.AddScoped<BookService>();
|
services.AddScoped<BookService>();
|
||||||
services.AddScoped<IoTDeviceService>();
|
services.AddScoped<IoTDeviceService>();
|
||||||
services.AddScoped<UserDatabaseService>();
|
services.AddScoped<UserDatabaseService>();
|
||||||
|
services.AddScoped<HomeDatabaseService>();
|
||||||
services.AddScoped<ProviderDatabaseService>();
|
services.AddScoped<ProviderDatabaseService>();
|
||||||
services.AddScoped<DeviceDatabaseService>();
|
services.AddScoped<DeviceDatabaseService>();
|
||||||
services.AddScoped<GroupDatabaseService>();
|
services.AddScoped<GroupDatabaseService>();
|
||||||
services.AddScoped<LocationDatabaseService>();
|
services.AddScoped<RoomDatabaseService>();
|
||||||
services.AddScoped<AutomationDatabaseService>();
|
services.AddScoped<AutomationDatabaseService>();
|
||||||
services.AddScoped<ActionService>();
|
services.AddScoped<ActionService>();
|
||||||
services.AddScoped<RoomDatabaseService>();
|
services.AddScoped<AlarmDatabaseService>();
|
||||||
|
|
||||||
|
services.AddMqttClientHostedService(); // Todo client files (a lot are useless)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user