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
Name | Version | Downloads |
---|---|---|
Thingsboard.Net.Abstractions | ||
Thingsboard.Net.Flurl | ||
Thingsboard.Net.Flurl.DependencyInjection |
Creating a client and trying to invoke getCurrentUser method:
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
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);
}
}
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 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.
Leave a comment on GitHub if you have any questions or suggestions.
Turn on the star if you like this project.
This project is licensed under the MIT License