OpenShockHttp add GetShockers method, returns list of shockerids
This commit is contained in:
parent
895f8c528d
commit
eb82034190
@ -7,7 +7,7 @@
|
|||||||
<Authors>Glax</Authors>
|
<Authors>Glax</Authors>
|
||||||
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
|
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<Version>1.2.0</Version>
|
<Version>1.2.1</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -3,11 +3,58 @@ using System.Text;
|
|||||||
using CShocker.Ranges;
|
using CShocker.Ranges;
|
||||||
using CShocker.Shockers.Abstract;
|
using CShocker.Shockers.Abstract;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace CShocker.Shockers.APIS;
|
namespace CShocker.Shockers.APIS;
|
||||||
|
|
||||||
public class OpenShockHttp : HttpShocker
|
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)
|
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user