Skip to content

Commit

Permalink
chore: communication foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszGarbarz committed Aug 1, 2024
1 parent 9b7e18d commit a42bfcc
Show file tree
Hide file tree
Showing 20 changed files with 702 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/build-dev-stage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: 'Build dev stage'

on:
pull_request:

jobs:
build:
runs-on: ubuntu-22.04
timeout-minutes: 10

steps:
- name: Prepare
run: echo "NUGET_URL=https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" >> $GITHUB_ENV

- name: Checkout repository
uses: actions/checkout@v3
with:
token: ${{ secrets.REPOS_ACCESS_TOKEN }}
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
global-json-file: global.json

- name: Restore dependencies
run: dotnet restore .

- name: Install GitVersion
uses: gittools/actions/gitversion/[email protected]
with:
versionSpec: '5.x'

- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/[email protected]
with:
useConfigFile: true

- name: Build
run: dotnet build -c Release --no-restore -p:Version=$GitVersion_SemVer . -p:RepositoryUrl=${{ github.server_url }}/${{ github.repository }}

- name: Test
run: dotnet test -c Release --no-build --verbosity normal .

- name: Pack
run: dotnet pack -c Release --no-build -p:Version=$GitVersion_SemVer --verbosity normal .

- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
path: Build/*.nupkg
retention-days: 5
61 changes: 61 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: 'Release'

on:
workflow_dispatch:
push:
branches:
- master
- release**

jobs:
build:
runs-on: ubuntu-22.04
timeout-minutes: 10

steps:
- name: Prepare
run: echo "NUGET_URL=https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" >> $GITHUB_ENV

- name: Checkout repository
uses: actions/checkout@v3
with:
token: ${{ secrets.REPOS_ACCESS_TOKEN }}
submodules: true
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
global-json-file: global.json

- name: Restore dependencies
run: dotnet restore .

- name: Install GitVersion
uses: gittools/actions/gitversion/[email protected]
with:
versionSpec: '5.x'

- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/[email protected]
with:
useConfigFile: true

- name: Build
run: dotnet build -c Release --no-restore . -p:RepositoryUrl=${{ github.server_url }}/${{ github.repository }}

- name: Test
run: dotnet test -c Release --no-build --verbosity normal .

- name: Pack nugets
run: dotnet pack -c Release -o ./Output/ -p:Version=$GitVersion_SemVer --verbosity normal --no-build .

- name: Push nugets
run: dotnet nuget push "./Output/*.nupkg" --skip-duplicate --source "github" --api-key "${{ secrets.WRITE_PACKAGES_TOKEN }}"

- name: Tag the commit
run: git tag -f v$GitVersion_SemVer

- name: Push the tag
run: git push -f --tags
34 changes: 34 additions & 0 deletions BleBoxNetSdk.Tests/BleBoxNetSdk.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BleBoxNetSdk\BleBoxNetSdk.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using BleBoxNetSdk.Common.JsonConverters;
using FluentAssertions;
using System.Text;
using System.Text.Json;

namespace BleBoxNetSdk.Tests.Common.JsonConverters;

internal class IntSecondsToTimeSpanConverterTests
{
[TestCase(12)]
[TestCase(0)]
[TestCase(45389)]
[TestCase(1)]
public void should_read_value(int seconds)
{
var bytes = Encoding.UTF8.GetBytes(seconds.ToString());
var reader = new Utf8JsonReader(bytes.AsSpan());
reader.Read();
var converter = PrepareConverter();

var value = converter.Read(ref reader, typeof(TimeSpan), JsonSerializerOptions.Default);

value.TotalSeconds.Should().Be(seconds);
}

[TestCase(87832)]
[TestCase(0)]
[TestCase(99)]
[TestCase(1)]
public void should_write_value(int seconds)
{
var converter = PrepareConverter();
var buffer = new byte[64];
using var memoryStream = new MemoryStream(buffer);
using var writer = new Utf8JsonWriter(memoryStream);
converter.Write(writer, TimeSpan.FromSeconds(seconds), JsonSerializerOptions.Default);
writer.Flush();

var value = Encoding.UTF8.GetString(buffer, 0, seconds.ToString().Length);

value.Should().Be($"{seconds}");
}

internal IntSecondsToTimeSpanConverter PrepareConverter() => new IntSecondsToTimeSpanConverter();
}
31 changes: 31 additions & 0 deletions BleBoxNetSdk.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BleBoxNetSdk", "BleBoxNetSdk\BleBoxNetSdk.csproj", "{EE09EC89-906A-41EB-8538-5C5A64036F88}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BleBoxNetSdk.Tests", "BleBoxNetSdk.Tests\BleBoxNetSdk.Tests.csproj", "{DC8A6BD6-7788-4CFD-942A-144F497D0E15}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EE09EC89-906A-41EB-8538-5C5A64036F88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE09EC89-906A-41EB-8538-5C5A64036F88}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE09EC89-906A-41EB-8538-5C5A64036F88}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE09EC89-906A-41EB-8538-5C5A64036F88}.Release|Any CPU.Build.0 = Release|Any CPU
{DC8A6BD6-7788-4CFD-942A-144F497D0E15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC8A6BD6-7788-4CFD-942A-144F497D0E15}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC8A6BD6-7788-4CFD-942A-144F497D0E15}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC8A6BD6-7788-4CFD-942A-144F497D0E15}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A145D61A-6E20-462B-A5E0-9727F15A188C}
EndGlobalSection
EndGlobal
18 changes: 18 additions & 0 deletions BleBoxNetSdk/BleBoxNetSdk.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

<ItemGroup>
<InternalsVisibleTo Include="BleBoxNetSdk.Tests" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions BleBoxNetSdk/Common/CommonApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BleBoxNetSdk.Common.Endpoints;
using BleBoxNetSdk.Common.Models;
using BleBoxNetSdk.Services;

namespace BleBoxNetSdk.Common;

internal class CommonApiClient(IApiHttpClient apiHttpClient) : ICommonApiClient
{
public async Task<Device?> Info(Uri deviceAddress, CancellationToken cancellationToken = default)
{
var request = new Info.Request();

var response = await apiHttpClient.Send<Info.ResponseResult>(deviceAddress, request, cancellationToken);

return response.Device;
}

public async Task<TimeSpan> GetDeviceUptime(Uri deviceAddress, CancellationToken cancellationToken = default)
{
var request = new DeviceUptime.Request();

var response = await apiHttpClient.Send<DeviceUptime.ResponseResult>(deviceAddress, request, cancellationToken);

return response.UpTimeS;
}

public async Task PerformDeviceUpdate(Uri deviceAddress, CancellationToken cancellationToken = default)
{
var request = new PerformUpdate.Request();

await apiHttpClient.Send<PerformUpdate.ResponseResult>(deviceAddress, request, cancellationToken);
}
}
11 changes: 11 additions & 0 deletions BleBoxNetSdk/Common/Endpoints/DeviceUptime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace BleBoxNetSdk.Common.Endpoints;

internal class DeviceUptime
{
internal class Request() : RequestBase(HttpMethod.Get, "/api/device/uptime") { }

internal class ResponseResult
{
public TimeSpan UpTimeS { get; set; }
}
}
13 changes: 13 additions & 0 deletions BleBoxNetSdk/Common/Endpoints/Info.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using BleBoxNetSdk.Common.Models;

namespace BleBoxNetSdk.Common.Endpoints;

internal static class Info
{
internal class Request() : RequestBase(HttpMethod.Get, "/info") { }

internal class ResponseResult
{
public Device? Device { get; set; }
}
}
8 changes: 8 additions & 0 deletions BleBoxNetSdk/Common/Endpoints/PerformUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BleBoxNetSdk.Common.Endpoints;

internal class PerformUpdate
{
internal class Request() : RequestBase(HttpMethod.Post, "/api/ota/update") { }

internal class ResponseResult { }
}
18 changes: 18 additions & 0 deletions BleBoxNetSdk/Common/Enums/ApiType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace BleBoxNetSdk.Common.Enums;

public enum ApiType
{
Unknown,
AirSensor,
ButtonBox,
GateBox,
MultiSensor,
ShutterBox,
SmartWindowBox,
SwitchBoxD,
SwitchBox,
TempSensor,
ThermoBox,
TvLiftBox,
WLightBox
}
42 changes: 42 additions & 0 deletions BleBoxNetSdk/Common/ICommonApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using BleBoxNetSdk.Common.Models;

namespace BleBoxNetSdk.Common
{
public interface ICommonApiClient
{
/// <summary>
/// Returns general information about device
/// </summary>
/// <param name="deviceAddress">Url to desired device</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Nullable object that contains all information about the device</returns>
/// <exception cref="ArgumentNullException">Received json is null</exception>
/// <exception cref="System.Text.Json.JsonException">Deserialization failed</exception>
/// <exception cref="NotSupportedException">There is no compatible converter</exception>
/// <exception cref="Exception">Request to API failed</exception>
Task<Device?> Info(Uri deviceAddress, CancellationToken cancellationToken = default);

/// <summary>
/// Returns information about number of seconds from boot
/// </summary>
/// <param name="deviceAddress">Url to desired device</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>TimeSpan of desired device uptime</returns>
/// <exception cref="ArgumentNullException">Received json is null</exception>
/// <exception cref="System.Text.Json.JsonException">Deserialization failed</exception>
/// <exception cref="NotSupportedException">There is no compatible converter</exception>
/// <exception cref="Exception">Request to API failed</exception>
Task<TimeSpan> GetDeviceUptime(Uri deviceAddress, CancellationToken cancellationToken = default);

/// <summary>
/// Perform firmware update. Return conflict if update is in progress
/// </summary>
/// <param name="deviceAddress">Url to desired device</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <exception cref="ArgumentNullException">Received json is null</exception>
/// <exception cref="System.Text.Json.JsonException">Deserialization failed</exception>
/// <exception cref="NotSupportedException">There is no compatible converter</exception>
/// <exception cref="Exception">Request to API failed. Device update may be in progress</exception>
Task PerformDeviceUpdate(Uri deviceAddress, CancellationToken cancellationToken = default);
}
}
Loading

0 comments on commit a42bfcc

Please sign in to comment.