Files
dotnet-screenlogic/FindUnits.cs
Parnic d6dfcb14a9 Added support for remote connection by name via Pentair servers
This brings along proper password encryption and validation for the login process (which is still not needed for local connections).

You can run the remote connection tests by passing a system name and password as 2 arguments (so the system name needs to be quoted, e.g.: `test "Pentair: xx-xx-xx" 1234`)

Also updated to latest .NET Core and C# versions while I was at it.
2019-02-20 15:19:42 -06:00

60 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace ScreenLogicConnect
{
public class FindUnits
{
public static readonly byte[] broadcastData = new byte[]
{
1, 0, 0, 0,
0, 0, 0, 0,
};
protected const short multicastPort = 1444;
private Socket searchSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
public static async Task<List<EasyTouchUnit>> Find()
{
var units = new List<EasyTouchUnit>();
using (var udpClient = new UdpClient(new IPEndPoint(GetMyIP(), 53112))
{
EnableBroadcast = true,
MulticastLoopback = false,
})
{
await udpClient.SendAsync(broadcastData, broadcastData.Length, new IPEndPoint(IPAddress.Broadcast, multicastPort));
var buf = await udpClient.ReceiveAsync().TimeoutAfter(TimeSpan.FromSeconds(1));
if (buf != null && buf.RemoteEndPoint != null)
{
var findServerResponse = new EasyTouchUnit(buf);
if (findServerResponse.isValid)
{
units.Add(findServerResponse);
}
}
}
return units;
}
public static IPAddress GetMyIP()
{
IPAddress localIP;
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
localIP = endPoint.Address;
}
return localIP;
}
}
}