Skip to content

Commit

Permalink
Air Client review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
uɐɯoɹ 🚀 authored and Riges committed Jan 28, 2019
1 parent b3f65a5 commit 19b7cb5
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 80 deletions.
56 changes: 56 additions & 0 deletions src/Netatmo.Tests/AirClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Flurl.Http.Testing;
using Moq;
using Netatmo.Enums;
using Netatmo.Models.Client.Air;
using NodaTime;
using Xunit;

namespace Netatmo.Tests
{
public class AirClient : IDisposable
{
public AirClient()
{
httpTest = new HttpTest();
httpTest.Configure(Configuration.ConfigureRequest);
}

public void Dispose()
{
httpTest.Dispose();
}

private readonly HttpTest httpTest;

[Fact]
public async Task GetStationsData_Should_Return_DataResponse_With_AirData()
{
var accessToken = "Super-Access-Token";
var credentialManagerMock = new Mock<ICredentialManager>();
credentialManagerMock.Setup(x => x.AccessToken).Returns(accessToken);

httpTest.RespondWith(
"{\"body\": {\"devices\": [{\"_id\": \"70:xx:xx:xx:xx:c2\",\"cipher_id\": \"enc:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa28Zn\",\"date_setup\": 1548316279,\"last_setup\": 1548316279,\"type\": \"NHC\",\"last_status_store\": 1548331813,\"module_name\": \"Indoor\",\"firmware\": 45,\"last_upgrade\": 1548316280,\"wifi_status\": 49,\"reachable\": true,\"co2_calibrating\": true,\"station_name\": \"Healthy ConX\",\"data_type\": [\"Temperature\",\"CO2\",\"Humidity\",\"Noise\",\"Pressure\",\"health_idx\"],\"place\": {\"altitude\": 491.70001220703,\"country\": \"CH\",\"timezone\": \"Europe/Zurich\",\"location\": [8.3085069,47.0450306]},\"dashboard_data\": {\"time_utc\": 1548331811,\"Temperature\": 22.1,\"CO2\": 647,\"Humidity\": 33,\"Noise\": 44,\"Pressure\": 1018.3,\"AbsolutePressure\": 960.4,\"health_idx\": 0,\"min_temp\": 17.3,\"max_temp\": 23.6,\"date_min_temp\": 1548316040,\"date_max_temp\": 1548316058}}],\"user\": {\"mail\": \"[email protected]\",\"administrative\": {\"lang\": \"en-US\",\"reg_locale\": \"en-US\",\"country\": \"CH\",\"unit\": 0,\"windunit\": 0,\"pressureunit\": 0,\"feel_like_algo\": 0}}},\"status\": \"ok\",\"time_exec\": 0.038990020751953,\"time_server\": 1548331875}");

var sut = new Netatmo.AirClient("https://api.netatmo.com/", credentialManagerMock.Object);
var result = await sut.GetHomeCoachsData();

httpTest
.ShouldHaveCalled("https://api.netatmo.com/api/gethomecoachsdata")
.WithVerb(HttpMethod.Post)
.WithOAuthBearerToken(accessToken)
.WithContentType("application/json").WithRequestJson(new GetHomeCoachsDataRequest())
.Times(1);

result.Body.Should().BeOfType<GetHomeCoachsData>();
result.Body.Devices[0].DashboardData.Noise.Should().Be(44);
result.Body.Devices[0].WifiStrength.Should().Be(WifiStrengthEnum.Good);
result.Body.Devices[0].DashboardData.HumidityPercent.Should().Be(33);
result.Body.Devices[0].Place.Timezone.Should().Be(DateTimeZoneProviders.Tzdb["Europe/Zurich"]);
}
}
}
7 changes: 5 additions & 2 deletions src/Netatmo/AirClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ public AirClient(string baseUrl, ICredentialManager credentialManager)
this.credentialManager = credentialManager;
}

