MC Add wip support for meross and arlo devices

This commit is contained in:
Thomas Fransolet 2020-03-31 11:07:23 +02:00
parent 7e1365c277
commit 4fe4c3ec66
6 changed files with 160 additions and 32 deletions

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Security.Authentication;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -143,6 +145,14 @@ namespace MyCore.Controllers.Devices
{ {
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
} }
catch (AuthenticationException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 401 };
}
catch (HttpRequestException ex) // Not ok if not http request..
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 421 };
}
catch (Exception ex) catch (Exception ex)
{ {
return new ObjectResult(ex.Message) { StatusCode = 500 }; return new ObjectResult(ex.Message) { StatusCode = 500 };

View File

@ -60,6 +60,8 @@ namespace MyCore.DTO.MyControlPanel
public string IpAddress { get; set; } public string IpAddress { get; set; }
public string ServiceIdentification { get; set; }
public bool Battery { get; set; } public bool Battery { get; set; }
public int BatteryStatus { get; set; } public int BatteryStatus { get; set; }

View File

@ -62,6 +62,9 @@ namespace MyCore.Models.MyControlPanel.Database
[BsonElement("IpAddress")] [BsonElement("IpAddress")]
public string IpAddress { get; set; } public string IpAddress { get; set; }
[BsonElement("ServiceIdentification")]
public string ServiceIdentification { get; set; }
[BsonElement("Battery")] [BsonElement("Battery")]
public bool Battery { get; set; } public bool Battery { get; set; }
@ -116,6 +119,7 @@ namespace MyCore.Models.MyControlPanel.Database
LastMessage = LastMessage, LastMessage = LastMessage,
LastMessageDate = LastMessageDate, LastMessageDate = LastMessageDate,
IpAddress = IpAddress, IpAddress = IpAddress,
ServiceIdentification = ServiceIdentification,
ProviderId = ProviderId, ProviderId = ProviderId,
Groups = GroupIds, Groups = GroupIds,
Properties = Properties, Properties = Properties,

View File

@ -7,6 +7,8 @@ using MyCore.Services.MyControlPanel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Security.Authentication;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MyCore.Services.Devices namespace MyCore.Services.Devices
@ -55,6 +57,7 @@ namespace MyCore.Services.Devices
device.MeansOfCommunications = deviceDetailDTO.MeansOfCommunications; device.MeansOfCommunications = deviceDetailDTO.MeansOfCommunications;
device.IpAddress = deviceDetailDTO.IpAddress; device.IpAddress = deviceDetailDTO.IpAddress;
device.ServiceIdentification = deviceDetailDTO.ServiceIdentification;
device.Battery = deviceDetailDTO.Battery; device.Battery = deviceDetailDTO.Battery;
device.BatteryStatus = deviceDetailDTO.BatteryStatus; device.BatteryStatus = deviceDetailDTO.BatteryStatus;
device.GroupIds = device.GroupIds; device.GroupIds = device.GroupIds;
@ -76,22 +79,116 @@ namespace MyCore.Services.Devices
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>(); List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
switch (provider.Name) try {
{ switch (provider.Name)
case "Arlo": {
ArloService arloService = new ArloService(provider.Username, provider.Password); case "Arlo":
break; List<Models.Arlo.ArloDevice> arloDevices = new ArloService(provider.Username, provider.Password).GetAllDevices();
case "Meross": createdDevice = CreateArloDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, arloDevices, provider);
break;
case "Meross":
List<Models.Meross.MerossDevice> merossDevices = new MerossService(provider.Username, provider.Password).GetMerossDevices();
createdDevice = CreateMerossDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, merossDevices, provider);
break;
case "Yeelight":
List<YeelightAPI.Device> yeelightDevices = await new YeelightService().GetDevices();
createdDevice = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, yeelightDevices, provider);
break;
}
}
catch (AuthenticationException ex) {
throw new AuthenticationException("Bad username or password for service " + provider.Name + " ex: " + ex);
}
catch (HttpRequestException ex) {
throw new HttpRequestException("Error retrieving devices for " + provider.Name + " ex: " + ex);
}
catch (Exception ex) {
break; }
case "Yeelight": return createdDevice;
List<YeelightAPI.Device> yeelightDevices = await new YeelightService().GetDevices(); }
createdDevice = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, yeelightDevices, provider);
break; public static List<DeviceDetailDTO> CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, List<Models.Arlo.ArloDevice> arloDevices, Provider provider)
{
List<DeviceDetailDTO> createdArloDevices = new List<DeviceDetailDTO>();
foreach (var arlo in arloDevices)
{
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
deviceDetailDTO.Name = arlo.deviceName;
deviceDetailDTO.IpAddress = arlo.deviceId; // TO CHECK
deviceDetailDTO.ServiceIdentification = arlo.deviceId;
deviceDetailDTO.ProviderId = provider.Id;
deviceDetailDTO.ConnectionStatus = arlo.connectivity.connected ? ConnectionStatus.Connected : ConnectionStatus.Unknown;
deviceDetailDTO.Status = arlo.connectivity.connected ? true : false; // TODO STATE
deviceDetailDTO.Model = arlo.deviceType;
//deviceDetailDTO.Port = arlo.; // TO CHECK
deviceDetailDTO.FirmwareVersion = arlo.interfaceVersion; // 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 !
List<string> supportedOperationsDTO = new List<string>();
supportedOperationsDTO.Add(arlo.lastImageUploaded.GetType().Name + " : " + arlo.lastImageUploaded);
supportedOperationsDTO.Add(arlo.presignedLastImageUrl.GetType().Name + " : " + arlo.presignedLastImageUrl);
supportedOperationsDTO.Add(arlo.presignedFullFrameSnapshotUrl.GetType().Name + " : " + arlo.presignedFullFrameSnapshotUrl);
supportedOperationsDTO.Add(arlo.presignedSnapshotUrl.GetType().Name + " : " + arlo.presignedSnapshotUrl);
deviceDetailDTO.SupportedOperations = supportedOperationsDTO;
deviceDetailDTO.MeansOfCommunications = new List<MeansOfCommunication>();
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi); // TO CHECK
deviceDetailDTO.CreatedDate = DateTime.Now;
deviceDetailDTO.UpdatedDate = DateTime.Now;
createdArloDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, deviceDetailDTO, true));
} }
return createdArloDevices;
}
return createdDevice; public static List<DeviceDetailDTO> CreateMerossDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, List<Models.Meross.MerossDevice> merossDevices, Provider provider)
{
List<DeviceDetailDTO> createdMerossDevices = new List<DeviceDetailDTO>();
foreach (var meross in merossDevices)
{
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
deviceDetailDTO.Name = meross.devName;
deviceDetailDTO.IpAddress = meross.uuid; // TO CHECK
deviceDetailDTO.ServiceIdentification = meross.uuid;
deviceDetailDTO.ProviderId = provider.Id;
deviceDetailDTO.ConnectionStatus = meross.onlineStatus == 1 ? ConnectionStatus.Connected : ConnectionStatus.Disconnected;
// deviceDetailDTO.Status = meross. ? true : false; // TODO STATE
deviceDetailDTO.Model = meross.deviceType;
//deviceDetailDTO.Port = arlo.; // TO CHECK
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>();
foreach (var property in meross.channels)
{
properties.Add(property.devName, property.type);
}
// deviceDetailDTO.SupportedOperations = supportedOperationsDTO; TODO
deviceDetailDTO.MeansOfCommunications = new List<MeansOfCommunication>();
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi); // TO CHECK
deviceDetailDTO.CreatedDate = DateTime.Now;
deviceDetailDTO.UpdatedDate = DateTime.Now;
createdMerossDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, deviceDetailDTO, true));
}
return createdMerossDevices;
} }
public static List<DeviceDetailDTO> CreateYeelightDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, List<YeelightAPI.Device> yeelightDevices, Provider provider) public static List<DeviceDetailDTO> CreateYeelightDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, List<YeelightAPI.Device> yeelightDevices, Provider provider)

