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

Weekly schedule working #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions BACnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Base\BacnetAddressTypes.cs" />
<Compile Include="Base\BacnetApplicationTags.cs" />
<Compile Include="Base\BacnetBinaryPv.cs" />
<Compile Include="Base\BacnetDailySchedule.cs" />
<Compile Include="Base\BacnetProgramError.cs" />
<Compile Include="Base\BacnetProgramRequest.cs" />
<Compile Include="Base\BacnetProgramState.cs" />
Expand Down Expand Up @@ -100,6 +101,7 @@
<Compile Include="Base\BacnetReliability.cs" />
<Compile Include="Base\BacnetServicesSupported.cs" />
<Compile Include="Base\BacnetStatusFlags.cs" />
<Compile Include="Base\BacnetTimeValue.cs" />
<Compile Include="Base\BacnetUnconfirmedServices.cs" />
<Compile Include="Base\BacnetTimestampTags.cs" />
<Compile Include="Base\BacnetTrendLogValueType.cs" />
Expand Down
78 changes: 78 additions & 0 deletions Base/BacnetDailySchedule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.IO.BACnet.Serialize;
using System.Linq;
using System.Text;

namespace System.IO.BACnet
{
public struct BacnetDailySchedule : ASN1.IEncode, ASN1.IDecode
{
public List<BacnetTimeValue> DaySchedule;



public int Decode(byte[] buffer, int offset, uint count)
Copy link
Member

Choose a reason for hiding this comment

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

Code cosmetics. Please remove extra new lines you added in some places like this, max one empty line is ok. And when you see a line wrapping here, then it means it's a bit too long, please try to make it shorter.

{
int len = 0;
DaySchedule = new List<BacnetTimeValue>();
//begin of daily sched
if (ASN1.IS_OPENING_TAG(buffer[offset + len]))
{
len++;
//end of daily sched
while (!ASN1.IS_CLOSING_TAG(buffer[offset + len]) )
{
len++; //ignore apptag time ?
len += ASN1.decode_bacnet_time(buffer, offset + len, out DateTime time);


var tagLen = ASN1.decode_tag_number_and_value(buffer, offset + len, out BacnetApplicationTags tagNumber, out uint lenValueType);
BacnetValue value;
if (tagLen > 0)
{
len += tagLen;
var decodeLen = ASN1.bacapp_decode_data(buffer, offset + len, offset + len + 1, tagNumber, lenValueType, out value);
len += decodeLen;
}
else
{
value = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_NULL, null);
}


DaySchedule.Add(new BacnetTimeValue(new BacnetGenericTime(time, BacnetTimestampTags.TIME_STAMP_TIME), value));

}
//closing tag
len++;
}

return len;
}

public void Encode(EncodeBuffer buffer)
{
ASN1.encode_opening_tag(buffer, 0);

if (DaySchedule != null)
{
foreach (var dayItem in DaySchedule)
{

ASN1.encode_tag(buffer, (byte)BacnetApplicationTags.BACNET_APPLICATION_TAG_TIME, false, 4);
ASN1.encode_bacnet_time(buffer, dayItem.Time.Time);

ASN1.bacapp_encode_application_data(buffer, dayItem.Value);
}
}
ASN1.encode_closing_tag(buffer, 0);
}

public override string ToString()
{
return $"DaySchedule Len: {DaySchedule?.Count()}";
Copy link
Member

@gralin gralin Jun 28, 2018

Choose a reason for hiding this comment

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

DaySchedule?.Count ?? 0

}

}
}
24 changes: 24 additions & 0 deletions Base/BacnetTimeValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.IO.BACnet
{
public struct BacnetTimeValue
{
public BacnetGenericTime Time;
public BacnetValue Value;

public BacnetTimeValue(BacnetGenericTime time, BacnetValue value)
{
Time = time;
Value = value;
}

public override string ToString()
{
return $"{Time} = {Value}";
}
}
}
2 changes: 2 additions & 0 deletions Base/BacnetValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public BacnetApplicationTags TagFromType(Type t)
return BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR;
if (t == typeof(BacnetDeviceObjectPropertyReference))
return BacnetApplicationTags.BACNET_APPLICATION_TAG_OBJECT_PROPERTY_REFERENCE;
if (t == typeof(BacnetDailySchedule[]))
return BacnetApplicationTags.BACNET_APPLICATION_TAG_WEEKLY_SCHEDULE;
if (t.IsEnum)
return BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED;

Expand Down
30 changes: 30 additions & 0 deletions Serialize/ASN1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,19 @@ public static void bacapp_encode_application_data(EncodeBuffer buffer, BacnetVal
throw new ArgumentException($"Unsupported destination value '{value.Value}' (type {value.GetType()})");
}
break;
case BacnetApplicationTags.BACNET_APPLICATION_TAG_WEEKLY_SCHEDULE:
if(value.Value is BacnetDailySchedule[] dayArr && dayArr.Length == 7)
{
for (int i = 0; i < 7; i++)
{
dayArr[i].Encode(buffer);
}
}
else
{
throw new ArgumentException("Value has to be array of 7 of daily schedule ");
}
break;
default:
//context specific
if (value.Value is byte[] arr)
Expand Down Expand Up @@ -2033,6 +2046,19 @@ public static int bacapp_decode_context_application_data(BacnetAddress address,

return len;
}
if(propertyId == BacnetPropertyIds.PROP_WEEKLY_SCHEDULE)
{
//has to be an array of 7
var schedule = new BacnetDailySchedule[7];
for (int i = 0; i < 7; i++)
{
schedule[i] = new BacnetDailySchedule();
len += schedule[i].Decode(buffer, offset + len, (uint)maxOffset);
}
value.Value = schedule;
value.Tag = BacnetApplicationTags.BACNET_APPLICATION_TAG_WEEKLY_SCHEDULE;
return len;
}

value.Tag = BacnetApplicationTags.BACNET_APPLICATION_TAG_CONTEXT_SPECIFIC_DECODED;
var list = new List<BacnetValue>();
Expand Down Expand Up @@ -2418,6 +2444,7 @@ public static int decode_property_state(byte[] buffer, int offset, out BacnetPro

return len + sectionLength;
}


public static int decode_context_bitstring(byte[] buffer, int offset, byte tagNumber, out BacnetBitString value)
{
Expand Down Expand Up @@ -2466,5 +2493,8 @@ public static int decode_context_unsigned(byte[] buffer, int offset, byte tagNum
var len = decode_tag_number_and_value(buffer, offset, out _, out var lenValue);
return len + decode_unsigned(buffer, offset + len, lenValue, out value);
}



}
}