Variable configuration
IoT Application Development (IoT Studio) provides a variable configuration feature for business logic that you can use to configure global and local variables on the business service editing page to store and pass data.
Add a variable
Create a business service. For more information, see Create a business service.
On the business logic editing page, click the Variable Configuration button
in the upper-right corner.In the Variables pane, click +Variable.
In the Add Variable dialog box, configure the parameters.
Parameter Description Variable name The name of the variable. The name can contain letters, digits, and underscores. It must start with a letter and be no more than 50 characters long. Global variable names must be unique within a project. Local variable names can be reused in different services. Within the same service, a global variable and a local variable cannot have the same name.
Variable type - Global variable: Applies to all services in the project.
- Local variable: Applies only to the current service.
Data type Available types: Num, String, Boolean, Array, and JSON. This cannot be modified after creation.
Default value The initial value of the variable. Description A description of the variable's purpose. This information helps with the maintenance of large services. Encryption Select Encryption if the variable stores sensitive information, such as account names, passwords, ID card numbers, or phone numbers. The value of an encrypted variable appears as a random string in operational logs. This prevents sensitive data from being exposed. Example
-
imageURL : An unencrypted image URL. The default value is https://www.test.com/***. -
idCardNum : An encrypted ID card number. The default value is a real ID card number.
In the operational log,
imageURL displays its configured value, whileidCardNum displays an encrypted string."variable":{ "imageURL":"https://www.test.com/***" "idCardNum":"VOtCb21WWJleCKQDLReIJi34Ct5zfy+0japG4fuzEig=" }Note The encryption feature encrypts the variable value when it is passed and decrypts it when it is used. This process slightly increases the service running time.Click Save.
After the variable is created, it appears in the Variables pane. You can edit its default value, description, and encryption setting, or delete the variable.You can search for variables by selecting a variable type or entering a keyword.
Use variables
You can use variables as parameters when you configure service nodes. IoT Studio lets you modify and pass variable values using the following nodes.
- Variable Settings: For more information, see the best practice Count meeting attendees.
- NodeJS Script: Specify a variable using
global.variable_name. In the following figure,test_list is a configured variable.
- When you use a NodeJS Script node to modify a variable value, the data type of the new value must match the data type of the variable.
If the data types do not match when you modify a global variable, all business services in the project fail. This causes the entire project to stop working.
For these reasons, avoid using NodeJS Script nodes to work with variables.
- The Variable Settings node validates the data type when you modify a variable value. Use this node to work with variables.