Skip to content

Commit 96247d1

Browse files
OATControl V0.9.2.0 updates
- Discovery of Wifi boards implemented. - Moved some comms code (Serial) into Framework DLL from Standard DLL - Implemented Chooser dialog in OATControl.
1 parent 4ca9a78 commit 96247d1

File tree

12 files changed

+227
-101
lines changed

12 files changed

+227
-101
lines changed

Software/OATMobile/OATCommunications/ClientAdapters/UdpClientAdapter.cs

Lines changed: 98 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,105 @@
33
using System.Net;
44
using System.Net.Sockets;
55
using System.Text;
6+
using System.Threading;
67

78
namespace OATCommunications.ClientAdapters
89
{
9-
class UdpClientAdapter
10-
{
11-
public event EventHandler<ClientFoundEventArgs> ClientFound;
12-
13-
private readonly string _hostToFind;
14-
private int _port;
15-
16-
public UdpClientAdapter(string hostToFind, int port) {
17-
_hostToFind = hostToFind;
18-
_port = port;
19-
}
20-
21-
public void StartClientSearch()
22-
{
23-
using (UdpClient c = new UdpClient(_port))
24-
{
25-
var data = Encoding.ASCII.GetBytes($"skyfi:{_hostToFind}?");
26-
c.Send(data, data.Length);
27-
}
28-
}
29-
30-
private void ClientListener() {
31-
// create a udp client to listen for requests
32-
var udpClient = new UdpClient();
33-
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
34-
// listen for computers other than localhost
35-
udpClient.ExclusiveAddressUse = false;
36-
37-
// IPAddress.Any allows us to listen to broadcast UDP packets
38-
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, _port));
39-
40-
var from = new IPEndPoint(0, 0);
41-
}
42-
43-
public string SendCommand(string command)
44-
{
45-
throw new NotImplementedException();
46-
}
47-
48-
protected virtual void OnClientFound(ClientFoundEventArgs e)
49-
{
50-
var handler = ClientFound;
51-
handler?.Invoke(this, e);
52-
}
53-
54-
public class ClientFoundEventArgs : EventArgs
55-
{
56-
public string Name { get; set; }
57-
public IPAddress Address { get; set; }
58-
}
59-
}
10+
public class UdpClientAdapter : IDisposable
11+
{
12+
public event EventHandler<ClientFoundEventArgs> ClientFound;
13+
14+
private readonly string _hostToFind;
15+
private int _port;
16+
private UdpClient _udpClient;
17+
private bool disposedValue;
18+
19+
public UdpClientAdapter(string hostToFind, int port)
20+
{
21+
_hostToFind = hostToFind;
22+
_port = port;
23+
// create a udp client to listen for requests
24+
_udpClient = new UdpClient();
25+
}
26+
27+
public void StartClientSearch()
28+
{
29+
_udpClient = new UdpClient();
30+
_udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
31+
// listen for computers other than localhost
32+
_udpClient.ExclusiveAddressUse = false;
33+
34+
// IPAddress.Any allows us to listen to broadcast UDP packets
35+
_udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, _port));
36+
37+
var data = Encoding.ASCII.GetBytes($"skyfi:{_hostToFind}?");
38+
_udpClient.Send(data, data.Length, new IPEndPoint(IPAddress.Broadcast, _port));
39+
_udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
40+
}
41+
42+
private void ReceiveCallback(IAsyncResult ar)
43+
{
44+
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, _port);
45+
byte[] received = _udpClient.EndReceive(ar, ref remoteIpEndPoint);
46+
string result = Encoding.UTF8.GetString(received);
47+
var parts = result.Split('@');
48+
if (parts.Length == 2)
49+
{
50+
OnClientFound(new ClientFoundEventArgs("OAT", IPAddress.Parse(parts[1])));
51+
}
52+
_udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
53+
}
54+
55+
public string SendCommand(string command)
56+
{
57+
throw new NotImplementedException();
58+
}
59+
60+
protected virtual void OnClientFound(ClientFoundEventArgs e)
61+
{
62+
var handler = ClientFound;
63+
handler?.Invoke(this, e);
64+
}
65+
66+
public class ClientFoundEventArgs : EventArgs
67+
{
68+
public ClientFoundEventArgs(string name, IPAddress ip)
69+
{
70+
Name = name;
71+
Address = ip;
72+
}
73+
74+
public string Name { get; set; }
75+
public IPAddress Address { get; set; }
76+
}
77+
78+
protected virtual void Dispose(bool disposing)
79+
{
80+
if (!disposedValue)
81+
{
82+
if (disposing)
83+
{
84+
_udpClient?.Dispose();
85+
}
86+
87+
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
88+
// TODO: set large fields to null
89+
disposedValue = true;
90+
}
91+
}
92+
93+
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
94+
// ~UdpClientAdapter()
95+
// {
96+
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
97+
// Dispose(disposing: false);
98+
// }
99+
100+
public void Dispose()
101+
{
102+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
103+
Dispose(disposing: true);
104+
GC.SuppressFinalize(this);
105+
}
106+
}
60107
}

