nepton / Thingsboard.Net

Thingsboard.NET is a .NET client library for Thingsboard IoT Platform. It is a .NET Standard 2.0 library, so it can be used in .NET Core and .NET Framework applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Thingsboard.Net

Build status CodeQL GitHub license

Thingsboard.NET is a .NET client library for Thingsboard IoT Platform. It is a .NET Standard 2.0 library, so it can be used in .NET Core and .NET Framework applications.

All client API are tested in Thingsboard v3.4.x

Nuget packages

Name Version Downloads
Thingsboard.Net.Abstractions nuget stats
Thingsboard.Net.Flurl nuget stats
Thingsboard.Net.Flurl.DependencyInjection nuget stats

Usage

Creating a client and trying to invoke getCurrentUser method:

Basic usage

Install the NuGet package Thingsboard.Net.Flurl.

PM> Install-Package Thingsboard.NET.Flurl

Then put following code into your project

// Initial factory
var factory = new FlurlTbClientFactory
{
    Options = new ThingsboardNetFlurlOptions()
    {
        BaseUrl  = "http://localhost:8080",
        Username = "tenant@thingsboard.org",
        Password = "tenant",
    }
};

// Get the client
var authClient = factory.CreateAuthClient();
var userInfo = await authClient.GetCurrentUserAsync();
Console.WriteLine($"Hello {userInfo.Email}");

You will get the output from console:

Hello tenant@thingsboard.org

Integration to ASP.NET Core

You can use the Thingsboard.NET.Flurl library in ASP.NET Core applications. Te dependency injection mode is supported.

First, add the Thingsboard.NET.Flurl.DependencyInjection library to your project:

PM> Install-Package Thingsboard.NET.Flurl.DependencyInjection

Then, register the Thingsboard.NET.Flurl services in the ConfigureServices method of Startup.cs:

// add package "Thingsboard.Net.Flurl.DependencyInjection" Version="3.4.1.1"
serviceBuilder.AddThingsboardNet(options =>
{
    options.Username = "tenant@thingsboard.org";
    options.Password = "tenant";
    options.BaseUrl  = "http://localhost:8080";
});

Then you can inject the client factory in your controllers:

public class HomeController : Controller
{
    private readonly ITbAuthClient _authClient;

    public HomeController(ITbAuthClient authClient)
    {
        _authClient = authClient;
    }

    public async Task<IActionResult> Index()
    {
        var userInfo = await _authClient.GetCurrentUserAsync();
        return View(userInfo);
    }
}

Customization options

You can customize the client options before invoke RPC methods:

public class HomeController : Controller
{
    private readonly ITbAuthClient _authClient;

    public HomeController(ITbAuthClient authClient)
    {
        _authClient = authClient;
    }

    public async Task<IActionResult> Index()
    {
        var userInfo = await auth
            .WithCredentials("newuser@thingsboard.com", "your-password")
            .WithBaseUrl("https://tb-server")
            .GetCurrentUserAsync();
        return View(userInfo);
    }
}

Thanks

  • Thanks to Flurl for the great HTTP library.
  • Thanks to Thingsboard for the great IoT platform.
  • Thanks to Polly for the great resilience and transient-fault-handling library.

Final

Leave a comment on GitHub if you have any questions or suggestions.

Turn on the star if you like this project.

License

This project is licensed under the MIT License

About

Thingsboard.NET is a .NET client library for Thingsboard IoT Platform. It is a .NET Standard 2.0 library, so it can be used in .NET Core and .NET Framework applications.

License:MIT License


Languages

Language:C# 100.0%