Abstract: “Internet of Things platform interface call experiment” explains the application of API Explorer in detail, according to the interface provided, combined with the real case, made a small program, really apply it, liberate repeated labor, small program is a good platform, as an application side application as a fast App environment.

This article is shared from Huawei Cloud community “Huawei Cloud IoT API Explorer Docking small program to achieve systematic application”, author: Shenlongju city.

“Internet of Things Platform Interface Call Experiment” explains the application of API Explorer in detail. According to the interface provided and combined with real cases, a small program is made to really apply it and liberate repetitive work. Small program is a good platform as an application side application as a fast App environment.

Materials:

1. Create a Huawei cloud account, open the IoTDA console, select the four regions in Beijing, and create a good resource space.

2. Bearpi-hm Nano Development board, and E53_IA1 extended version.

3, wechat small program account, if there is no authentication, you can also use the development of a temporary account.

4. Send one of the cases and export the Profile file. If the data fields of the Profile file and API interface are different, you can replace them in batches

How it works and the API Explorer interface used.

The workflow and calling interface relationship are shown below:

The interface calls the wrapper code

1. First of all, the login interface needs to have access to the resource space of the IoT platform in four areas of Beijing

Var tokenReq = require('.. /jsons/reqJson/tokenReq.js'); Id export function authToken(Params) {var data = tokenReq.reqTokenjson; data.auth.identity.password.user.name = params.iamAccount data.auth.identity.password.user.password = params.iamPassword  data.auth.identity.password.user.domain.name = params.account console.log("auth_data", data) return request({ url: `${global.IAMEndpoint}/v3/auth/tokens`, method: 'post', data: Data})} // Get project_id from token as global data for later use with wx.setStoragesync ('project_id', res.tok.project.id)Copy the code

2, Find and initialize the resource, if project_id corresponding to the specified resource space exists, otherwise create resource space

Let params ={'project_id': global.project_id} init(params, res => {global.products = res; wx.navigateTo({ url: '.. /home/index', }) }) export function init(params, func) { getAppSpace({ "project_id": params.project_id }).then(res => { var app_name = global.app_name; var applications = res.applications; for (var item of applications) { if (item.app_name == app_name) { global.app_id = item.app_id break; } } if (global.app_id) { console.log('app_id', global.app_id); func(true) } else { addAppSpace({ 'project_id': global.project_id || wx.getStorageSync('project_id') }).then(res => { global.app_id = res.app_id console.log('app_id', global.app_id); func(true) }) } }); }Copy the code

3. Add device is to add the selected product. Therefore, at the same time of adding device, check whether the product exists first. The Profile file exported by IoTDA platform is not consistent with the API Explorer interface. The JSON structure is consistent, but the field name is inconsistent. I have also commented on this on the platform, it has been adopted, but it is not implemented yet. The CreateProduct parameter of the IOT APIExplorer is inconsistent with the Profile parameter. Suggestion feedback. Developer center – huaweicloud

AddOrGetDevice: function (func) {/ / check input var json = deviceCreate deviceCreateJson; json.node_id = this.data.device_name json.device_name = this.data.device_name // json.project_id = global.project_id Json. Product_id = global. Product_id json. App_id = global. Whether there is this app_id / / products. CheckProduct (this. Data. SelectedProduct, Var has = this.data.devices.filter((x) => {return x.davide_name == this.data.device_name; }); if (has == false) { var params = { "deviceCreateJson": { "node_id": this.data.device_name, "device_name": this.data.device_name, "product_id": product.product_id, "app_id": product.app_id, "description": "XXXXXX device" }, "project_id": global.project_id } addDevice(params).then(device => { if (device.device_id) { this.setData({ showAddDevice: false }) func({ device_id: device.device_id, secret: device.auth_info.secret }, { ssid: this.data.wifiSSID, pwd: This.data.wifipwd})} else {wx.showmodal ({title: 'Failed to create device ', Content:' ${res.error_msg} ', showCancel: false }) } }) } else { func(null, { ssid: this.data.wifiSSID, pwd: this.data.wifiPwd }) } }) }Copy the code

4, the dynamic display of the device, according to the product Profile attribute. The device attribute value cannot be read directly from the device. The device shadow data is read here

Export function getDeviceProperties(params) {if (! params) params = {} let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id') let device_id = params.device_id return request({ url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/devices/${device_id}/shadow`, method: 'get' }) }Copy the code

Applet related interface

Click to follow, the first time to learn about Huawei cloud fresh technology ~