MC Add CreateDevicesFromZigbee2Mqtt (WIP)

This commit is contained in:
Thomas Fransolet 2020-03-31 18:51:10 +02:00
parent 28903e1a16
commit 139b6d86db
4 changed files with 75 additions and 13 deletions

View File

@ -165,6 +165,38 @@ namespace MyCore.Controllers.Devices
} }
} }
/// <summary>
/// Create devices from provider
/// </summary>
/// <param name="userId">User Id</param>
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
[HttpPost("fromZigbee2Mqtt/{userId}")]
public async Task<ObjectResult> CreateDevicesFromZigbee2Mqtt(string userId)
{
try
{
if (userId == null)
throw new InvalidOperationException("User not found");
if (!UserService.IsExist(_UserDatabaseService, userId))
throw new KeyNotFoundException("User not found");
// TODO HANDLE DUPLICATE DEVICES
List<DeviceDetailDTO> devicesCreated = await DeviceService.CreateFromZigbee(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, userId);
return new OkObjectResult(devicesCreated);
}
catch (InvalidOperationException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary> /// <summary>
/// Update a device /// Update a device
/// </summary> /// </summary>

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Services.Devices
{
public class ActionService
{
// TODO it's here that action are thrown.. Call from Mqtt Or other service like controller if from RpiServices
}
}

View File

@ -100,10 +100,10 @@ namespace MyCore.Services.Devices
} }
} }
catch (AuthenticationException ex) { catch (AuthenticationException ex) {
throw new AuthenticationException("Bad username or password for service " + provider.Name + " ex: " + ex); throw new AuthenticationException("Bad username or password for service " + provider.Name + " ex: " + ex.Message);
} }
catch (HttpRequestException ex) { catch (HttpRequestException ex) {
throw new HttpRequestException("Error retrieving devices for " + provider.Name + " ex: " + ex); throw new HttpRequestException("Error retrieving devices for " + provider.Name + " ex: " + ex.Message);
} }
catch (Exception ex) { catch (Exception ex) {
@ -111,6 +111,25 @@ namespace MyCore.Services.Devices
return createdDevice; return createdDevice;
} }
public async static Task<List<DeviceDetailDTO>> CreateFromZigbee(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId)
{
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
try
{
// TODO MQTT Connexion
// TODO Server..
MQTTService mQTTService = new MQTTService("192.168.31.140", "mqtt", "mqtt");
}
catch (UnauthorizedAccessException ex)
{
throw new UnauthorizedAccessException("Error connecting to mqtt server: " + ex.Message);
}
return createdDevice;
}
public static List<DeviceDetailDTO> CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Models.Arlo.ArloDevice> arloDevices, Provider provider) public static List<DeviceDetailDTO> CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Models.Arlo.ArloDevice> arloDevices, Provider provider)
{ {
List<DeviceDetailDTO> createdArloDevices = new List<DeviceDetailDTO>(); List<DeviceDetailDTO> createdArloDevices = new List<DeviceDetailDTO>();
@ -189,16 +208,7 @@ namespace MyCore.Services.Devices
deviceDetailDTO.Type = DeviceType.Actuator; deviceDetailDTO.Type = DeviceType.Actuator;
break; break;
} }
//deviceDetailDTO.Port = arlo.; // TO CHECK
deviceDetailDTO.FirmwareVersion = meross.firmwareVersion; // TODO deviceDetailDTO.FirmwareVersion = meross.firmwareVersion; // TODO
/*Dictionary<string, object> properties = new Dictionary<string, object>();
foreach (var property in arlo.properties)
{
properties.Add(property.Key, property.Value);
}*/ // TODO
// deviceDetailDTO.Properties = properties;
// TODO !
Dictionary<string, object> properties = new Dictionary<string, object>(); Dictionary<string, object> properties = new Dictionary<string, object>();
foreach (var property in meross.channels) foreach (var property in meross.channels)
{ {

View File

@ -31,10 +31,14 @@ namespace MyCore.Services
private YeelightService yeelightService = new YeelightService(); private YeelightService yeelightService = new YeelightService();
public MQTTService() public MQTTService(string server, string user, string password)
{ {
try try
{ {
_mqttServer = server;
_user = user;
_password = password;
// Create a new MQTT client. // Create a new MQTT client.
_client = new MqttFactory().CreateMqttClient(); _client = new MqttFactory().CreateMqttClient();
@ -53,6 +57,7 @@ namespace MyCore.Services
else else
{ {
Console.WriteLine($"Error connecting to {_mqttServer}"); Console.WriteLine($"Error connecting to {_mqttServer}");
throw new UnauthorizedAccessException("Error connecting to mqtt server: " + _mqttServer);
} }
}); });
@ -93,6 +98,9 @@ namespace MyCore.Services
var topic = e.ApplicationMessage.Topic; var topic = e.ApplicationMessage.Topic;
var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
// As soon as we received all the info =>
//_client.DisconnectAsync();
// TODO // TODO
// Here take only zigbee2mqtt * // Here take only zigbee2mqtt *
@ -109,7 +117,7 @@ namespace MyCore.Services
// Load everydevice in cache.. ? Performance ? // Load everydevice in cache.. ? Performance ?
// TODO - TO CLARIFY
switch (topic) switch (topic)
{ {
case "zigbee2mqtt/0x00158d00029a7b65": case "zigbee2mqtt/0x00158d00029a7b65":