Repository navigation
Expand file tree
/
Copy pathExampleAuthenticate.cs
More file actions
73 lines (60 loc) · 1.87 KB
/
Copy pathExampleAuthenticate.cs
File metadata and controls
73 lines (60 loc) · 1.87 KB
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
using Tinkerforge;
class ExampleAuthenticate
{
private static string HOST = "localhost";
private static int PORT = 4223;
private static string SECRET = "My Authentication Secret!";
// Authenticate each time the connection got (re-)established
static void ConnectedCB(IPConnection sender, short connectReason)
{
switch(connectReason)
{
case IPConnection.CONNECT_REASON_REQUEST:
System.Console.WriteLine("Connected by request");
break;
case IPConnection.CONNECT_REASON_AUTO_RECONNECT:
System.Console.WriteLine("Auto-Reconnected");
break;
}
// Authenticate first...
try
{
sender.Authenticate(SECRET);
System.Console.WriteLine("Authentication succeeded");
}
catch(TinkerforgeException)
{
System.Console.WriteLine("Could not authenticate");
return;
}
// ...reenable auto reconnect mechanism, as described below...
sender.SetAutoReconnect(true);
// ...then trigger enumerate
sender.Enumerate();
}
// Print incoming enumeration
static void EnumerateCB(IPConnection sender,
string uid, string connectedUid, char position,
short[] hardwareVersion, short[] firmwareVersion,
int deviceIdentifier, short enumerationType)
{
System.Console.WriteLine("UID: " + uid + ", Enumeration Type: " + enumerationType);
}
static void Main()
{
// Create IP Connection
IPConnection ipcon = new IPConnection();
// Disable auto reconnect mechanism, in case we have the wrong secret.
// If the authentication is successful, reenable it.
ipcon.SetAutoReconnect(false);
// Register Connected Callback
ipcon.Connected += ConnectedCB;
// Register Enumerate Callback
ipcon.EnumerateCallback += EnumerateCB;
// Connect to brickd
ipcon.Connect(HOST, PORT);
System.Console.WriteLine("Press key to exit");
System.Console.ReadLine();
ipcon.Disconnect();
}
}