Skip to content

Commit

Permalink
Bring up to date with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
CumpsD committed Dec 27, 2018
1 parent 9c46350 commit 114461c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 36 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ The following datapoints are available in the API:

| DPT | input type | input range | output type | output range | Description |
| ------- |-------------------------------------------- | --------------- | ----------- | --------------- | ----------------------------------- |
| `3.007` | `int`, `float`, `long`, `double`, `decimal` | `[-7,7]` | `int` | `[-7,7]` | Control blinds (steps) [`0` stops] |
| `3.008` | `int`, `float`, `long`, `double`, `decimal` | `[-7,7]` | `int` | `[-7,7]` | Control dimming (steps) [`0` stops] |
| `3.007` | `int`, `float`, `long`, `double`, `decimal` | `[-7,7]` | `int` | `[-7,7]` | Control dimming (steps) [`0` stops] |
| `3.008` | `int`, `float`, `long`, `double`, `decimal` | `[-7,7]` | `int` | `[-7,7]` | Control blinds (steps) [`0` stops] |
| `5.001` | `int`, `float`, `long`, `double`, `decimal` | `[0,100]` | `decimal` | `[0,100]` | Percentage (%) |
| `5.003` | `int`, `float`, `long`, `double`, `decimal` | `[0,360]` | `decimal` | `[0,360]` | Angle (°) |
| `5.004` | `int`, `float`, `long`, `double`, `decimal` | `[0,255]` | `int` | `[0,255]` | Percentage `[0,255]` (%) |
| `5.004` | `int`, `float`, `long`, `double`, `decimal` | `[0,255]` | `int` | `[0,255]` | Percentage `[0,255]` (%) |
| `5.010` | `int`, `float`, `long`, `double`, `decimal` | `[0,255]` | `int` | `[0,255]` | Counter Pulses |
| `6.001` | `int`, `float`, `long`, `double`, `decimal` | `[-128,127]` | `int` | `[-128,127]` | Percentage (%) |
| `6.010` | `int`, `float`, `long`, `double`, `decimal` | `[-128,127]` | `int` | `[-128,127]` | Counter Pulses |
Expand Down Expand Up @@ -140,4 +140,4 @@ If connecting in tunneling mode:

`./packages/NUnit.ConsoleRunner/tools/nunit3-console.exe ./tests/InfluxDB.FSharp.UnitTests/bin/Release/InfluxDB.FSharp.UnitTests.dll --labels=On --nocolor --verbose --workers=1 --full --result:"./nunit-result.xml;format=nunit2"`

`./packages/xunit.runners/tools/xunit.console.clr4.exe ./tests/FunctionalLiving.Parser.Tests/bin/Release/FunctionalLiving.Parser.Tests.dll`
`./packages/xunit.runners/tools/xunit.console.clr4.exe ./tests/FunctionalLiving.Parser.Tests/bin/Release/FunctionalLiving.Parser.Tests.dll`
13 changes: 9 additions & 4 deletions src/KNXLib/DPT/DataPoint8BitNoSignScaledScaling.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace KNXLib.DPT
using System.Linq;

