Removed Example Console Project
Removed Old Api Classes Added new Http Api Service and Classes TODO Dynamic Effect Methods
This commit is contained in:
@ -1,48 +1,37 @@
|
||||
using GoveeCSharpConnector.Objects;
|
||||
using GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
namespace GoveeCSharpConnector.Interfaces;
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
|
||||
public interface IGoveeApiService
|
||||
public interface IGoveeHttpService
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the required Api Key for the Govee Api.
|
||||
/// Request Api Key in the Mobile Phone App.
|
||||
/// </summary>
|
||||
/// <param name="apiKey">Api Key as String</param>
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
|
||||
void SetApiKey(string apiKey);
|
||||
|
||||
/// <summary>
|
||||
/// Returns current set Govee Api Key
|
||||
/// </summary>
|
||||
/// <returns>Govee Api Key as String</returns>
|
||||
string GetApiKey();
|
||||
|
||||
/// <summary>
|
||||
/// Removes the Set Api Key and resets the HTTP Header
|
||||
/// </summary>
|
||||
void RemoveApiKey();
|
||||
|
||||
/// <summary>
|
||||
/// Requests all Devices registered to Api Key Govee Account
|
||||
/// </summary>
|
||||
/// <returns>List of GoveeApiDevices</returns>
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
|
||||
Task<List<GoveeApiDevice>> GetDevices();
|
||||
|
||||
/// <returns>List of GoveeHttpDevices</returns>
|
||||
Task<ServiceResponse<List<GoveeHttpDevice>>> GetDevices();
|
||||
/// <summary>
|
||||
/// Requests the State of a single Govee Device
|
||||
/// </summary>
|
||||
/// <param name="deviceId">Device Id Guid as string</param>
|
||||
/// <param name="deviceModel">Device Model Number as string</param>
|
||||
/// <returns>GoveeApiStat Object</returns>
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
|
||||
Task<GoveeApiState> GetDeviceState(string deviceId, string deviceModel);
|
||||
|
||||
/// <returns>GoveeHttpState Object</returns>
|
||||
Task<ServiceResponse<GoveeHttpState>> GetDeviceState(string deviceId, string deviceModel);
|
||||
/// <summary>
|
||||
/// Sets the On/Off state of a single Govee Device
|
||||
/// </summary>
|
||||
@ -50,21 +39,7 @@ public interface IGoveeApiService
|
||||
/// <param name="deviceModel">Device Model Number as string</param>
|
||||
/// <param name="on"></param>
|
||||
/// <returns></returns>
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
|
||||
Task ToggleState(string deviceId, string deviceModel, bool on);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the Brightness in Percent of a single Govee Device
|
||||
/// </summary>
|
||||
/// <param name="deviceId">Device Id Guid as string</param>
|
||||
/// <param name="deviceModel">Device Model Number as string</param>
|
||||
/// <param name="value">Brightness in Percent as Int</param>
|
||||
/// <returns></returns>
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
|
||||
Task SetBrightness(string deviceId, string deviceModel, int value);
|
||||
|
||||
Task<ServiceResponse<bool>> SetOnOff(string deviceId, string deviceModel, bool on);
|
||||
/// <summary>
|
||||
/// Sets a Rgb Color of a single Govee Device
|
||||
/// </summary>
|
||||
@ -72,10 +47,7 @@ public interface IGoveeApiService
|
||||
/// <param name="deviceModel">Device Model Number as string</param>
|
||||
/// <param name="color">Rgb Color</param>
|
||||
/// <returns></returns>
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
|
||||
Task SetColor(string deviceId, string deviceModel, RgbColor color);
|
||||
|
||||
Task<ServiceResponse<bool>> SetColor(string deviceId, string deviceModel, RgbColor color);
|
||||
/// <summary>
|
||||
/// Sets the Color Temperature of a single Govee Device
|
||||
/// </summary>
|
||||
@ -83,7 +55,14 @@ public interface IGoveeApiService
|
||||
/// <param name="deviceModel">Device Model Number as string</param>
|
||||
/// <param name="value">Color Temp in Kelvin as Int</param>
|
||||
/// <returns></returns>
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
Task<ServiceResponse<bool>> SetColorTemp(string deviceId, string deviceModel, int value);
|
||||
/// <summary>
|
||||
/// Sets the Brightness of a single Govee Device
|
||||
/// </summary>
|
||||
/// <param name="deviceId">Device Id Guid as string</param>
|
||||
/// <param name="deviceModel">Device Model Number as string</param>
|
||||
/// <param name="value">Value 1-100</param>
|
||||
/// <returns></returns>
|
||||
Task<ServiceResponse<bool>> SetBrightness(string deviceId, string deviceModel, int value);
|
||||
|
||||
Task SetColorTemp(string deviceId, string deviceModel, int value);
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
|
||||
public class ApiResponse
|
||||
{
|
||||
public string? Message { get; set; }
|
||||
public int Code { get; set; }
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
|
||||
public class Data
|
||||
{
|
||||
public List<GoveeApiDevice> Devices { get; set; }
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
|
||||
public class GoveeApiCommand
|
||||
{
|
||||
public string Device { get; set; }
|
||||
public string Model { get; set; }
|
||||
public Command Cmd { get; set; }
|
||||
}
|
||||
|
||||
public class Command
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public object Value { get; set; }
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
|
||||
public class GoveeApiDevice
|
||||
{
|
||||
[JsonPropertyName("device")]
|
||||
public string DeviceId { get; set; }
|
||||
public string Model { get; set; }
|
||||
public string DeviceName { get; set; }
|
||||
public bool Controllable { get; set; }
|
||||
public bool Retrievable { get; set; }
|
||||
[JsonPropertyName("supportCmds")]
|
||||
public List<string> SupportedCommands { get; set; }
|
||||
public Properties Properties { get; set; }
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
|
||||
public class GoveeApiState
|
||||
{
|
||||
[JsonPropertyName("device")]
|
||||
public string DeviceId { get; set; }
|
||||
|
||||
public string Model { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public Properties Properties { get; set; }
|
||||
}
|
14
GoveeCSharpConnector/Objects/GoveeHttpDevice.cs
Normal file
14
GoveeCSharpConnector/Objects/GoveeHttpDevice.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
|
||||
public class GoveeHttpDevice
|
||||
{
|
||||
[JsonPropertyName("sku")]
|
||||
public string Model { get; set; }
|
||||
[JsonPropertyName("device")]
|
||||
public string Device { get; set; }
|
||||
[JsonPropertyName("capabilities")]
|
||||
public List<Capability> Capabilities { get; set; }
|
||||
}
|
16
GoveeCSharpConnector/Objects/GoveeHttpState.cs
Normal file
16
GoveeCSharpConnector/Objects/GoveeHttpState.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
|
||||
public class GoveeHttpState
|
||||
{
|
||||
[JsonPropertyName("requestId")]
|
||||
public string RequestId { get; set; }
|
||||
[JsonPropertyName("msg")]
|
||||
public string Msg { get; set; }
|
||||
[JsonPropertyName("code")]
|
||||
public long Code { get; set; }
|
||||
[JsonPropertyName("payload")]
|
||||
public Payload Payload { get; set; }
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
|
||||
public class GoveeResponse : ApiResponse
|
||||
{
|
||||
public Data Data { get; set; }
|
||||
}
|
13
GoveeCSharpConnector/Objects/Misc/ApiResponse.cs
Normal file
13
GoveeCSharpConnector/Objects/Misc/ApiResponse.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class ApiResponse
|
||||
{
|
||||
[JsonPropertyName("code")]
|
||||
public int Code { get; set; }
|
||||
[JsonPropertyName("message")]
|
||||
public string Message { get; set; }
|
||||
[JsonPropertyName("data")]
|
||||
public List<object> Data { get; set; }
|
||||
}
|
13
GoveeCSharpConnector/Objects/Misc/Capability.cs
Normal file
13
GoveeCSharpConnector/Objects/Misc/Capability.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class Capability
|
||||
{
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; }
|
||||
[JsonPropertyName("instance")]
|
||||
public string Instance { get; set; }
|
||||
[JsonPropertyName("state")]
|
||||
public State State { get; set; }
|
||||
}
|
21
GoveeCSharpConnector/Objects/Misc/Field.cs
Normal file
21
GoveeCSharpConnector/Objects/Misc/Field.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class Field
|
||||
{
|
||||
[JsonPropertyName("fieldName")]
|
||||
public string FieldName { get; set; }
|
||||
[JsonPropertyName("dataType")]
|
||||
public string DataType { get; set; }
|
||||
[JsonPropertyName("options")]
|
||||
public List<Option> Options { get; set; }
|
||||
[JsonPropertyName("required")]
|
||||
public bool Required { get; set; }
|
||||
[JsonPropertyName("range")]
|
||||
public Range Range { get; set; }
|
||||
[JsonPropertyName("unit")]
|
||||
public string Unit { get; set; }
|
||||
[JsonPropertyName("reauired")]
|
||||
public bool? Reauired { get; set; }
|
||||
}
|
11
GoveeCSharpConnector/Objects/Misc/Option.cs
Normal file
11
GoveeCSharpConnector/Objects/Misc/Option.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class Option
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
[JsonPropertyName("value")]
|
||||
public int Value { get; set; }
|
||||
}
|
17
GoveeCSharpConnector/Objects/Misc/Parameters.cs
Normal file
17
GoveeCSharpConnector/Objects/Misc/Parameters.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class Parameters
|
||||
{
|
||||
[JsonPropertyName("dataType")]
|
||||
public string DataType { get; set; }
|
||||
[JsonPropertyName("options")]
|
||||
public List<Option> Options { get; set; }
|
||||
[JsonPropertyName("unit")]
|
||||
public string Unit { get; set; }
|
||||
[JsonPropertyName("range")]
|
||||
public Range Range { get; set; }
|
||||
[JsonPropertyName("fields")]
|
||||
public List<Field> Fields { get; set; }
|
||||
}
|
13
GoveeCSharpConnector/Objects/Misc/Payload.cs
Normal file
13
GoveeCSharpConnector/Objects/Misc/Payload.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class Payload
|
||||
{
|
||||
[JsonPropertyName("sku")]
|
||||
public string Model { get; set; }
|
||||
[JsonPropertyName("device")]
|
||||
public string Device { get; set; }
|
||||
[JsonPropertyName("capabilities")]
|
||||
public List<Capability> Capabilities { get; set; }
|
||||
}
|
@ -1,12 +1,18 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using GoveeCSharpConnector.Enums;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects;
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class Properties
|
||||
{
|
||||
[JsonPropertyName("online")]
|
||||
public bool Online { get; set; }
|
||||
[JsonPropertyName("powerState")]
|
||||
public PowerState PowerState { get; set; }
|
||||
[JsonPropertyName("brightness")]
|
||||
public int Brightness { get; set; }
|
||||
[JsonPropertyName("colorTemp")]
|
||||
public int ColorTemp { get; set; }
|
||||
[JsonPropertyName("color")]
|
||||
public RgbColor Color { get; set; }
|
||||
}
|
13
GoveeCSharpConnector/Objects/Misc/Range.cs
Normal file
13
GoveeCSharpConnector/Objects/Misc/Range.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class Range
|
||||
{
|
||||
[JsonPropertyName("min")]
|
||||
public int Min { get; set; }
|
||||
[JsonPropertyName("max")]
|
||||
public int Max { get; set; }
|
||||
[JsonPropertyName("precision")]
|
||||
public int Precision { get; set; }
|
||||
}
|
8
GoveeCSharpConnector/Objects/Misc/ServiceResponse.cs
Normal file
8
GoveeCSharpConnector/Objects/Misc/ServiceResponse.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class ServiceResponse<T>
|
||||
{
|
||||
public T? Data { get; set; }
|
||||
public bool Success { get; set; } = true;
|
||||
public string Message { get; set; } = string.Empty;
|
||||
}
|
9
GoveeCSharpConnector/Objects/Misc/State.cs
Normal file
9
GoveeCSharpConnector/Objects/Misc/State.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
public class State
|
||||
{
|
||||
[JsonPropertyName("value")]
|
||||
public object Value { get; set; }
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using GoveeCSharpConnector.Interfaces;
|
||||
using GoveeCSharpConnector.Objects;
|
||||
|
||||
namespace GoveeCSharpConnector.Services;
|
||||
|
||||
[Obsolete("New Api Reference: https://developer.govee.com/reference/" ,true)]
|
||||
public class GoveeApiService : IGoveeApiService
|
||||
{
|
||||
private string _apiKey = string.Empty;
|
||||
private const string GoveeApiAddress = "https://developer-api.govee.com/v1";
|
||||
private readonly HttpClient _httpClient = new();
|
||||
private readonly JsonSerializerOptions? _jsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
};
|
||||
/// <inheritdoc/>
|
||||
public void SetApiKey(string apiKey)
|
||||
{
|
||||
_apiKey = apiKey;
|
||||
_httpClient.DefaultRequestHeaders.Add("Govee-API-Key", _apiKey);
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public string GetApiKey()
|
||||
{
|
||||
return _apiKey;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public void RemoveApiKey()
|
||||
{
|
||||
_apiKey = string.Empty;
|
||||
_httpClient.DefaultRequestHeaders.Remove("Govee-Api-Key");
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task<List<GoveeApiDevice>> GetDevices()
|
||||
{
|
||||
var response = await _httpClient.GetFromJsonAsync<GoveeResponse>($"{GoveeApiAddress}/devices");
|
||||
|
||||
return response.Data.Devices;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task<GoveeApiState> GetDeviceState(string deviceId, string deviceModel)
|
||||
{
|
||||
return await _httpClient.GetFromJsonAsync<GoveeApiState>($"{GoveeApiAddress}/devices/state?device={deviceId}&model={deviceModel}");
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task ToggleState(string deviceId, string deviceModel, bool on)
|
||||
{
|
||||
await SendCommand(deviceId, deviceModel, "turn", on ? "on" : "off");
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task SetBrightness(string deviceId, string deviceModel, int value)
|
||||
{
|
||||
await SendCommand(deviceId, deviceModel, "brightness", value);
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task SetColor(string deviceId, string deviceModel, RgbColor color)
|
||||
{
|
||||
await SendCommand(deviceId, deviceModel, "color", color);
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task SetColorTemp(string deviceId, string deviceModel, int value)
|
||||
{
|
||||
await SendCommand(deviceId, deviceModel, "colorTem", value);
|
||||
}
|
||||
|
||||
private async Task SendCommand(string deviceId, string deviceModel, string command, object commandObject)
|
||||
{
|
||||
var commandRequest = new GoveeApiCommand()
|
||||
{
|
||||
Device = deviceId,
|
||||
Model = deviceModel,
|
||||
Cmd = new Command()
|
||||
{
|
||||
Name = command,
|
||||
Value = commandObject
|
||||
}
|
||||
};
|
||||
var httpContent = new StringContent(JsonSerializer.Serialize(commandRequest, _jsonOptions), Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.PutAsync($"{GoveeApiAddress}/devices/control", httpContent);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new Exception($"Govee Api Request failed. Status code: {response.StatusCode}, Message: {response.Content}");
|
||||
}
|
||||
}
|
214
GoveeCSharpConnector/Services/GoveeHttpService.cs
Normal file
214
GoveeCSharpConnector/Services/GoveeHttpService.cs
Normal file
@ -0,0 +1,214 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using GoveeCSharpConnector.Interfaces;
|
||||
using GoveeCSharpConnector.Objects;
|
||||
using GoveeCSharpConnector.Objects.Misc;
|
||||
|
||||
namespace GoveeCSharpConnector.Services;
|
||||
|
||||
public class GoveeHttpService : IGoveeHttpService
|
||||
{
|
||||
private string _apiKey = string.Empty;
|
||||
private const string GoveeApiAddress = "https://openapi.api.govee.com";
|
||||
private const string GoveeDevicesEndpoint = "/router/api/v1/user/devices";
|
||||
private const string GoveeControlEndpoint = "/router/api/v1/device/control";
|
||||
private const string GoveeStateEndpoint = "/router/api/v1/device/state";
|
||||
private readonly HttpClient _httpClient = new();
|
||||
private readonly JsonSerializerOptions? _jsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
};
|
||||
|
||||
public GoveeHttpService()
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public void SetApiKey(string apiKey)
|
||||
{
|
||||
_apiKey = apiKey;
|
||||
_httpClient.DefaultRequestHeaders.Add("Govee-API-Key", _apiKey);
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public string GetApiKey()
|
||||
{
|
||||
return _apiKey;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public void RemoveApiKey()
|
||||
{
|
||||
_apiKey = string.Empty;
|
||||
_httpClient.DefaultRequestHeaders.Remove("Govee-API-Key");
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task<ServiceResponse<List<GoveeHttpDevice>>> GetDevices()
|
||||
{
|
||||
var serviceResponse = new ServiceResponse<List<GoveeHttpDevice>>();
|
||||
var response = await _httpClient.GetFromJsonAsync<ApiResponse>($"{GoveeApiAddress}{GoveeDevicesEndpoint}");
|
||||
if (response.Code != 200)
|
||||
{
|
||||
if (response.Code == 429)
|
||||
{
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = "Api Limit reached! 10000/Account/Day";
|
||||
return serviceResponse;
|
||||
}
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = response.Message;
|
||||
return serviceResponse;
|
||||
}
|
||||
var allDevices = response.Data.OfType<GoveeHttpDevice>().ToList();
|
||||
|
||||
var devices = (from device in allDevices where device.Capabilities.Exists(x => x.Type == "devices.capabilities.on_off")
|
||||
where device.Capabilities.Exists(x => x.Type == "devices.capabilities.color_setting")
|
||||
where device.Capabilities.Exists(x => x.Type == "devices.capabilities.range")
|
||||
where device.Capabilities.Exists(x => x.Type == "devices.capabilities.dynamic_scene")
|
||||
select device).ToList();
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Data = devices;
|
||||
serviceResponse.Message = "Request Successful";
|
||||
return serviceResponse;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task<ServiceResponse<GoveeHttpState>> GetDeviceState(string deviceId, string deviceModel)
|
||||
{
|
||||
var serviceResponse = new ServiceResponse<GoveeHttpState>();
|
||||
|
||||
var jsonPayload = $@"
|
||||
{{
|
||||
""requestId"": ""uuid"",
|
||||
""payload"": {{
|
||||
""sku"": ""{deviceModel}"",
|
||||
""device"": ""{deviceId}""
|
||||
}}
|
||||
}}";
|
||||
|
||||
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.PostAsync($"{GoveeApiAddress}{GoveeStateEndpoint}", content);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = response.ReasonPhrase;
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
var state = await response.Content.ReadFromJsonAsync<GoveeHttpState>();
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Message = "";
|
||||
serviceResponse.Data = state;
|
||||
return serviceResponse;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task<ServiceResponse<bool>> SetOnOff(string deviceId, string deviceModel, bool on)
|
||||
{
|
||||
var serviceResponse = new ServiceResponse<bool>();
|
||||
|
||||
var value = "0";
|
||||
if (on)
|
||||
{
|
||||
value = "1";
|
||||
}
|
||||
|
||||
var jsonPayload = $@"
|
||||
{{
|
||||
""requestId"": ""uuid"",
|
||||
""payload"": {{
|
||||
""sku"": ""{deviceModel}"",
|
||||
""device"": ""{deviceId}"",
|
||||
""capability"": {{
|
||||
""type"": ""devices.capabilities.on_off"",
|
||||
""instance"": ""powerSwitch"",
|
||||
""value"": {value}
|
||||
}}
|
||||
}}
|
||||
}}";
|
||||
|
||||
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.PostAsync($"{GoveeApiAddress}{GoveeStateEndpoint}", content);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = response.ReasonPhrase;
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Message = "";
|
||||
return serviceResponse;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task<ServiceResponse<bool>> SetColor(string deviceId, string deviceModel, RgbColor color)
|
||||
{
|
||||
var serviceResponse = new ServiceResponse<bool>();
|
||||
|
||||
var value = ((color.R & 0xFF) << 16) | ((color.G & 0xFF) << 8) | (color.B & 0xFF);
|
||||
|
||||
var jsonPayload = $@"
|
||||
{{
|
||||
""requestId"": ""uuid"",
|
||||
""payload"": {{
|
||||
""sku"": ""{deviceModel}"",
|
||||
""device"": ""{deviceId}"",
|
||||
""capability"": {{
|
||||
""type"": ""devices.capabilities.color_setting"",
|
||||
""instance"": ""colorRgb"",
|
||||
""value"": {value}
|
||||
}}
|
||||
}}
|
||||
}}";
|
||||
|
||||
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.PostAsync($"{GoveeApiAddress}{GoveeStateEndpoint}", content);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = response.ReasonPhrase;
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Message = "";
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
public Task<ServiceResponse<bool>> SetColorTemp(string deviceId, string deviceModel, int value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<ServiceResponse<bool>> SetBrightness(string deviceId, string deviceModel, int value)
|
||||
{
|
||||
var serviceResponse = new ServiceResponse<bool>();
|
||||
|
||||
var jsonPayload = $@"
|
||||
{{
|
||||
""requestId"": ""uuid"",
|
||||
""payload"": {{
|
||||
""sku"": ""{deviceModel}"",
|
||||
""device"": ""{deviceId}"",
|
||||
""capability"": {{
|
||||
""type"": ""devices.capabilities.range"",
|
||||
""instance"": ""brightness"",
|
||||
""value"": {value}
|
||||
}}
|
||||
}}
|
||||
}}";
|
||||
|
||||
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.PostAsync($"{GoveeApiAddress}{GoveeStateEndpoint}", content);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
serviceResponse.Success = false;
|
||||
serviceResponse.Message = response.ReasonPhrase;
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Message = "";
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user