Compare commits

...

6 Commits

Author SHA1 Message Date
34fb4d89bd Fix Default endpoint for openshock 2025-01-16 22:40:49 +01:00
828a1a2bfa Test program 2025-01-16 22:36:57 +01:00
0b98638cd5 Less CPU usage by creating tasks per action, instead of thread 2025-01-16 22:36:52 +01:00
74d30ba1b9 Fix Openshock default endpoint 2025-01-16 22:36:24 +01:00
385dba1cf3 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	CShocker/Devices/Additional/ApiHttpClient.cs
2025-01-16 21:47:48 +01:00
1ff67a5b8b Fix Assembly string empty on Publish 2025-01-16 21:46:13 +01:00
7 changed files with 33 additions and 20 deletions

View File

@ -1,5 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=C_003A_005CUsers_005CGlax_005CRiderProjects_005CGlaxLogger_005CGlaxLogger_005Cbin_005CDebug_005Cnet7_002E0_005CGlaxLogger_002Edll/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AHttpClient_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc439425da351c75ac7d966a1cc8324b51a9c471865af79d2f2f3fcb65e392_003FHttpClient_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;&#xD;
&lt;Assembly Path="C:\Users\Glax\RiderProjects\GlaxLogger\GlaxLogger\bin\Debug\net7.0\GlaxLogger.dll" /&gt;&#xD;

View File

@ -6,7 +6,7 @@
<Authors>Glax</Authors>
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>3.0.1</Version>
<Version>3.1.0</Version>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<LangVersion>latestmajor</LangVersion>
<PackageProjectUrl>https://github.com/C9Glax/CShocker</PackageProjectUrl>

View File

@ -31,7 +31,7 @@ public class OpenShockHttp : OpenShockApi
ApiHttpClient.MakeAPICall(HttpMethod.Post, $"{Endpoint}/2/shockers/control", json, this.Logger, new ValueTuple<string, string>("OpenShockToken", ApiKey));
}
public OpenShockHttp(string apiKey, string endpoint = "https://api.shocklink.app", ILogger? logger = null) : base(DeviceApi.OpenShockHttp, apiKey, endpoint, logger)
public OpenShockHttp(string apiKey, string? endpoint = null, ILogger? logger = null) : base(DeviceApi.OpenShockHttp, apiKey, endpoint??DefaultEndpoint, logger)
{
}
}

View File

@ -13,7 +13,7 @@ public class OpenShockSerial : OpenShockApi
public SerialPortInfo SerialPortI;
private readonly SerialPort _serialPort;
public OpenShockSerial(SerialPortInfo serialPortI, string apiKey, string endpoint = "https://api.shocklink.net", ILogger? logger = null) : base(DeviceApi.OpenShockSerial, apiKey, endpoint, logger)
public OpenShockSerial(SerialPortInfo serialPortI, string apiKey, string? endpoint = null, ILogger? logger = null) : base(DeviceApi.OpenShockSerial, apiKey, endpoint??DefaultEndpoint, logger)
{
this.SerialPortI = serialPortI;
this._serialPort = new SerialPort(serialPortI.PortName, BaudRate);

View File

@ -10,10 +10,9 @@ public abstract class Api : IDisposable
// ReSharper disable 4 times MemberCanBePrivate.Global -> Exposed
protected ILogger? Logger;
public readonly DeviceApi ApiType;
private readonly Queue<ValueTuple<ControlAction, Shocker, int, int>> _queue = new();
private bool _workOnQueue = true;
private Queue<DateTime> order = new();
private Dictionary<DateTime, Task> tasks = new();
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
private readonly Thread _workQueueThread;
private const short CommandDelay = 50;
internal readonly IntegerRange ValidIntensityRange, ValidDurationRange;
@ -38,7 +37,12 @@ public abstract class Api : IDisposable
foreach (Shocker shocker in shockers)
{
this.Logger?.Log(LogLevel.Debug, $"Enqueueing {action} Intensity: {intensity} Duration: {duration}\nShocker:\n{shocker}");
_queue.Enqueue(new(action, shocker, intensity, duration));
ValueTuple<ControlAction, Shocker, int, int> tuple = new(action, shocker, intensity, duration);
DateTime now = DateTime.Now;
Task t = new (() => ExecuteTask(now, tuple));
order.Enqueue(now);
tasks.Add(now, t);
t.Start();
}
}
@ -50,19 +54,17 @@ public abstract class Api : IDisposable
this.Logger = logger;
this.ValidIntensityRange = validIntensityRange;
this.ValidDurationRange = validDurationRange;
this._workQueueThread = new Thread(QueueThread);
this._workQueueThread.Start();
}
private void QueueThread()
private void ExecuteTask(DateTime when, ValueTuple<ControlAction, Shocker, int, int> tuple)
{
while (_workOnQueue)
if (_queue.Count > 0 && _queue.Dequeue() is { } action)
{
this.Logger?.Log(LogLevel.Information, $"Executing: {Enum.GetName(action.Item1)} Intensity: {action.Item3} Duration: {action.Item4}\nShocker:\n{action.Item2}");
ControlInternal(action.Item1, action.Item2, action.Item3, action.Item4);
Thread.Sleep(action.Item4 + CommandDelay);
}
while (order.First() != when)
Thread.Sleep(CommandDelay);
this.Logger?.Log(LogLevel.Information, $"Executing: {Enum.GetName(tuple.Item1)} Intensity: {tuple.Item3} Duration: {tuple.Item4}\nShocker:\n{tuple.Item2}");
ControlInternal(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4);
Thread.Sleep(tuple.Item4);
tasks.Remove(when);
order.Dequeue();
}
public void SetLogger(ILogger? logger)
@ -92,6 +94,7 @@ public abstract class Api : IDisposable
public void Dispose()
{
_workOnQueue = false;
foreach ((DateTime when, Task? task) in tasks)
task?.Dispose();
}
}

View File

@ -13,15 +13,19 @@ public static class ApiHttpClient
private static ProductInfoHeaderValue GetUserAgent()
{
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi;
if (assembly.Location == String.Empty)
{
DirectoryInfo dir = new (AppContext.BaseDirectory);
FileInfo? f = dir.GetFiles("*.exe").FirstOrDefault();
if (f is null)
return new("CShocker", "Release");
assembly = Assembly.LoadFrom(f.FullName);
fvi = FileVersionInfo.GetVersionInfo(f.FullName);
}
else
{
fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
}
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
return new (fvi.ProductName ?? fvi.FileName, fvi.ProductVersion);
}

View File

@ -15,6 +15,8 @@ while(apiKey is null || apiKey.Length < 1)
OpenShockHttp openShockHttp = new (apiKey, logger: logger);
foreach (OpenShockShocker shocker in openShockHttp.GetShockers())
{
shocker.Control(ControlAction.Vibrate, 20, 1000);
shocker.Control(ControlAction.Vibrate, 20, 1000);
shocker.Control(ControlAction.Vibrate, 20, 1000);
Thread.Sleep(1100);
}
@ -55,3 +57,6 @@ OpenShockShocker deserialized = JsonConvert.DeserializeObject<OpenShockShocker>(
shocker.Dispose();
deserialized.Dispose();
*/
while(!Console.KeyAvailable)
Thread.Sleep(100);