Skip to content

Commit

Permalink
- Added models to post/serialize
Browse files Browse the repository at this point in the history
- Use JsonSerializerSettings with en-US culture
  • Loading branch information
KoalaBear84 committed Jan 16, 2021
1 parent a2401ac commit 590ad7d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions HAClimateDeskband/HAClimateDeskband.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
<DependentUpon>HAClimateUserControl.cs</DependentUpon>
</Compile>
<Compile Include="Library.cs" />
<Compile Include="Models\EntityModel.cs" />
<Compile Include="Models\HAClimateDeskBandSettings.cs" />
<Compile Include="Models\SetTemperatureModel.cs" />
<Compile Include="PlotViewTransparent.cs">
<SubType>Component</SubType>
</Compile>
Expand Down
20 changes: 16 additions & 4 deletions HAClimateDeskband/HAClimateUserControl.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using HAClimateDeskband.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand All @@ -23,8 +25,9 @@ public partial class HAClimateUserControl : UserControl
private PlotViewTransparent PlotViewTemperature { get; set; }
private HttpClient HttpClient { get; set; }
private HAClimateDeskBandSettings HAClimateDeskBandSettings { get; set; }
private Label LblMeasurePowerWidth;
private bool Initialized;
private readonly Label LblMeasurePowerWidth;
private readonly bool Initialized;
private readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { Culture = new CultureInfo("en-US") };

const int WindowsTaskbarSmallIconsSingleRow = 30;
const int WindowsTaskbarSmallIconsDoubleRow = WindowsTaskbarSmallIconsSingleRow * 2;
Expand Down Expand Up @@ -194,7 +197,13 @@ private void SetTemperature(double temperatureDelta)
double currentSetTemperature = climateGarageState.SelectToken(".attributes.temperature").Value<double>();
double newSetTemperature = currentSetTemperature += temperatureDelta;

HttpResponseMessage httpResponseMessage = HttpClient.PostAsync($"services/climate/set_temperature", new StringContent($"{{ \"entity_id\": \"{HAClimateDeskBandSettings.ClimateEntityId}\", \"temperature\": {newSetTemperature} }}")).GetAwaiter().GetResult();

HttpResponseMessage httpResponseMessage = HttpClient.PostAsync($"services/climate/set_temperature", new StringContent(JsonConvert.SerializeObject(new SetTemperatureModel
{
ClimateEntityId = HAClimateDeskBandSettings.ClimateEntityId,
Temperature = newSetTemperature
}, JsonSerializerSettings))).GetAwaiter().GetResult();

httpResponseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}
catch (Exception ex)
Expand Down Expand Up @@ -432,7 +441,10 @@ private void SetClimate(bool on)
{
string command = on ? "turn_on" : "turn_off";

HttpResponseMessage httpResponseMessage = HttpClient.PostAsync($"services/climate/{command}", new StringContent($"{{ \"entity_id\": \"{HAClimateDeskBandSettings.ClimateEntityId}\" }}")).GetAwaiter().GetResult();
HttpResponseMessage httpResponseMessage = HttpClient.PostAsync($"services/climate/{command}", new StringContent(JsonConvert.SerializeObject(new EntityModel
{
ClimateEntityId = HAClimateDeskBandSettings.ClimateEntityId
}, JsonSerializerSettings))).GetAwaiter().GetResult();
httpResponseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}
catch (Exception ex)
Expand Down
10 changes: 10 additions & 0 deletions HAClimateDeskband/Models/EntityModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace HAClimateDeskband.Models
{
public class EntityModel
{
[JsonProperty("entity_id")]
public string ClimateEntityId { get; set; }
}
}
10 changes: 10 additions & 0 deletions HAClimateDeskband/Models/SetTemperatureModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace HAClimateDeskband.Models
{
public class SetTemperatureModel : EntityModel
{
[JsonProperty("temperature")]
public double Temperature { get; set; }
}
}

0 comments on commit 590ad7d

Please sign in to comment.