2024年1月5日 星期五

node-red 擷取 環境部環境資料開放平台開放資料 UVI PM2.5

 node-red 擷取 環境部環境資料開放平台開放資料 UVI PM2.5





   先向環境部註冊 取得 金鑰 









=======================================================

var rawdata =msg.payload;

var site_number=rawdata.length;

var site_array=[];

var i;


for (i=0; i<site_number;i++)

{

    site_array[i]=rawdata[i].sitename;

}


msg.payload=site_array;

 

flow.set("uvi_sitename",site_array);

return msg;

=======================================================
get  Sitename  all records function 的寫法
 j變數的用法



var rawdata =context.global.uvi_records;
var site_number=rawdata.length;

var site_name=msg.payload;
var site_array=[];
var i;
var j=0;

for (i=0; i<site_number;i++)
{
    if (site_name==rawdata[i].sitename)
    {
      site_array[j]=rawdata[i];
      j=j+1;
    }
}

msg.payload=site_array;
 
flow.set("uvi_getdata",site_array);

return msg;

金鑰請修改成自己的


[{"id":"65dae696.2f3e18","type":"http request","z":"8dd731973f8200b5","name":"UVI request","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://data.moenv.gov.tw/api/v2/uv_s_01?api_key=aa1bc2bf-24c2-4c55-980f-a12075c2caec","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{},"x":250,"y":60,"wires":[["58205e1.a85d4a","795b098f.61bd98","94b22684.161848"]]},{"id":"58205e1.a85d4a","type":"debug","z":"8dd731973f8200b5","name":"UVImessage","active":true,"console":"false","complete":"payload","x":430,"y":20,"wires":[]},{"id":"f56e1f7f.56b2b","type":"http request","z":"8dd731973f8200b5","name":"air request","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://data.moenv.gov.tw/api/v2/aqx_p_02?api_key=aa1bc2bf-24c2-4c55-980f-a12075c2caec","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":250,"y":420,"wires":[["87094d4d3b7b0f14","691ff5b6.20c93c","b81ecd4b.f4715"]]},{"id":"795b098f.61bd98","type":"function","z":"8dd731973f8200b5","name":"UVI data process","func":"msg.payload=msg.payload.records;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":100,"wires":[["cd54c09b.0fdde","3d6bf7736e070f9b","041701e385183150"]]},{"id":"cd54c09b.0fdde","type":"debug","z":"8dd731973f8200b5","name":"UVI process message","active":true,"console":"false","complete":"payload","x":740,"y":60,"wires":[]},{"id":"94b22684.161848","type":"function","z":"8dd731973f8200b5","name":"Set context.global.uvnum","func":"context.global.uvi_records=msg.payload.records;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":60,"wires":[[]]},{"id":"d86ca1ce.c9da7","type":"debug","z":"8dd731973f8200b5","name":"air process message","active":true,"console":"false","complete":"payload","x":680,"y":460,"wires":[]},{"id":"691ff5b6.20c93c","type":"function","z":"8dd731973f8200b5","name":"air data process","func":"var n=context.global.airnum;\nmsg.payload=n;\n\n\n//msg.payload=airobj;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":420,"wires":[["d86ca1ce.c9da7","118109bd0c0227f5","f451a2f94e2005b6"]]},{"id":"b81ecd4b.f4715","type":"function","z":"8dd731973f8200b5","name":"Set context.global.airnum","func":"context.global.airnum=msg.payload.records;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":360,"wires":[["036ccd78a22dfa05"]]},{"id":"87094d4d3b7b0f14","type":"debug","z":"8dd731973f8200b5","name":"air message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":320,"wires":[]},{"id":"036ccd78a22dfa05","type":"debug","z":"8dd731973f8200b5","name":"debug  ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.records","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":360,"wires":[]},{"id":"fe4598cf96b92790","type":"function","z":"8dd731973f8200b5","name":"get 站名 pm2.5","func":"var a =context.global.airnum;\nvar site_name=msg.payload;\nvar pm25msg;\n\na.forEach(function(e,i){\n\n    if(e.site==site_name)  //e.site 參考下面 site: \"大城\"\n\n    {\n        pm25msg = site_name+ 'PM2.5='+ e['pm25'];\n        pm25msg = pm25msg + e['itemunit'];\n        pm25msg = pm25msg + '發佈時間' + e['datacreationdate'];\n\n\n    }\n\n});\n\nmsg.payload=  pm25msg;\nreturn msg;\n\n\n//site: \"大城\"\n//county: \"彰化縣\"\n//pm25: \"41\"\n//datacreationdate: \"2024-01-06 11:00\"\n//itemunit: \"μg/m3\"","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":640,"wires":[["89ae4c363462cfae","0e2d4f329c2cddaa"]]},{"id":"89ae4c363462cfae","type":"debug","z":"8dd731973f8200b5","name":"debug  ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":640,"wires":[]},{"id":"118109bd0c0227f5","type":"function","z":"8dd731973f8200b5","name":"get All Sitename","func":"var rawdata =msg.payload;\nvar site_number=rawdata.length;\nvar site_array=[];\nvar i;\n\nfor (i=0; i<site_number;i++)\n{\n    site_array[i]=rawdata[i].site;\n}\n\nmsg.payload=site_array;\n \nflow.set(\"site_name\",site_array);\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":500,"wires":[["af59690d583e8e39","f9c4a854844b15d6"]]},{"id":"af59690d583e8e39","type":"debug","z":"8dd731973f8200b5","name":"debug 241","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":500,"wires":[]},{"id":"0e2d4f329c2cddaa","type":"ui_text","z":"8dd731973f8200b5","group":"38f8f1c1d386a910","order":1,"width":9,"height":1,"name":"","label":"","format":"{{msg.payload}}","layout":"row-left","className":"","x":830,"y":680,"wires":[]},{"id":"6dfa48d541a75df3","type":"ui_button","z":"8dd731973f8200b5","name":"","group":"38f8f1c1d386a910","order":4,"width":4,"height":1,"passthru":false,"label":"取得PM2.5","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":420,"wires":[["f56e1f7f.56b2b"]]},{"id":"f451a2f94e2005b6","type":"ui_table","z":"8dd731973f8200b5","group":"38f8f1c1d386a910","name":"PM2.5","order":11,"width":9,"height":12,"columns":[],"outputs":0,"cts":false,"x":630,"y":420,"wires":[]},{"id":"3d6bf7736e070f9b","type":"ui_table","z":"8dd731973f8200b5","group":"38f8f1c1d386a910","name":"UVI","order":13,"width":12,"height":12,"columns":[],"outputs":0,"cts":false,"x":1090,"y":120,"wires":[]},{"id":"ed229a62fdb90ec5","type":"ui_button","z":"8dd731973f8200b5","name":"","group":"38f8f1c1d386a910","order":8,"width":4,"height":1,"passthru":false,"label":"取得UVI","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":60,"wires":[["65dae696.2f3e18"]]},{"id":"4672aa403394c7c5","type":"ui_dropdown","z":"8dd731973f8200b5","name":"","label":"PM2.5","tooltip":"","place":"Select option","group":"38f8f1c1d386a910","order":6,"width":4,"height":1,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","className":"","x":670,"y":580,"wires":[["fe4598cf96b92790","08cd50e1d51d6b4a"]]},{"id":"f9c4a854844b15d6","type":"change","z":"8dd731973f8200b5","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":580,"wires":[["4672aa403394c7c5"]]},{"id":"2a7da5e8aa5f2e52","type":"inject","z":"8dd731973f8200b5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"","payloadType":"date","x":100,"y":360,"wires":[["f56e1f7f.56b2b"]]},{"id":"08cd50e1d51d6b4a","type":"debug","z":"8dd731973f8200b5","name":"debug 242","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":580,"wires":[]},{"id":"041701e385183150","type":"function","z":"8dd731973f8200b5","name":"get All Sitename","func":"var rawdata =msg.payload;\nvar site_number=rawdata.length;\nvar site_array=[];\nvar i;\n\nfor (i=0; i<site_number;i++)\n{\n    site_array[i]=rawdata[i].sitename;\n}\n\nmsg.payload=site_array;\n \nflow.set(\"uvi_sitename\",site_array);\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":160,"wires":[["2bfb62008dc3a122","1069b14d28973a0e"]]},{"id":"2bfb62008dc3a122","type":"debug","z":"8dd731973f8200b5","name":"debug 243","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":160,"wires":[]},{"id":"1069b14d28973a0e","type":"change","z":"8dd731973f8200b5","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":220,"wires":[["f9e2f38b0cdd677d"]]},{"id":"f9e2f38b0cdd677d","type":"ui_dropdown","z":"8dd731973f8200b5","name":"","label":"UVI","tooltip":"","place":"Select option","group":"38f8f1c1d386a910","order":10,"width":4,"height":1,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","className":"","x":610,"y":220,"wires":[["722be191a09c5d51","3120ad45b4b187b9","44733a1a49f1132b"]]},{"id":"3120ad45b4b187b9","type":"debug","z":"8dd731973f8200b5","name":"debug 244","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":220,"wires":[]},{"id":"51225089c3d8794d","type":"debug","z":"8dd731973f8200b5","name":"debug  ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":260,"wires":[]},{"id":"c4dbb6c49ff477a3","type":"ui_text","z":"8dd731973f8200b5","group":"38f8f1c1d386a910","order":3,"width":12,"height":1,"name":"","label":"","format":"{{msg.payload}}","layout":"row-left","className":"","x":870,"y":300,"wires":[]},{"id":"722be191a09c5d51","type":"function","z":"8dd731973f8200b5","name":"get 站名 UVI","func":"var a =context.global.uvi_records;\nvar site_name=msg.payload;\n\nvar vuimsg;\n\na.forEach(function(e,i){\n\n    if(e.sitename==site_name)   //e.sitename 參考下面sitename: \"塔塔加\"\n\n    {\n        vuimsg = site_name+ 'UVI='+ e['uvi'];\n        vuimsg = vuimsg + '發佈時間' + e['datacreationdate'];\n\n    }\n\n});\n\nmsg.payload=  vuimsg;\nreturn msg;\n\n//sitename: \"塔塔加\"\n//uvi: \"6.2\"\n//unit: \"環境部\"\n//county: \"嘉義縣\"\n//wgs84_lon: \"120,51,35\"\n//wgs84_lat: \"23,28,19\"\n//datacreationdate: \"2024-01-06 11:00\"\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":280,"wires":[["51225089c3d8794d","c4dbb6c49ff477a3"]]},{"id":"0f2030e76f5601ba","type":"inject","z":"8dd731973f8200b5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"","payloadType":"date","x":100,"y":20,"wires":[["65dae696.2f3e18"]]},{"id":"44733a1a49f1132b","type":"function","z":"8dd731973f8200b5","name":"get  Sitename  all records","func":"var rawdata =context.global.uvi_records;\nvar site_number=rawdata.length;\n\nvar site_name=msg.payload;\nvar site_array=[];\nvar i;\nvar j=0;\n\nfor (i=0; i<site_number;i++)\n{\n    if (site_name==rawdata[i].sitename)\n    {\n      site_array[j]=rawdata[i];\n      j=j+1;\n    }\n}\n\nmsg.payload=site_array;\n \nflow.set(\"uvi_getdata\",site_array);\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":930,"y":180,"wires":[["3d6bf7736e070f9b","438df72945fd2c90"]]},{"id":"438df72945fd2c90","type":"debug","z":"8dd731973f8200b5","name":"debug 245","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1130,"y":260,"wires":[]},{"id":"38f8f1c1d386a910","type":"ui_group","name":"Default","tab":"eeb8e179.a47a4","order":2,"disp":true,"width":22,"collapse":false,"className":""},{"id":"eeb8e179.a47a4","type":"ui_tab","name":"2024 PM2.5 UVI","icon":"dashboard","disabled":false,"hidden":false}]

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...