本教程演示,如何通过AIoTedge边缘计算平台的NodeRED软网关,对接大牧人智慧养殖平台。
下面为大牧人智慧养殖平台的API文档:
最新下载地址请联系客服!
1、示例代码 #
[{"id":"26ef2b98c8105de7","type":"inject","z":"a2661e147581c937","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":500,"wires":[["a2e2c0ac9563fcb6"]]},{"id":"a2e2c0ac9563fcb6","type":"function","z":"a2661e147581c937","name":"配置用户信息","func":"msg.payload = {\n \"username\":\"********\",\n \"password\":\"********\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":260,"y":500,"wires":[["4c1e2c1aa35efa60"]]},{"id":"4c1e2c1aa35efa60","type":"http request","z":"a2661e147581c937","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":460,"y":500,"wires":[["08bc16d777ef5ffd"]]},{"id":"964fa856aafe8c9c","type":"comment","z":"a2661e147581c937","name":"3、获取大牧人平台【数据】","info":"","x":140,"y":460,"wires":[]},{"id":"f1c38ab617696d13","type":"function","z":"a2661e147581c937","name":"获取数据","func":"msg.headers={\n authorization: \"Token \" + msg.payload.data.token\n}\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":120,"y":380,"wires":[["d032eaf8eda9b073"]]},{"id":"d032eaf8eda9b073","type":"http request","z":"a2661e147581c937","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":320,"y":380,"wires":[["dc57e6f0834de146"]]},{"id":"08bc16d777ef5ffd","type":"function","z":"a2661e147581c937","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":660,"y":500,"wires":[["5a11e7e776fee8b7"]]},{"id":"5a11e7e776fee8b7","type":"http request","z":"a2661e147581c937","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":160,"y":560,"wires":[["67433b82316191fb","042cd8569f33aa7a"]]},{"id":"67433b82316191fb","type":"function","z":"a2661e147581c937","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":400,"y":560,"wires":[["871436738d3f77c1"]]},{"id":"871436738d3f77c1","type":"debug","z":"a2661e147581c937","name":"GET请求响应","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":560,"wires":[]},{"id":"dc57e6f0834de146","type":"debug","z":"a2661e147581c937","name":"GET请求响应","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":380,"wires":[]},{"id":"a9f221b12307bdf8","type":"inject","z":"a2661e147581c937","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":320,"wires":[["d874d2cf302ba6ac"]]},{"id":"d874d2cf302ba6ac","type":"function","z":"a2661e147581c937","name":"配置用户登录信息","func":"msg.payload = {\n \"username\":\"********\",\n \"password\":\"********\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":320,"wires":[["c01bbcc4ea7ff050"]]},{"id":"c01bbcc4ea7ff050","type":"http request","z":"a2661e147581c937","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":480,"y":320,"wires":[["f1c38ab617696d13"]]},{"id":"04999de98d550993","type":"comment","z":"a2661e147581c937","name":"2、获取大牧人【养殖场编码】","info":"","x":140,"y":280,"wires":[]},{"id":"7e812e68341278c7","type":"comment","z":"a2661e147581c937","name":"使用注意:记得修改【用户信息】","info":"","x":230,"y":60,"wires":[]},{"id":"042cd8569f33aa7a","type":"debug","z":"a2661e147581c937","name":"GET请求响应","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":400,"y":600,"wires":[]},{"id":"cd94f8aebd631e0d","type":"comment","z":"a2661e147581c937","name":"双击打开查看【官网使用文档】","info":"官网使用文档:下面全部复制\nhttps://yunteng.yuque.com/to2an3/rrd8yw/gh9yedlmlvgkstu4?singleDoc# 《大牧人【智慧养殖平台】API接入示例》","x":510,"y":60,"wires":[]},{"id":"2292d16993a6cd56","type":"inject","z":"a2661e147581c937","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":200,"wires":[["278439374cdf8088"]]},{"id":"278439374cdf8088","type":"function","z":"a2661e147581c937","name":"配置用户登录信息","func":"msg.payload = {\n \"username\":\"********\",\n \"password\":\"********\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":200,"wires":[["d0e3ea0f8fe17b9c"]]},{"id":"d0e3ea0f8fe17b9c","type":"http request","z":"a2661e147581c937","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":480,"y":200,"wires":[["50229a26105f74d4"]]},{"id":"179e775226111af6","type":"comment","z":"a2661e147581c937","name":"1、通过POST请求,获取Token","info":"","x":150,"y":160,"wires":[]},{"id":"50229a26105f74d4","type":"debug","z":"a2661e147581c937","name":"GET请求响应","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":200,"wires":[]}]
2、获取Token #
首先打开调试窗口,方可查看输出结果。
按照步骤1,打开调试节点,方可在调试窗口看到日志输出;PS:实心为打开,空心为关闭。
单击步骤2,执行流程;然后在右侧调试窗口,即可看到HTTP请求返回的Token结果。
3、获取养殖场编码 #
按照API请求文档,在请求平台数据前,首选需要获得养殖场编码;
所以这一步,会发送2条请求:
第一条请求根据用户信息获得Token,这里采用的是POST请求
第二条请求根据Token信息,再发送一条GET请求,获取【养殖场编码】
4、获取智慧养殖平台数据 #
通过调用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(体感温度)。
在获取平台数据时,注意修改养殖场编码。