Use the MASA family bucket to build an IoT platform from scratch (1) Environment preparation

foreword

This series of articles is based on the perspective of IoT development novice, and uses .Net as the main technology to build a simple IoT platform from scratch. Since it is an introductory series, all codes are mainly based on IoT basic business scenarios, and DDD and other design ideas will not be involved .

architecture diagram

Here is the architecture diagram of our entire IoT platform.

1. Device access

1. For devices that can be connected to the Internet, directly connect to our MQTT service through the MQTT protocol (you can use domestic Alibaba Cloud, Onenet or use open source EMQX). Green wireframe part 2. For devices that cannot be connected to the Internet, such as those that can only communicate through infrared, SMS, and other non-Internet technologies, the device gateway is used to take over the unified management. This mainly involves hardware-related content, which we will not discuss in depth. Orange wireframe part

2. Process device telemetry data

We subscribe to the messages published by the device to MQTT through the MQTT Hub, and send them to our IoT Core cluster through Dapr's Pub/Sub method. We can implement our high-availability cluster through the shared subscription method in the MQTT 5.0 protocol. blue wireframe part

3. Equipment control

Directly issue control commands to MQTT directly in IoT Core. red arrow

Four, management background

We use Blazor to build the management background combined with MASA Auth to achieve permission control. In the red wireframe part, there is a layer of Gateway between IoT Core and IoT UI, which is mainly convenient for us to mock some dependent third-party business interfaces at this layer, such as a certain link I need to obtain some information from the production MES system to continue the following operations. We can mock these interfaces and data at the Gateway layer to facilitate our testing.

5. Data Storage

We store business data in a relational database, and store device message data (telemetry data) in a time-series database for easy statistical lookup and report generation.

6. Rule engine

For different types of messages, we can configure them in the rule engine so that the messages can go through different processing procedures. For example, for connected third-party system devices, device messages may be encrypted. We use the rule engine to pass this type of message After being sent to a third-party system for decryption, it is stored in the database. The telemetry data can also be analyzed and filtered here to generate hierarchical alarm information and sent to the corresponding processing system or stakeholder mailbox.

Environmental preparation

1. Install EMQX

1. Run the following command to get the Docker image:

docker pull emqx/emqx:5.0.20

2. Run the following command to start the Docker container

docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:5.0.20

2. Create a solution

Create solution MASA.IoT, add 1, WebApi project MASA.IoT.Core respectively, corresponding to IoT Core 2 in the architecture diagram, WebApi project MASA.IoT.Hub, corresponding to IoT Hub 3 in the architecture diagram, and Blazor application MASA.IoT.UI , corresponding to the IoT.UI in the architecture diagram, that is, the management background 4. The class library MASA.IoT.Common stores some static public methods

3. Install Dapr development environment

The full name of Dapr is "Distributed Application Runtime", that is, "Distributed Application Runtime". It is an open source project, initiated by Microsoft, students who don't know about Dapr can learn, Dapr series of articles by Mr. Ghost

Use Masa DaprStarter to build a development environment

Install Dapr

There are many ways to install Dapr on Windows, you can refer to the official documentation

https://docs.dapr.io/getting-started/install-dapr-cli/

If the access is slow in China, it is recommended to download the msi file directly for installation

https://github.com/dapr/cli/releases

Install all the way to the next step.

Install Docker Desktop

Directly refer to the official document, but here is not too much

https://docs.docker.com/desktop/install/windows-install/

Initialize Dapr

command line execution

dapr init

If there is a similar network error here, you need to consider magic Internet access or directly download the dashboard_darwin_amd64.zip and daprd_windows_amd64.zip two compressed packages from dapr’s github, execute dapr init, and after you have the bin directory, look for time to compress the downloaded two files Copy the package into the bin directory under C:\Users\Administrator\.dapr, for example. The compressed package in this directory will be automatically decompressed. If it fails, you can uninstall it through the dapr uninstall command and try again . After the installation is successful, you can see the following version information through dapr -v

Three containers will be created in Docker Desktop

code development

We create two new WebApi projects to simulate the scenario of calling through dapr between microservices. Create new webapi projects PubDemo and SubDemo , and install Dapr.Client and Masa.Contrib.Development.DaprStarter.AspNetCore respectively, which is convenient for us to manage the life cycle of dapr in the development environment. We additionally install Dapr.AspNetCore in the SubDemo project . Add in Program.cs of PubDemo

