Skip to content

Commit

Permalink
#108 Mid0902 tests and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickedb committed Dec 10, 2023
1 parent a359549 commit 435f729
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 92 deletions.
55 changes: 55 additions & 0 deletions src/MIDTesters.Core/Tightening/TestMid0902.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenProtocolInterpreter.Tightening;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MIDTesters.Core.Tightening
{
[TestClass]
[TestCategory("Tightening")]
public class TestMid0902 : MidTester
{

[TestMethod]
[TestCategory("Revision 1"), TestCategory("ASCII")]
public void Mid0902Revision1()
{
string package = "01450902001 000000213400000000012022-12-21:13:02:3300000058192023-12-10:12:28:54002000050010600000001000100190500000002023-11-09:18:48:14";
var mid = _midInterpreter.Parse<Mid0902>(package);

Assert.AreEqual(typeof(Mid0902), mid.GetType());
Assert.AreNotEqual(0, mid.Capacity);
Assert.AreNotEqual(0, mid.OldestSequenceNumber);
Assert.IsNotNull(mid.OldestTime);
Assert.AreNotEqual(0, mid.NewestSequenceNumber);
Assert.IsNotNull(mid.NewestTime);
Assert.AreNotEqual(0, mid.NumberOfPIDs);
Assert.IsNotNull(mid.VariableDataFields);
Assert.AreNotEqual(0, mid.VariableDataFields.Count);
AssertEqualPackages(package, mid);
}

[TestMethod]
[TestCategory("Revision 1"), TestCategory("ByteArray")]
public void Mid0902ByteRevision1()
{
string package = "01450902001 000000213400000000012022-12-21:13:02:3300000058192023-12-10:12:28:54002000050010600000001000100190500000002023-11-09:18:48:14";
byte[] bytes = GetAsciiBytes(package);
var mid = _midInterpreter.Parse<Mid0902>(bytes);

Assert.AreEqual(typeof(Mid0902), mid.GetType());
Assert.AreNotEqual(0, mid.Capacity);
Assert.AreNotEqual(0, mid.OldestSequenceNumber);
Assert.IsNotNull(mid.OldestTime);
Assert.AreNotEqual(0, mid.NewestSequenceNumber);
Assert.IsNotNull(mid.NewestTime);
Assert.AreNotEqual(0, mid.NumberOfPIDs);
Assert.IsNotNull(mid.VariableDataFields);
Assert.AreNotEqual(0, mid.VariableDataFields.Count);
AssertEqualPackages(package, mid);
}
}
}
2 changes: 1 addition & 1 deletion src/OpenProtocolInterpreter/Communication/Mid0006.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace OpenProtocolInterpreter.Communication
{
/// <summary>
/// Application Communication positive acknowledge
/// Application data message request
/// <para>
/// Do a request for data. This message is used for ALL request handling.
/// When used it substitutes the use of all MID special request messages.
Expand Down
10 changes: 0 additions & 10 deletions src/OpenProtocolInterpreter/OpenProtocolConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ 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
1 change: 0 additions & 1 deletion src/OpenProtocolInterpreter/Tightening/Mid0066.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Text;

namespace OpenProtocolInterpreter.Tightening
{
Expand Down
64 changes: 47 additions & 17 deletions src/OpenProtocolInterpreter/Tightening/Mid0902.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpenProtocolInterpreter.Tightening
{
public class Mid0902 : Mid, ITightening, IIntegrator
/// <summary>
/// Tightening Result DB Info Upload
/// <para>This message contains information concerning the tightening result database on the controller.</para>
/// <para><see cref="Communication.Mid0006"/> Application Data Message Request shall be used for fetching this message</para>
/// <para>Message sent by: Controller</para>
/// <para>Answer: None</para>
/// </summary>
public class Mid0902 : Mid, ITightening, IController
{
public const int Mid = 902;
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 long Capacity
{
get => GetField(1, DataFields.Capacity).GetValue(OpenProtocolConvert.ToInt32);
set => GetField(1, DataFields.Capacity).SetValue(OpenProtocolConvert.ToString, value);
}
public long OldestSequenceNumber
{
get => GetField(1, DataFields.OldestSequenceNumber).GetValue(OpenProtocolConvert.ToInt32);
set => GetField(1, DataFields.OldestSequenceNumber).SetValue(OpenProtocolConvert.ToString, value);
}
public DateTime OldestTime
{
get => GetField(1, DataFields.OldestTime).GetValue(OpenProtocolConvert.ToDateTime);
set => GetField(1, DataFields.OldestTime).SetValue(OpenProtocolConvert.ToString, value);
}
public long NewestSequenceNumber
{
get => GetField(1, DataFields.NewestSequenceNumber).GetValue(OpenProtocolConvert.ToInt32);
set => GetField(1, DataFields.NewestTime).SetValue(OpenProtocolConvert.ToString, value);
}
public DateTime NewestTime
{
get => GetField(1, DataFields.NewestTime).GetValue(OpenProtocolConvert.ToDateTime);
set => GetField(1, DataFields.NewestTime).SetValue(OpenProtocolConvert.ToString, value);
}
public int NumberOfPIDs
{
get => GetField(1, DataFields.NumberOfPIDs).GetValue(OpenProtocolConvert.ToInt32);
set => GetField(1, DataFields.NumberOfPIDs).SetValue(OpenProtocolConvert.ToString, value);
}
public List<VariableDataField> VariableDataFields { get; set; }

public Mid0902() : this(new Header()
{
Mid = Mid,
Mid = MID,
Revision = DEFAULT_REVISION
})
{
Expand All @@ -33,7 +63,7 @@ public Mid0902(Header header) : base(header)
public override string Pack()
{
var revision = Header.StandardizedRevision;
GetField(revision, DataFields.DataFields).SetValue(OpenProtocolConvert.ToString(TighteningDataFields));
GetField(revision, DataFields.VariableDataFields).SetValue(OpenProtocolConvert.ToString(VariableDataFields));

var index = 1;
return string.Concat(BuildHeader(), base.Pack(revision, ref index));
Expand All @@ -43,10 +73,10 @@ public override Mid Parse(string package)
{
Header = ProcessHeader(package);

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

Expand All @@ -62,8 +92,8 @@ protected override Dictionary<int, List<DataField>> RegisterDatafields()
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)
DataField.Number(DataFields.NumberOfPIDs, 88, 3, false),
DataField.Volatile(DataFields.VariableDataFields, 91, false)
}
}
};
Expand All @@ -77,7 +107,7 @@ protected enum DataFields
NewestSequenceNumber,
NewestTime,
NumberOfPIDs,
DataFields
VariableDataFields
}
}
}
5 changes: 3 additions & 2 deletions src/OpenProtocolInterpreter/Tightening/TighteningMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public TighteningMessages() : base()
{ Mid0063.MID, new MidCompiledInstance(typeof(Mid0063)) },
{ Mid0064.MID, new MidCompiledInstance(typeof(Mid0064)) },
{ Mid0065.MID, new MidCompiledInstance(typeof(Mid0065)) },
{ Mid0066.MID, new MidCompiledInstance(typeof(Mid0066)) }
{ Mid0066.MID, new MidCompiledInstance(typeof(Mid0066)) },
{ Mid0902.MID, new MidCompiledInstance(typeof(Mid0902)) }
};
}

Expand All @@ -33,6 +34,6 @@ public TighteningMessages(InterpreterMode mode) : this()
FilterSelectedMids(mode);
}

public override bool IsAssignableTo(int mid) => mid > 59 && mid < 67;
public override bool IsAssignableTo(int mid) => (mid > 59 && mid < 68) || (mid > 899 && mid < 903);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected void FilterSelectedMids(IEnumerable<Type> mids)
/// <param name="mids">Ignored mid instances</param>
private void FilterSelectedMids(IEnumerable<MidCompiledInstance> mids)
{
var ignoredMids = _templates.Where(x => !mids.Contains(x.Value)).ToList();
var ignoredMids = _templates.Where(x => !mids.Contains(x.Value));
foreach (var ignore in ignoredMids)
_templates.Remove(ignore);
}
Expand Down

0 comments on commit 435f729

Please sign in to comment.