2022年10月2日 星期日

Node-Red 一般天氣預報-今明36小時天氣預報

 


flow.set("temp",msg.payload);

var rawdata = flow.get("temp");

 

var citynumber=rawdata.cwbopendata.dataset.location.length;

var records_array=[];

var i;

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

{

    records_array[i]=rawdata.cwbopendata.dataset.location[i]

}

 

msg.payload=records_array;

flow.set("RECORDS",records_array);

return msg;



var i= msg.payload;

var rawdata = flow.get("RECORDS");

// flow.set("RECORDS",records_array);

 

var citynumber=rawdata.length;

var Loaction;

var Stime;

var W1status, W3status , W4status;

var MAXTemp , MINTemp;

var pay1 , pay2 ;

 

 

Loaction=rawdata[i].locationName;

Stime=rawdata[i].weatherElement[0].time[0].startTime;

W1status=rawdata[i].weatherElement[0].time[0].parameter.parameterName;

MAXTemp=rawdata[i].weatherElement[1].time[0].parameter.parameterName;

MINTemp=rawdata[i].weatherElement[2].time[0].parameter.parameterName;

W3status=rawdata[i].weatherElement[3].time[2].parameter.parameterName;

W4status=rawdata[i].weatherElement[4].time[2].parameter.parameterName;

 

//pay1 = '地點=' + Loaction + '發佈時間' + Stime ' 天氣狀況 : ' + W1status ;

//pay2 = '最高溫度='+MAXTemp+'°C , 最低溫度='+ MINTemp+'°C , '+ W3status +'降雨機率 = '+  W4status + '%';

 

msg.payload='地點=' +Loaction + ', 發佈時間:' + Stime + ',天氣狀況 = ' + W1status + ', 最高溫度='+MAXTemp+'°C , 最低溫度='+ MINTemp+'°C , '+ W3status +', 降雨機率='+  W4status +'%';

//msg.payload = {pay1 , pay2};

return msg;

 



擷取 前12小時 














