-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyTouchUnit.cs
88 lines (78 loc) · 2.57 KB
/
EasyTouchUnit.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ScreenLogicConnect;
public class EasyTouchUnit
{
public string? GatewayName { get; private set; }
public byte GatewaySubType { get; private set; }
public byte GatewayType { get; private set; }
public IPAddress? IPAddress { get; private set; }
public bool IsValid { get; private set; }
public short Port { get; private set; }
/// <summary>
/// Creates a new EasyTouchUnit if the message contains valid unit information and null otherwise.
/// </summary>
/// <param name="result">The message received from a connection</param>
/// <returns>The EasyTouchUnit if the message is valid</returns>
public static EasyTouchUnit? Create(UdpReceiveResult result)
{
EasyTouchUnit unit = new(result);
if (unit.IsValid)
{
return unit;
}
return null;
}
/// <summary>
/// Creates a new EasyTouchUnit if the message contains valid unit information and null otherwise.
/// </summary>
/// <param name="data">The message received from a remote connection request</param>
/// <returns>The EasyTouchUnit if the message is valid</returns>
public static EasyTouchUnit? Create(Messages.GetGatewayData data)
{
EasyTouchUnit unit = new(data);
if (unit.IsValid)
{
return unit;
}
return null;
}
public EasyTouchUnit(UdpReceiveResult result)
{
try
{
IPAddress = result.RemoteEndPoint.Address;
using var ms = new MemoryStream(result.Buffer);
using var br = new BinaryReader(ms);
var unitType = br.ReadInt32();
if (unitType == 2)
{
br.ReadBytes(4);
Port = br.ReadInt16();
GatewayType = br.ReadByte();
GatewaySubType = br.ReadByte();
GatewayName = Encoding.ASCII.GetString(result.Buffer.Skip((int)ms.Position).TakeWhile(x => x != 0).ToArray());
IsValid = true;
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
}
public EasyTouchUnit(Messages.GetGatewayData data)
{
try
{
GatewayName = data.GatewayName;
IPAddress = IPAddress.Parse(data.IPAddr!);
Port = data.Port;
IsValid = data.GatewayFound && data.PortOpen;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
}
}