From 4d258049c5b02341c61368a9b69b42314e6b8928 Mon Sep 17 00:00:00 2001 From: Andrew Gould Date: Wed, 18 Aug 2021 17:04:52 +0100 Subject: [PATCH] Implemented HVAC functionality --- MazdaApiLib/MazdaApiClient.cs | 28 ++++++++++++++ MazdaApiLib/MazdaApiController.cs | 20 ++++++++++ MazdaApiLib/MazdaApiLib.csproj | 6 +-- MazdaApiLib/MazdaApiLib.xml | 28 ++++++++++++++ MazdaApiLib/Model/HvacSettings.cs | 49 ++++++++++++++++++++++++ MazdaApiLib/README.md | 62 +++++++++++++++++++++++++++++++ 6 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 MazdaApiLib/Model/HvacSettings.cs diff --git a/MazdaApiLib/MazdaApiClient.cs b/MazdaApiLib/MazdaApiClient.cs index aada0bb..bc5910b 100644 --- a/MazdaApiLib/MazdaApiClient.cs +++ b/MazdaApiLib/MazdaApiClient.cs @@ -82,6 +82,34 @@ public MazdaApiClient(string emailAddress, string password, string region, HttpC } } + /// + /// Update the HVAC settings for a given vehicle. + /// + /// The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles + /// An HvacSetting object containing the values to be updated + public void SetHvacSettings(string internalVin, HvacSettings hvacSettings) => SetHvacSettingsAsync(internalVin, hvacSettings).Wait(); + + /// + /// Update the HVAC settings for a given vehicle asynchronously. + /// + /// The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles + /// An HvacSetting object containing the values to be updated + public Task SetHvacSettingsAsync(string internalVin, HvacSettings hvacSettings) => _controller.SetHvacSettingsAsync(internalVin, hvacSettings); + + /// + /// Get the HVAC settings for a given vehicle asynchronously. + /// + /// The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles + /// An HvacSettings object containing the HVAC settings + public HvacSettings GetHvacSettings(string internalVin) => GetHvacSettingsAsync(internalVin).GetAwaiter().GetResult(); + + /// + /// Get the HVAC settings for a given vehicle asynchronously. + /// + /// The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles + /// An HvacSettings object containing the HVAC settings + public Task GetHvacSettingsAsync(string internalVin) => _controller.GetHvacSettingsAsync(internalVin); + /// /// Get the remote permissions for a given vehicle. /// diff --git a/MazdaApiLib/MazdaApiController.cs b/MazdaApiLib/MazdaApiController.cs index 51548f5..ea65683 100644 --- a/MazdaApiLib/MazdaApiController.cs +++ b/MazdaApiLib/MazdaApiController.cs @@ -74,6 +74,25 @@ public async Task GetVehicleStatusAsync(string internalVin) throw new MazdaApiException("Failed to get vehicle status"); } + public async Task SetHvacSettingsAsync(string internalVin, HvacSettings hvacSettings) + { + ApiResult result = JsonConvert.DeserializeObject(await _connection.ApiRequestAsync(HttpMethod.Post, "/remoteServices/updateHVACSetting/v4", $"{{\"internaluserid\": \"__INTERNAL_ID__\", \"internalvin\": {internalVin}, \"hvacsettings\": {JsonConvert.SerializeObject(hvacSettings)}}}", true, true)); + + if ((result?.ResultCode ?? string.Empty) != "200S00") + { + throw new MazdaApiException("Failed to update hvac settings"); + } + } + + public async Task GetHvacSettingsAsync(string vin) + { + ApiResult result = JsonConvert.DeserializeObject(await _connection.ApiRequestAsync(HttpMethod.Post, "/remoteServices/getHVACSetting/v4", $"{{\"internaluserid\": \"__INTERNAL_ID__\", \"vin\": \"{vin}\"}}", true, true)); + + if ((result?.ResultCode ?? string.Empty) == "200S00") return result?.HvacSettings; + + throw new MazdaApiException("Failed to get hvac settings"); + } + public async Task GetRemotePermissionsAsync(string vin) { ApiResult result = JsonConvert.DeserializeObject(await _connection.ApiRequestAsync(HttpMethod.Post, "/remoteServices/getRemoteControlPermission/v4", $"{{\"internaluserid\": \"__INTERNAL_ID__\", \"vin\": \"{vin}\"}}", true, true)); @@ -124,6 +143,7 @@ public async Task GetNicknameAsync(string vin) private class ApiResult { public string ResultCode { get; set; } + public HvacSettings HvacSettings { get; set; } public string AvailableService { get; set; } public string CarlineDesc { get; set; } public object RemoteControl { get; set; } diff --git a/MazdaApiLib/MazdaApiLib.csproj b/MazdaApiLib/MazdaApiLib.csproj index f80f685..ff1dfaf 100644 --- a/MazdaApiLib/MazdaApiLib.csproj +++ b/MazdaApiLib/MazdaApiLib.csproj @@ -8,9 +8,9 @@ LICENSE true - 0.3.0.0 - 0.3.0.0 - 0.3.0 + 0.4.0.0 + 0.4.0.0 + 0.4.0 diff --git a/MazdaApiLib/MazdaApiLib.xml b/MazdaApiLib/MazdaApiLib.xml index 3710121..f2669ff 100644 --- a/MazdaApiLib/MazdaApiLib.xml +++ b/MazdaApiLib/MazdaApiLib.xml @@ -85,6 +85,34 @@ A flag that when set to true caches calls to methods that return vehicles. (Optional, defaults to false) An ILogger that can be used for debugging and tracing purposes. (Optional, defaults to null) + + + Update the HVAC settings for a given vehicle. + + The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles + An HvacSetting object containing the values to be updated + + + + Update the HVAC settings for a given vehicle asynchronously. + + The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles + An HvacSetting object containing the values to be updated + + + + Get the HVAC settings for a given vehicle asynchronously. + + The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles + An HvacSettings object containing the HVAC settings + + + + Get the HVAC settings for a given vehicle asynchronously. + + The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles + An HvacSettings object containing the HVAC settings + Get the remote permissions for a given vehicle. diff --git a/MazdaApiLib/Model/HvacSettings.cs b/MazdaApiLib/Model/HvacSettings.cs new file mode 100644 index 0000000..9c6ee88 --- /dev/null +++ b/MazdaApiLib/Model/HvacSettings.cs @@ -0,0 +1,49 @@ +// +// HvacSettings.cs +// +// MIT License +// +// Copyright (c) 2021 Andrew Gould +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +using Newtonsoft.Json; + +namespace WingandPrayer.MazdaApi.Model +{ + public enum TemperatureType + { + Celsius = 1, + Farenheit = 2 + } + + public class HvacSettings + { + [JsonConverter(typeof(BoolConverter))] + public bool FrontDefroster { get; set; } + + [JsonConverter(typeof(BoolConverter))] + public bool RearDefogger { get; set; } + + public double Temperature { get; set; } + + public TemperatureType TemperatureType { get; set; } + } +} \ No newline at end of file diff --git a/MazdaApiLib/README.md b/MazdaApiLib/README.md index 3551bfb..3a43e58 100644 --- a/MazdaApiLib/README.md +++ b/MazdaApiLib/README.md @@ -162,6 +162,40 @@ An EvVehicleStatus | ---- | ---- | ----------- | | internalVin | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles | + +### GetHvacSettings(internalVin) `method` + +##### Summary + +Get the HVAC settings for a given vehicle asynchronously. + +##### Returns + +An HvacSettings object containing the HVAC settings + +##### Parameters + +| Name | Type | Description | +| ---- | ---- | ----------- | +| internalVin | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles | + + +### GetHvacSettingsAsync(internalVin) `method` + +##### Summary + +Get the HVAC settings for a given vehicle asynchronously. + +##### Returns + +An HvacSettings object containing the HVAC settings + +##### Parameters + +| Name | Type | Description | +| ---- | ---- | ----------- | +| internalVin | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles | + ### GetRawVehicleStatus(internalVin) `method` @@ -358,6 +392,34 @@ Locks the doors of the vehicle asynchronously. | ---- | ---- | ----------- | | internalVin | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles | + +### SetHvacSettings(internalVin,hvacSettings) `method` + +##### Summary + +Update the HVAC settings for a given vehicle. + +##### Parameters + +| Name | Type | Description | +| ---- | ---- | ----------- | +| internalVin | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles | +| hvacSettings | [WingandPrayer.MazdaApi.Model.HvacSettings](#T-WingandPrayer-MazdaApi-Model-HvacSettings 'WingandPrayer.MazdaApi.Model.HvacSettings') | An HvacSetting object containing the values to be updated | + + +### SetHvacSettingsAsync(internalVin,hvacSettings) `method` + +##### Summary + +Update the HVAC settings for a given vehicle asynchronously. + +##### Parameters + +| Name | Type | Description | +| ---- | ---- | ----------- | +| internalVin | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The internal vehicle identity number for the vehicle which can be found with calls to methods that return vehicles | +| hvacSettings | [WingandPrayer.MazdaApi.Model.HvacSettings](#T-WingandPrayer-MazdaApi-Model-HvacSettings 'WingandPrayer.MazdaApi.Model.HvacSettings') | An HvacSetting object containing the values to be updated | + ### TurnOffHazzardLights(internalVin) `method`