[{"id":"3e91dcdcddcaf617","type":"http request","z":"493ca6fe02fe9f18","name":"temp request","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://opendata.cwb.gov.tw/fileapi/v1/opendataapi/F-C0032-001?Authorization=CWB-40C25FFF-1224-4250-B9D9-3735AAE17DBF&downloadType=WEB&format=JSON","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":250,"y":100,"wires":[["45017166c0a3135c","e0d426f793cdc6b4"]]},{"id":"c3814ded3a17e952","type":"inject","z":"493ca6fe02fe9f18","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"600","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"","payloadType":"date","x":100,"y":100,"wires":[["3e91dcdcddcaf617"]]},{"id":"45017166c0a3135c","type":"debug","z":"493ca6fe02fe9f18","name":"temp request result","active":true,"console":"false","complete":"payload","x":450,"y":100,"wires":[]},{"id":"e0d426f793cdc6b4","type":"function","z":"493ca6fe02fe9f18","name":"set global temp","func":"\nflow.set(\"temp\",msg.payload);\nvar rawdata = flow.get(\"temp\");\n\nvar citynumber=rawdata.cwbopendata.dataset.location.length;\nvar records_array=[];\nvar i;\nfor (i=0; i<citynumber;i++)\n{\n    records_array[i]=rawdata.cwbopendata.dataset.location[i]\n}\n\nmsg.payload=records_array;\nflow.set(\"RECORDS\",records_array);\nreturn msg;\n\n//flow.set('YourVariable', value);    // to store a variable (YourVariable)\n//var x = flow.get('YourVariable'); //to retrieve a variable (YourVariable)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":160,"wires":[["7b7788f7de286c4f","3422d90aba7db27d"]]},{"id":"eb7087364c6de216","type":"debug","z":"493ca6fe02fe9f18","name":"selected city ","active":true,"tosidebar":true,"console":false,"complete":"payload","statusVal":"","statusType":"auto","x":510,"y":220,"wires":[]},{"id":"a51c43faf36b295b","type":"ui_text","z":"493ca6fe02fe9f18","group":"11aebcbd.bc7383","order":0,"width":"12","height":"2","name":"","label":"","format":"{{msg.payload}}","layout":"row-center","className":"","x":550,"y":300,"wires":[]},{"id":"7b7788f7de286c4f","type":"debug","z":"493ca6fe02fe9f18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":160,"wires":[]},{"id":"db65853db3e9d728","type":"function","z":"493ca6fe02fe9f18","name":"city  number to name ","func":"var i= msg.payload;\nvar rawdata = flow.get(\"RECORDS\");\n// flow.set(\"RECORDS\",records_array);\n\nvar citynumber=rawdata.length;\nvar Loaction;\nvar Stime;\nvar W1status, W3status , W4status;\nvar MAXTemp , MINTemp;\nvar pay1 , pay2 ;\n\n\nLoaction=rawdata[i].locationName;\nStime=rawdata[i].weatherElement[0].time[0].startTime;\nW1status=rawdata[i].weatherElement[0].time[0].parameter.parameterName;\nMAXTemp=rawdata[i].weatherElement[1].time[0].parameter.parameterName;\nMINTemp=rawdata[i].weatherElement[2].time[0].parameter.parameterName;\nW3status=rawdata[i].weatherElement[3].time[2].parameter.parameterName;\nW4status=rawdata[i].weatherElement[4].time[2].parameter.parameterName;\n\n//pay1 = '地點=' + Loaction + '發佈時間' + Stime ' 天氣狀況 : ' + W1status ;\n//pay2 = '最高溫度='+MAXTemp+'°C , 最低溫度='+ MINTemp+'°C , '+ W3status +'降雨機率 = '+  W4status + '%';\n\nmsg.payload='地點=' +Loaction + ', 發佈時間:' + Stime + ',天氣狀況 = ' + W1status + ', 最高溫度='+MAXTemp+'°C , 最低溫度='+ MINTemp+'°C , '+ W3status +', 降雨機率='+  W4status +'%';\n//msg.payload = {pay1 , pay2};\nreturn msg;\n\n\n\n\n\n\n\n\n/*\n//context.global.locationnumber=0;\nflow.set(\"locationnumber\",0);\ntry {\n   cityname=msg.payload;\n   \n   if(cityname.indexOf('台北市市')!=-1 ||cityname.indexOf('臺北市')!=-1 )\n{\n    //context.global.locationnumber=0;\n    flow.set(\"locationnumber\",0);\n   \n}\nelse if(cityname.indexOf('新北市')!=-1)\n{\n    //context.global.locationnumber=1;\n    flow.set(\"locationnumber\",1);\n}\nelse if(cityname.indexOf('桃園市')!=-1) //\n{\n    //context.global.locationnumber=2;\n    flow.set(\"locationnumber\",2);\n}\nelse if(cityname.indexOf('臺中市')!=-1 || cityname.indexOf('台中市')!=-1) \n{\n    //context.global.locationnumber= 3;\n    flow.set(\"locationnumber\",3);\n}\nelse if(cityname.indexOf('臺南市')!=-1 || cityname.indexOf('台南市')!=-1) \n{\n    //context.global.locationnumber= 4;\n    flow.set(\"locationnumber\",4);\n   \n}\nelse if(cityname.indexOf('高雄市')!=-1 ) \n{\n    //context.global.locationnumber= 5 ;\n    flow.set(\"locationnumber\",5);\n   \n}\nelse if(cityname.indexOf('基隆市')!=-1 ) \n{\n    //context.global.locationnumber= 6 ;\n    flow.set(\"locationnumber\",6);\n}\nelse if(cityname.indexOf('新竹縣')!=-1 ) \n{\n    //context.global.locationnumber= 7 ;\n    flow.set(\"locationnumber\",7);\n}\nelse if(cityname.indexOf('新竹市')!=-1 ) \n{\n    //context.global.locationnumber= 8 ;\n    flow.set(\"locationnumber\",8);\n}\nelse if(cityname.indexOf('苗栗縣')!=-1 ) \n{\n    //context.global.locationnumber= 9 ;\n    flow.set(\"locationnumber\",9);\n}\nelse if(cityname.indexOf('彰化縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",10);\n}\nelse if(cityname.indexOf('南投縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",11);\n}\nelse if(cityname.indexOf('雲林縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",12);\n}\nelse if(cityname.indexOf('嘉義縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",13);\n}\nelse if(cityname.indexOf('嘉義市')!=-1 ) \n{\n      flow.set(\"locationnumber\",14);\n}\nelse if(cityname.indexOf('屏東縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",15);\n}\nelse if(cityname.indexOf('宜蘭縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",16);\n}\nelse if(cityname.indexOf('花蓮縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",17);\n}\nelse if(cityname.indexOf('臺東縣')!=-1 || cityname.indexOf('台東縣')!=-1)\n{\n      flow.set(\"locationnumber\",18);\n}\nelse if(cityname.indexOf('澎湖縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",19);\n}\nelse if(cityname.indexOf('金門縣')!=-1 ) \n{\n      flow.set(\"locationnumber\",20);\n}\nelse if(cityname.indexOf('連江')!=-1 ) \n{\n    //context.global.locationnumber= 21 ;\n    flow.set(\"locationnumber\",21);\n}\nelse  \n{\n   // context.global.locationnumber= 0 ;\n    flow.set(\"locationnumber\",0);\n}\n   \n   \n}\ncatch(err) {\n    cityname=\"台北市\";\n    //context.global.locationnumber=0;\n    flow.set(\"locationnumber\",0);\n}\n\nmsg.payload= flow.get(\"locationnumber\");\nreturn msg;\n*/","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":300,"wires":[["1ffc7b20b16f8f9d","a51c43faf36b295b"]]},{"id":"1ffc7b20b16f8f9d","type":"debug","z":"493ca6fe02fe9f18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":470,"y":360,"wires":[]},{"id":"3422d90aba7db27d","type":"ui_dropdown","z":"493ca6fe02fe9f18","name":"","label":"選擇城市","tooltip":"","place":"Select city","group":"11aebcbd.bc7383","order":0,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"臺北市","value":0,"type":"num"},{"label":"新北市","value":1,"type":"num"},{"label":"桃園市","value":2,"type":"num"},{"label":"臺中市","value":3,"type":"num"},{"label":"臺南市","value":4,"type":"num"},{"label":"高雄市","value":5,"type":"num"},{"label":"基隆市","value":6,"type":"num"},{"label":"新竹縣","value":7,"type":"num"},{"label":"新竹市","value":8,"type":"num"},{"label":"苗栗縣","value":9,"type":"num"},{"label":"彰化縣","value":10,"type":"num"},{"label":"南投縣","value":11,"type":"num"},{"label":"雲林縣","value":12,"type":"num"},{"label":"嘉義縣","value":13,"type":"num"},{"label":"嘉義市","value":14,"type":"num"},{"label":"屏東縣","value":15,"type":"num"},{"label":"宜蘭縣","value":16,"type":"num"},{"label":"花蓮縣","value":17,"type":"num"},{"label":"臺東縣","value":18,"type":"num"},{"label":"澎湖縣","value":19,"type":"num"},{"label":"金門縣","value":20,"type":"num"},{"label":"連江縣","value":21,"type":"num"}],"payload":"","topic":"","topicType":"str","className":"","x":300,"y":220,"wires":[["db65853db3e9d728","eb7087364c6de216"]]},{"id":"6bba00efa406496f","type":"http response","z":"493ca6fe02fe9f18","name":"http out","x":480,"y":460,"wires":[]},{"id":"2016f4697957981a","type":"debug","z":"493ca6fe02fe9f18","name":"temp data process","active":true,"console":"false","complete":"payload","x":490,"y":500,"wires":[]},{"id":"fc125a4d24e03c12","type":"function","z":"493ca6fe02fe9f18","name":"show raw data","func":"\nvar rawdata = flow.get(\"RECORDS\");\nmsg.payload = rawdata;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":460,"wires":[["6bba00efa406496f","2016f4697957981a"]]},{"id":"3e9ce5fa166a1b11","type":"http in","z":"493ca6fe02fe9f18","name":"tempjson","url":"/tempjson","method":"get","upload":false,"swaggerDoc":"","x":100,"y":460,"wires":[["fc125a4d24e03c12"]]},{"id":"11aebcbd.bc7383","type":"ui_group","name":"City temperature","tab":"dc7935b7.cd8c68","order":1,"disp":true,"width":"12","collapse":false,"className":""},{"id":"dc7935b7.cd8c68","type":"ui_tab","name":"Home","icon":"dashboard"}]

沒有留言:

張貼留言

Messaging API作為替代方案

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