Software/OATMobile/OATCommunications/OATCommunications.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1111
</PackageReference>
1212
<PackageReference Include="PropertyChanged.Fody" Version="3.2.8" />
13-
<PackageReference Include="System.IO.Ports" Version="4.7.0" />
1413
</ItemGroup>
1514

1615
</Project>

Software/OATMobile/OATCommunications/CommunicationHandlers/CommunicationHandlerFactory.cs renamed to Software/OpenAstroTracker ASCOM/OATCommuncations.WPF/CommunicationHandlers/CommunicationHandlerFactory.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@
55
using System.IO.Ports;
66
using OATCommunications.ClientAdapters;
77
using static OATCommunications.ClientAdapters.UdpClientAdapter;
8+
using OATCommunications.CommunicationHandlers;
9+
using System.Collections.ObjectModel;
810

9-
namespace OATCommunications.CommunicationHandlers
11+
namespace OATCommunications.WPF.CommunicationHandlers
1012
{
1113
public static class CommunicationHandlerFactory
1214
{
13-
static IList<string> _available = new List<string>();
15+
static ObservableCollection<string> _available = new ObservableCollection<string>();
1416
public static void DiscoverDevices()
1517
{
1618
foreach (var port in SerialPort.GetPortNames())
1719
{
1820
_available.Add("Serial : " + port);
1921
}
2022

21-
var searcher = new UdpClientAdapter("OATerScope", 4030);
23+
var searcher = new UdpClientAdapter("OATerScope", 4031);
2224
searcher.ClientFound += OnWifiClientFound;
2325
searcher.StartClientSearch();
2426
}
2527

2628
private static void OnWifiClientFound(object sender, ClientFoundEventArgs e)
2729
{
28-
_available.Add($"WiFi : {e.Name} ({e.Address})");
30+
_available.Add($"WiFi : {e.Name} ({e.Address}:4030)");
2931
}
3032

31-
public static IList<String> AvailableDevices { get { return _available; } }
33+
public static ObservableCollection<String> AvailableDevices { get { return _available; } }
3234

3335
public static ICommunicationHandler ConnectToDevice(string device)
3436
{
@@ -39,7 +41,8 @@ public static ICommunicationHandler ConnectToDevice(string device)
3941
}
4042
else if (device.StartsWith("WiFi : "))
4143
{
42-
string ipAddress = device.Substring("WiFi : ".Length);
44+
var parts = device.Split("()".ToCharArray());
45+
string ipAddress = parts[1];
4346
return new TcpCommunicationHandler(ipAddress);
4447
}
4548

Software/OATMobile/OATCommunications/CommunicationHandlers/SerialCommunicationHandler.cs renamed to Software/OpenAstroTracker ASCOM/OATCommuncations.WPF/CommunicationHandlers/SerialCommunicationHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System;
1+
using OATCommunications.CommunicationHandlers;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO.Ports;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
78

8-
namespace OATCommunications.CommunicationHandlers
9+
namespace OATCommunications.WPF.CommunicationHandlers
910
{
1011
public class SerialCommunicationHandler : ICommunicationHandler
1112
{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{33D73B16-4C06-4FB0-95EF-350929570F03}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>OATCommuncations.WPF</RootNamespace>
11+
<AssemblyName>OATCommuncations.WPF</AssemblyName>
12+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Net.Http" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="CommunicationHandlers\CommunicationHandlerFactory.cs" />
45+
<Compile Include="CommunicationHandlers\SerialCommunicationHandler.cs" />
46+
<Compile Include="Properties\AssemblyInfo.cs" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<ProjectReference Include="..\..\OATMobile\OATCommunications\OATCommunications.csproj">
50+
<Project>{83ca4f93-43ce-4da1-839a-f65f51fc34ed}</Project>
51+
<Name>OATCommunications</Name>
52+
</ProjectReference>
53+
</ItemGroup>
54+
<ItemGroup />
55+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
56+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("OATCommuncations.WPF")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("OATCommuncations.WPF")]
13+
[assembly: AssemblyCopyright("Copyright © 2020")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("33d73b16-4c06-4fb0-95ef-350929570f03")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Software/OpenAstroTracker ASCOM/OATControl/DlgChooseOat.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
xmlns:local="clr-namespace:OATControl"
88
mc:Ignorable="d"
9-
Title="Connect to OpenAstroTracker" Height="148.39" Width="394.862" ResizeMode="NoResize" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow">
9+
Title="Connect to OpenAstroTracker" Height="225.39" Width="349.862" ResizeMode="NoResize" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow">
1010
<Grid>
1111
<Grid.ColumnDefinitions>
1212
<ColumnDefinition Width="Auto" />
@@ -17,11 +17,11 @@
1717
<RowDefinition Height="*" />
1818
<RowDefinition Height="Auto" />
1919
</Grid.RowDefinitions>
20-
<TextBlock Grid.Row="0" Grid.Column="0" Text="Available Connections" Margin="10,24,10,0" Style="{StaticResource TextBlockHeading}" />
21-
<ComboBox Grid.Row="0" Grid.Column="1" Text="--- Choose Connection ---" Margin="0,20,10,0" ItemsSource="{Binding AvailableDevices}" SelectedIndex="{Binding SelectedDevice}"/>
20+
<TextBlock Grid.Row="0" Grid.Column="0" Text="Available Connections" Margin="10,24,10,0" Style="{StaticResource MetroTextBlock}" />
21+
<ComboBox Grid.Row="0" Grid.Column="1" Text="--- Choose Connection ---" Margin="0,20,10,0" ItemsSource="{Binding AvailableDevices}" SelectedItem="{Binding SelectedDevice}"/>
2222
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
2323
<Button Content="OK" Command="{Binding OKCommand}" Margin="10" Width="80" Style="{StaticResource AccentedSquareButtonStyle}" IsDefault="True" />
24-
<Button Content="Cancel" Command="{Binding CancelCommand}" Margin="10" Width="80" Style="{StaticResource AccentedSquareButtonStyle}" IsCancel="True" />
24+
<Button Content="Cancel" Margin="10" Width="80" Style="{StaticResource AccentedSquareButtonStyle}" IsCancel="True" />
2525
</StackPanel>
2626
</Grid>
2727
</Controls:MetroWindow>

Software/OpenAstroTracker ASCOM/OATControl/DlgChooseOat.xaml.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MahApps.Metro.Controls;
2-
using OATCommunications.CommunicationHandlers;
2+
using OATCommunications.WPF.CommunicationHandlers;
3+
using OATControl.ViewModels;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -21,11 +22,17 @@ namespace OATControl
2122
/// </summary>
2223
public partial class DlgChooseOat : MetroWindow
2324
{
25+
private DelegateCommand _okCommand;
2426
public DlgChooseOat()
2527
{
28+
_okCommand = new DelegateCommand(() => { this.DialogResult = true; Close(); });
29+
30+
this.DataContext = this;
2631
InitializeComponent();
32+
2733
}
2834

35+
public ICommand OKCommand { get { return _okCommand; } }
2936

3037
public IList<string> AvailableDevices
3138
{

0 commit comments

Comments
 (0)