Click or drag to resize
Lawo.EmberPlusSharp.S101 Namespace Tutorial

The most frequently used class of this namespace certainly is S101Client, the usage of which is demonstrated on Page 4: Create a Local Copy of the Provider Database.

The other types in this namespace offer lower level abstractions like S101 messages (S101Message) plus commands (KeepAliveRequest, KeepAliveResponse, EmberData) as well as their encoding (S101Writer) and decoding (S101Reader). The usage is demonstrated in the following test:

C#
var writtenMessage = new S101Message(0x00, new EmberData(0x01, 0x0A, 0x02));
var writtenPayload = new byte[8192];
this.Random.NextBytes(writtenPayload);

using (var encodedStream = new MemoryStream())
{
    // First we create a writer, which can be used to write multiple messages.
    // We specify which methods are used to write encoded output and flush it plus the size the internal
    // buffer should have.
    var writer = new S101Writer(encodedStream.WriteAsync);

    // Next we write the message. In return we get a Stream object for the payload.
    using (var payloadStream =
        await writer.WriteMessageAsync(writtenMessage, CancellationToken.None))
    {
        // Now we write the payload.
        await payloadStream.WriteAsync(writtenPayload, 0, writtenPayload.Length);
        await payloadStream.DisposeAsync(CancellationToken.None);
    }

    await writer.DisposeAsync(CancellationToken.None);

    // Reset the encoded stream to the beginning, so that we can read from it.
    encodedStream.Position = 0;

    // First we create a reader, which can be used to read multiple messages.
    // We specify which methods are used to read encoded input.
    var reader = new S101Reader(encodedStream.ReadAsync);
    Assert.IsTrue(await reader.ReadAsync(CancellationToken.None)); // Read the first message
    var readMessage = reader.Message;

    // Assert the written and read messages are equal
    Assert.AreEqual(writtenMessage.Slot, readMessage.Slot);
    Assert.AreEqual(writtenMessage.Command, readMessage.Command);

    using (var readPayload = new MemoryStream())
    {
        await reader.Payload.CopyToAsync(readPayload); // Copy the payload.
        // Assert that there is only one message
        Assert.IsFalse(await reader.ReadAsync(CancellationToken.None));
        CollectionAssert.AreEqual(writtenPayload, readPayload.ToArray());
    }

    await reader.DisposeAsync(CancellationToken.None);
}