Skip to content

Commit 374897d

Browse files
committed
Add examples
1 parent 7f38e03 commit 374897d

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

Examples.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
### Examples
2+
3+
#### Getting Started
4+
To start using the library first instantiate the controller.
5+
```c#
6+
// Option 1
7+
Controller controller = new Controller("Example Matter Fabric");
8+
9+
// Option 2
10+
Controller controller = Controller.Load("example.fabric", "example.key");
11+
```
12+
_Option 1: Creates a new controller then generates the fabric named "Example Matter Fabric" and the certificates._\
13+
_Option 2: Load an existing fabric into a controller._
14+
15+
#### Example 1: Interviewing the Fabric
16+
On first startup of a fabric or after a long disconnected period it is a good idea to re-enumerate the fabric. This will interview each node on the fabric for some basic information.
17+
```c#
18+
await controller.EnumerateFabric();
19+
```
20+
21+
#### Example 2: Adding Nodes (Commissioning an already commissioned device):
22+
This example adds a node to our fabric that has already been setup by another device. To commission a node, we need a commissioning payload. This is generated with either an 11/21 digit PIN Code, a QR code or an NFC tag.
23+
```c#
24+
// Option 1
25+
CommissioningPayload payload = CommissioningPayload.FromPIN("00362159269");
26+
27+
// Option 2
28+
CommissioningPayload payload = CommissioningPayload.FromQR("MT:Y.K9042C00KA0648G00");
29+
30+
// Option 3
31+
CommissioningPayload payload = CommissioningPayload.FromNFC(nfcBytes);
32+
```
33+
_Option 1: Creates a commissioning payload from a PIN Number._\
34+
_Option 2: Creates a commissioning payload from a QR Code._\
35+
_Option 3: Creates a commissioning payload from an NFC tag._
36+
37+
Once we have the payload, we can activate commissioning which will automatically search bluetooth and/or the network and setup the device.
38+
39+
```c#
40+
CommissioningState info = await controller.StartCommissioning(payload);
41+
await controller.CompleteCommissioning(info);
42+
controller.Save("example.fabric", "example.key");
43+
```
44+
_Line 1 Find the device and begin commissioning._\
45+
_Line 2 Complete commissioning._\
46+
_Line 3 Save the fabric with the certificates for the newly added node._
47+
48+
#### Example 3: Adding Nodes (Commissioning a New/Reset Device):
49+
To commission a node, we need a commissioning payload. This is generated with either an 11/21 digit PIN Code, a QR code or an NFC tag.
50+
```c#
51+
// Option 1
52+
CommissioningPayload payload = CommissioningPayload.FromPIN("00362159269");
53+
54+
// Option 2
55+
CommissioningPayload payload = CommissioningPayload.FromQR("MT:Y.K9042C00KA0648G00");
56+
57+
// Option 3
58+
CommissioningPayload payload = CommissioningPayload.FromNFC(nfcBytes);
59+
```
60+
_Option 1: Creates a commissioning payload from a PIN Number._\
61+
_Option 2: Creates a commissioning payload from a QR Code._\
62+
_Option 3: Creates a commissioning payload from an NFC tag._
63+
64+
Once we have the payload, we can activate commissioning which will automatically search bluetooth and/or the network and setup the device.
65+
66+
```c#
67+
CommissioningState info = await controller.StartCommissioning(payload);
68+
var result = info.FindWiFi("Linksys-24G")!;
69+
await controller.CompleteCommissioning(info, result, "password123");
70+
controller.Save("example.fabric", "example.key");
71+
```
72+
_Line 1 Find the device and begin commissioning._\
73+
_Line 2 Select a WiFi network out of the networks the device can see. This step can be skipped if the device is already on the network._\
74+
_Line 3 Complete commissioning and connect to the provided WiFi network._\
75+
_Line 4 Save the fabric with the certificates for the newly added node._
76+
77+
Alternatively, line 2 can be replaced by selecting a thread network or an ethernet connection.

MatterDotNet/Entities/Controller.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ public Controller(Fabric fabric)
4444
{
4545
this.fabric = fabric;
4646
}
47+
48+
/// <summary>
49+
/// Create a controller with a new fabric
50+
/// </summary>
51+
/// <param name="fabricName"></param>
52+
public Controller(string fabricName = "MatterDotNot") : this((ulong)Random.Shared.NextInt64(), fabricName) { }
4753

4854
/// <summary>
4955
/// Create a controller with a new fabric
5056
/// </summary>
5157
/// <param name="fabricId"></param>
5258
/// <param name="fabricName"></param>
53-
public Controller(uint fabricId, string fabricName = "MatterDotNot")
59+
public Controller(ulong fabricId, string fabricName = "MatterDotNot")
5460
{
5561
this.fabric = new Fabric(fabricName, fabricId, RandomNumberGenerator.GetBytes(16));
5662
this.fabric.CreateCommissioner();

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@
2424
#### Other Projects:
2525
* Check out my other projects for [HomeKit](https://github.com/SmartHomeOS/HomeKitDotNet) and [ZWave](https://github.com/SmartHomeOS/ZWaveDotNet)
2626

27+
## Getting Started
28+
* See our [Examples Page](Examples.md)
29+
2730
**Financial Support will help the project achieve certification as an officially supported library:**
2831
<br/><a href="https://www.buymeacoffee.com/jdomnitz" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-red.png" alt="Buy Me A Pizza" style="height: 60px !important;width: 217px !important;" ></a>

0 commit comments

Comments
 (0)