Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced CLI #1345

Merged
merged 6 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion LenovoLegionToolkit.CLI.Lib/IpcRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,26 @@

public class IpcRequest
{
public string Name { get; init; } = "";
public enum OperationType
{
Unknown,
ListFeatures,
ListFeatureValues,
ListQuickActions,
GetFeatureValue,
SetFeatureValue,
GetSpectrumProfile,
SetSpectrumProfile,
GetSpectrumBrightness,
SetSpectrumBrightness,
GetRGBPreset,
SetRGBPreset,
QuickAction,
}

public OperationType? Operation { get; init; }

public string? Name { get; init; }

public string? Value { get; init; }
}
140 changes: 138 additions & 2 deletions LenovoLegionToolkit.CLI/IpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,142 @@ public static class IpcClient
{
private static bool PipeExists => Directory.GetFiles(@"\\.\pipe\", Constants.PIPE_NAME).Length > 0;

public static async Task RunQuickActionAsync(string name)
public static async Task<string> ListQuickActionsAsync()
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.ListQuickActions
};

return await SendRequestAsync(req).ConfigureAwait(false)
?? throw new IpcException("Missing return message");
}

public static Task RunQuickActionAsync(string name)
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.QuickAction,
Name = name
};

return SendRequestAsync(req);
}

public static async Task<string> ListFeaturesAsync()
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.ListFeatures,
};

return await SendRequestAsync(req).ConfigureAwait(false)
?? throw new IpcException("Missing return message");
}

public static async Task<string> ListFeatureValuesAsync(string name)
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.ListFeatureValues,
Name = name,
};

return await SendRequestAsync(req).ConfigureAwait(false)
?? throw new IpcException("Missing return message");
}

public static Task SetFeatureValueAsync(string name, string value)
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.SetFeatureValue,
Name = name,
Value = value
};

return SendRequestAsync(req);
}

public static async Task<string> GetFeatureValueAsync(string name)
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.GetFeatureValue,
Name = name
};

return await SendRequestAsync(req).ConfigureAwait(false)
?? throw new IpcException("Missing return message");
}

public static async Task<string> GetSpectrumProfileAsync()
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.GetSpectrumProfile
};

return await SendRequestAsync(req).ConfigureAwait(false)
?? throw new IpcException("Missing return message");
}

public static Task SetSpectrumProfileAsync(string value)
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.SetSpectrumProfile,
Value = value
};

return SendRequestAsync(req);
}

public static async Task<string> GetSpectrumBrightnessAsync()
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.GetSpectrumBrightness
};

return await SendRequestAsync(req).ConfigureAwait(false)
?? throw new IpcException("Missing return message");
}

public static Task SetSpectrumBrightnessAsync(string value)
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.SetSpectrumBrightness,
Value = value
};

return SendRequestAsync(req);
}

public static async Task<string> GetRGBPresetAsync()
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.GetRGBPreset
};

return await SendRequestAsync(req).ConfigureAwait(false)
?? throw new IpcException("Missing return message");
}

public static Task SetRGBPresetAsync(string value)
{
var req = new IpcRequest
{
Operation = IpcRequest.OperationType.SetRGBPreset,
Value = value
};

return SendRequestAsync(req);
}

private static async Task<string?> SendRequestAsync(IpcRequest req)
{
if (!PipeExists)
throw new IpcException("Server unavailable");
Expand All @@ -21,12 +156,13 @@ public static async Task RunQuickActionAsync(string name)

await ConnectAsync(pipe).ConfigureAwait(false);

var req = new IpcRequest { Name = name };
await pipe.WriteObjectAsync(req).ConfigureAwait(false);
var res = await pipe.ReadObjectAsync<IpcResponse>().ConfigureAwait(false);

if (res is null || !res.Success)
throw new IpcException(res?.Message ?? "Unknown failure");

return res.Message;
}

private static async Task ConnectAsync(NamedPipeClientStream pipe)
Expand Down
3 changes: 3 additions & 0 deletions LenovoLegionToolkit.CLI/LenovoLegionToolkit.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LenovoLegionToolkit.CLI.Lib\LenovoLegionToolkit.CLI.Lib.csproj" />
</ItemGroup>
Expand Down
Loading
Loading