mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
MC Add wip support for meross and arlo devices
This commit is contained in:
parent
7e1365c277
commit
4fe4c3ec66
@ -1,6 +1,8 @@
|
||||
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;
|
||||
@ -143,6 +145,14 @@ namespace MyCore.Controllers.Devices
|
||||
{
|
||||
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)
|
||||
{
|
||||
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||
|
||||
@ -60,6 +60,8 @@ namespace MyCore.DTO.MyControlPanel
|
||||
|
||||
public string IpAddress { get; set; }
|
||||
|
||||
public string ServiceIdentification { get; set; }
|
||||
|
||||
public bool Battery { get; set; }
|
||||
|
||||
public int BatteryStatus { get; set; }
|
||||
|
||||
@ -62,6 +62,9 @@ namespace MyCore.Models.MyControlPanel.Database
|
||||
[BsonElement("IpAddress")]
|
||||
public string IpAddress { get; set; }
|
||||
|
||||
[BsonElement("ServiceIdentification")]
|
||||
public string ServiceIdentification { get; set; }
|
||||
|
||||
[BsonElement("Battery")]
|
||||
public bool Battery { get; set; }
|
||||
|
||||
@ -116,6 +119,7 @@ namespace MyCore.Models.MyControlPanel.Database
|
||||
LastMessage = LastMessage,
|
||||
LastMessageDate = LastMessageDate,
|
||||
IpAddress = IpAddress,
|
||||
ServiceIdentification = ServiceIdentification,
|
||||
ProviderId = ProviderId,
|
||||
Groups = GroupIds,
|
||||
Properties = Properties,
|
||||
|
||||
@ -7,6 +7,8 @@ using MyCore.Services.MyControlPanel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Security.Authentication;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Services.Devices
|
||||
@ -55,6 +57,7 @@ namespace MyCore.Services.Devices
|
||||
|
||||
device.MeansOfCommunications = deviceDetailDTO.MeansOfCommunications;
|
||||
device.IpAddress = deviceDetailDTO.IpAddress;
|
||||
device.ServiceIdentification = deviceDetailDTO.ServiceIdentification;
|
||||
device.Battery = deviceDetailDTO.Battery;
|
||||
device.BatteryStatus = deviceDetailDTO.BatteryStatus;
|
||||
device.GroupIds = device.GroupIds;
|
||||
@ -76,24 +79,118 @@ namespace MyCore.Services.Devices
|
||||
|
||||
List<DeviceDetailDTO> createdDevice = new List<DeviceDetailDTO>();
|
||||
|
||||
try {
|
||||
switch (provider.Name)
|
||||
{
|
||||
case "Arlo":
|
||||
ArloService arloService = new ArloService(provider.Username, provider.Password);
|
||||
List<Models.Arlo.ArloDevice> arloDevices = new ArloService(provider.Username, provider.Password).GetAllDevices();
|
||||
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) {
|
||||
|
||||
|
||||
}
|
||||
return createdDevice;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
List<DeviceDetailDTO> createdYeelightDevices = new List<DeviceDetailDTO>();
|
||||
|
||||
@ -125,12 +125,13 @@ namespace MyCore.Services
|
||||
resultToken = JsonConvert.DeserializeObject<LoginResult>(data.ToString());
|
||||
|
||||
//SSE CONNEXION
|
||||
ConnexionToSSE();
|
||||
// ConnexionToSSE(); // TODO in Raspi not here
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
resultToken = null;
|
||||
throw new AuthenticationException("Bad service username or password");
|
||||
}
|
||||
}
|
||||
|
||||
@ -198,6 +199,8 @@ namespace MyCore.Services
|
||||
// RETRIEVE ALL ARLO DEVICES
|
||||
allArloDevices = JsonConvert.DeserializeObject<List<ArloDevice>>(data.ToString());
|
||||
}
|
||||
else
|
||||
throw new HttpRequestException("Error retrieving arlo devices");
|
||||
|
||||
return allArloDevices;
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Authentication;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -27,7 +28,7 @@ namespace MyCore.Services
|
||||
private string _logUrl = $"{_merossUrl}/v1/log/user";
|
||||
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 ResultToken resultToken;
|
||||
@ -64,11 +65,15 @@ namespace MyCore.Services
|
||||
DeviceList
|
||||
}
|
||||
|
||||
public MerossService()
|
||||
public MerossService(string username, string password)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
// TODO
|
||||
/*this.username = username;
|
||||
this.password = password;*/
|
||||
|
||||
// LOGIN
|
||||
var loginTask = Task.Run(() => PostURI(new Uri(_loginUrl), RequestType.Login));
|
||||
loginTask.Wait();
|
||||
@ -79,34 +84,41 @@ namespace MyCore.Services
|
||||
var data = ((JObject)JsonConvert.DeserializeObject(loginTask.Result))["data"];
|
||||
resultToken = JsonConvert.DeserializeObject<ResultToken>(data.ToString());
|
||||
|
||||
// RETRIEVE LOG TODO Create a GetLogs method
|
||||
/*var logTask = Task.Run(() => PostURI(new Uri(_logUrl), RequestType.Log));
|
||||
logTask.Wait();
|
||||
|
||||
if (logTask.Result != "")
|
||||
{
|
||||
data = ((JObject)JsonConvert.DeserializeObject(logTask.Result))["data"];
|
||||
}*/
|
||||
|
||||
//MQTT CONNEXION TODO Create a Connexion To MQTT method
|
||||
// ConnexionToMqtt();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
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 != "")
|
||||
{
|
||||
data = ((JObject)JsonConvert.DeserializeObject(deviceTask.Result))["data"];
|
||||
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");
|
||||
|
||||
// RETRIEVE LOG
|
||||
var logTask = Task.Run(() => PostURI(new Uri(_logUrl), RequestType.Log));
|
||||
logTask.Wait();
|
||||
|
||||
if (logTask.Result != "")
|
||||
{
|
||||
data = ((JObject)JsonConvert.DeserializeObject(logTask.Result))["data"];
|
||||
}
|
||||
|
||||
//MQTT CONNEXION
|
||||
ConnexionToMqtt();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
resultToken = null;
|
||||
}
|
||||
return allDevices;
|
||||
}
|
||||
|
||||
static async Task<string> PostURI(Uri u, RequestType requestType)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user