Converted as much ugly code as I could find into appropriate C#-friendly versions. For example, all the manual ByteHelper stuff, buffer index tracking, etc. are now BinaryWriters/BinaryReaders. Also cleaned up a bunch of getter and setter methods to use C# properties.

There's still more to be done here, but this greatly simplifies the code for reading and comprehension.
This commit is contained in:
2018-03-28 15:28:18 -05:00
parent f893882c4e
commit e524e394c6
16 changed files with 355 additions and 921 deletions

View File

@ -1,32 +1,34 @@
using System;
using System.IO;
namespace ScreenLogicConnect.Messages
{
public class GetControllerConfig : HLMessage
{
private const int PUM_CIRC_COUNT = 8;
private BodyDataStructure[] bodyArray;
private int colorCount;
private int m_CircuitCount;
private PentLightColor[] m_ColorArray;
private byte m_ControllerData;
private int m_ControllerID;
private byte m_ControllerType;
private byte m_DegC;
private int m_EquipFlags;
private byte m_HWType;
private int m_InterfaceTabFlags;
private byte[] m_MaxSetPoint;
private byte[] m_MinSetPoint;
private byte[] m_PumpCircArray;
private int m_ShowAlarms;
private String m_genCircuitName;
public const short HLM_POOL_GETCTLRCONFIGQ = (short)12532;
public BodyDataStructure[] bodyArray { get; private set; }
public int colorCount { get; private set; }
public int m_CircuitCount { get; private set; }
public PentLightColor[] m_ColorArray { get; private set; }
public byte m_ControllerData { get; private set; }
public int m_ControllerID { get; private set; }
public byte m_ControllerType { get; private set; }
public byte m_DegC { get; private set; }
public int m_EquipFlags { get; private set; }
public byte m_HWType { get; private set; }
public int m_InterfaceTabFlags { get; private set; }
public byte[] m_MaxSetPoint { get; private set; } = new byte[2];
public byte[] m_MinSetPoint { get; private set; } = new byte[2];
public byte[] m_PumpCircArray { get; private set; } = new byte[PUM_CIRC_COUNT];
public int m_ShowAlarms { get; private set; }
public String m_genCircuitName { get; private set; }
public const short HLM_POOL_GETCTLRCONFIGQ = 12532;
public static GetControllerConfig QUERY(short senderID)
{
return new GetControllerConfig(senderID, (short)HLM_POOL_GETCTLRCONFIGQ);
return new GetControllerConfig(senderID, HLM_POOL_GETCTLRCONFIGQ);
}
private GetControllerConfig(short senderID, short msgID)
@ -34,7 +36,7 @@ namespace ScreenLogicConnect.Messages
{
}
public GetControllerConfig(sbyte[] header, sbyte[] data)
public GetControllerConfig(byte[] header, byte[] data)
: base(header, data)
{
}
@ -44,176 +46,75 @@ namespace ScreenLogicConnect.Messages
{
}
public override sbyte[] asByteArray()
public override byte[] asByteArray()
{
putInteger(0);
putInteger(0);
using (var ms = new MemoryStream())
{
using (var bw = new BinaryWriter(ms))
{
bw.Write(0);
bw.Write(0);
}
data = ms.ToArray();
}
return base.asByteArray();
}
protected override void decode()
{
int i;
this.m_MinSetPoint = new byte[2];
this.m_MaxSetPoint = new byte[2];
this.startIndex = 0;
this.m_ControllerID = ByteHelper.getIntFromByteArrayLittleEndian(this.data, this.startIndex);
this.startIndex += 4;
this.m_MinSetPoint[0] = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.m_MaxSetPoint[0] = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.m_MinSetPoint[1] = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.m_MaxSetPoint[1] = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.m_DegC = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.m_ControllerType = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.m_HWType = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.m_ControllerData = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.m_EquipFlags = ByteHelper.getIntFromByteArrayLittleEndian(this.data, this.startIndex);
this.startIndex += 4;
this.m_genCircuitName = HLMessageTypeHelper.extractString(this.data, ref this.startIndex);
this.m_CircuitCount = ByteHelper.getIntFromByteArrayLittleEndian(this.data, this.startIndex);
this.startIndex += 4;
this.bodyArray = new BodyDataStructure[this.m_CircuitCount];
for (i = 0; i < this.m_CircuitCount; i++)
using (var ms = new MemoryStream(data))
{
this.bodyArray[i] = new BodyDataStructure();
this.bodyArray[i].m_circuitID = ByteHelper.getIntFromByteArrayLittleEndian(this.data, this.startIndex);
this.startIndex += 4;
this.bodyArray[i].m_name = HLMessageTypeHelper.extractString(this.data, ref this.startIndex);
this.bodyArray[i].m_nameIndex = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_function = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_interface = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_flags = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_colorSet = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_colorPos = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_colorStagger = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_deviceID = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_dfaultRT = ByteHelper.getShortFromByteArrayAsLittleEndian(this.data, this.startIndex);
this.startIndex += 2;
this.bodyArray[i].m_Pad1 = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
this.bodyArray[i].m_Pad2 = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
using (var br = new BinaryReader(ms))
{
m_ControllerID = br.ReadInt32();
for (int i = 0; i < 2; i++)
{
m_MinSetPoint[i] = br.ReadByte();
m_MaxSetPoint[i] = br.ReadByte();
}
m_DegC = br.ReadByte();
m_ControllerType = br.ReadByte();
m_HWType = br.ReadByte();
m_ControllerData = br.ReadByte();
m_EquipFlags = br.ReadInt32();
m_genCircuitName = HLMessageTypeHelper.extractString(br);
m_CircuitCount = br.ReadInt32();
bodyArray = new BodyDataStructure[m_CircuitCount];
for (int i = 0; i < m_CircuitCount; i++)
{
bodyArray[i] = new BodyDataStructure()
{
m_circuitID = br.ReadInt32(),
m_name = HLMessageTypeHelper.extractString(br),
m_nameIndex = br.ReadByte(),
m_function = br.ReadByte(),
m_interface = br.ReadByte(),
m_flags = br.ReadByte(),
m_colorSet = br.ReadByte(),
m_colorPos = br.ReadByte(),
m_colorStagger = br.ReadByte(),
m_deviceID = br.ReadByte(),
m_dfaultRT = br.ReadInt16(),
m_Pad1 = br.ReadByte(),
m_Pad2 = br.ReadByte(),
};
}
colorCount = br.ReadInt32();
m_ColorArray = new PentLightColor[colorCount];
for (int i = 0; i < colorCount; i++)
{
m_ColorArray[i] = new PentLightColor(HLMessageTypeHelper.extractString(br), HLMessageTypeHelper.extractColor(br));
}
for (int i = 0; i < PUM_CIRC_COUNT; i++)
{
m_PumpCircArray[i] = br.ReadByte();
}
m_InterfaceTabFlags = br.ReadInt32();
m_ShowAlarms = br.ReadInt32();
}
}
this.colorCount = ByteHelper.getIntFromByteArrayLittleEndian(this.data, this.startIndex);
this.startIndex += 4;
this.m_ColorArray = new PentLightColor[this.colorCount];
for (i = 0; i < this.colorCount; i++)
{
String name = HLMessageTypeHelper.extractString(this.data, ref this.startIndex);
RgbColor color = HLMessageTypeHelper.extractColor(this.data, ref this.startIndex);
this.m_ColorArray[i] = new PentLightColor(name, color);
}
this.m_PumpCircArray = new byte[8];
for (i = 0; i < 8; i++)
{
this.m_PumpCircArray[i] = ByteHelper.getUnsignedByteFromByteArray(this.data, this.startIndex);
this.startIndex++;
}
this.m_InterfaceTabFlags = ByteHelper.getIntFromByteArrayLittleEndian(this.data, this.startIndex);
this.startIndex += 4;
this.m_ShowAlarms = ByteHelper.getIntFromByteArrayLittleEndian(this.data, this.startIndex);
this.startIndex += 4;
}
public int getM_ControllerID()
{
return this.m_ControllerID;
}
public byte getM_DegC()
{
return this.m_DegC;
}
public byte getM_ControllerType()
{
return this.m_ControllerType;
}
public byte getM_HWType()
{
return this.m_HWType;
}
public byte getM_ControllerData()
{
return this.m_ControllerData;
}
public int getM_EquipFlags()
{
return this.m_EquipFlags;
}
public String getM_genCircuitName()
{
return this.m_genCircuitName;
}
public int getM_CircuitCount()
{
return this.m_CircuitCount;
}
public byte[] getMinSetPoint()
{
return this.m_MinSetPoint;
}
public byte[] getMaxSetPoint()
{
return this.m_MaxSetPoint;
}
public BodyDataStructure[] getBodyArray()
{
return this.bodyArray;
}
public int getM_interfaceTabFlags()
{
return this.m_InterfaceTabFlags;
}
public int getM_showAlarms()
{
return this.m_ShowAlarms;
}
public PentLightColor[] getColorLightList()
{
return this.m_ColorArray;
}
public int getColorCount()
{
return this.colorCount;
}
public int getPumpCirCount()
{
return 8;
}
public byte[] getPumpCirList()
{
return this.m_PumpCircArray;
}
}
}