Get Push Messages by Pulsar (C# SDK)

Last Updated on : 2024-06-20 06:24:35download

A cloud project provides the message subscription feature. If the device status in the project changes, such as registration, data reporting, and offline events, Message Service is used to actively push event data to external partners with Pulsar. This helps to implement real-time and persistent messages.

Prerequisites

  • You have created a project. For more information about project creation, see Quick Start.
  • Message Service is enabled. For more information, see Message Service.
  • You have installed the C# development tool Rider.
  • You have installed the .NET IDE.

Procedure

Currently, Tuya Smart Message Service is customized and modified based on open source Pulsar. The Pulsar SDK for C# is provided.
In this topic, the C# development tool Rider is used to receive messages.

Step 1: Download demo source code

Click Pulsar SDK to download the package to the local computer and decompress the package.

Step 2: Open the source code project

  1. Use Rider to open the source code project: Click New Solution to create a solution.
    On the New Solution page, select Console Application, enter the solution name in the field of Solution Name, and select C# for Language. Select net5.0 for Framework, and click Create.

    Get Push Messages by Pulsar (C# SDK)

  2. Copy the downloaded source code to the Program.cs file in the development project.

    Get Push Messages by Pulsar (C# SDK)

  3. Add the dependency packages.
    The project requires the following dependency packages:

    • System.Net.WebSockets Version=4.3.0
    • Newtonsoft.Json Version=13.0.1

    (1) In the top navigation bar, choose Tools > NuGet > Manage NuGet Packages for Solution to open the NuGet manager.

    Get Push Messages by Pulsar (C# SDK)

    (2) Enter the name of the package to be installed, select the desired one, select a version number from the drop-down list of Version, and click + to install it.

    The following figure shows how to install Newtonsoft.Json:

    Get Push Messages by Pulsar (C# SDK)

    (3) Click Install to confirm the installation.
    Get Push Messages by Pulsar (C# SDK)

    The following figure shows how to install System.Net.WebSockets.

    Get Push Messages by Pulsar (C# SDK)

Step 3: Configure project parameters

Modify the parameters in the sample code Program.cs. This topic takes the test channel as an example.

# accessId, accessKey, serverUrl, MQ_ENV
private static string ACCESS_ID = "xt*****ff**n1****8ufo";
private static string ACCESS_KEY = "479bcb7345******582d9c******4ef7";
private static string WSS_SERVER_URL = "wss://mqe.tuyacn.com:8285/";
private static string MQ_ENV= MQ_ENV_TEST;

The parameters are described as follows.

  • ACCESS_ID: Enter the value of Access ID in the Authorization Key section.

  • ACCESS_KEY: Enter the value of Access Secret in the Authorization Key section.

  • WSS_SERVER_URL: Choose the endpoints according to your location.

    • China Data Center: wss://mqe.tuyacn.com:8285/
    • Western America Data Center: wss://mqe.tuyaus.com:8285/
    • Central Europe Data Center: wss://mqe.tuyaeu.com:8285/
    • India Data Center: wss://mqe.tuyain.com:8285/
  • MQ_ENV: message subscription channel.

    • MQ_ENV_PROD represents the formal environment.
    • MQ_ENV_TEST represents the test environment.

    Get Push Messages by Pulsar (C# SDK)

Step 4: Operate a device on the app and receive messages in the test environment

  1. Configure a test device.

    (1) On the page of Message Service, after your subscription is ready, Subscribed appears in the Status column. Click the Message Test tab.

    Get Push Messages by Pulsar (C# SDK)

    (2) Enter the associated device ID in the search box, click the magnifier icon, and then double-click the selected device.
    Get Push Messages by Pulsar (C# SDK)

    (3) If an associated device is specified as a test device, it will report a message to the test channel for you to debug device features.

  2. On the development tool Rider, right-click the Pulsar-demo project and select Run ‘Pulsar-demo’ to run it.

    Get Push Messages by Pulsar (C# SDK)

  3. Control the device with a mobile app.
    (1) Turn on the switch of the test light.
    (2) Adjust the brightness of the light.

  4. Automatically get the push messages on Rider.

    Get Push Messages by Pulsar (C# SDK)

  5. For more information about field descriptions, see Message Queue.

Parse push messages

Turn on the light

  • Received message

    payload={"data":"uSqWibG1DRsqYj/EWW5xRP4NlAo948wkhatFSvvtl3lgYzHRCUfLB8bAhjohz5jqzQtf9wFhkSQ6IBE2P3yYPmXksA0dqEMhr3RmkClnnfWt9xi+AvqZ5E3OZgI3dkpaD+3oi9XCRpgXXnjyjoyXPxk8qvcNe3J+kdU32jkbjxlzxxG+g6m1fHPH7m4+OHuh8Q09f8Ff1CNONQFvMLfStqmh6zyXimOxTy0x9pIPxUPxHwhUndn4xepP09qbODn0","protocol":4,"pv":"2.0","sign":"16cc5405b1e843bdd6f25063fa8ce330","t":1631158677498}
    payloadJson={
      "data": "uSqWibG1DRsqYj/EWW5xRP4NlAo948wkhatFSvvtl3lgYzHRCUfLB8bAhjohz5jqzQtf9wFhkSQ6IBE2P3yYPmXksA0dqEMhr3RmkClnnfWt9xi+AvqZ5E3OZgI3dkpaD+3oi9XCRpgXXnjyjoyXPxk8qvcNe3J+kdU32jkbjxlzxxG+g6m1fHPH7m4+OHuh8Q09f8Ff1CNONQFvMLfStqmh6zyXimOxTy0x9pIPxUPxHwhUndn4xepP09qbODn0",
      "protocol": 4,
      "pv": "2.0",
      "sign": "16cc5405b1e843bdd6f25063fa8ce330",
      "t": 1631158677498
    }
    
    
  • Parse payload data

    data={"dataId":"52effb10-111f-11ec-935e-02426139acc4","devId":"vde*******70149816","productKey":"AAi******Tgmo64Hq","status":[{"1":"true","code":"switch_led","t":1631158677476,"value":true}]}
    

    For more information about the data parsing method, see Message Queue. The payload decryption method DecryptByAes has been encapsulated in the SDK.

Adjust the brightness of the light

  • Received message

    payload={"data":"vJuyzlQPkXrZn/9OSwTOP5bmlNruRVFOZoc1Zod3Ul9gYzHRCUfLB8bAhjohz5jqzQtf9wFhkSQ6IBE2P3yYPmXksA0dqEMhr3RmkClnnfWt9xi+AvqZ5E3OZgI3dkpaD+3oi9XCRpgXXnjyjoyXP4F82++oke4J6O8vAvLCpDxsOTqBLnuQXj9p+sknAK6Z1NAGbES2LQ4fDPlg/ZFqoccIX3s9PhIo9cPZSzctc9MrqoBg1e7jFRfIAiY3bPYx","protocol":4,"pv":"2.0","sign":"53b8014e0c2d445e8dca665a02236100","t":1631158740025}
    payloadJson={
      "data": "vJuyzlQPkXrZn/9OSwTOP5bmlNruRVFOZoc1Zod3Ul9gYzHRCUfLB8bAhjohz5jqzQtf9wFhkSQ6IBE2P3yYPmXksA0dqEMhr3RmkClnnfWt9xi+AvqZ5E3OZgI3dkpaD+3oi9XCRpgXXnjyjoyXP4F82++oke4J6O8vAvLCpDxsOTqBLnuQXj9p+sknAK6Z1NaAGbES2LQ4fDPlg/ZFqoccIX3s9PhIo9cPZSzctc9MrqoBg1e7jFRfIAiY3bPYx",
      "protocol": 4,
      "pv": "2.0",
      "sign": "53b8014e0c2d445e8dca665a02236100",
      "t": 1631158740025
    }
    
  • Parse the data

    data={"dataId":"782ee45f-111f-11ec-935e-02426139acc4","devId":"vde*******70149816","productKey":"AAi******Tgmo64Hq","status":[{"3":"177","code":"bright_value","t":1631158739964,"value":177}]}