namespace KNXLib.DPT
{
using System.Globalization;
using Log;
Expand All @@ -18,7 +20,9 @@ public override object FromDataPoint(string data)

public override object FromDataPoint(byte[] data)
{
if (data == null || data.Length != 1)
if (data?.Length == 2)
data = data.Skip(1).ToArray();
else
return 0;

var value = (int) data[0];
Expand All @@ -33,8 +37,9 @@ public override object FromDataPoint(byte[] data)

public override byte[] ToDataPoint(object val)
{
var dataPoint = new byte[1];
var dataPoint = new byte[2];
dataPoint[0] = 0x00;
dataPoint[1] = 0x00;

decimal input;

Expand Down Expand Up @@ -73,7 +78,7 @@ public override byte[] ToDataPoint(object val)
input = input * 255;
input = input / 100;

dataPoint[0] = (byte) input;
dataPoint[1] = (byte) input;

return dataPoint;
}
Expand Down
25 changes: 22 additions & 3 deletions src/KNXLib/KnxConnection.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ internal void Status(string address, byte[] state)
Logger.Debug(ClassName, "Device {0} sent status 0x{1}", address, string.Join(string.Empty, state.Select(c => ((int) c).ToString("X2"))));
}

/// <summary>
/// Set the lock interval between requests sent to the network (in ms)
/// </summary>
/// <param name="interval">time in ms for the interval</param>
public void SetLockIntervalMs(int interval)
{
_lockManager.IntervalMs = interval;
}

/// <summary>
/// Send a bit value as data to specified address
/// </summary>
Expand All @@ -203,7 +212,7 @@ public void Action(string address, bool data)
if (val == null)
throw new InvalidKnxDataException(data.ToString());

Action(address, val);
Action(address, val, addTruncateByte: false);
}

/// <summary>
Expand Down Expand Up @@ -266,15 +275,25 @@ public void Action(string address, int data)
/// </summary>
/// <param name="address">KNX Address</param>
/// <param name="data">byte value</param>
public void Action(string address, byte data) => Action(address, new byte[] { 0x00, data });
public void Action(string address, byte data) => Action(address, new [] { data });

/// <summary>
/// Send a byte array value as data to specified address
/// </summary>
/// <param name="address">KNX Address</param>
/// <param name="data">Byte array value</param>
public void Action(string address, byte[] data)
/// <param name="addTruncateByte">adds extra byte to chop off for payload</param>
public void Action(string address, byte[] data, bool addTruncateByte = true)
{
if (addTruncateByte)
{
// reverse bytes temporary to add byte in front
Array.Reverse(data);
Array.Resize(ref data, data.Length + 1);
data[data.Length - 1] = 0x00;
Array.Reverse(data);
}

Logger.Debug(ClassName, "Sending 0x{0} to {1}.", BitConverter.ToString(data), address);

_lockManager.PerformLockedOperation(() => KnxSender.Action(address, data));
Expand Down
14 changes: 3 additions & 11 deletions src/KNXLib/KnxHelper.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -328,18 +328,10 @@ public static void WriteData(byte[] datagram, byte[] data, int dataStart)
}
else if (data.Length > 1)
{
if (data[0] < 0x3F)
{
datagram[dataStart] = (byte) (datagram[dataStart] | data[0]);
datagram[dataStart] = (byte) (datagram[dataStart] | data[0]);

for (var i = 1; i < data.Length; i++)
datagram[dataStart + i] = data[i];
}
else
{
for (var i = 0; i < data.Length; i++)
datagram[dataStart + 1 + i] = data[i];
}
for (var i = 1; i < data.Length; i++)
datagram[dataStart + i] = data[i];
}
}
#endregion
Expand Down
10 changes: 9 additions & 1 deletion src/KNXLib/KnxLockManager.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal class KnxLockManager
private readonly object _connectedLock = new object();
private bool _isConnected;

internal int IntervalMs { get; set; } = 200;

public int LockCount => _sendLock.CurrentCount;

public void LockConnection()
Expand Down Expand Up @@ -62,13 +64,19 @@ private void SendUnlock()

private void SendUnlockPause()
{
if (IntervalMs == 0)
{
_sendLock.Release();
return;
}

var t = new Thread(SendUnlockPauseThread) { IsBackground = true };
t.Start();
}

private void SendUnlockPauseThread()
{
Thread.Sleep(200);
Thread.Sleep(IntervalMs);
_sendLock.Release();
}
}
Expand Down
23 changes: 18 additions & 5 deletions src/KNXLib/KnxReceiver.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal abstract class KnxReceiver
private Thread _receiverThread;
private Thread _consumerThread;

private const ThreadState StateAlive = ThreadState.Running | ThreadState.Background | ThreadState.WaitSleepJoin;

