Created
July 8, 2019 10:44
-
-
Save kenichimiki/42fb1736da90a911dd9c3bffda4a8f10 to your computer and use it in GitHub Desktop.
Using Yahoo!JAPAN weather API for check condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/************************************************************************* | |
* home-api (Weather) | |
* Home Tools for private. Using IFTTT and Google Home etc | |
* | |
* PHP 5 or later | |
* | |
* @category Home IoT | |
* @author Miki | |
* @url https://www.miki-ie.com/ | |
* @copyright 2019 (c) MIKI-IE All rights Reserved. | |
* @license https://opensource.org/licenses/mit-license.html MIT License | |
* @version 1.0 | |
*************************************************************************/ | |
//各種設定 | |
//ログのファイル名 | |
define("HOME_API_LOG_NAME","log-weather"); | |
//前回通知時間記録ファイル | |
define("HOME_API_TEMP_NAME","temp"); | |
//home-api-key | |
define("HOME_API_URL","http://@URL4HOMEAPI@/home-api.php"); | |
define("HOME_API_KEY","@APIKEY@"); | |
//動作設定 | |
//雨判定閾値(通常0.5mm/hが雨(小雨)) | |
define("MIN_RAIN_VALUE",0.55); | |
//雨判定閾値(小雨以上の雨、通常1.0mm/h以上) | |
define("STD_RAIN_VALUE",1.0); | |
//雨アナウンス後の次アナウンスまでの間隔(分) アナウンスなし期間の小雨以上判定で期間延長 | |
define("NO_NOTICE_TIME",70); | |
//Yahoo API Infromation | |
$appid = '@YAHOO_API_ID@'; | |
$coordinates ='@PLACE@'; | |
$base_url = 'https://map.yahooapis.jp/weather/V1/place'; | |
$url = $base_url.'?interval=10&output=json&coordinates='.$coordinates.'&appid='.$appid; | |
function logger($text, $level) { | |
$datetime = date('Y-m-d H:i:s'); | |
$yearMonth = date('Ym'); | |
$file_name = __DIR__ . "/log/".HOME_API_LOG_NAME."-{$yearMonth}.log"; | |
$text = "{$datetime} [{$level}] {$text}" . PHP_EOL; | |
echo $text; | |
if(!(file_exists($file_name))){ | |
touch($file_name); | |
chmod($file_name, 0777); | |
} | |
return error_log(print_r($text, TRUE), 3, $file_name); | |
} | |
function checkNotice(){ //true:通知する、false:通知スキップし通知スキップ時間延長 | |
$datetime = date('Y-m-d H:i:s'); | |
$file_name = __DIR__ . "/log/".HOME_API_TEMP_NAME.".txt"; | |
$text = "{$datetime}"; | |
if(!(file_exists($file_name))){ | |
touch($file_name); | |
chmod($file_name, 0777); | |
$content = "2019-05-01 00:00:00"; | |
}else{ | |
$content = file_get_contents($file_name); | |
} | |
$diff_min = (strtotime($text) - strtotime($content)) / 60; | |
logger("updateTime in checkNotice. last_time=".$content." diff_min=".$diff_min,"INFO"); | |
file_put_contents($file_name, $text); | |
if ($diff_min > NO_NOTICE_TIME){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
function notice($text){ | |
logger("Start notice function TEXT=".$text,"INFO"); | |
$data = array( | |
"APIKEY"=> HOME_API_KEY, | |
"KEY" => "Weather", | |
"TEXT" => "$text" | |
); | |
$url = HOME_API_URL; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_POST, TRUE); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // jsonデータを送信 | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 証明書の検証を行わない | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // curl_execの結果を文字列で返す | |
$response = curl_exec($curl); | |
$result = json_decode($response, true); | |
curl_close($curl); | |
} | |
logger("Start weather script","INFO"); | |
//echo $url; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 証明書の検証を行わない | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // curl_execの結果を文字列で返す | |
$response = curl_exec($curl); | |
$result = json_decode($response, true); | |
curl_close($curl); | |
$weatherList = $result['Feature']['0']['Property']['WeatherList']['Weather']; | |
$current_date = $weatherList[0]["Date"]; | |
$current_rainfall = $weatherList[0]["Rainfall"]; | |
for ($i = 1; $i <= 6; $i++) { | |
$target_date = $weatherList["$i"]["Date"]; | |
$target_rainfall = $weatherList["$i"]["Rainfall"]; | |
$diff_min = (strtotime($target_date) - strtotime($current_date)) / 60; | |
if($current_rainfall < MIN_RAIN_VALUE){ | |
if($target_rainfall >= MIN_RAIN_VALUE){ | |
if($target_rainfall >= STD_RAIN_VALUE){ | |
$text = $diff_min."分後に雨が降ります。".$target_rainfall."ミリの雨予報です。"; | |
logger("天候変化:".$text." Current_date=".$current_date.", current_rainfall=".$current_rainfall. | |
", new_date=".$target_date.", new_rainfall=".$target_rainfall,"INFO"); | |
if(checkNotice()){ | |
notice($text); | |
} | |
break; | |
}else{ | |
$text = $diff_min."分後に小雨が降ります。".$target_rainfall."ミリの雨予報です。"; | |
logger("天候変化:".$text." Current_date=".$current_date.", current_rainfall=".$current_rainfall. | |
", new_date=".$target_date.", new_rainfall=".$target_rainfall,"INFO"); | |
if(checkNotice()){ | |
notice($text); | |
} | |
break; | |
} | |
} | |
}elseif($current_rainfall >= STD_RAIN_VALUE){ | |
if($target_rainfall <= STD_RAIN_VALUE){ | |
if($target_rainfall <= MIN_RAIN_VALUE){ | |
$text = $diff_min."分後に雨が上がります。".$target_rainfall."ミリの雨予報です。"; | |
logger("天候変化:".$text." Current_date=".$current_date.", current_rainfall=".$current_rainfall. | |
", new_date=".$target_date.", new_rainfall=".$target_rainfall,"INFO"); | |
//notice($text); | |
break; | |
}else{ | |
$text = $diff_min."分後に雨が小雨になります。".$target_rainfall."ミリの雨予報です。"; | |
logger("天候変化:".$text." Current_date=".$current_date.", current_rainfall=".$current_rainfall. | |
", new_date=".$target_date.", new_rainfall=".$target_rainfall,"INFO"); | |
if(checkNotice()){ | |
//notice($text); | |
} | |
break; | |
} | |
} | |
}else{ // MIN_RAIN_VALUE <= $current_rainfall < STD_RAIN_VALUE | |
if($target_rainfall < MIN_RAIN_VALUE){ | |
$text = $diff_min."分後に雨が弱くなります".$target_rainfall."ミリの雨予報です。"; | |
logger("天候変化:".$text." Current_date=".$current_date.", current_rainfall=".$current_rainfall. | |
", new_date=".$target_date.", new_rainfall=".$target_rainfall,"INFO"); | |
if(checkNotice()){ | |
//notice($text); | |
} | |
break; | |
} | |
if($target_rainfall >= STD_RAIN_VALUE){ | |
$text = $diff_min."分後に雨が強くなります。".$target_rainfall."ミリの雨予報です。"; | |
logger("天候変化:".$text." Current_date=".$current_date.", current_rainfall=".$current_rainfall. | |
", new_date=".$target_date.", new_rainfall=".$target_rainfall,"INFO"); | |
if(checkNotice()){ | |
//notice($text); | |
} | |
break; | |
} | |
} | |
} | |
logger("END weather script. current_date=".$current_date.", current_rainfall=".$current_rainfall,"INFO"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment