2023年10月9日 星期一

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

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

https://opendata.cwa.gov.tw/dataset/all/F-C0032-001


 註冊 登入

取得 API授權碼
資料主題 :  搜尋 今明36小時天氣預報 -->API

中央氣象署開放資料平臺之資料擷取API


執行 -->拷貝 網址
  

如下 (被修改過了錯誤)

https://opendata.cwa.gov.tw/api/v1/rest/datastore/F-C0032-001?Authorization=CWB-40C25FFF-1224-4250-B9D9-3735AAE17DBF








 

function  (location  data)
============================
var a= msg.payload.cwaopendata.dataset.location;
msg.payload=a;
return msg;

set global temp
===================

global.set("tempera",msg.payload); //global. Set
return msg;

show raw data
===================
var rawdata = global.get("tempera"); //global.get
msg.payload=rawdata;
return msg;


List locationname
===================
var rawdata = global.get("tempera");
var cityarray=[];

for (let i=0; i<rawdata.length;i++)
{
    cityarray[i]=rawdata[i].locationName;
}

msg.payload=cityarray;

global.set("loname",cityarray);

return msg;

city name to number
===================
global.set("locationnumber",0);
try {
    cityname=msg.payload.cityname;
   
   if(cityname.indexOf('台北市')!=-1 ||cityname.indexOf('臺北市')!=-1 )
    {
        //context.global.locationnumber=0;
        global.set("locationnumber",0);
   
    }
    else if(cityname.indexOf('新北市')!=-1)
    {
        //context.global.locationnumber=1;
        global.set("locationnumber",1);
    }
    else if(cityname.indexOf('桃園市')!=-1) //
    {
        //context.global.locationnumber=2;
        global.set("locationnumber",2);
    }
    else if(cityname.indexOf('臺中市')!=-1 || cityname.indexOf('台中市')!=-1) 
    {
        //context.global.locationnumber= 3;
        global.set("locationnumber",3);
    }
    else if(cityname.indexOf('臺南市')!=-1 || cityname.indexOf('台南市')!=-1) 
    {
        //context.global.locationnumber= 4;
        global.set("locationnumber",4);
   
    }
    else if(cityname.indexOf('高雄市')!=-1 ) 
    {
        //context.global.locationnumber= 5 ;
        global.set("locationnumber",5);
   
    }
    else if(cityname.indexOf('基隆市')!=-1 ) 
    {
        //context.global.locationnumber= 6 ;
        flow.set("locationnumber",6);
    }
    else if(cityname.indexOf('新竹縣')!=-1 ) 
    {
        //context.global.locationnumber= 7 ;
        global.set("locationnumber",7);
    }
    else if(cityname.indexOf('新竹市')!=-1 ) 
    {
        //context.global.locationnumber= 8 ;
        global.set("locationnumber",8);
    }
    else if(cityname.indexOf('苗栗縣')!=-1 ) 
    {
        //context.global.locationnumber= 9 ;
        global.set("locationnumber",9);
    }
    else if(cityname.indexOf('彰化縣')!=-1 ) 
    {
        global.set("locationnumber",10);
    }
    else if(cityname.indexOf('南投縣')!=-1 ) 
    {
        global.set("locationnumber",11);
    }
    else if(cityname.indexOf('雲林縣')!=-1 ) 
    {
        global.set("locationnumber",12);
    }
    else if(cityname.indexOf('嘉義縣')!=-1 ) 
    {
        global.set("locationnumber",13);
    }
    else if(cityname.indexOf('嘉義市')!=-1 ) 
    {
        global.set("locationnumber",14);
    }
    else if(cityname.indexOf('屏東縣')!=-1 ) 
    {
        global.set("locationnumber",15);
    }
    else if(cityname.indexOf('宜蘭縣')!=-1 ) 
    {
        global.set("locationnumber",16);
    }
    else if(cityname.indexOf('花蓮縣')!=-1 ) 
    {
        global.set("locationnumber",17);
    }
    else if(cityname.indexOf('臺東縣')!=-1 || cityname.indexOf('台東市')!=-1)
    {
        global.set("locationnumber",18);
    }
    else if(cityname.indexOf('澎湖縣')!=-1 ) 
    {
        global.set("locationnumber",19);
    }
    else if(cityname.indexOf('金門縣')!=-1 ) 
    {
        global.set("locationnumber",20);
    }
    else if(cityname.indexOf('連江縣')!=-1 ) 
    {
        //context.global.locationnumber= 21 ;
        global.set("locationnumber",21);
    }
    else  
    {
        // context.global.locationnumber= 0 ;
        global.set("locationnumber",0);
    }
   
   
    }

catch(err) {
    cityname="台北市";
    //context.global.locationnumber=0;
    global.set("locationnumber",0);
}

msg.payload= global.get("locationnumber");
return msg;

取得 max min 溫度
===================
var loc=msg.payload;
var rawdata = global.get("tempera");

var locationName=rawdata[loc].locationName;
var period0max=rawdata[loc].weatherElement[1].time[0].parameter.parameterName;
var period0min=rawdata[loc].weatherElement[2].time[0].parameter.parameterName;
var period0start=rawdata[loc].weatherElement[1].time[0].startTime;
var period0end=rawdata[loc].weatherElement[1].time[0].endTime;

var starthourtemp= period0start.split("T");
var starthour=starthourtemp[1].split(":")[0];
var endhourtemp= period0end.split("T");
var endhour=endhourtemp[1].split(":")[0];

msg.payload={"locationName":locationName,"time":starthour+"-"+endhour, "max":period0max,
"min":period0min};
return msg;

set options
===========================
var cityname = global.get("loname");

var cityarray=[];

for (let i=0; i<cityname.length;i++)
{
    cityarray[i]=cityname[i];

}
msg.options=cityarray;
return msg;



city name to number 2
==============================
global. Set("locationnumber",0);
try {
    cityname=msg.payload;
   
   if(cityname.indexOf('台北市')!=-1 ||cityname.indexOf('臺北市')!=-1 )
    {
        //context.global.locationnumber=0;
        global.set("locationnumber",0);
   
    }
    else if(cityname.indexOf('新北市')!=-1)
    {
        //context.global.locationnumber=1;
        global.set("locationnumber",1);
    }
    else if(cityname.indexOf('桃園市')!=-1) //
    {
        //context.global.locationnumber=2;
        global.set("locationnumber",2);
    }
    else if(cityname.indexOf('臺中市')!=-1 || cityname.indexOf('台中市')!=-1) 
    {
        //context.global.locationnumber= 3;
        global.set("locationnumber",3);
    }
    else if(cityname.indexOf('臺南市')!=-1 || cityname.indexOf('台南市')!=-1) 
    {
        //context.global.locationnumber= 4;
        global.set("locationnumber",4);
   
    }
    else if(cityname.indexOf('高雄市')!=-1 ) 
    {
        //context.global.locationnumber= 5 ;
        global.set("locationnumber",5);
   
    }
    else if(cityname.indexOf('基隆市')!=-1 ) 
    {
        //context.global.locationnumber= 6 ;
        flow.set("locationnumber",6);
    }
    else if(cityname.indexOf('新竹縣')!=-1 ) 
    {
        //context.global.locationnumber= 7 ;
        global.set("locationnumber",7);
    }
    else if(cityname.indexOf('新竹市')!=-1 ) 
    {
        //context.global.locationnumber= 8 ;
        global.set("locationnumber",8);
    }
    else if(cityname.indexOf('苗栗縣')!=-1 ) 
    {
        //context.global.locationnumber= 9 ;
        global.set("locationnumber",9);
    }
    else if(cityname.indexOf('彰化縣')!=-1 ) 
    {
        global.set("locationnumber",10);
    }
    else if(cityname.indexOf('南投縣')!=-1 ) 
    {
        global.set("locationnumber",11);
    }
    else if(cityname.indexOf('雲林縣')!=-1 ) 
    {
        global.set("locationnumber",12);
    }
    else if(cityname.indexOf('嘉義縣')!=-1 ) 
    {
        global.set("locationnumber",13);
    }
    else if(cityname.indexOf('嘉義市')!=-1 ) 
    {
        global.set("locationnumber",14);
    }
    else if(cityname.indexOf('屏東縣')!=-1 ) 
    {
        global.set("locationnumber",15);
    }
    else if(cityname.indexOf('宜蘭縣')!=-1 ) 
    {
        global.set("locationnumber",16);
    }
    else if(cityname.indexOf('花蓮縣')!=-1 ) 
    {
        global.set("locationnumber",17);
    }
    else if(cityname.indexOf('臺東縣')!=-1 || cityname.indexOf('台東市')!=-1)
    {
        global.set("locationnumber",18);
    }
    else if(cityname.indexOf('澎湖縣')!=-1 ) 
    {
        global.set("locationnumber",19);
    }
    else if(cityname.indexOf('金門縣')!=-1 ) 
    {
        global.set("locationnumber",20);
    }
    else if(cityname.indexOf('連江縣')!=-1 ) 
    {
        //context.global.locationnumber= 21 ;
        global.set("locationnumber",21);
    }
    else  
    {
        // context.global.locationnumber= 0 ;
        global.set("locationnumber",0);
    }
   
   
    }

catch(err) {
    cityname="台北市";
    //context.global.locationnumber=0;
    global.set("locationnumber",0);
}

msg.payload= global.get("locationnumber");
return msg;



report
===============================
var loc=msg.payload;
var rawdata = global.get("tempera");

var locationName=rawdata[loc].locationName;
var period0max=rawdata[loc].weatherElement[1].time[0].parameter.parameterName;
var period0min=rawdata[loc].weatherElement[2].time[0].parameter.parameterName;
var period0start=rawdata[loc].weatherElement[1].time[0].startTime;
var period0end=rawdata[loc].weatherElement[1].time[0].endTime;

var starthourtemp= period0start.split("T");
var starthour=starthourtemp[1].split(":")[0];
var endhourtemp= period0end.split("T");
var endhour=endhourtemp[1].split(":")[0];

//msg.payload={"locationName":locationName,"time":starthour+"-"+endhour, "max":period0max,
//"min":period0min};
//return msg;

msg.payload=locationName+'-->' + starthour+"點到"+endhour+"點 溫度最低:"+period0min+"度 , 最高"+period0max+"度";
return msg;





(被修改過了錯誤)

[{"id":"b980f83e7e252a26","type":"http request","z":"d7693fd56d8b2efc","name":"temp request","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://opendata.cwb.gov.tw/fileapi/v1/opendataapi/F-C0032-001?Authorization=CWB-42C25FAF-1124-4279-BAD9-3735AAE27DDF&downloadType=WEB&format=JSON","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":230,"y":40,"wires":[["9de1016334d8623c","26ebff77b2828b4a"]]},{"id":"4bb96a171569d0f9","type":"inject","z":"d7693fd56d8b2efc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"7200","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"","payloadType":"date","x":90,"y":40,"wires":[["b980f83e7e252a26"]]},{"id":"9de1016334d8623c","type":"debug","z":"d7693fd56d8b2efc","name":"temp request result","active":true,"console":"false","complete":"payload","x":450,"y":20,"wires":[]},{"id":"3cd5cb269957a36a","type":"debug","z":"d7693fd56d8b2efc","name":"xml to json","active":true,"console":"false","complete":"payload","x":570,"y":60,"wires":[]},{"id":"bb13b23202d4439b","type":"function","z":"d7693fd56d8b2efc","name":"set global temp","func":"\nglobal.set(\"tempera\",msg.payload); //global.set\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":"","x":580,"y":100,"wires":[["bf512f83c4d093e0"]]},{"id":"7acc072c5b4a8d5b","type":"http in","z":"d7693fd56d8b2efc","name":"city?cityname=台中市","url":"/city","method":"get","upload":false,"swaggerDoc":"","x":120,"y":320,"wires":[["73054b4771d188f2","d03c603ca6be3d38"]]},{"id":"31fb20cd0f456310","type":"http response","z":"d7693fd56d8b2efc","name":"http out","x":720,"y":320,"wires":[]},{"id":"73054b4771d188f2","type":"function","z":"d7693fd56d8b2efc","name":"city name to number","func":"global.set(\"locationnumber\",0);\ntry {\n    cityname=msg.payload.cityname;\n   \n   if(cityname.indexOf('台北市')!=-1 ||cityname.indexOf('臺北市')!=-1 )\n    {\n        //context.global.locationnumber=0;\n        global.set(\"locationnumber\",0);\n   \n    }\n    else if(cityname.indexOf('新北市')!=-1)\n    {\n        //context.global.locationnumber=1;\n        global.set(\"locationnumber\",1);\n    }\n    else if(cityname.indexOf('桃園市')!=-1) //\n    {\n        //context.global.locationnumber=2;\n        global.set(\"locationnumber\",2);\n    }\n    else if(cityname.indexOf('臺中市')!=-1 || cityname.indexOf('台中市')!=-1) \n    {\n        //context.global.locationnumber= 3;\n        global.set(\"locationnumber\",3);\n    }\n    else if(cityname.indexOf('臺南市')!=-1 || cityname.indexOf('台南市')!=-1) \n    {\n        //context.global.locationnumber= 4;\n        global.set(\"locationnumber\",4);\n   \n    }\n    else if(cityname.indexOf('高雄市')!=-1 ) \n    {\n        //context.global.locationnumber= 5 ;\n        global.set(\"locationnumber\",5);\n   \n    }\n    else if(cityname.indexOf('基隆市')!=-1 ) \n    {\n        //context.global.locationnumber= 6 ;\n        flow.set(\"locationnumber\",6);\n    }\n    else if(cityname.indexOf('新竹縣')!=-1 ) \n    {\n        //context.global.locationnumber= 7 ;\n        global.set(\"locationnumber\",7);\n    }\n    else if(cityname.indexOf('新竹市')!=-1 ) \n    {\n        //context.global.locationnumber= 8 ;\n        global.set(\"locationnumber\",8);\n    }\n    else if(cityname.indexOf('苗栗縣')!=-1 ) \n    {\n        //context.global.locationnumber= 9 ;\n        global.set(\"locationnumber\",9);\n    }\n    else if(cityname.indexOf('彰化縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",10);\n    }\n    else if(cityname.indexOf('南投縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",11);\n    }\n    else if(cityname.indexOf('雲林縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",12);\n    }\n    else if(cityname.indexOf('嘉義縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",13);\n    }\n    else if(cityname.indexOf('嘉義市')!=-1 ) \n    {\n        global.set(\"locationnumber\",14);\n    }\n    else if(cityname.indexOf('屏東縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",15);\n    }\n    else if(cityname.indexOf('宜蘭縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",16);\n    }\n    else if(cityname.indexOf('花蓮縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",17);\n    }\n    else if(cityname.indexOf('臺東縣')!=-1 || cityname.indexOf('台東市')!=-1)\n    {\n        global.set(\"locationnumber\",18);\n    }\n    else if(cityname.indexOf('澎湖縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",19);\n    }\n    else if(cityname.indexOf('金門縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",20);\n    }\n    else if(cityname.indexOf('連江縣')!=-1 ) \n    {\n        //context.global.locationnumber= 21 ;\n        global.set(\"locationnumber\",21);\n    }\n    else  \n    {\n        // context.global.locationnumber= 0 ;\n        global.set(\"locationnumber\",0);\n    }\n   \n   \n    }\n\ncatch(err) {\n    cityname=\"台北市\";\n    //context.global.locationnumber=0;\n    global.set(\"locationnumber\",0);\n}\n\nmsg.payload= global.get(\"locationnumber\");\nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":320,"wires":[["1491002b1d477b16","468d7901f9c1ed09"]]},{"id":"1491002b1d477b16","type":"debug","z":"d7693fd56d8b2efc","name":"city name to number result","active":true,"console":"false","complete":"payload","x":560,"y":280,"wires":[]},{"id":"c70236eb819e3239","type":"http in","z":"d7693fd56d8b2efc","name":"tempjson","url":"/tempjson","method":"get","upload":false,"swaggerDoc":"","x":80,"y":140,"wires":[["f9b8c0b903a70eca","53849aa8331f5ebd"]]},{"id":"51c1b494c061e296","type":"http response","z":"d7693fd56d8b2efc","name":"http out","x":420,"y":140,"wires":[]},{"id":"f9b8c0b903a70eca","type":"function","z":"d7693fd56d8b2efc","name":"show raw data","func":"\nvar rawdata = global.get(\"tempera\"); //global.get\nmsg.payload=rawdata;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":140,"wires":[["51c1b494c061e296","d49c24996ccca256"]]},{"id":"d49c24996ccca256","type":"debug","z":"d7693fd56d8b2efc","name":"temp data process","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":160,"wires":[]},{"id":"f06206d380ceea1a","type":"function","z":"d7693fd56d8b2efc","name":"List locationname","func":"var rawdata = global.get(\"tempera\");\nvar cityarray=[];\n\nfor (let i=0; i<rawdata.length;i++)\n{\n    cityarray[i]=rawdata[i].locationName;\n}\n\nmsg.payload=cityarray;\n\nglobal.set(\"loname\",cityarray);\n\nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":240,"wires":[["9f42ecd51f3bfbec"]]},{"id":"9f42ecd51f3bfbec","type":"debug","z":"d7693fd56d8b2efc","name":"List all locationnames ","active":true,"tosidebar":true,"console":false,"complete":"payload","statusVal":"","statusType":"auto","x":480,"y":240,"wires":[]},{"id":"468d7901f9c1ed09","type":"function","z":"d7693fd56d8b2efc","name":"取得 max min 溫度","func":"var loc=msg.payload;\nvar rawdata = global.get(\"tempera\");\n\nvar locationName=rawdata[loc].locationName;\nvar period0max=rawdata[loc].weatherElement[1].time[0].parameter.parameterName;\nvar period0min=rawdata[loc].weatherElement[2].time[0].parameter.parameterName;\nvar period0start=rawdata[loc].weatherElement[1].time[0].startTime;\nvar period0end=rawdata[loc].weatherElement[1].time[0].endTime;\n\nvar starthourtemp= period0start.split(\"T\");\nvar starthour=starthourtemp[1].split(\":\")[0];\nvar endhourtemp= period0end.split(\"T\");\nvar endhour=endhourtemp[1].split(\":\")[0];\n\nmsg.payload={\"locationName\":locationName,\"time\":starthour+\"-\"+endhour, \"max\":period0max,\n\"min\":period0min};\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":320,"wires":[["31fb20cd0f456310"]]},{"id":"425d95d9d66b86e5","type":"debug","z":"d7693fd56d8b2efc","name":"report result","active":true,"console":"false","complete":"payload","x":650,"y":460,"wires":[]},{"id":"7f602fb2fc83ea5d","type":"inject","z":"d7693fd56d8b2efc","name":"inject city options","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"7200","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"","payloadType":"date","x":110,"y":400,"wires":[["0ce94b043103237f"]]},{"id":"0ce94b043103237f","type":"function","z":"d7693fd56d8b2efc","name":"set options","func":"var cityname = global.get(\"loname\");\n\nvar cityarray=[];\n\nfor (let i=0; i<cityname.length;i++)\n{\n    cityarray[i]=cityname[i];\n\n}\nmsg.options=cityarray;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":400,"wires":[["121c9ef10b189beb","7075347cd78180f5"]]},{"id":"f3ea573988ad08cd","type":"inject","z":"d7693fd56d8b2efc","name":"inject city","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"3","topic":"","payload":"","payloadType":"date","x":100,"y":240,"wires":[["f06206d380ceea1a"]]},{"id":"d5b2479b4db9628e","type":"function","z":"d7693fd56d8b2efc","name":"city to number","func":"\nvar cityname;\ncontext.global.locationnumber=0;\ntry {\n   cityname=msg.payload;\n   \n   if(cityname.indexOf('台北')!=-1 ||cityname.indexOf('臺北')!=-1 )\n{\n    context.global.locationnumber=0;\n   \n}\nelse if(cityname.indexOf('新北')!=-1)\n{\n    context.global.locationnumber=1;\n   \n}\nelse if(cityname.indexOf('桃園')!=-1) //\n{\n    context.global.locationnumber=2;\n   \n}\nelse if(cityname.indexOf('臺中')!=-1 || cityname.indexOf('台中')!=-1) \n{\n    context.global.locationnumber= 3;\n   \n}\nelse if(cityname.indexOf('臺南')!=-1 || cityname.indexOf('台南')!=-1) \n{\n    context.global.locationnumber= 4;\n   \n}\nelse if(cityname.indexOf('高雄')!=-1 ) \n{\n    context.global.locationnumber= 5 ;\n   \n}\nelse if(cityname.indexOf('基隆')!=-1 ) \n{\n    context.global.locationnumber= 6 ;\n   \n}\nelse if(cityname.indexOf('新竹縣')!=-1 ) \n{\n    context.global.locationnumber= 7 ;\n   \n}\nelse if(cityname.indexOf('新竹')!=-1 ) \n{\n    context.global.locationnumber= 8 ;\n   \n}\nelse if(cityname.indexOf('連江')!=-1 ) \n{\n    context.global.locationnumber= 21 ;\n   \n}\nelse  \n{\n    context.global.locationnumber= 0 ;\n   \n}\n   \n   \n}\ncatch(err) {\n    cityname=\"台北\";\n    context.global.locationnumber=0;\n}\n\n\n msg.payload= context.global.locationnumber;\nreturn msg;\n","outputs":1,"noerr":0,"x":300,"y":600,"wires":[[]]},{"id":"a194220ee277441f","type":"debug","z":"d7693fd56d8b2efc","name":"selected city ","active":true,"tosidebar":true,"console":false,"complete":"payload","statusVal":"","statusType":"auto","x":650,"y":400,"wires":[]},{"id":"121c9ef10b189beb","type":"ui_dropdown","z":"d7693fd56d8b2efc","name":"","label":"city options","tooltip":"","place":"Select city","group":"11aebcbd.bc7383","order":0,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"","topicType":"str","className":"","x":470,"y":400,"wires":[["a194220ee277441f","2f6ebe1b1bf47f35"]]},{"id":"ab1fad20da79e263","type":"ui_text","z":"d7693fd56d8b2efc","group":"11aebcbd.bc7383","order":0,"width":0,"height":0,"name":"","label":"","format":"{{msg.payload}}","layout":"row-center","className":"","x":630,"y":500,"wires":[]},{"id":"bf512f83c4d093e0","type":"debug","z":"d7693fd56d8b2efc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":100,"wires":[]},{"id":"7075347cd78180f5","type":"debug","z":"d7693fd56d8b2efc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"options","targetType":"msg","statusVal":"","statusType":"auto","x":470,"y":360,"wires":[]},{"id":"26ebff77b2828b4a","type":"function","z":"d7693fd56d8b2efc","name":"function  (location  data)","func":"var a= msg.payload.cwaopendata.dataset.location;\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":100,"wires":[["bb13b23202d4439b","3cd5cb269957a36a"]]},{"id":"53849aa8331f5ebd","type":"function","z":"d7693fd56d8b2efc","name":"show raw data","func":"\nvar rawdata = global.get(\"tempera\"); //global.get\nmsg.payload=rawdata[1].locationName;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":200,"wires":[["60ee24555bcce7b6"]]},{"id":"60ee24555bcce7b6","type":"debug","z":"d7693fd56d8b2efc","name":"debug 209","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":200,"wires":[]},{"id":"d03c603ca6be3d38","type":"debug","z":"d7693fd56d8b2efc","name":"debug 210","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.cityname","targetType":"msg","statusVal":"","statusType":"auto","x":290,"y":280,"wires":[]},{"id":"55631d8ecaa2eb98","type":"comment","z":"d7693fd56d8b2efc","name":"","info":"http://127.0.0.1:1880/city?cityname=連江縣\n\n{\"locationName\":\"連江縣\",\"time\":\"12-18\",\"max\":\"23\",\"min\":\"22\"}","x":70,"y":280,"wires":[]},{"id":"2f6ebe1b1bf47f35","type":"function","z":"d7693fd56d8b2efc","name":"city name to number","func":"global.set(\"locationnumber\",0);\ntry {\n    cityname=msg.payload;\n   \n   if(cityname.indexOf('台北市')!=-1 ||cityname.indexOf('臺北市')!=-1 )\n    {\n        //context.global.locationnumber=0;\n        global.set(\"locationnumber\",0);\n   \n    }\n    else if(cityname.indexOf('新北市')!=-1)\n    {\n        //context.global.locationnumber=1;\n        global.set(\"locationnumber\",1);\n    }\n    else if(cityname.indexOf('桃園市')!=-1) //\n    {\n        //context.global.locationnumber=2;\n        global.set(\"locationnumber\",2);\n    }\n    else if(cityname.indexOf('臺中市')!=-1 || cityname.indexOf('台中市')!=-1) \n    {\n        //context.global.locationnumber= 3;\n        global.set(\"locationnumber\",3);\n    }\n    else if(cityname.indexOf('臺南市')!=-1 || cityname.indexOf('台南市')!=-1) \n    {\n        //context.global.locationnumber= 4;\n        global.set(\"locationnumber\",4);\n   \n    }\n    else if(cityname.indexOf('高雄市')!=-1 ) \n    {\n        //context.global.locationnumber= 5 ;\n        global.set(\"locationnumber\",5);\n   \n    }\n    else if(cityname.indexOf('基隆市')!=-1 ) \n    {\n        //context.global.locationnumber= 6 ;\n        flow.set(\"locationnumber\",6);\n    }\n    else if(cityname.indexOf('新竹縣')!=-1 ) \n    {\n        //context.global.locationnumber= 7 ;\n        global.set(\"locationnumber\",7);\n    }\n    else if(cityname.indexOf('新竹市')!=-1 ) \n    {\n        //context.global.locationnumber= 8 ;\n        global.set(\"locationnumber\",8);\n    }\n    else if(cityname.indexOf('苗栗縣')!=-1 ) \n    {\n        //context.global.locationnumber= 9 ;\n        global.set(\"locationnumber\",9);\n    }\n    else if(cityname.indexOf('彰化縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",10);\n    }\n    else if(cityname.indexOf('南投縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",11);\n    }\n    else if(cityname.indexOf('雲林縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",12);\n    }\n    else if(cityname.indexOf('嘉義縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",13);\n    }\n    else if(cityname.indexOf('嘉義市')!=-1 ) \n    {\n        global.set(\"locationnumber\",14);\n    }\n    else if(cityname.indexOf('屏東縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",15);\n    }\n    else if(cityname.indexOf('宜蘭縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",16);\n    }\n    else if(cityname.indexOf('花蓮縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",17);\n    }\n    else if(cityname.indexOf('臺東縣')!=-1 || cityname.indexOf('台東市')!=-1)\n    {\n        global.set(\"locationnumber\",18);\n    }\n    else if(cityname.indexOf('澎湖縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",19);\n    }\n    else if(cityname.indexOf('金門縣')!=-1 ) \n    {\n        global.set(\"locationnumber\",20);\n    }\n    else if(cityname.indexOf('連江縣')!=-1 ) \n    {\n        //context.global.locationnumber= 21 ;\n        global.set(\"locationnumber\",21);\n    }\n    else  \n    {\n        // context.global.locationnumber= 0 ;\n        global.set(\"locationnumber\",0);\n    }\n   \n   \n    }\n\ncatch(err) {\n    cityname=\"台北市\";\n    //context.global.locationnumber=0;\n    global.set(\"locationnumber\",0);\n}\n\nmsg.payload= global.get(\"locationnumber\");\nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":480,"wires":[["5d47059b6015dcb7","6eecf6286be86c0f"]]},{"id":"5d47059b6015dcb7","type":"function","z":"d7693fd56d8b2efc","name":"report","func":"var loc=msg.payload;\nvar rawdata = global.get(\"tempera\");\n\nvar locationName=rawdata[loc].locationName;\nvar period0max=rawdata[loc].weatherElement[1].time[0].parameter.parameterName;\nvar period0min=rawdata[loc].weatherElement[2].time[0].parameter.parameterName;\nvar period0start=rawdata[loc].weatherElement[1].time[0].startTime;\nvar period0end=rawdata[loc].weatherElement[1].time[0].endTime;\n\nvar starthourtemp= period0start.split(\"T\");\nvar starthour=starthourtemp[1].split(\":\")[0];\nvar endhourtemp= period0end.split(\"T\");\nvar endhour=endhourtemp[1].split(\":\")[0];\n\n//msg.payload={\"locationName\":locationName,\"time\":starthour+\"-\"+endhour, \"max\":period0max,\n//\"min\":period0min};\n//return msg;\n\nmsg.payload=locationName+'-->' + starthour+\"點到\"+endhour+\"點 溫度最低:\"+period0min+\"度 , 最高\"+period0max+\"度\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":480,"wires":[["425d95d9d66b86e5","ab1fad20da79e263"]]},{"id":"4749c3261fa9c119","type":"function","z":"d7693fd56d8b2efc","name":"report","func":"var loc=msg.payload;\n//var rawdata=context.global.temp;\nvar rawdata = flow.get(\"tempera\");\nvar locationName=rawdata.cwbopendata.dataset[0].location[loc].locationName[0];\nvar period0max=rawdata.cwbopendata.dataset[0].location[loc].weatherElement[1].time[0].parameter[0].parameterName[0];\nvar period0min=rawdata.cwbopendata.dataset[0].location[loc].weatherElement[2].time[0].parameter[0].parameterName[0];\nvar period0start=rawdata.cwbopendata.dataset[0].location[loc].weatherElement[1].time[0].startTime[0];\nvar period0end=rawdata.cwbopendata.dataset[0].location[loc].weatherElement[1].time[0].endTime[0];\n\nvar  starthourtemp= period0start.split(\"T\");\nvar starthour=starthourtemp[1].split(\":\")[0];\nvar  endhourtemp= period0end.split(\"T\");\nvar endhour=endhourtemp[1].split(\":\")[0];\n\nmsg.payload=locationName+starthour+\"到\"+endhour+\"點溫度最低\"+period0min+\"度最高\"+period0max+\"度\";\nreturn msg;\n","outputs":1,"noerr":0,"x":270,"y":560,"wires":[[]]},{"id":"6eecf6286be86c0f","type":"debug","z":"d7693fd56d8b2efc","name":"debug 211","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":520,"wires":[]},{"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"}]


沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...