-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathKnxConnection.cs
368 lines (315 loc) · 12.4 KB
/
KnxConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Net;
using System.Text;
using System.Linq;
using KNXLib.DPT;
using KNXLib.Exceptions;
using KNXLib.Log;
namespace KNXLib
{
/// <summary>
/// Base class that controls the KNX connection, implemented by KnxConnectionRouting and KnxConnetionTunneling
/// </summary>
public abstract class KnxConnection
{
private static readonly string ClassName = typeof(KnxConnection).ToString();
/// <summary>
/// Delegate function for connection established trigger
/// </summary>
public delegate void KnxConnected();
/// <summary>
/// Event triggered when connection is established
/// </summary>
public KnxConnected KnxConnectedDelegate = null;
/// <summary>
/// Delegate function for disconnection trigger
/// </summary>
public delegate void KnxDisconnected();
/// <summary>
/// Event triggered when connection drops
/// </summary>
public KnxDisconnected KnxDisconnectedDelegate = null;
/// <summary>
/// Delegate function for KNX events
/// </summary>
/// <param name="address"></param>
/// <param name="state"></param>
public delegate void KnxEvent(string address, string state);
/// <summary>
/// Event triggered when there is a new KNX event
/// </summary>
public KnxEvent KnxEventDelegate = (address, state) => { };
/// <summary>
/// Delegate function for KNX status queries
/// </summary>
/// <param name="address"></param>
/// <param name="state"></param>
public delegate void KnxStatus(string address, string state);
/// <summary>
/// Event triggered when received a status after a query
/// </summary>
public KnxStatus KnxStatusDelegate = (address, state) => { };
private readonly KnxLockManager _lockManager = new KnxLockManager();
/// <summary>
/// Create a new KNX Connection to specified host and port
/// </summary>
/// <param name="host">Host to connect</param>
/// <param name="port">Port to use</param>
protected KnxConnection(string host, int port)
{
ConnectionConfiguration = new KnxConnectionConfiguration(host, port);
ActionMessageCode = 0x00;
ThreeLevelGroupAddressing = true;
Debug = false;
}
internal KnxConnectionConfiguration ConnectionConfiguration { get; private set; }
/// <summary>
/// Get the IPEndPoint instance representing the remote KNX gateway
/// </summary>
public IPEndPoint RemoteEndpoint
{
get
{
return ConnectionConfiguration.EndPoint;
}
}
internal KnxReceiver KnxReceiver { get; set; }
internal KnxSender KnxSender { get; set; }
/// <summary>
/// Configure this paramenter based on the KNX installation:
/// - true: 3-level group address: main/middle/sub(5/3/8 bits)
/// - false: 2-level group address: main/sub (5/11 bits)
/// Default: true
/// </summary>
public bool ThreeLevelGroupAddressing { get; set; }
/// <summary>
/// Set to true to receive debug log messages
/// </summary>
public bool Debug { get; set; }
/// <summary>
/// Some KNX Routers/Interfaces might need this parameter defined, some need this to be 0x29.
/// Default: 0x00
/// </summary>
public byte ActionMessageCode { get; set; }
/// <summary>
/// Start the connection
/// </summary>
public abstract void Connect();
/// <summary>
/// Stop the connection
/// </summary>
public abstract void Disconnect();
/// <summary>
/// Event triggered by implementing class to notify that the connection has been established
/// </summary>
internal virtual void Connected()
{
_lockManager.UnlockConnection();
try
{
if (KnxConnectedDelegate != null)
KnxConnectedDelegate();
}
catch(Exception e)
{
Logger.Error(ClassName, e);
}
Logger.Debug(ClassName, "KNX is connected. Unlocking send - {0} free locks", _lockManager.LockCount);
}
/// <summary>
/// Event triggered by implementing class to notify that the connection has been established
/// </summary>
internal virtual void Disconnected()
{
_lockManager.LockConnection();
try
{
if (KnxDisconnectedDelegate != null)
KnxDisconnectedDelegate();
}
catch (Exception e)
{
Logger.Error(ClassName, e);
}
Logger.Debug(ClassName, "KNX is disconnected");
Logger.Debug(ClassName, "Send locked - {0} free locks", _lockManager.LockCount);
}
internal void Event(string address, string state)
{
try
{
KnxEventDelegate(address, state);
}
catch (Exception e)
{
Logger.Error(ClassName, e);
}
Logger.Debug(ClassName, "Device {0} sent event 0x{1}", address, string.Join("", state.Select(c => ((int)c).ToString("X2"))));
}
internal void Status(string address, string state)
{
try
{
KnxStatusDelegate(address, state);
}
catch (Exception e)
{
Logger.Error(ClassName, e);
}
Logger.Debug(ClassName, "Device {0} has status {1}", address, state);
}
/// <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>
/// <param name="address">KNX Address</param>
/// <param name="data">Bit value</param>
/// <exception cref="InvalidKnxDataException"></exception>
public void Action(string address, bool data)
{
byte[] val;
try
{
val = new[] {Convert.ToByte(data)};
}
catch
{
throw new InvalidKnxDataException(data.ToString());
}
if (val == null)
throw new InvalidKnxDataException(data.ToString());
Action(address, val);
}
/// <summary>
/// Send a string value as data to specified address
/// </summary>
/// <param name="address">KNX Address</param>
/// <param name="data">String value</param>
/// <exception cref="InvalidKnxDataException"></exception>
public void Action(string address, string data)
{
byte[] val;
try
{
val = Encoding.ASCII.GetBytes(data);
}
catch
{
throw new InvalidKnxDataException(data);
}
if (val == null)
throw new InvalidKnxDataException(data);
Action(address, val);
}
/// <summary>
/// Send an int value as data to specified address
/// </summary>
/// <param name="address">KNX Address</param>
/// <param name="data">Int value</param>
/// <exception cref="InvalidKnxDataException"></exception>
public void Action(string address, int data)
{
var val = new byte[2];
if (data <= 255)
{
val[0] = 0x00;
val[1] = (byte) data;
}
else if (data <= 65535)
{
val[0] = (byte) data;
val[1] = (byte) (data >> 8);
}
else
{
// allowing only positive integers less than 65535 (2 bytes), maybe it is incorrect...???
throw new InvalidKnxDataException(data.ToString());
}
if (val == null)
throw new InvalidKnxDataException(data.ToString());
Action(address, val);
}
/// <summary>
/// Send a byte value as data to specified address
/// </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});
}
/// <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)
{
Logger.Debug(ClassName, "Sending 0x{0} to {1}.", BitConverter.ToString(data), address);
_lockManager.PerformLockedOperation(() => KnxSender.Action(address, data));
Logger.Debug(ClassName, "Sent 0x{0} to {1}.", BitConverter.ToString(data), address);
}
// TODO: It would be good to make a type for address, to make sure not any random string can be passed in
/// <summary>
/// Send a request to KNX asking for specified address current status
/// </summary>
/// <param name="address"></param>
public void RequestStatus(string address)
{
Logger.Debug(ClassName, "Sending request status to {0}.", address);
_lockManager.PerformLockedOperation(() => KnxSender.RequestStatus(address));
Logger.Debug(ClassName, "Sent request status to {0}.", address);
}
/// <summary>
/// Convert a value received from KNX using datapoint translator, e.g.,
/// get a temperature value in Celsius
/// </summary>
/// <param name="type">Datapoint type, e.g.: 9.001</param>
/// <param name="data">Data to convert</param>
/// <returns></returns>
public object FromDataPoint(string type, string data)
{
return DataPointTranslator.Instance.FromDataPoint(type, data);
}
/// <summary>
/// Convert a value received from KNX using datapoint translator, e.g.,
/// get a temperature value in Celsius
/// </summary>
/// <param name="type">Datapoint type, e.g.: 9.001</param>
/// <param name="data">Data to convert</param>
/// <returns></returns>
public object FromDataPoint(string type, byte[] data)
{
return DataPointTranslator.Instance.FromDataPoint(type, data);
}
/// <summary>
/// Convert a value to send to KNX using datapoint translator, e.g.,
/// get a temperature value in Celsius in a byte representation
/// </summary>
/// <param name="type">Datapoint type, e.g.: 9.001</param>
/// <param name="value">Value to convert</param>
/// <returns></returns>
public byte[] ToDataPoint(string type, string value)
{
return DataPointTranslator.Instance.ToDataPoint(type, value);
}
/// <summary>
/// Convert a value to send to KNX using datapoint translator, e.g.,
/// get a temperature value in Celsius in a byte representation
/// </summary>
/// <param name="type">Datapoint type, e.g.: 9.001</param>
/// <param name="value">Value to convert</param>
/// <returns></returns>
public byte[] ToDataPoint(string type, object value)
{
return DataPointTranslator.Instance.ToDataPoint(type, value);
}
}
}