Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a few tests, fixed bugs that those tests turned up, and refactored a thew things on the way #25

Open
wants to merge 19 commits into
base: v2
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored ChangeOfState
DarkStarDS9 committed Feb 27, 2018
commit 926c1223289a892e641cdbbf7d7fcef94a12837e
3 changes: 2 additions & 1 deletion BACnet.csproj
Original file line number Diff line number Diff line change
@@ -81,6 +81,7 @@
<Compile Include="Base\Enums\EnumUtils.cs" />
<Compile Include="Base\EventNotification\EventValues\EventValuesBase.cs" />
<Compile Include="Base\EventNotification\EventValues\ChangeOfLifeSafety.cs" />
<Compile Include="Helpers\FactoryHelper.cs" />
<Compile Include="Base\EventNotification\EventValues\UnsignedRange.cs" />
<Compile Include="Base\EventNotification\EventValues\ChangeOfState.cs" />
<Compile Include="Base\EventNotification\EventValues\OutOfRange.cs" />
@@ -108,7 +109,6 @@
<Compile Include="Base\BacnetPropertyIds.cs" />
<Compile Include="Base\BacnetPropertyReference.cs" />
<Compile Include="Base\BacnetPropertyValue.cs" />
<Compile Include="Base\BacnetPropertyState.cs" />
<Compile Include="Base\BacnetPtpDisconnectReasons.cs" />
<Compile Include="Base\BacnetPtpFrameTypes.cs" />
<Compile Include="Base\BacnetReadAccessResult.cs" />
@@ -139,6 +139,7 @@
<Compile Include="Base\Enums\BacnetLimitEnable.cs" />
<Compile Include="Base\Enums\BacnetNotifyTypes.cs" />
<Compile Include="Base\Enums\BacnetCOVTypes.cs" />
<Compile Include="Base\IHasStatusFlags.cs" />
<Compile Include="Helpers\BacnetValuesExtensions.cs" />
<Compile Include="Helpers\StringFormatterExtension.cs" />
<Compile Include="Base\BacnetBvlcV6Functions.cs" />
2 changes: 1 addition & 1 deletion BACnetClient.cs
Original file line number Diff line number Diff line change
@@ -1398,7 +1398,7 @@ public byte[] EndRawEncodedDecodedPropertyConfirmedRequest(BacnetAsyncResult req
len += ASN1.decode_tag_number_and_value(buffer, offset + len, out var tagNumber, out var lenValueType);
if (tagNumber != 1)
throw new Exception("Failed to decode");
len += ASN1.decode_unsigned(buffer, offset + len, lenValueType, out _);
len += ASN1.decode_unsigned(buffer, offset + len, lenValueType, out uint _);

var outBuffer = new byte[buffer.Length - len];
Array.Copy(buffer, len, outBuffer, 0, outBuffer.Length);
51 changes: 0 additions & 51 deletions Base/BacnetPropertyState.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Base/BacnetScale.cs
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ public int Decode(byte[] buffer, int offset, uint count)
else if (tagNumber == (byte)ValueType.INTEGER)
{
len += tagLen;
len += ASN1.decode_unsigned(buffer, offset + len, lenValueType, out var oval);
len += ASN1.decode_unsigned(buffer, offset + len, lenValueType, out uint oval);

value = oval;
}
11 changes: 10 additions & 1 deletion Base/Enums/EnumUtils.cs
Original file line number Diff line number Diff line change
@@ -10,9 +10,18 @@ namespace System.IO.BACnet
{
public abstract class EnumClassUtils<TClass> where TClass : class
{
public static TEnum DecodeEnumerated<TEnum>(byte[] buffer, int offset, uint lenValue, out int sectionLength) where TEnum : struct, TClass
{
sectionLength = ASN1.decode_unsigned(buffer, offset, lenValue, out uint rawValue);
if(sectionLength == -1)
throw new InvalidOperationException($"{nameof(ASN1.decode_unsigned)} returned -1");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other DecodeEnumerate<> method doesn't throw exception in case of -1. Shouldn't this be consistent?


return (TEnum)(dynamic)rawValue;
}

public static int DecodeEnumerated<TEnum>(byte[] buffer, int offset, uint lenValue, out TEnum value) where TEnum: struct, TClass
{
var len = ASN1.decode_unsigned(buffer, offset, lenValue, out var rawValue);
var len = ASN1.decode_unsigned(buffer, offset, lenValue, out uint rawValue);
value = (TEnum)(dynamic)rawValue;
return len;
}
70 changes: 68 additions & 2 deletions Base/EventNotification/EventValues/ChangeOfState.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
using System.Diagnostics.CodeAnalysis;
using System.IO.BACnet.Base;
using System.IO.BACnet.Helpers;
using System.Reflection;

namespace System.IO.BACnet.EventNotification.EventValues
{
public class ChangeOfState : EventValuesBase
public abstract class ChangeOfState : EventValuesBase, IHasStatusFlags
{
public override BacnetEventTypes EventType => BacnetEventTypes.EVENT_CHANGE_OF_STATE;

public BacnetPropertyState NewState { get; set; }
public BacnetBitString StatusFlags { get; set; }
}

public class ChangeOfState<T> : ChangeOfState
{

public T NewState { get; set; }

protected ChangeOfState(T value)
{
NewState = value;
}

public static ChangeOfState<bool> Create(bool value)
=> new ChangeOfState<bool>(value);

public static ChangeOfState<BacnetBinaryPv> Create(BacnetBinaryPv value)
=> new ChangeOfState<BacnetBinaryPv>(value);

public static ChangeOfState<BacnetEventTypes> Create(BacnetEventTypes value)
=> new ChangeOfState<BacnetEventTypes>(value);

public static ChangeOfState<BacnetPolarity> Create(BacnetPolarity value)
=> new ChangeOfState<BacnetPolarity>(value);

public static ChangeOfState<BacnetProgramRequest> Create(BacnetProgramRequest value)
=> new ChangeOfState<BacnetProgramRequest>(value);

public static ChangeOfState<BacnetProgramState> Create(BacnetProgramState value)
=> new ChangeOfState<BacnetProgramState>(value);

public static ChangeOfState<BacnetProgramError> Create(BacnetProgramError value)
=> new ChangeOfState<BacnetProgramError>(value);

public static ChangeOfState<BacnetReliability> Create(BacnetReliability value)
=> new ChangeOfState<BacnetReliability>(value);

public static ChangeOfState<BacnetEventStates> Create(BacnetEventStates value)
=> new ChangeOfState<BacnetEventStates>(value);

public static ChangeOfState<BacnetDeviceStatus> Create(BacnetDeviceStatus value)
=> new ChangeOfState<BacnetDeviceStatus>(value);

public static ChangeOfState<BacnetUnitsId> Create(BacnetUnitsId value)
=> new ChangeOfState<BacnetUnitsId>(value);

public static ChangeOfState<uint> Create(uint value)
=> new ChangeOfState<uint>(value);

public static ChangeOfState<BacnetLifeSafetyModes> Create(BacnetLifeSafetyModes value)
=> new ChangeOfState<BacnetLifeSafetyModes>(value);

public static ChangeOfState<BacnetLifeSafetyStates> Create(BacnetLifeSafetyStates value)
=> new ChangeOfState<BacnetLifeSafetyStates>(value);
}

[ExcludeFromCodeCoverage]
public abstract class ChangeOfStateFactory : ChangeOfState<object>
{
protected ChangeOfStateFactory(object value) : base(value)
{
throw new InvalidOperationException("this is a dummy class to avoid static call with generic type parameter");
}
}
}
15 changes: 3 additions & 12 deletions Base/EventNotification/EventValues/ChangeOfValue.cs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

namespace System.IO.BACnet.EventNotification.EventValues
{
public abstract class ChangeOfValue : EventValuesBase
public abstract class ChangeOfValue : EventValuesBase, IHasStatusFlags
{
public override BacnetEventTypes EventType => BacnetEventTypes.EVENT_CHANGE_OF_VALUE;
public BacnetCOVTypes Tag { get; protected set; }
@@ -19,10 +19,10 @@ protected ChangeOfValue(T value, BacnetCOVTypes type)
Tag = type;
}

public static ChangeOfValue<float> CreateNew(float value)
public static ChangeOfValue<float> Create(float value)
=> new ChangeOfValue<float>(value, BacnetCOVTypes.CHANGE_OF_VALUE_REAL);

public static ChangeOfValue<BacnetBitString> CreateNew(BacnetBitString value)
public static ChangeOfValue<BacnetBitString> Create(BacnetBitString value)
=> new ChangeOfValue<BacnetBitString>(value, BacnetCOVTypes.CHANGE_OF_VALUE_BITS);
}

@@ -34,13 +34,4 @@ protected ChangeOfValueFactory(object value, BacnetCOVTypes type) : base(value,
throw new InvalidOperationException("this is a dummy class to avoid static call with generic type parameter");
}
}

public static class Extensions
{
public static ChangeOfValue<T> SetStatusFlags<T>(this ChangeOfValue<T> cov, BacnetBitString statusFlags)
{
cov.StatusFlags = statusFlags;
return cov;
}
}
}
18 changes: 18 additions & 0 deletions Base/IHasStatusFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.IO.BACnet.EventNotification.EventValues;

namespace System.IO.BACnet
{
public interface IHasStatusFlags
{
BacnetBitString StatusFlags { get; set; }
}

public static class Extensions
{
public static T SetStatusFlags<T>(this T obj, BacnetBitString statusFlags) where T:IHasStatusFlags
{
obj.StatusFlags = statusFlags;
return obj;
}
}
}
18 changes: 18 additions & 0 deletions Helpers/FactoryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Reflection;

namespace System.IO.BACnet.Helpers
{
public static class FactoryHelper
{
public static TReturn CreateReflected<TFactory, TReturn>(object value) where TReturn : class
{
var mi = typeof(TFactory).GetMethod(
"Create", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy, null, new[] { value.GetType() }, null);

if (mi == null)
throw new InvalidOperationException("factory method not found");

return mi.Invoke(null, new[] { value }) as TReturn;
}
}
}
Loading