Skip to content

Commit

Permalink
feat: synapse sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBabbitt97 committed Jul 9, 2024
1 parent 1c9ed67 commit 66227ec
Show file tree
Hide file tree
Showing 16 changed files with 633 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ChromaControl.SDK.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35017.193
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChromaControl.SDK.Synapse", "src\ChromaControl.SDK.Synapse\ChromaControl.SDK.Synapse.csproj", "{D192C5EA-EC40-4BA9-AE43-ADA9FB4F446A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChromaControl.SDK.Synapse.Sample", "src\ChromaControl.SDK.Synapse.Sample\ChromaControl.SDK.Synapse.Sample.csproj", "{D8F00B39-3709-4157-9F87-521FDEA6041C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D192C5EA-EC40-4BA9-AE43-ADA9FB4F446A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D192C5EA-EC40-4BA9-AE43-ADA9FB4F446A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D192C5EA-EC40-4BA9-AE43-ADA9FB4F446A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D192C5EA-EC40-4BA9-AE43-ADA9FB4F446A}.Release|Any CPU.Build.0 = Release|Any CPU
{D8F00B39-3709-4157-9F87-521FDEA6041C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8F00B39-3709-4157-9F87-521FDEA6041C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8F00B39-3709-4157-9F87-521FDEA6041C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8F00B39-3709-4157-9F87-521FDEA6041C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ChromaControl.SDK.Synapse\ChromaControl.SDK.Synapse.csproj" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions src/ChromaControl.SDK.Synapse.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the Chroma Control Contributors under one or more agreements.
// The Chroma Control Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using ChromaControl.SDK.Synapse.Extensions;
using ChromaControl.SDK.Synapse.Sample;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddSynapseSDK();
builder.Services.AddHostedService<Worker>();

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"ChromaControl.SDK.Synapse.Sample": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
88 changes: 88 additions & 0 deletions src/ChromaControl.SDK.Synapse.Sample/Worker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Licensed to the Chroma Control Contributors under one or more agreements.
// The Chroma Control Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using ChromaControl.SDK.Synapse.Enums;
using ChromaControl.SDK.Synapse.Structs;
using System.Drawing;

namespace ChromaControl.SDK.Synapse.Sample;

/// <summary>
/// The worker.
/// </summary>
public partial class Worker : IHostedService
{
private readonly ILogger<Worker> _logger;
private readonly ISynapseSDK _sdk;

private Color _colorCache;

[LoggerMessage(Level = LogLevel.Information, Message = "Starting Synapse SDK...")]
private static partial void LogStartMessage(ILogger logger);

[LoggerMessage(Level = LogLevel.Information, Message = "Stopping Synapse SDK...")]
private static partial void LogStopMessage(ILogger logger);

[LoggerMessage(Level = LogLevel.Information, Message = "Synapse Status Changed To {status}")]
private static partial void LogStatusMessage(ILogger logger, SynapseStatus status);

[LoggerMessage(Level = LogLevel.Information, Message = "Synapse Color Set To [{r}, {g}, {b}]")]
private static partial void LogEffectMessage(ILogger logger, byte r, byte g, byte b);

/// <summary>
/// Creates a <see cref="Worker"/> instance.
/// </summary>
/// <param name="logger">The <see cref="ILogger{TCategoryName}"/>.</param>
/// <param name="sdk">The <see cref="ISynapseSDK"/>.</param>
public Worker(ILogger<Worker> logger, ISynapseSDK sdk)
{
_logger = logger;
_sdk = sdk;
_sdk.StatusChanged += StatusChanged;
_sdk.EffectReceived += EffectReceived;
}

/// <summary>
/// Starts the worker.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="Task"/> representing the worker starting.</returns>
public Task StartAsync(CancellationToken cancellationToken)
{
LogStartMessage(_logger);

_sdk.StartSDK(new("00000000-0000-0000-0000-000000000000"));

return Task.CompletedTask;
}

/// <summary>
/// Stops the worker.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="Task"/> representing the worker stopping.</returns>
public Task StopAsync(CancellationToken cancellationToken)
{
LogStopMessage(_logger);

_sdk.StopSDK();

return Task.CompletedTask;
}

private void StatusChanged(object? sender, SynapseStatus e)
{
LogStatusMessage(_logger, e);
}

private void EffectReceived(object? sender, SynapseEffect e)
{
if (_colorCache != e.Color1)
{
_colorCache = e.Color1;

LogEffectMessage(_logger, e.Color1.R, e.Color1.G, e.Color1.B);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
8 changes: 8 additions & 0 deletions src/ChromaControl.SDK.Synapse.Sample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
12 changes: 12 additions & 0 deletions src/ChromaControl.SDK.Synapse/ChromaControl.SDK.Synapse.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/ChromaControl.SDK.Synapse/Enums/SynapseEventType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the Chroma Control Contributors under one or more agreements.
// The Chroma Control Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace ChromaControl.SDK.Synapse.Enums;

internal enum SynapseEventType
{
Effect = 1,
Status = 2
}
116 changes: 116 additions & 0 deletions src/ChromaControl.SDK.Synapse/Enums/SynapseResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Licensed to the Chroma Control Contributors under one or more agreements.
// The Chroma Control Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace ChromaControl.SDK.Synapse.Enums;

/// <summary>
/// Synapse return result.
/// </summary>
public enum SynapseResult : long
{
/// <summary>
/// Invalid.
/// </summary>
Invalid = -1L,

/// <summary>
/// Success.
/// </summary>
Success = 0L,

/// <summary>
/// Access denied.
/// </summary>
AccessDenied = 5L,

/// <summary>
/// Invalid handle.
/// </summary>
InvalidHandle = 6L,

/// <summary>
/// Invalid access.
/// </summary>
InvalidAccess = 12L,

/// <summary>
/// Not supported.
/// </summary>
NotSupported = 50L,

/// <summary>
/// Invalid parameter.
/// </summary>
InvalidParameter = 87L,

/// <summary>
/// The service does not exist.
/// </summary>
ServiceNotExist = 1060L,

/// <summary>
/// The service has not been started.
/// </summary>
ServiceNotActive = 1062L,

/// <summary>
/// Cannot start more than one instance of the specified program.
/// </summary>
SingleInstanceApp = 1152L,

/// <summary>
/// Device not connected.
/// </summary>
DeviceNotConnected = 1167L,

/// <summary>
/// Element not found.
/// </summary>
NotFound = 1168L,

/// <summary>
/// Request aborted.
/// </summary>
RequestAborted = 1235L,

/// <summary>
/// Requested operation is not performed because the user has not been authenticated.
/// </summary>
NotAuthenticated = 1244L,

/// <summary>
/// An attempt was made to perform an initialization operation when initialization has already been completed.
/// </summary>
AlreadyInitialized = 1247L,

/// <summary>
/// Resource not available or disabled.
/// </summary>
ResourceDisabled = 4309L,

/// <summary>
/// Device not available or supported.
/// </summary>
DeviceNotAvailable = 4319L,

/// <summary>
/// The group or resource is not in the correct state to perform the requested operation.
/// </summary>
NotValidState = 5023L,

/// <summary>
/// Insufficient access rights, administrator privilege required.
/// </summary>
InsufficientAccessRights = 8344L,

/// <summary>
/// No more items.
/// </summary>
NoMoreItems = 259L,

/// <summary>
/// General failure.
/// </summary>
Failed = 2147483647L
}
21 changes: 21 additions & 0 deletions src/ChromaControl.SDK.Synapse/Enums/SynapseStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the Chroma Control Contributors under one or more agreements.
// The Chroma Control Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace ChromaControl.SDK.Synapse.Enums;

/// <summary>
/// Synapse status.
/// </summary>
public enum SynapseStatus
{
/// <summary>
/// <see cref="Live"/> occurs when all expected conditions are met.
/// </summary>
Live = 1,

/// <summary>
/// <see cref="NotLive"/> occurs when one of the expected conditions is not fulfilled.
/// </summary>
NotLive = 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the Chroma Control Contributors under one or more agreements.
// The Chroma Control Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Extensions.DependencyInjection;

namespace ChromaControl.SDK.Synapse.Extensions;

/// <summary>
/// Extension methods for adding the Synapse SDK to an <see cref="IServiceCollection" />.
/// </summary>
public static class ServiceCollectionSynapseExtensions
{
/// <summary>
/// Add an <see cref="ISynapseSDK"/> registration.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
/// <returns>The original <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddSynapseSDK(this IServiceCollection services)
{
services.AddSingleton<ISynapseSDK>(new SynapseSDK());

return services;
}
}
Loading

0 comments on commit 66227ec

Please sign in to comment.