if (builder.Environment.IsDevelopment())
{
    builder.Services.AddDaprStarter(builder.Configuration.GetSection("DaprOptions"), false);
}

And add DaprOptions configuration in the configuration file

  "DaprOptions": {
    "AppId": "pub-demo-webapi",
    "AppPort": 18009,
    "AppIdSuffix": "",
    "DaprGrpcPort": 20333,
    "DaprHttpPort": 20334,
    "LogLevel": "debug"
  }

Also add the above content in SubDemo , pay attention not to repeat the port number.

  "DaprOptions": {
    "AppId": "sub-demo-webapi",
    "AppPort": 19009,
    "AppIdSuffix": "",
    "DaprGrpcPort": 20233,
    "DaprHttpPort": 20234,
    "LogLevel": "debug"
  }

It should be noted here that the AppPort in the SubDemo project needs to be consistent with the startup port of the application in launchSettings.json , which is 19009 here . This AppPort is not the port of dapr but the port address of the application that needs to communicate with dapr sidecar .

    "SubDemo": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:19009",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }

In SubDemo, you also need to subscribe to related configurations. The complete code is as follows

var builder = WebApplication.CreateBuilder(args);
//注册 Dapr
//将 AddDapr 扩展方法附加到 AddControllers 扩展方法会注册必要的服务以将 Dapr 集成到 MVC 管道中
builder.Services.AddControllers().AddDapr();
if (builder.Environment.IsDevelopment())
{
    builder.Services.AddDaprStarter(builder.Configuration.GetSection("DaprOptions"), false);
}
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
// 将 CloudEvents 中间件添加到 ASP.NET Core 中间件管道中
app.UseCloudEvents();
app.MapControllers();
// 终结点路由配置中对 MapSubscribeHandler 的调用会将 Dapr 订阅终结点添加到应用程序。
app.MapSubscribeHandler();
app.Run();

Note here, app.MapSubscribeHandler(); This endpoint will respond to requests on /dapr/subscribe. When this endpoint is called, it will automatically find all WebAPI action methods decorated with the Topic attribute and instruct Dapr to create subscriptions for these methods. We create a new WebApi interface in SubDemo to subscribe to Pub data

namespace SubDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SubTestController : ControllerBase
    {
        [Topic("pubsub", "testdata")]
        [HttpPost("testdata")]
        public void TestData([FromBody] string testStrData)
        {
            Console.WriteLine("Subscriber received : " + testStrData);
        }
    }
}

Here, the first parameter of Topic is the name of the pubsub component, and the second parameter is the name of the subscribed topic. The default pubsub component is configured in the C:\Users\Administrator.dapr\components directory.

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: pubsub
spec:
  type: pubsub.redis
  version: v1
  metadata:
  - name: redisHost
    value: localhost:6379
  - name: redisPassword
    value: ""

After SubDemo is written, add a test API to PubDemo

using Dapr.Client;
using Microsoft.AspNetCore.Mvc;

namespace PubDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PubTestController : ControllerBase
    {
        private readonly Dapr.Client.DaprClient daprClient;
        public PubTestController()
        {
            daprClient = new DaprClientBuilder().Build();
        }
        [HttpPost]
        public async Task PostTestDataAsync([FromBody] string testStrData)
        {
            await daprClient.PublishEventAsync("pubsub", "testdata", testStrData);
        }
    }
}

Here, a text message is written to testdata directly through the PublishEventAsync method.

Test effect

We start both SubDemo and PubDemo projects at the same time. After the application is fully started, you can see that DaprStarter has started two dapr instances for us through dapr list on the command line .

In the log of SubDemo, we can see the log of Dapr startup and subscription success

We call the test Api in PubDemo, and you can see the log of successful consumption in the log of SubDemo

You can also see the corresponding records in redis. We can also see the link information when we open zipkin . This is only for demonstration, later we will connect to the powerful MASA Stack family bucket, and it will be more convenient and intuitive to use the TSC service to analyze link information.

You can also view dependencies, which is very convenient.

The full text ends. The above is what I want to talk about today. This article only briefly introduces the basic structure of our IoT background. In the next chapter, we will talk about the life cycle of devices.


If you are interested in our MASA Framework, whether it is code contribution, use, or issue, please contact us

WeChat:MasaStackTechOps

QQ:7424099

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/5447363/blog/8685877