Node-RED软网关使用手册-最佳实践(第三方平台API接入示例)

本教程演示,如何通过AIoTedge边缘计算平台的NodeRED软网关,对接【大牧人智慧养殖平台】。

访问地址:https://iot1.bigherdsman.com/

Node-RED软网关使用手册-最佳实践(第三方平台API接入示例)

下面为大牧人智慧养殖平台的API文档

大牧人物联网系统API文档V2.1.pdf

大牧人物联网数据字段说明.xlsx

1、示例代码

[{"id":"26ef2b98c8105de7","type":"inject","z":"13be994a34f4ec68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":520,"wires":[["a2e2c0ac9563fcb6"]]},{"id":"a2e2c0ac9563fcb6","type":"function","z":"13be994a34f4ec68","name":"配置用户信息","func":"msg.payload = {\n    \"username\":\"********\",\n    \"password\":\"********\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":520,"wires":[["4c1e2c1aa35efa60"]]},{"id":"4c1e2c1aa35efa60","type":"http request","z":"13be994a34f4ec68","name":"POST请求-获取Token","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://iot1.bigherdsman.com/api/v2/account/login/","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":540,"y":520,"wires":[["08bc16d777ef5ffd"]]},{"id":"964fa856aafe8c9c","type":"comment","z":"13be994a34f4ec68","name":"3、获取大牧人平台【数据】","info":"","x":220,"y":480,"wires":[]},{"id":"f1c38ab617696d13","type":"function","z":"13be994a34f4ec68","name":"获取数据","func":"msg.headers={\n    authorization: \"Token \" + msg.payload.data.token\n}\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":400,"wires":[["d032eaf8eda9b073"]]},{"id":"d032eaf8eda9b073","type":"http request","z":"13be994a34f4ec68","name":"GET请求【获取养殖编码】","method":"GET","ret":"obj","paytoqs":"body","url":"https://iot1.bigherdsman.com/api/v2/entity/farm","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":400,"y":400,"wires":[["dc57e6f0834de146"]]},{"id":"08bc16d777ef5ffd","type":"function","z":"13be994a34f4ec68","name":"获取列表数据","func":"msg.headers={\n    authorization: \"Token \" + msg.payload.data.token,\n}\n\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":520,"wires":[["5a11e7e776fee8b7"]]},{"id":"5a11e7e776fee8b7","type":"http request","z":"13be994a34f4ec68","name":"GET请求【获取数据】","method":"GET","ret":"obj","paytoqs":"body","url":"https://iot1.bigherdsman.com/api/v2/current/list/?code=20*******12","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":240,"y":580,"wires":[["67433b82316191fb","042cd8569f33aa7a"]]},{"id":"67433b82316191fb","type":"function","z":"13be994a34f4ec68","name":"上报数据处理","func":"const list = msg.payload.data\nif(list&&list.length>0){\n    const data = list.reduce((acc,curr)=>{\n        const objData={}\n        const objArr =[\n            {\n                ts:curr.ts*1000,\n                values:curr.data\n            }\n        ]\n        objData[curr.i+'号舍']=objArr\n\n        const allObj = Object.assign(acc, objData)\n        return allObj\n    },{})\n    msg.payload = data;\n}\n\n\nmsg.topic=\"v1/gateway/telemetry\"\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":580,"wires":[["871436738d3f77c1"]]},{"id":"871436738d3f77c1","type":"debug","z":"13be994a34f4ec68","name":"GET请求响应","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":580,"wires":[]},{"id":"dc57e6f0834de146","type":"debug","z":"13be994a34f4ec68","name":"GET请求响应","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":400,"wires":[]},{"id":"a9f221b12307bdf8","type":"inject","z":"13be994a34f4ec68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":340,"wires":[["d874d2cf302ba6ac"]]},{"id":"d874d2cf302ba6ac","type":"function","z":"13be994a34f4ec68","name":"配置用户登录信息","func":"msg.payload = {\n    \"username\":\"********\",\n    \"password\":\"********\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":340,"wires":[["c01bbcc4ea7ff050"]]},{"id":"c01bbcc4ea7ff050","type":"http request","z":"13be994a34f4ec68","name":"POST请求-获取Token","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://iot1.bigherdsman.com/api/v2/account/login/","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":560,"y":340,"wires":[["f1c38ab617696d13"]]},{"id":"04999de98d550993","type":"comment","z":"13be994a34f4ec68","name":"2、获取大牧人【养殖场编码】","info":"","x":220,"y":300,"wires":[]},{"id":"7e812e68341278c7","type":"comment","z":"13be994a34f4ec68","name":"使用注意:记得修改【用户信息】","info":"","x":310,"y":80,"wires":[]},{"id":"042cd8569f33aa7a","type":"debug","z":"13be994a34f4ec68","name":"GET请求响应","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":480,"y":620,"wires":[]},{"id":"cd94f8aebd631e0d","type":"comment","z":"13be994a34f4ec68","name":"双击打开查看【官网使用文档】","info":"官网使用文档:下面全部复制\nhttps://yunteng.yuque.com/to2an3/rrd8yw/gh9yedlmlvgkstu4#ZmjwC","x":590,"y":80,"wires":[]},{"id":"2292d16993a6cd56","type":"inject","z":"13be994a34f4ec68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":220,"wires":[["278439374cdf8088"]]},{"id":"278439374cdf8088","type":"function","z":"13be994a34f4ec68","name":"配置用户登录信息","func":"msg.payload = {\n    \"username\":\"********\",\n    \"password\":\"********\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":220,"wires":[["d0e3ea0f8fe17b9c"]]},{"id":"d0e3ea0f8fe17b9c","type":"http request","z":"13be994a34f4ec68","name":"POST请求-获取Token","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://iot1.bigherdsman.com/api/v2/account/login/","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":560,"y":220,"wires":[["50229a26105f74d4"]]},{"id":"179e775226111af6","type":"comment","z":"13be994a34f4ec68","name":"1、通过POST请求,获取Token","info":"","x":230,"y":180,"wires":[]},{"id":"50229a26105f74d4","type":"debug","z":"13be994a34f4ec68","name":"GET请求响应","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":220,"wires":[]}]

