Test Drivers for Tuya Edge Gateway

Last Updated on : 2024-04-12 05:48:20download

This topic describes how to test drivers and driver instances for Tuya Edge Gateway (TEdge). An OPC Unified Architecture (UA) driver is used in the following example. Driver configurations might slightly vary based on different drivers. But the configuration procedure is similar.

Test the driver with the edge gateway

Prerequisites

The edge gateway is deployed and activated. For more information, see Deploy and Activate Tuya Edge Gateway.

How it works

Use the ID of the driver instance that matches the driver protocol at the edge gateway to run the driver on the on-premises host. Before you run the driver on the premises, stop the driver at the edge gateway.

Procedure

  1. Deploy the Docker image of the driver to your host. The package can be placed in the directory /var/tedge/edgex-driver-data/upload/library/. You can also start the image on the premises without image packaging.

  2. Add your driver to the driver library: Visit the URL http://localhost:3000. In the left-side navigation pane of Tuya Edge Gateway, choose Driver Settings > Driver Libraries and click Create.

    Test Drivers for Tuya Edge Gateway

    In the dialog box that appears, select the required driver file from the Driver File field, click Download Configuration Template, modify the downloaded template, click Upload Configuration File, and then continue to configure the template.

  3. Configure the template: The template must be in the JSON format and consist of the following parts: deviceServer, deviceProtocols, and diviceDpAttrs.

    • deviceProtocols: Enter the value of Protocols defined in [[DeviceList]] of the driver configuration file cmd/res/configuration.toml.
    • diviceDpAttrs: Enter the value of attributes defined in deviceResources of the device configuration file. The file is the YAML file in the directory cmd/res/.

    For example, the following code block shows the configuration file [[DeviceList]]:

    [[DeviceList]]
    Name = "SimulationOpcuaDevice"
    Profile = "ABC.Device.Opcua.Profile"
    Description = "OPC UA Device"
    Labels = ["test", "opcua"]
    [DeviceList.Protocols]
    [DeviceList.Protocols.opcua]
    Endpoint = "opc.tcp://0.0.0.0:40840/tuyaopcua/server/"
    

    The following code block shows the device configuration file deviceResources:

    deviceResources:
    	-  name: "1"
    		description: "Switch"
    		attributes: {
    				nodeId: "ns=2;i=2"
    		}
    		properties:
    		value:	 {
    				type: "Bool",
    				readWrite: "R",
    				defaultValue: False
    		}
    

    The following code block shows the configuration template based on the preceding configuration files. The fields that are not set must be reserved and cannot be deleted.

    {
    	"deviceServer": {},
    	"deviceProtocols": {
    		"opcua": [
    			{
    				"name": "Endpoint",
    				"display": "endpoint",
    				"type": "string"
    			}
    			]
    		},
    	"deviceDpAttrs": [
    		{
    			"name": "nodeId",
    			"display": "nodeId",
    			"type": "string"
    		}
    	]
    }
    
  4. Create a driver instance: In the left-side navigation pane, choose Driver Settings > Driver Instances and click Create. In the dialog box that appears, enter a preferred but unique driver instance name. Set Driver Library Name to the name of the newly created driver library.

    The newly created driver instance is stopped by default. You do not need to start the instance for the on-premises test. The driver instance ID is used to start the driver instance on the premises.:::

    Test Drivers for Tuya Edge Gateway

  5. Create a test product on the Tuya Developer Platform: Go to the Create page on the platform. On the Standard Category tab, choose Electrical > Socket.

    Click the Custom Solution tab and select Plug or another type of product.

    Test Drivers for Tuya Edge Gateway

    Enter the product name and keep the default settings of Protocol and Power Type.

    Then, select socket data points (DPs). In this example, Switch 1 and Countdown 1 are selected.

    Test Drivers for Tuya Edge Gateway

    After you create the product, the product ID (PID) appears in the product details section. Save the PID for later use.

    Test Drivers for Tuya Edge Gateway

  6. Add the socket product to Tuya Edge Gateway: Visit the URL http://localhost:3000. In the left-side navigation pane of Tuya Edge Gateway, choose Devices > Products and click Add.

    Test Drivers for Tuya Edge Gateway

    Enter the details of the product that is newly created on the platform.

    • PID: Copy the saved PID and paste it to this field.
    • Name: Enter a preferred product name such as My Socket.
    • Description: Enter the product description.
    • Driver Library: Select a driver library. Fuzzy matching is supported. For example, to support the OPC UA protocol, enter the keyword opcua and select the driver that appears.

    Go back to the deployed edge gateway. In the left-side navigation pane, choose Devices > Products. Click DP for the newly added product and click Create. The created DPs must be the same as those specified on the platform.

    Test Drivers for Tuya Edge Gateway

    Note: nodeId is set to the value of deviceDpAttrs that is defined in the configuration template. You can customize deviceDpAttrs.

    Test Drivers for Tuya Edge Gateway

    The following figure shows the added DPs.

    Test Drivers for Tuya Edge Gateway

  7. Create a sub-device: In the left-side navigation pane of Tuya Edge Gateway, choose Devices > Sub-devices and click Add Sub-device.

    Test Drivers for Tuya Edge Gateway

    In the dialog box that appears, enter the details of the sub-device: In the Product field, fuzzy matching is supported. You can enter the keyword socket and select the newly created socket product that appears. In the Driver Instance field, enter the keyword opcua and select the OPC UA driver that appears. In the Protocol Type field, select the protocol to be tested.

    Endpoint is set to the value of deviceProtocols defined in the configuration template. You can customize deviceProtocols.

    Test Drivers for Tuya Edge Gateway
  8. Activate the sub-device.

    The newly added sub-device is inactive by default. Click Activate Online to activate the sub-device.

    Test Drivers for Tuya Edge Gateway

    After the sub-device is activated online, you can copy and save the device ID that will be used to send commands in the follow-up management.

    Test Drivers for Tuya Edge Gateway

  9. Run the driver on the premises.

    • The edge gateway and the host must be connected to the same network.

    • Modify the configuration file /cmd/res/configuration.toml. The fields boxed in red in the following figure must be modified. Other fields can be modified as needed.

      Set ID in [Service] to the driver instance ID and Host to the driver service IP address.

      Set host in [Client.Data] and [Client.Metadata] to the IP address of the host.

      Test Drivers for Tuya Edge Gateway

      The following figure shows the driver instance ID.

      Test Drivers for Tuya Edge Gateway

  10. Run the device simulator: You can create a simulator that can vary based on different drivers. You can also use a real device for the test.

  11. Check the driver logs: After a simulator is started, the driver runs automatic tasks. By default, the driver reads information about the simulator at an interval of 20 seconds. The driver logs can indicate whether these tasks are completed as expected.

Test the driver on a standalone host

Prerequisites

Only the basic functions of the driver are tested. The Arm platform does not support this test.

How it works

During the driver initialization, information about other services is required. In this test, the information can be read from local configuration files.

Procedure

  1. Create the test directory in the project file. The following code block shows the structure of the test directory:

    device-service-template
    └── test
    	├── README.md
    	├── data
    	│	├── device
    	│	│	├── devices.json
    	│	│	└── service.json
    	│	└── deviceprofile
    	│		├── profile1.json
    	│		└── profile2.json
    	├── main.go
    	└── res
    		├── Simple-Driver.yaml
    		└── configuration.toml
    
  2. In the data directory, configure the .json files of the service, device, and product.

    The structure of the data directory cannot be modified.

  3. Call the test.Bootstrap method to start the program on the standalone host.

For more information about the implementation example, see tedge-driver-sdk-go/device-service-template/test and README.md in tuya-edge-driver-sdk-go.