private BlockingCollection<KnxDatagram> _rxDatagrams;

protected KnxReceiver(KnxConnection connection) => KnxConnection = connection;
Expand All @@ -33,8 +35,10 @@ private void ConsumerThreadFlow()
{
datagram = _rxDatagrams.Take();
}
catch (InvalidOperationException)
catch (Exception e)
{
if (e is ThreadAbortException)
throw;
}

if (datagram != null)
Expand All @@ -61,16 +65,25 @@ public void Stop()
{
try
{
if (!_receiverThread.ThreadState.Equals(ThreadState.Running))
return;
if (_receiverThread.ThreadState.Equals(StateAlive))
_receiverThread.Abort();
}
catch
{
Thread.ResetAbort();
}

_receiverThread.Abort();
_consumerThread.Abort();
try
{
if (_consumerThread.ThreadState.Equals(StateAlive))
_consumerThread.Abort();
}
catch
{
Thread.ResetAbort();
}

_rxDatagrams.Dispose();
}

protected void ProcessCEMI(KnxDatagram datagram, byte[] cemi)
Expand Down
10 changes: 5 additions & 5 deletions tests/KNXLibTests/Unit/DataPoint/DataPoint8BitNoSignScaledScaling.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public void DataPoint8BitNoSignScaledScalingTest()
var dptType = "5.001";

var scale0 = 0;
var scale0Bytes = new byte[] { 0x00 };
var scale0Bytes = new byte[] { 0x00, 0x00 };
var scale20 = 20;
var scale20Bytes = new byte[] { 0x33 };
var scale20Bytes = new byte[] { 0x00, 0x33 };
var scale60 = 60;
var scale60Bytes = new byte[] { 0x99 };
var scale60Bytes = new byte[] { 0x00, 0x99 };
var scale80 = 80;
var scale80Bytes = new byte[] { 0xCC };
var scale80Bytes = new byte[] { 0x00, 0xCC };
var scale100 = 100;
var scale100Bytes = new byte[] { 0xFF };
var scale100Bytes = new byte[] { 0x00, 0xFF };

Assert.AreEqual(scale0, ((int) (decimal) DataPointTranslator.Instance.FromDataPoint(dptType, scale0Bytes)));
Assert.AreEqual(scale20, ((int) (decimal) DataPointTranslator.Instance.FromDataPoint(dptType, scale20Bytes)));
Expand Down
13 changes: 10 additions & 3 deletions tests/KNXTestRouting/TestRouting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ public class TestRouting
{
private static KnxConnection _connection;

private const string LightOnOffAddress = "5/0/2";
private const string LightOnOffAddress = "2/3/33";

private static readonly IList<string> Lights = new List<string> { "5/1/2" };
private static readonly IDictionary<string, string> Lights = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
{
{ "2/3/31", "Bureau - Spots Buitencirkel - Aan/Uit" },
{ "2/3/32", "Bureau - Spots Binnencirkel - Aan/Uit" },
{ "2/3/33", "Bureau - Spots - Aan/Uit" },
});

// 1.001 Switches
private static readonly IDictionary<string, string> Switches = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
Expand Down Expand Up @@ -127,6 +132,8 @@ private static void Main()
_logFile = new StreamWriter("telegrams.txt");

_connection = new KnxConnectionRouting { Debug = false, ActionMessageCode = 0x29 };
_connection.SetLockIntervalMs(20);

_connection.KnxConnectedDelegate += Connected;
_connection.KnxDisconnectedDelegate += Disconnected;
_connection.KnxEventDelegate += Event;
Expand Down Expand Up @@ -157,7 +164,7 @@ private static void Status(string address, byte[] state)
private static void Print(string address, byte[] state)
{
const int categoryWidth = 15;
const int descriptionWidth = -70;
const int descriptionWidth = -60;
string description;

if (Switches.TryGetValue(address, out description))
Expand Down

0 comments on commit 114461c

Please sign in to comment.