Files
dotnet-screenlogic/UnitConnection.cs
Parnic ba089e3c00 Added controller config message
Added circuit data to pool status message
Sanitized byte signedness
Added helper to get pool/spa on/off state from pool status
Added simple color class
Removed debug output
Added basic status output to test program
2018-03-27 17:29:34 -05:00

141 lines
4.6 KiB
C#

using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace ScreenLogicConnect
{
public class UnitConnection : IDisposable
{
TcpClient client;
public async Task ConnectTo(EasyTouchUnit unit)
{
if (client != null)
{
client.Dispose();
}
client = new TcpClient();
await client.ConnectAsync(unit.ipAddress, unit.port);
var connMsg = CreateConnectServerSoftMessage();
var stream = client.GetStream();
stream.Write(connMsg, 0, connMsg.Length);
stream.SendHLMessage(Messages.ChallengeString.QUERY(0));
Debug.WriteLine("sent challenge string");
var recvBuf = new byte[1024];
var readBytes = stream.Read(recvBuf, 0, recvBuf.Length);
Debug.WriteLine("read {0}", readBytes);
stream.SendHLMessage(createLoginMessage(new sbyte[16]));
Debug.WriteLine("sent login message");
readBytes = stream.Read(recvBuf, 0, recvBuf.Length);
Debug.WriteLine("read {0}", readBytes);
}
public Messages.GetPoolStatus GetPoolStatus()
{
client.GetStream().SendHLMessage(Messages.GetPoolStatus.QUERY(0));
Debug.WriteLine("sent status message");
return new Messages.GetPoolStatus(getMessage(client.GetStream()));
}
public Messages.GetControllerConfig GetControllerConfig()
{
client.GetStream().SendHLMessage(Messages.GetControllerConfig.QUERY(0));
Debug.WriteLine("sent controller config message");
return new Messages.GetControllerConfig(getMessage(client.GetStream()));
}
public Messages.GetMode GetMode()
{
client.GetStream().SendHLMessage(Messages.GetMode.QUERY(0));
Debug.WriteLine("sent get-mode message");
return new Messages.GetMode(getMessage(client.GetStream()));
}
private static Messages.HLMessage getMessage(NetworkStream ns)
{
int bytesRead = 0;
sbyte[] headerBuffer = new sbyte[8];
while (bytesRead < 8)
{
try
{
bytesRead += ns.Read((byte[])(Array)headerBuffer, bytesRead, 8 - bytesRead);
if (bytesRead < 0)
{
return null;
}
}
catch { }
}
int msgDataSize = Messages.HLMessage.extractDataSize(headerBuffer);
if (msgDataSize <= 0 || msgDataSize >= 100000)
{
return null;
}
sbyte[] dataBuffer = new sbyte[msgDataSize];
bytesRead = 0;
while (bytesRead < msgDataSize)
{
bytesRead += ns.Read((byte[])(Array)dataBuffer, bytesRead, msgDataSize - bytesRead);
if (bytesRead < 0)
{
return null;
}
}
return new Messages.HLMessage(headerBuffer, dataBuffer);
}
private static string connectionMessage = "CONNECTSERVERHOST";
private byte[] CreateConnectServerSoftMessage()
{
int iLen = connectionMessage.Length;
byte[] bytes = new byte[(iLen + 4)];
var connBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(connectionMessage);
for (int i = 0; i < iLen; i++)
{
bytes[i] = connBytes[i];
}
bytes[iLen + 0] = (byte)13;
bytes[iLen + 1] = (byte)10;
bytes[iLen + 2] = (byte)13;
bytes[iLen + 3] = (byte)10;
return bytes;
}
private Messages.HLMessage createLoginMessage(sbyte[] encodedPwd)
{
Messages.ClientLogin login = new Messages.ClientLogin((short)0, (short)27);
login.set_schema(348);
login.set_connectionType(0);
login.set_version("Android");
if (encodedPwd.Length > 16)
{
sbyte[] temp = new sbyte[16];
for (int i = 0; i < 16; i++)
{
temp[i] = encodedPwd[i];
}
login.set_byteArray(temp);
}
else
{
login.set_byteArray(encodedPwd);
}
login.set_procID(2);
return login;
}
public void Dispose()
{
if (client != null)
{
client.Dispose();
}
}
}
}