OpenShockHttp add GetShockers method, returns list of shockerids

This commit is contained in:
glax 2024-01-19 01:54:14 +01:00
parent 895f8c528d
commit eb82034190
2 changed files with 48 additions and 1 deletions

View File

@ -7,7 +7,7 @@
<Authors>Glax</Authors>
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -3,11 +3,58 @@ using System.Text;
using CShocker.Ranges;
using CShocker.Shockers.Abstract;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
namespace CShocker.Shockers.APIS;
public class OpenShockHttp : HttpShocker
{
public List<string> GetShockers()
{
HttpRequestMessage requestDevices = new (HttpMethod.Post, $"{Endpoint}/2/devices")
{
Headers =
{
UserAgent = { new ProductInfoHeaderValue("CShocker", "1") },
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
}
};
requestDevices.Headers.Add("OpenShockToken", ApiKey);
HttpResponseMessage responseDevices = HttpClient.Send(requestDevices);
StreamReader deviceStreamReader = new(responseDevices.Content.ReadAsStream());
string deviceJson = deviceStreamReader.ReadToEnd();
this.Logger?.Log(LogLevel.Debug, $"{requestDevices.RequestUri} response: {responseDevices.StatusCode}\n{deviceJson}");
JObject deviceListJObj = JObject.Parse(deviceJson);
List<string> deviceIds = new();
foreach(JToken device in deviceListJObj.SelectTokens("data"))
deviceIds.Add(device.SelectToken("id")!.Value<string>()!);
List<string> shockerIds = new();
foreach (string deviceId in deviceIds)
{
HttpRequestMessage requestShockers = new (HttpMethod.Post, $"{Endpoint}/2/devices/{deviceId}/shockers")
{
Headers =
{
UserAgent = { new ProductInfoHeaderValue("CShocker", "1") },
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
}
};
requestShockers.Headers.Add("OpenShockToken", ApiKey);
HttpResponseMessage response = HttpClient.Send(requestShockers);
StreamReader shockerStreamReader = new(response.Content.ReadAsStream());
string shockerJson = shockerStreamReader.ReadToEnd();
this.Logger?.Log(LogLevel.Debug, $"{requestShockers.RequestUri} response: {response.StatusCode}\n{shockerJson}");
JObject shockerListJObj = JObject.Parse(shockerJson);
foreach(JToken shocker in shockerListJObj.SelectTokens("data"))
shockerIds.Add(shocker.SelectToken("id")!.Value<string>()!);
}
return shockerIds;
}
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{