天気予報ブロックは、天気予報をWeb API経由で取得し、、天気によって出力を振り分けるソフトウェアブロックです。
今回の例では、天気予報を取得できるWeb APIとして、OpenWeatherMapを使います。
利用規約、API の詳細情報やAPI Key の取得方法はOpenWeatherMapのサイトを確認してください。
実際に利用するには、カスタムブロックをインポート後、天気予報の場所・API Keyをプロパティで指定してください。
天気予報ブロック : コード
"Check" Function
"Check" Functionのソースコードは以下の通りです。"Execute"のソースコードの一部にAPI Keyを記述する行があります。このブロックを利用する際には、ご自身でOpenWeatherMap serviceでアカウント作成、API Keyを取得していただき、該当行を取得したAPI Keyに置き換えた上で利用してください。
Initialize
// outputIndexを初期化します。
return {
runtimeValues : {
outputIndex : 0
},
resultType : "continue"
};
Receive
/*** No Receive Codes ***/
Execute
// PropertyのAPI Keyに、取得したAPI Keyを入力してください
log("API KEY: " + properties.apiKey);
//天気予報を取得するAPIです。
var apiURL = "https://api.openweathermap.org/data/2.5/forecast";
//APIを呼び出すパラメータです。
var data = {
"q" : properties.location,
"APPID" : properties.apiKey
};
//非同期通信で呼び出します。
ajax ({
url : apiURL,
data : data,
type : "get",
timeout : 5000,
success : function ( contents ) {
//コンテンツのチェック
if ( !contents.list || !contents.list[ 0 ] || !contents.list[ 0 ].weather || !contents.list[ 0 ].weather[ 0 ] || !contents.list[ 0 ].weather[ 0 ].id ) {
log("Weather : Invalid data");
runtimeValues.outputIndex = -1;
callbackSuccess( {
resultType : "continue",
runtimeValues : runtimeValues
} );
}
log(JSON.stringify(contents));
//天気を示すIDを取得します。
var idNum = contents.list[ 0 ].weather[ 0 ].id + 0;
log("Weather condition code = " + idNum);
if (idNum < 200) { // Undefined
runtimeValues.outputIndex = 0;
} else if ( idNum < 300 ) { // Thunderstorm
runtimeValues.outputIndex = 3;
} else if ( idNum < 500 ) { // Drizzle
runtimeValues.outputIndex = 3;
} else if ( idNum < 600 ) { // Rain
runtimeValues.outputIndex = 3;
} else if ( idNum < 700 ) { // Snow
runtimeValues.outputIndex = 4;
} else if ( idNum < 800 ) { // Atmosohere
runtimeValues.outputIndex = 5;
} else if ( idNum == 800 ) { // Clear
runtimeValues.outputIndex = 1;
} else { // Clouds
runtimeValues.outputIndex = 2;
}
log(runtimeValues.outputIndex);
callbackSuccess( {
resultType : "continue",
runtimeValues : runtimeValues
} );
},
error : function ( request, errorMessage ) {
log(JSON.stringify(request));
log(JSON.stringify(errorMessage));
runtimeValues.outputIndex = -1;
callbackSuccess( {
resultType : "continue",
runtimeValues : runtimeValues
} );
}
});
return {
resultType : "pause"
};
Result
// outputIndexで指定された出力を返します。
return {
indexes : [ runtimeValues.outputIndex ],
resultType : "continue"
};
天気予報ブロック : Import用 JSONデータ
以下にImport用のJSONデータを記載します。「Import機能」を参考にJSONデータをImportすることで、SDK上でブロックの作成、設定項目、コードの確認、編集が可能です。
Import用 JSONデータ
{"formatVersion":"1.0","tagData":{"name":"天気予報を取得","icon":"./res/x2/default_icon.png","description":"OpenWeatherMapから指定された場所の天気を取得して出力を切り替えます。","functions":[{"id":"function_0","name":"天気予報取得","connector":{"inputs":[{"label":""}],"outputs":[{"label":"エラー"},{"label":"晴れ"},{"label":"曇り"},{"label":"雨"},{"label":"雪"},{"label":"その他"}]},"properties":[{"name":"場所(英語指定)","referenceName":"location","type":"string","defaultValue":"Tokyo"},{"name":"API Key","referenceName":"apiKey","type":"string","defaultValue":"REPLACE YOUR API KEY"}],"extension":{"initialize":"// outputIndexを初期化します。\nreturn {\n\truntimeValues : {\n\t\toutputIndex : 0\n\t},\n\tresultType : \"continue\"\n};","receive":"","execute":"// PropertyのAPI Keyに、取得したAPI Keyを入力してください\nlog(\"API KEY: \" + properties.apiKey);\n\n//天気予報を取得するAPIです。\nvar apiURL = \"https://api.openweathermap.org/data/2.5/forecast\";\n\n//APIを呼び出すパラメータです。\nvar data = {\n\t\"q\" : properties.location,\n\t\"APPID\" : properties.apiKey\n};\n\n//非同期通信で呼び出します。\najax ({\n\turl : apiURL,\n\tdata : data,\n\ttype : \"get\",\n\ttimeout : 5000,\n\tsuccess : function ( contents ) {\n\t\t//コンテンツのチェック\n\t\tif ( !contents.list || !contents.list[ 0 ] || !contents.list[ 0 ].weather || !contents.list[ 0 ].weather[ 0 ] || !contents.list[ 0 ].weather[ 0 ].id ) {\n\t\t\tlog(\"Weather : Invalid data\");\n\t\t\truntimeValues.outputIndex = -1;\n\t\t\tcallbackSuccess( {\n\t\t\t\tresultType : \"continue\",\n\t\t\t\truntimeValues : runtimeValues\n\t\t\t} );\n\t\t}\n\n\t\tlog(JSON.stringify(contents));\n\n\t\t//天気を示すIDを取得します。\n\t\tvar idNum = contents.list[ 0 ].weather[ 0 ].id + 0;\n\t\tlog(\"Weather condition code = \" + idNum);\n\n\t\tif (idNum < 200) { // Undefined\n\t\t\truntimeValues.outputIndex = 0;\n\t\t} else if ( idNum < 300 ) { // Thunderstorm\n\t\t\truntimeValues.outputIndex = 3;\n\t\t} else if ( idNum < 500 ) { // Drizzle\n\t\t\truntimeValues.outputIndex = 3;\n\t\t} else if ( idNum < 600 ) { // Rain\n\t\t\truntimeValues.outputIndex = 3;\n\t\t} else if ( idNum < 700 ) { // Snow\n\t\t\truntimeValues.outputIndex = 4;\n\t\t} else if ( idNum < 800 ) { // Atmosohere\n\t\t\truntimeValues.outputIndex = 5;\n\t\t} else if ( idNum == 800 ) { // Clear\n\t\t\truntimeValues.outputIndex = 1;\n\t\t} else { // Clouds\n\t\t\truntimeValues.outputIndex = 2;\n\t\t}\n\n\t\tlog(runtimeValues.outputIndex);\n\t\t\n\t\tcallbackSuccess( {\n\t\t\tresultType : \"continue\",\n\t\t\truntimeValues : runtimeValues\n\t\t} );\n\t},\n\terror : function ( request, errorMessage ) {\n\t\tlog(JSON.stringify(request));\n\t\tlog(JSON.stringify(errorMessage));\n\t\truntimeValues.outputIndex = -1;\n\t\tcallbackSuccess( {\n\t\t\tresultType : \"continue\",\n\t\t\truntimeValues : runtimeValues\n\t\t} );\n\t}\n});\n \nreturn {\n\tresultType : \"pause\"\n};","result":"// outputIndexで指定された出力を返します。\nreturn {\n\tindexes : [ runtimeValues.outputIndex ],\n\tresultType : \"continue\"\n};"}}]}}
以下のリンクからダウンロードできます。