Skip to content

Commit

Permalink
#108 Mid0902 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickedb committed Dec 10, 2023
1 parent bbadb7d commit a359549
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/OpenProtocolInterpreter/DataField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static DataField Boolean(int field, int index, bool hasPrefix = true)
public static DataField Boolean(Enum field, int index, bool hasPrefix = true)
=> new(field, index, 1, hasPrefix);

public static DataField DateTime(int field, int index, bool hasPrefix = true)
public static DataField Timestamp(int field, int index, bool hasPrefix = true)
=> new(field, index, 19, hasPrefix);
public static DataField Timestamp(Enum field, int index, bool hasPrefix = true)
=> new(field, index, 19, hasPrefix);
Expand Down
15 changes: 13 additions & 2 deletions src/OpenProtocolInterpreter/OpenProtocolConvert.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using OpenProtocolInterpreter.Tightening;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
Expand Down Expand Up @@ -37,7 +38,7 @@ public static DateTime ToDateTime(string value)
if (!string.IsNullOrWhiteSpace(value.ToString()))
{
var date = value.ToString();
DateTime.TryParse(date.Substring(0, 10) + " " + date.Substring(11, 8), out convertedValue);
DateTime.TryParse($"{date.Substring(0, 10)} {date.Substring(11, 8)}", out convertedValue);
}

return convertedValue;
Expand Down Expand Up @@ -121,6 +122,16 @@ public static string ToString(IEnumerable<VariableDataField> value)
return builder.ToString();
}

public static string ToString(IEnumerable<TighteningResultDataField> value)
{
var builder = new StringBuilder();
foreach (var v in value)
{
builder.Append(v.Pack());
}
return builder.ToString();
}

public static bool GetBit(byte b, int bitNumber) => (b & (1 << bitNumber - 1)) != 0;

public static byte ToByte(bool[] values)
Expand Down
83 changes: 83 additions & 0 deletions src/OpenProtocolInterpreter/Tightening/Mid0902.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpenProtocolInterpreter.Tightening
{
public class Mid0902 : Mid, ITightening, IIntegrator
{
public const int Mid = 902;

public long Capacity { get; set; }
public long OldestSequenceNumber { get; set; }
public DateTime OldestTime { get; set; }
public long NewestSequenceNumber { get; set; }
public DateTime NewestTime { get; set; }
public int NumberOfPIDs { get; set; }
public List<TighteningResultDataField> TighteningDataFields { get; set; }

public Mid0902() : this(new Header()
{
Mid = Mid,
Revision = DEFAULT_REVISION
})
{

}

public Mid0902(Header header) : base(header)
{
}

public override string Pack()
{
var revision = Header.StandardizedRevision;
GetField(revision, DataFields.DataFields).SetValue(OpenProtocolConvert.ToString(TighteningDataFields));

var index = 1;
return string.Concat(BuildHeader(), base.Pack(revision, ref index));
}

public override Mid Parse(string package)
{
Header = ProcessHeader(package);

var field = GetField(1, DataFields.DataFields);
field.Size = Header.Length - field.Index;
base.Parse(package);
TighteningDataFields = TighteningResultDataField.ParseAll(field.Value).ToList();
return this;
}

protected override Dictionary<int, List<DataField>> RegisterDatafields()
{
return new Dictionary<int, List<DataField>>()
{
{
1, new List<DataField>()
{
DataField.Number(DataFields.Capacity, 20, 10, false),
DataField.Number(DataFields.OldestSequenceNumber, 30, 10, false),
DataField.Timestamp(DataFields.OldestTime, 40, false),
DataField.Number(DataFields.NewestSequenceNumber, 59, 10, false),
DataField.Timestamp(DataFields.NewestTime, 69, false),
DataField.Number(DataFields.NumberOfPIDs, 88, 10, false),
DataField.Volatile(DataFields.DataFields, 98, false)
}
}
};
}

protected enum DataFields
{
Capacity,
OldestSequenceNumber,
OldestTime,
NewestSequenceNumber,
NewestTime,
NumberOfPIDs,
DataFields
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenProtocolInterpreter.Tightening
{
public class TighteningResultDataField
{
public int ParameterId { get; set; }
public int Length { get; set; }
public DataTypeDefinition DataType { get; set; }
public DataUnitType Unit { get; set; }
public string DataValue { get; set; }

public string Pack()
{
return OpenProtocolConvert.ToString('0', 5, PaddingOrientation.LeftPadded, ParameterId) +
OpenProtocolConvert.ToString('0', 3, PaddingOrientation.LeftPadded, Length) +
OpenProtocolConvert.ToString('0', 2, PaddingOrientation.LeftPadded, DataType) +
OpenProtocolConvert.ToString('0', 3, PaddingOrientation.LeftPadded, Unit) +
OpenProtocolConvert.TruncatePadded(' ', Length, PaddingOrientation.RightPadded, DataValue);
}

public static TighteningResultDataField Parse(string value)
{
var length = OpenProtocolConvert.ToInt32(value.Substring(5, 3));
return Parse(value, length);
}

public static IEnumerable<TighteningResultDataField> ParseAll(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
yield break;
}

int valueLength;
const int fixedLength = 13;
for (int i = 0; i < value.Length; i += fixedLength + valueLength)
{
valueLength = OpenProtocolConvert.ToInt32(value.Substring(i + 5, 3));
var section = value.Substring(i, fixedLength + valueLength);
yield return Parse(section, valueLength);
}
}

private static TighteningResultDataField Parse(string value, int length)
{
return new TighteningResultDataField()
{
ParameterId = OpenProtocolConvert.ToInt32(value.Substring(0, 5)),
Length = length,
DataType = (DataTypeDefinition)OpenProtocolConvert.ToInt32(value.Substring(8, 2)),
Unit = (DataUnitType)OpenProtocolConvert.ToInt32(value.Substring(10, 3)),
DataValue = value.Substring(13, length)
};
}

}
}

0 comments on commit a359549

Please sign in to comment.