-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
435 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using NUnit.Framework; |
25 changes: 25 additions & 0 deletions
25
app/Wissance.Zerial/Wissance.Zerial.Common.Tests/Utils/TestSerialPortNameHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Wissance.Zerial.Common.Utils; | ||
|
||
namespace Wissance.Zerial.Common.Tests.Utils | ||
{ | ||
public class TestSerialPortNameHelper | ||
{ | ||
[TestCase(true, 4, "COM4", true)] | ||
[TestCase(false, 1, "/dev/tty1", false)] | ||
[TestCase(false, 2, "/dev/ttyUSB2", true)] | ||
public void TestBuildSerialDeviceName(bool isWindows, int portNumber, string expectedDeviceName, bool isUsb) | ||
{ | ||
if (OperatingSystem.IsWindows() && isWindows || OperatingSystem.IsLinux() && !isWindows) | ||
Assert.That(SerialDeviceHelper.BuildSerialDeviceName(portNumber, isUsb), Is.EqualTo(expectedDeviceName)); | ||
} | ||
|
||
[TestCase(true, "COM4",4)] | ||
[TestCase(false, "/dev/tty1",1)] | ||
[TestCase(false, "/dev/ttyUSB2",2)] | ||
public void TestGetSerialDeviceNumber(bool isWindows, string deviceName, int expectedPortNumber) | ||
{ | ||
if (OperatingSystem.IsWindows() && isWindows || OperatingSystem.IsLinux() && !isWindows) | ||
Assert.That(SerialDeviceHelper.GetSerialDeviceNumber(deviceName), Is.EqualTo(expectedPortNumber)); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/Wissance.Zerial/Wissance.Zerial.Common.Tests/Wissance.Zerial.Common.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/> | ||
<PackageReference Include="NUnit" Version="3.13.3"/> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.3.0"/> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Wissance.Zerial.Common\Wissance.Zerial.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
app/Wissance.Zerial/Wissance.Zerial.Common/Utils/SerialDeviceHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace Wissance.Zerial.Common.Utils | ||
{ | ||
public static class SerialDeviceHelper | ||
{ | ||
/// <summary> | ||
/// Constructs name depends on Operation System. | ||
/// For Windows | ||
/// </summary> | ||
/// <param name="number"> | ||
/// In Windows name is COM4, COM5, therefore number is 4, 5 | ||
/// In Linux device name is /dev/tty0 for real COM, /dev/ttyUSB0 for USB-COM, therefore number is 0 | ||
/// </param> | ||
/// <param name="isUsbDevice"> | ||
/// Don't have any sense for Windows, but have for Linux | ||
/// </param> | ||
public static string BuildSerialDeviceName(int number, bool isUsbDevice = true) | ||
{ | ||
if (OperatingSystem.IsWindows()) | ||
return string.Format(WindowsComPortNamePattern, number); | ||
if (OperatingSystem.IsLinux()) | ||
{ | ||
string deviceName = string.Format(isUsbDevice ? LinuxUsbDeviceNamePattern : LinuxComDeviceNamePattern, number); | ||
return string.Format(LinuxSerialDeviceNamePattern, deviceName); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static int GetSerialDeviceNumber(string deviceName) | ||
{ | ||
try | ||
{ | ||
if (OperatingSystem.IsWindows()) | ||
{ | ||
int portNumber = 0; | ||
bool portParseResult = int.TryParse(deviceName.Substring(WindowsComDeviceNameSign.Length), | ||
out portNumber); | ||
if (portParseResult) | ||
return portNumber; | ||
return -1; | ||
} | ||
|
||
if (OperatingSystem.IsLinux()) | ||
{ | ||
string deviceNumber = ""; | ||
// name is /dev/ttyUSB{N}, where N - number | ||
if (deviceName.Contains(LinuxUsbDeviceNameSign)) | ||
{ | ||
int nameSignStartIndex = deviceName.IndexOf(LinuxUsbDeviceNameSign); | ||
deviceNumber = deviceName.Substring(nameSignStartIndex + LinuxUsbDeviceNameSign.Length); | ||
} | ||
else | ||
{ | ||
if (deviceName.Contains(LinuxComDeviceNameSign)) | ||
{ | ||
int nameSignStartIndex = deviceName.IndexOf(LinuxComDeviceNameSign); | ||
deviceNumber = deviceName.Substring(nameSignStartIndex + LinuxComDeviceNameSign.Length); | ||
} | ||
} | ||
|
||
int portNumber = 0; | ||
bool portParseResult = int.TryParse(deviceNumber, out portNumber); | ||
if (portParseResult) | ||
return portNumber; | ||
return -1; | ||
|
||
} | ||
|
||
return -2; | ||
} | ||
catch (Exception) | ||
{ | ||
return -255; | ||
} | ||
} | ||
|
||
private const string WindowsComDeviceNameSign = "COM"; | ||
private const string LinuxComDeviceNameSign = "tty"; | ||
private const string LinuxUsbDeviceNameSign = "ttyUSB"; | ||
private const string WindowsComPortNamePattern = $"{WindowsComDeviceNameSign}{{0}}"; | ||
private const string LinuxSerialDeviceNamePattern = "/dev/{0}"; | ||
private const string LinuxComDeviceNamePattern = $"{LinuxComDeviceNameSign}{{0}}"; | ||
private const string LinuxUsbDeviceNamePattern = $"{LinuxUsbDeviceNameSign}{{0}}"; | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
app/Wissance.Zerial/Wissance.Zerial.Common/Utils/SerialPortNumberExtractor.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
using System; | ||
using System.IO; | ||
using Avalonia; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Markup.Xaml; | ||
using Wissance.Zerial.Desktop.ViewModels; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Wissance.Zerial.Desktop.Views; | ||
|
||
namespace Wissance.Zerial.Desktop; | ||
|
||
public partial class App : Application | ||
namespace Wissance.Zerial.Desktop | ||
{ | ||
public override void Initialize() | ||
public class App : Application | ||
{ | ||
AvaloniaXamlLoader.Load(this); | ||
} | ||
static App() | ||
{ | ||
IConfigurationBuilder builder = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile(AppSettingsFile, optional: false, reloadOnChange: true); | ||
|
||
public override void OnFrameworkInitializationCompleted() | ||
{ | ||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||
Configuration = builder.Build(); | ||
|
||
ServiceCollection services = new ServiceCollection(); | ||
services.AddLogging(); | ||
|
||
ServiceProvider = services.BuildServiceProvider(); | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
AvaloniaXamlLoader.Load(this); | ||
} | ||
|
||
public override void OnFrameworkInitializationCompleted() | ||
{ | ||
desktop.MainWindow = new SplashScreenWindow(); | ||
//new MainWindow(); | ||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||
{ | ||
desktop.MainWindow = new SplashScreenWindow(); | ||
} | ||
|
||
base.OnFrameworkInitializationCompleted(); | ||
} | ||
|
||
public static IServiceProvider ServiceProvider { get; private set; } | ||
public static IConfiguration Configuration { get; private set; } | ||
|
||
base.OnFrameworkInitializationCompleted(); | ||
private const string AppSettingsFile = "appsettings.json"; | ||
} | ||
} |
Oops, something went wrong.