public Task<DataResponse<GetHomeCoachsData>> GetHomeCoachsData()
public Task<DataResponse<GetHomeCoachsData>> GetHomeCoachsData(string deviceId = null)
{
return baseUrl
.ConfigureRequest(Configuration.ConfigureRequest)
.AppendPathSegment("/api/gethomecoachsdata")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new { })
.PostJsonAsync(new GetHomeCoachsDataRequest
{
DeviceId = deviceId
})
.ReceiveJson<DataResponse<GetHomeCoachsData>>();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Netatmo/IAirClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Netatmo
{
public interface IAirClient
{
Task<DataResponse<GetHomeCoachsData>> GetHomeCoachsData();
Task<DataResponse<GetHomeCoachsData>> GetHomeCoachsData(string deviceId = null);
}
}
7 changes: 7 additions & 0 deletions src/Netatmo/Models/Client/Air/GetHomeCoachsDataRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Netatmo.Models.Client.Air
{
public class GetHomeCoachsDataRequest
{
public string DeviceId { get; set; }
}
}
69 changes: 33 additions & 36 deletions src/Netatmo/Models/Client/Air/HomesCoachs/DashBoardData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,40 @@ namespace Netatmo.Models.Client.Air.HomesCoachs
{
public class DashBoardData
{
[JsonProperty("time_utc")]
Instant TimeUtc { get; set; }
[JsonProperty("Temperature")]
float Temperature {get;set;}
[JsonProperty("CO2")]
int CO2 {get;set;}
[JsonProperty("Humidity")]
int Humidity {get;set;}
[JsonProperty("Noise")]
float Noise {get;set;}
[JsonProperty("Pressure")]
float Pressure {get;set;}
[JsonProperty("time_utc")]
public Instant TimeUtc { get; set; }

[JsonProperty("Temperature")]
public double Temperature { get; set; }

[JsonProperty("CO2")]
public int CO2 { get; set; }

[JsonProperty("Humidity")]
public int HumidityPercent { get; set; }

[JsonProperty("Noise")]
public double Noise { get; set; }

[JsonProperty("Pressure")]
public double Pressure { get; set; }

[JsonProperty("AbsolutePressure")]
float AbsolutePressure {get;set;}
[JsonProperty("health_idx")]
float HealthIdx {get;set;}
[JsonProperty("min_temp")]
float MinTemp {get;set;}
[JsonProperty("max_temp")]
float MaxTemp {get;set;}
public double AbsolutePressure { get; set; }

[JsonProperty("health_idx")]
public HealthIdx HealthIdx { get; set; }

[JsonProperty("min_temp")]
public decimal MinTemp { get; set; }

[JsonProperty("max_temp")]
public decimal MaxTemp { get; set; }

[JsonProperty("date_min_temp")]
Instant DateMinTemp {get;set;}

[JsonProperty("date_max_temp")]
Instant DateMaxTemp {get;set;}



public Instant DateMinTemp { get; set; }

[JsonProperty("date_max_temp")]
public Instant DateMaxTemp { get; set; }
}
}
55 changes: 33 additions & 22 deletions src/Netatmo/Models/Client/Air/HomesCoachs/Devices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Netatmo.Models.Client.Weather.StationsData;
using Netatmo.Enums;
using Newtonsoft.Json;
using NodaTime;

Expand All @@ -7,54 +7,65 @@ namespace Netatmo.Models.Client.Air.HomesCoachs
public class Devices
{
[JsonProperty("_id")]
string Id { get; set; }
public string Id { get; set; }

[JsonProperty("cipher_id")]
string CipherId { get; set; }
public string CipherId { get; set; }

[JsonProperty("last_status_store")]
Instant LastStatusStore { get; set; }

[JsonProperty("modules")]
dynamic[] Modules { get; set; }
public Instant LastStatusStore { get; set; }

[JsonProperty("place")]
Place Place { get; set; }
public Place Place { get; set; }

[JsonProperty("type")]
string Type { get; set; }
public string Type { get; set; }

[JsonProperty("dashboard_data")]
DashBoardData DashboardData { get; set; }
public DashBoardData DashboardData { get; set; }

[JsonProperty("data_type")]
string[] DataType { get; set; }
public string[] DataType { get; set; }

[JsonProperty("co2_calibrating")]
bool Co2Calibrating { get; set; }
public bool Co2Calibrating { get; set; }

[JsonProperty("reachable")]
bool Reachable { get; set; }
public bool Reachable { get; set; }

[JsonProperty("date_setup")]
Instant DateSetup { get; set; }
public Instant DateSetup { get; set; }

[JsonProperty("last_setup")]
Instant LastSetup { get; set; }
public Instant LastSetup { get; set; }

[JsonProperty("module_name")]
string ModuleName { get; set; }
public string ModuleName { get; set; }

[JsonProperty("firmware")]
int Firmware { get; set; }
public int Firmware { get; set; }

[JsonProperty("last_upgrade")]
Instant LastUpgrade { get; set; }
public Instant LastUpgrade { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("wifi_status")]
int WifiStatus { get; set; }

[JsonProperty("name")]
string Name { get; set; }
public int WifiStatus { get; set; }

public WifiStrengthEnum WifiStrength
{
get
{
if (WifiStatus <= 56) return WifiStrengthEnum.Good;
if (WifiStatus <= 71) return WifiStrengthEnum.Average;
return WifiStrengthEnum.Bad;
}
}


}


}
11 changes: 11 additions & 0 deletions src/Netatmo/Models/Client/Air/HomesCoachs/HealthIdx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Netatmo.Models.Client.Air.HomesCoachs
{
public enum HealthIdx
{
Healthy = 0,
Fine = 1,
Fair = 2,
Poor = 3,
Unhealthy = 4
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using NodaTime;

namespace Netatmo.Models.Client.Weather.StationsData
namespace Netatmo.Models.Client
{
public class Place
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Netatmo.Models.Client.Weather.StationsData;
using Newtonsoft.Json;

namespace Netatmo.Models.Client.Air.HomesCoachs
namespace Netatmo.Models.Client
{
public class User
{
[JsonProperty("mail")]
public string Mail { get; set; }

[JsonProperty("administrative")]
public Administrative Administrative { get; set; }

[JsonProperty("mail")]
public string Mail { get; set; }
}
}
13 changes: 0 additions & 13 deletions src/Netatmo/Models/Client/Weather/StationsData/User.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Netatmo/Netatmo.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.2.1</Version>
<Version>1.3.0</Version>
<TargetFrameworks>netcoreapp2.2;netcoreapp2.1;netstandard2.0</TargetFrameworks>
<PackageId>Netatmo</PackageId>
<Authors>Luc FASQUELLE</Authors>
Expand Down

0 comments on commit 19b7cb5

Please sign in to comment.