2、获取Token

首先打开调试窗口,方可查看输出结果。

按照步骤1,打开调试节点,方可在调试窗口看到日志输出;PS:实心为打开,空心为关闭。

单击步骤2,执行流程;然后在右侧调试窗口,即可看到HTTP请求返回的Token结果。

Node-RED软网关使用手册-最佳实践(第三方平台API接入示例)

3、获取养殖场编码

按照API请求文档,在请求平台数据前,首选需要获得养殖场编码;

所以这一步,会发送2条请求:

第一条请求根据用户信息获得Token,这里采用的是POST请求

第二条请求根据Token信息,再发送一条GET请求,获取【养殖场编码】

Node-RED软网关使用手册-最佳实践(第三方平台API接入示例)

4、获取智慧养殖平台数据

PS:注意在请求平台数据时,记得修改养殖场编码

Node-RED软网关使用手册-最佳实践(第三方平台API接入示例)

通过调用API接口【https://iot1.bigherdsman.com/api/v2/current/list/?code=202******112】;获得该养殖场各个栋舍得数据,其中包括有f_v(通风量)、h_r(相对湿度)、ht_2(加热2)、qty_re(剩余数量)、co2(二氧化碳)、t_t(目标温度)、t_cur(当前温度)、age(日龄)、ht_3(加热3)、ht_1(加热1)、t_s(体感温度)。

Node-RED软网关使用手册-最佳实践(第三方平台API接入示例)

在获取平台数据时,注意修改养殖场编码。

Node-RED软网关使用手册-最佳实践(第三方平台API接入示例)