View File

@ -125,12 +125,13 @@ namespace MyCore.Services
resultToken = JsonConvert.DeserializeObject<LoginResult>(data.ToString()); resultToken = JsonConvert.DeserializeObject<LoginResult>(data.ToString());
//SSE CONNEXION //SSE CONNEXION
ConnexionToSSE(); // ConnexionToSSE(); // TODO in Raspi not here
} }
} }
catch (Exception e) catch (Exception e)
{ {
resultToken = null; resultToken = null;
throw new AuthenticationException("Bad service username or password");
} }
} }
@ -198,6 +199,8 @@ namespace MyCore.Services
// RETRIEVE ALL ARLO DEVICES // RETRIEVE ALL ARLO DEVICES
allArloDevices = JsonConvert.DeserializeObject<List<ArloDevice>>(data.ToString()); allArloDevices = JsonConvert.DeserializeObject<List<ArloDevice>>(data.ToString());
} }
else
throw new HttpRequestException("Error retrieving arlo devices");
return allArloDevices; return allArloDevices;
} }

View File

@ -11,6 +11,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Security.Authentication;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -27,7 +28,7 @@ namespace MyCore.Services
private string _logUrl = $"{_merossUrl}/v1/log/user"; private string _logUrl = $"{_merossUrl}/v1/log/user";
private string _devList = $"{_merossUrl}/v1/Device/devList"; private string _devList = $"{_merossUrl}/v1/Device/devList";
private static string username = "thomas.fransolet@hotmail.be"; private string username = "thomas.fransolet@hotmail.be";
private static string password = "Coconuts07"; private static string password = "Coconuts07";
private static ResultToken resultToken; private static ResultToken resultToken;
@ -64,11 +65,15 @@ namespace MyCore.Services
DeviceList DeviceList
} }
public MerossService() public MerossService(string username, string password)
{ {
try try
{ {
// TODO
/*this.username = username;
this.password = password;*/
// LOGIN // LOGIN
var loginTask = Task.Run(() => PostURI(new Uri(_loginUrl), RequestType.Login)); var loginTask = Task.Run(() => PostURI(new Uri(_loginUrl), RequestType.Login));
loginTask.Wait(); loginTask.Wait();
@ -79,36 +84,43 @@ namespace MyCore.Services
var data = ((JObject)JsonConvert.DeserializeObject(loginTask.Result))["data"]; var data = ((JObject)JsonConvert.DeserializeObject(loginTask.Result))["data"];
resultToken = JsonConvert.DeserializeObject<ResultToken>(data.ToString()); resultToken = JsonConvert.DeserializeObject<ResultToken>(data.ToString());
// GET DEVICE LIST // RETRIEVE LOG TODO Create a GetLogs method
var deviceTask = Task.Run(() => PostURI(new Uri(_devList), RequestType.DeviceList)); /*var logTask = Task.Run(() => PostURI(new Uri(_logUrl), RequestType.Log));
deviceTask.Wait();
if (deviceTask.Result != "")
{
data = ((JObject)JsonConvert.DeserializeObject(deviceTask.Result))["data"];
// RETRIEVE ALL DEVICES
allDevices = JsonConvert.DeserializeObject<List<MerossDevice>>(data.ToString());
}
// RETRIEVE LOG
var logTask = Task.Run(() => PostURI(new Uri(_logUrl), RequestType.Log));
logTask.Wait(); logTask.Wait();
if (logTask.Result != "") if (logTask.Result != "")
{ {
data = ((JObject)JsonConvert.DeserializeObject(logTask.Result))["data"]; data = ((JObject)JsonConvert.DeserializeObject(logTask.Result))["data"];
} }*/
//MQTT CONNEXION //MQTT CONNEXION TODO Create a Connexion To MQTT method
ConnexionToMqtt(); // ConnexionToMqtt();
} }
} }
catch (Exception e) catch (Exception e)
{ {
resultToken = null; resultToken = null;
throw new AuthenticationException("Bad service username or password");
} }
} }
public List<MerossDevice> GetMerossDevices() {
// GET DEVICE LIST
var deviceTask = Task.Run(() => PostURI(new Uri(_devList), RequestType.DeviceList));
deviceTask.Wait();
if (deviceTask.Result != "")
{
var data = ((JObject)JsonConvert.DeserializeObject(deviceTask.Result))["data"];
// RETRIEVE ALL DEVICES
allDevices = JsonConvert.DeserializeObject<List<MerossDevice>>(data.ToString());
}
else
throw new HttpRequestException("Error retrieving meross devices");
return allDevices;
}
static async Task<string> PostURI(Uri u, RequestType requestType) static async Task<string> PostURI(Uri u, RequestType requestType)
{ {
var response = string.Empty; var response = string.Empty;