diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 4a91d95..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: .NET Application Build - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - workflow_dispatch: - -jobs: - build: - runs-on: windows-latest # For a list of available runner types, refer to - # https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - # Install the .NET Core workload - - name: Install .NET Core - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 6.0.x - - # Add MSBuild to the PATH: https://github.com/microsoft/setup-msbuild - - name: Setup MSBuild.exe - uses: microsoft/setup-msbuild@v2 - - - name: Build application - run: dotnet build diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 0000000..1d049f7 --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -0,0 +1,31 @@ +name: .NET Test and Build + +on: + push: + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-latest + container: mcr.microsoft.com/dotnet/sdk:8.0 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Run tests + run: dotnet test + + build: + needs: test + runs-on: ubuntu-latest + container: mcr.microsoft.com/dotnet/sdk:8.0 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Build application + run: dotnet build diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 5b31cc3..e939e22 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,7 +7,7 @@ "type": "process", "args": [ "build", - "${workspaceFolder}/RS485 Monitor.csproj", + "${workspaceFolder}/SuperSoco485Monitor.sln", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], @@ -19,7 +19,7 @@ "type": "process", "args": [ "publish", - "${workspaceFolder}/RS485 Monitor.csproj", + "${workspaceFolder}/SuperSoco485Monitor.sln", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary", "-c", @@ -43,4 +43,4 @@ "problemMatcher": "$msCompile" } ] -} \ No newline at end of file +} diff --git a/README.md b/README.md index 092bfff..988702d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![.NET Workflow for test building](https://github.com/stprograms/SuperSoco485Monitor/actions/workflows/build.yml/badge.svg)](https://github.com/stprograms/SuperSoco485Monitor/actions/workflows/build.yml) +[![.NET Unit Test](https://github.com/stprograms/SuperSoco485Monitor/actions/workflows/dotnet.yml/badge.svg)](https://github.com/stprograms/SuperSoco485Monitor/actions/workflows/dotnet.yml) # SuperSoco485Monitor C# Application to monitor the communication on the internal RS485 Bus on SuperSoco Motorcycles diff --git a/RS485 Monitor.code-workspace b/RS485 Monitor.code-workspace index a087624..7e29a07 100644 --- a/RS485 Monitor.code-workspace +++ b/RS485 Monitor.code-workspace @@ -17,4 +17,4 @@ "Xmanu" ] } -} \ No newline at end of file +} diff --git a/RS485 Monitor.csproj b/RS485 Monitor/RS485 Monitor.csproj similarity index 96% rename from RS485 Monitor.csproj rename to RS485 Monitor/RS485 Monitor.csproj index ea182ca..3e93316 100644 --- a/RS485 Monitor.csproj +++ b/RS485 Monitor/RS485 Monitor.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 RS485_Monitor enable enable diff --git a/appsettings.json b/RS485 Monitor/appsettings.json similarity index 100% rename from appsettings.json rename to RS485 Monitor/appsettings.json diff --git a/src/CmdOptions.cs b/RS485 Monitor/src/CmdOptions.cs similarity index 100% rename from src/CmdOptions.cs rename to RS485 Monitor/src/CmdOptions.cs diff --git a/src/Configuration.cs b/RS485 Monitor/src/Configuration.cs similarity index 100% rename from src/Configuration.cs rename to RS485 Monitor/src/Configuration.cs diff --git a/src/Program.cs b/RS485 Monitor/src/Program.cs similarity index 100% rename from src/Program.cs rename to RS485 Monitor/src/Program.cs diff --git a/src/SerialMonitor.cs b/RS485 Monitor/src/SerialMonitor.cs similarity index 100% rename from src/SerialMonitor.cs rename to RS485 Monitor/src/SerialMonitor.cs diff --git a/src/TelegramParser.cs b/RS485 Monitor/src/TelegramParser.cs similarity index 100% rename from src/TelegramParser.cs rename to RS485 Monitor/src/TelegramParser.cs diff --git a/src/Telegrams/BaseTelegram.cs b/RS485 Monitor/src/Telegrams/BaseTelegram.cs similarity index 100% rename from src/Telegrams/BaseTelegram.cs rename to RS485 Monitor/src/Telegrams/BaseTelegram.cs diff --git a/src/Telegrams/BatteryStatus.cs b/RS485 Monitor/src/Telegrams/BatteryStatus.cs similarity index 100% rename from src/Telegrams/BatteryStatus.cs rename to RS485 Monitor/src/Telegrams/BatteryStatus.cs diff --git a/src/Telegrams/ECUStatus.cs b/RS485 Monitor/src/Telegrams/ECUStatus.cs similarity index 100% rename from src/Telegrams/ECUStatus.cs rename to RS485 Monitor/src/Telegrams/ECUStatus.cs diff --git a/src/Telegrams/GSMStatus.cs b/RS485 Monitor/src/Telegrams/GSMStatus.cs similarity index 100% rename from src/Telegrams/GSMStatus.cs rename to RS485 Monitor/src/Telegrams/GSMStatus.cs diff --git a/src/Utils/ConsolePrinter.cs b/RS485 Monitor/src/Utils/ConsolePrinter.cs similarity index 100% rename from src/Utils/ConsolePrinter.cs rename to RS485 Monitor/src/Utils/ConsolePrinter.cs diff --git a/src/Utils/IUserVisualizable.cs b/RS485 Monitor/src/Utils/IUserVisualizable.cs similarity index 100% rename from src/Utils/IUserVisualizable.cs rename to RS485 Monitor/src/Utils/IUserVisualizable.cs diff --git a/src/Utils/LogPrinter.cs b/RS485 Monitor/src/Utils/LogPrinter.cs similarity index 100% rename from src/Utils/LogPrinter.cs rename to RS485 Monitor/src/Utils/LogPrinter.cs diff --git a/src/Utils/SerialSimulator.cs b/RS485 Monitor/src/Utils/SerialSimulator.cs similarity index 100% rename from src/Utils/SerialSimulator.cs rename to RS485 Monitor/src/Utils/SerialSimulator.cs diff --git a/src/Utils/TelegramPlayer.cs b/RS485 Monitor/src/Utils/TelegramPlayer.cs similarity index 100% rename from src/Utils/TelegramPlayer.cs rename to RS485 Monitor/src/Utils/TelegramPlayer.cs diff --git a/SuperSoco485Monitor.sln b/SuperSoco485Monitor.sln new file mode 100644 index 0000000..38fe37b --- /dev/null +++ b/SuperSoco485Monitor.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RS485 Monitor", "RS485 Monitor\RS485 Monitor.csproj", "{48CB4F5C-8ED9-411C-BB59-9977016601EA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "tests\tests.csproj", "{6A504182-F91E-4FC5-9148-C8ACFF2420BB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {48CB4F5C-8ED9-411C-BB59-9977016601EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {48CB4F5C-8ED9-411C-BB59-9977016601EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {48CB4F5C-8ED9-411C-BB59-9977016601EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {48CB4F5C-8ED9-411C-BB59-9977016601EA}.Release|Any CPU.Build.0 = Release|Any CPU + {6A504182-F91E-4FC5-9148-C8ACFF2420BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A504182-F91E-4FC5-9148-C8ACFF2420BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A504182-F91E-4FC5-9148-C8ACFF2420BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A504182-F91E-4FC5-9148-C8ACFF2420BB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/tests/BaseTelegramTest.cs b/tests/BaseTelegramTest.cs new file mode 100644 index 0000000..86f8791 --- /dev/null +++ b/tests/BaseTelegramTest.cs @@ -0,0 +1,45 @@ +namespace RS485_Monitor.tests; +using NUnit.Framework; + +public class BaseTelegramTest +{ + [Test] + public void ParseTelegramSuccessful() + { + byte[] raw = new byte[] { 0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D }; + + BaseTelegram telegram = new(raw); + + Assert.That(telegram.Type, Is.EqualTo(BaseTelegram.TelegramType.READ_RESPONSE)); + Assert.That(telegram.Destination, Is.EqualTo(0xDA)); + Assert.That(telegram.Source, Is.EqualTo(0xAA)); + Assert.That(telegram.Valid, Is.EqualTo(true)); + } + + [Test] + public void ParseTelegramRequestSuccessful() + { + byte[] raw = new byte[] { 0xC5, 0x5C, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D }; + + BaseTelegram telegram = new(raw); + + Assert.That(telegram.Type, Is.EqualTo(BaseTelegram.TelegramType.READ_REQUEST)); + Assert.That(telegram.Destination, Is.EqualTo(0xDA)); + Assert.That(telegram.Source, Is.EqualTo(0xAA)); + Assert.That(telegram.Valid, Is.EqualTo(true)); + } + + [Test] + public void ParseTelegramWrongChecksum() + { + byte[] raw = new byte[] { 0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x10, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D }; + + BaseTelegram telegram = new(raw); + + Assert.That(telegram.Type, Is.EqualTo(BaseTelegram.TelegramType.READ_RESPONSE)); + Assert.That(telegram.Destination, Is.EqualTo(0xDA)); + Assert.That(telegram.Source, Is.EqualTo(0xAA)); + Assert.That(telegram.Valid, Is.EqualTo(false)); + } + +} diff --git a/tests/BatteryStatusTest.cs b/tests/BatteryStatusTest.cs new file mode 100644 index 0000000..de8da92 --- /dev/null +++ b/tests/BatteryStatusTest.cs @@ -0,0 +1,48 @@ +namespace RS485_Monitor.tests; +using NUnit.Framework; + +public class BatteryStatusTest +{ + private BaseTelegram? batteryBase = null; + + [SetUp] + public void Setup() + { + byte[] raw = new byte[] { 0xB6, 0x6B, 0xAA, 0x5A, 0x0A, 0x4D, 0x48, 0x17, 0x00, 0x00, 0x23, 0x00, 0x0B, 0x00, 0x00, 0x30, 0x0D }; + batteryBase = new BaseTelegram(raw); + } + + [Test] + public void ConvertToBatteryStatusSuccess() + { + BatteryStatus status = new(batteryBase!); + Assert.That(status, Is.Not.Null); + } + + [Test] + public void ConvertToBatteryStatusFail() + { + byte[] raw = new byte[] { 0xB6, 0x6B, 0xAA, 0x5A, 0x09, 0x4D, 0x48, 0x17, 0x00, 0x00, 0x23, 0x00, 0x0B, 0x00, 0x30, 0x0D }; + BaseTelegram invalidTelegram = new(raw); + + Assert.Throws(() => new BatteryStatus(invalidTelegram)); + } + + [Test] + public void CheckContent() + { + BatteryStatus status = new(batteryBase!); + using (Assert.EnterMultipleScope()) + { + Assert.That(status.Voltage, Is.EqualTo(77)); + Assert.That(status.SoC, Is.EqualTo(72)); + Assert.That(status.Temperature, Is.EqualTo(23)); + Assert.That(status.Charge, Is.EqualTo(0x0)); + Assert.That(status.Cycles, Is.EqualTo(35)); + Assert.That(status.DischargeCycles, Is.EqualTo(11)); + Assert.That(status.VBreaker, Is.EqualTo(BatteryStatus.VBreakerStatus.OK)); + Assert.That(status.Activity, Is.EqualTo(BatteryStatus.BatteryActivity.NO_ACTIVITY)); + Assert.That(status.Charging, Is.False); + } + } +} diff --git a/tests/ECUStatusTest.cs b/tests/ECUStatusTest.cs new file mode 100644 index 0000000..978fbc1 --- /dev/null +++ b/tests/ECUStatusTest.cs @@ -0,0 +1,45 @@ +namespace RS485_Monitor.tests; +using NUnit.Framework; + +public class ECUStatusTest +{ + private BaseTelegram? ecuBase; + + [SetUp] + public void Setup() + { + byte[] raw = new byte[] { 0xB6, 0x6B, 0xAA, 0xDA, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x13, 0x00, 0x00, 0x02, 0x01, 0x1C, 0x0D }; + ecuBase = new BaseTelegram(raw); + } + + [Test] + public void ConvertToEcuStatusSuccess() + { + ECUStatus status = new(ecuBase!); + Assert.That(status, Is.Not.Null); + } + + [Test] + public void ConvertToEcuStatusFail() + { + byte[] raw = new byte[] { 0xC5, 0x5C, 0xBA, 0xAA, 0x0E, 0x48, 0x00, 0x00, 0x00, 0x16, 0x19, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x40, 0x0A, 0x0D }; + BaseTelegram gsmTelegram = new(raw); + + Assert.Throws(() => new ECUStatus(gsmTelegram)); + } + + [Test] + public void CheckContent() + { + ECUStatus status = new(ecuBase!); + using (Assert.EnterMultipleScope()) + { + Assert.That(status.Mode, Is.EqualTo(2)); + Assert.That(status.Current, Is.EqualTo(0.4).Within(0.1)); + Assert.That(status.Speed, Is.EqualTo(0)); + Assert.That(status.Temperature, Is.EqualTo(0x13)); + Assert.That(status.IsParking, Is.EqualTo(true)); + } + + } +} diff --git a/tests/tests.csproj b/tests/tests.csproj new file mode 100644 index 0000000..96e71e7 --- /dev/null +++ b/tests/tests.csproj @@ -0,0 +1,30 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + + + + + +