Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
Currently only supports Brennenstuhl devices RCS1000N and RCR1000N
  • Loading branch information
tefinger committed Nov 8, 2018
1 parent 784569f commit 3c7e6f5
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Brematic.Net.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2041
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Brematic.Net", "src\Brematic.Net\Brematic.Net.csproj", "{AB55CB3E-977B-4101-9419-13F866F8D6D2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{C4A1C06F-9D6D-40C5-95A2-18D2B129618B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Brematic.Net.Tests", "tests\Brematic.Net.Tests\Brematic.Net.Tests.csproj", "{65E659DB-34CD-4961-990F-2463DE6B096B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{CC115A2C-A9E5-44AC-8002-8E0E5309C130}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Brematic.Net.ConsoleSample", "samples\Brematic.Net.ConsoleSample\Brematic.Net.ConsoleSample.csproj", "{D5A85DD6-403B-4192-832C-C091D7E99BB9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AB55CB3E-977B-4101-9419-13F866F8D6D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB55CB3E-977B-4101-9419-13F866F8D6D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB55CB3E-977B-4101-9419-13F866F8D6D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB55CB3E-977B-4101-9419-13F866F8D6D2}.Release|Any CPU.Build.0 = Release|Any CPU
{65E659DB-34CD-4961-990F-2463DE6B096B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65E659DB-34CD-4961-990F-2463DE6B096B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65E659DB-34CD-4961-990F-2463DE6B096B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65E659DB-34CD-4961-990F-2463DE6B096B}.Release|Any CPU.Build.0 = Release|Any CPU
{D5A85DD6-403B-4192-832C-C091D7E99BB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D5A85DD6-403B-4192-832C-C091D7E99BB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5A85DD6-403B-4192-832C-C091D7E99BB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5A85DD6-403B-4192-832C-C091D7E99BB9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{65E659DB-34CD-4961-990F-2463DE6B096B} = {C4A1C06F-9D6D-40C5-95A2-18D2B129618B}
{D5A85DD6-403B-4192-832C-C091D7E99BB9} = {CC115A2C-A9E5-44AC-8002-8E0E5309C130}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {80F2A93D-9921-4E99-984F-B43C220A64A5}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Brematic.Net\Brematic.Net.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions samples/Brematic.Net.ConsoleSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Brematic.Net.Devices;
using Brematic.Net.Devices.Brennenstuhl;
using Brematic.Net.Gateways;

namespace Brematic.Net.ConsoleSample
{
internal class Program
{
private static void Main(string[] args)
{
var systemCode = "01010";
var unitCode = "00010";

var device = new RCS1000N(systemCode, unitCode);

var gateway = new BrennenstuhlGateway("192.168.178.213");

gateway.SendRequest(device, DeviceAction.On);
}
}
}
12 changes: 12 additions & 0 deletions src/Brematic.Net/Brematic.Net.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Tobias Efinger</Authors>
<Version>0.0.1</Version>
<PackageProjectUrl>https://github.com/tefinger/Brematic.Net</PackageProjectUrl>
<RepositoryUrl>https://github.com/tefinger/Brematic.Net</RepositoryUrl>
<PackageTags>brematic intertechno brennenstuhl smarthome</PackageTags>
</PropertyGroup>

</Project>
7 changes: 7 additions & 0 deletions src/Brematic.Net/Devices/Brennenstuhl/RCR1000N.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Brematic.Net.Devices.Brennenstuhl
{
public class RCR1000N : RCS1000N
{
public RCR1000N(string systemCode, string unitCode) : base(systemCode, unitCode) { }
}
}
83 changes: 83 additions & 0 deletions src/Brematic.Net/Devices/Brennenstuhl/RCS1000N.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Brematic.Net.Exceptions;
using Brematic.Net.Gateways;

namespace Brematic.Net.Devices.Brennenstuhl
{
public class RCS1000N : Device
{
private const int Repeat = 10;
private const int PauseBS = 5600;
private const int PauseIT = 11200;
private const int Tune = 350;
private const int BaudBS = 25;
private const int BaudIT = 26;
private const int SpeedBS = 16;
private const int SpeedIT = 32;
private const int TxVersion = 1;

private const string Low = "1,";
private const string High = "3,";

private const string SeqLow = Low + High + Low + High;
private const string SeqHigh = Low + High + High + Low;

private const string Additional = SeqLow + SeqHigh;
private const string On = SeqLow + SeqHigh;
private const string Off = SeqHigh + SeqLow;

public RCS1000N(string systemCode, string unitCode) : base(systemCode, unitCode) { }

private string HeadBSGW => $"TXP:0,0,{Repeat},{PauseBS},{Tune},{BaudBS},";
private string TailBSGW => $"{TxVersion},{SpeedBS};";
private string HeadITGW => $"0,0,{Repeat},{PauseIT},{Tune},{BaudIT},0,";
private string TailITGW => $"{TxVersion},{SpeedIT},0";

private string Encode(string code)
{
var encodedMsg = "";
foreach (var c in code)
switch (c)
{
case '0':
encodedMsg += SeqHigh;
break;
case '1':
encodedMsg += SeqLow;
break;
default:
throw new InvalidCodeException();
}

return encodedMsg;
}

public override string GetSignal(Gateway gateway, DeviceAction action)
{
var systemMsg = Encode(SystemCode);
var unitMsg = Encode(UnitCode);

string head;
string tail;

if (gateway.GetType() == typeof(BrennenstuhlGateway))
{
head = HeadBSGW;
tail = TailBSGW;
}
else if (gateway.GetType() == typeof(IntertechnoGateway))
{
head = HeadITGW;
tail = TailITGW;
}
else
{
throw new UnsupportedGatewayException();
}

var actionValue = action == DeviceAction.On ? On : Off;
var data = head + systemMsg + unitMsg + actionValue + tail;

return data;
}
}
}
19 changes: 19 additions & 0 deletions src/Brematic.Net/Devices/Device.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Brematic.Net.Gateways;

namespace Brematic.Net.Devices
{
public abstract class Device
{
protected Device(string systemCode, string unitCode)
{
SystemCode = systemCode;
UnitCode = unitCode;
}

protected string SystemCode { get; }

protected string UnitCode { get; }

public abstract string GetSignal(Gateway gateway, DeviceAction action);
}
}
8 changes: 8 additions & 0 deletions src/Brematic.Net/Devices/DeviceAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Brematic.Net.Devices
{
public enum DeviceAction
{
On,
Off
}
}
9 changes: 9 additions & 0 deletions src/Brematic.Net/Exceptions/InvalidCodeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Brematic.Net.Exceptions
{
public class InvalidCodeException : Exception
{
public InvalidCodeException() : base("Invalid value in system or unit code!") { }
}
}
9 changes: 9 additions & 0 deletions src/Brematic.Net/Exceptions/UnsupportedGatewayException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Brematic.Net.Exceptions
{
public class UnsupportedGatewayException : Exception
{
public UnsupportedGatewayException() : base("Unsupported gateway") { }
}
}
11 changes: 11 additions & 0 deletions src/Brematic.Net/Gateways/BrennenstuhlGateway.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Brematic.Net.Devices;

namespace Brematic.Net.Gateways
{
public class BrennenstuhlGateway : Gateway
{
public BrennenstuhlGateway(string ipAddress) : this(ipAddress, 49880) { }

public BrennenstuhlGateway(string ipAddress, int port) : base(ipAddress, port) { }
}
}
51 changes: 51 additions & 0 deletions src/Brematic.Net/Gateways/Gateway.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using Brematic.Net.Devices;

namespace Brematic.Net.Gateways
{
public abstract class Gateway
{
protected Gateway(string ipAddress, int port)
{
IPAddress = ipAddress;
Port = port;
}

private string IPAddress { get; }

private int Port { get; }

public void SendRequest(Device device, DeviceAction action)
{
var data = device.GetSignal(this, action);
SendDataToSocket(data);
}

private Socket ConnectSocket()
{
var hostEntry = Dns.GetHostEntry(IPAddress);

foreach (var address in hostEntry.AddressList)
{
var ipEndPoint = new IPEndPoint(address, Port);
var tempSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);
tempSocket.Connect(ipEndPoint);

if (tempSocket.Connected) return tempSocket;
}

return null;
}

protected void SendDataToSocket(string data)
{
var byteData = Encoding.UTF8.GetBytes(data);
using (var socket = ConnectSocket())
{
socket.Send(byteData);
}
}
}
}
12 changes: 12 additions & 0 deletions src/Brematic.Net/Gateways/IntertechnoGateway.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Brematic.Net.Devices;

namespace Brematic.Net.Gateways
{
public class IntertechnoGateway : Gateway
{
public IntertechnoGateway(string ipAddress) : this(ipAddress, 49880) { }

public IntertechnoGateway(string ipAddress, int port) : base(ipAddress, port) { }
}
}
20 changes: 20 additions & 0 deletions tests/Brematic.Net.Tests/Brematic.Net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net471</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Brematic.Net\Brematic.Net.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 3c7e6f5

Please sign in to comment.