Version1ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | <?php /************************************************************************* * home-api (MAIN) * 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 *************************************************************************/ //コンフィグ読み込み require_once("home-api-config.php"); //関数読み込み require_once("home-api-functions.php"); //V1.0 functions //function checkTime($startTime, $endTime) //function pushLINE($value1, $value2) //function announce($api_url, $text) //function shutdownPC($target_ip, $target_user, $target_pass) //function WakeupPC($MAC) //function logger($text, $level) logger("Start API KEY=".$_POST['KEY'],"INFO"); if(isset($_POST['KEY']) && strcmp($_POST['APIKEY'], HOME_API_KEY) == 0) { if(isset($_POST['TEXT'])){ $text = $_POST['TEXT']; } switch ($_POST['KEY']) { case 'ExecAnnounce1': //Living logger("Start ExecAnnounce1","INFO"); if(checkTime('6:00','22:00')) { announce(GOOGLE_HOME_1, $text); } break; case 'ExecAnnounce2': //Son logger("Start ExecAnnounce2","INFO"); if(checkTime('6:00','22:00')) { announce(GOOGLE_HOME_2, $text); } break; case 'ExecAnnounce3': //Doughter logger("Start ExecAnnounce3","INFO"); if(checkTime('6:00','22:00')) { announce(GOOGLE_HOME_3, $text); } break; case 'TemperatureWarn': logger("Start TemperatureWarn","INFO"); //Over 30 pushLine('【リビング室温警告】',$text); shutdownPC(WINDOWS_IP, WINDOWS_USER, WINDOWS_PASS); break; case 'TransportationInfo': logger("Start TransportatioknInfo","INFO"); //Delay warning $text = mb_substr($text,0,mb_strpos($text,'#',0,"UTF-8"),"UTF-8"); pushLine('【公共交通機関情報】',$text); if(checkTime('6:00','22:00')) { announce(GOOGLE_HOME_1, $text); } if(checkTime('6:00','8:00')) { announce(GOOGLE_HOME_2, $text); } break; case 'Weather': logger("Start Weather","INFO"); //Rain alert pushLine('【天気情報】',$text); if(checkTime('7:00','19:00')) { announce(GOOGLE_HOME_2, $text); announce(GOOGLE_HOME_3, $text); } if(checkTime('6:00','23:00')) { announce(GOOGLE_HOME_1, $text); } break; case 'UrgentInfo': logger("Start info from tweet","INFO"); pushLine('【緊急通知】',$text); if(checkTime('7:00','19:00')) { announce(GOOGLE_HOME_2, $text); announce(GOOGLE_HOME_3, $text); } if(checkTime('6:00','23:00')) { announce(GOOGLE_HOME_1, $text); } break; case 'shutdownPC': logger("Start shutdownPC","INFO"); shutdownPC(WINDOWS_IP, WINDOWS_USER, WINDOWS_PASS); break; case 'WakeupPC': logger("Start wakeupPC","INFO"); WakeupPC(WINDOWS_MAC); break; default: logger("This is private API. (in Default)","ERROR"); } }else{ logger("This is private API. (in else)","ERROR"); } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php /************************************************************************* * home-api (config) * 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 *************************************************************************/ //サンプルファイル名を変更して利用下さい。 home-api-config.php に変更。 //@HOME_API_LOG_NAME@ ログファイル名、書き込み権限が必要です //@HOME_API_KEY@ POST受信時の簡易的なAPI-KEYのトークン確認を行います //@IFTTT_POST_API_KEY@ IFTTTのWebhooks用API-KEY //@GOOGLE_HOME_1@ http://192.168.0.200:9081 などgoogle home notifier向けのURL //@GOOGLE_HOME_2@ google home notifier向けのURL //@GOOGLE_HOME_3@ google home notifier向けのURL //@WINDOWS_IP@ WindowsのIPアドレス //@WINDOWS_USER@ Windowsのユーザー名 //@WINDOWS_PASS@ Windowsのパスワード //@WINDOWS_MAC@ WindowsのMACアドレス //各種設定 //ログのファイル名 define("HOME_API_LOG_NAME","@HOME_API_LOG_NAME@"); //home-api-key define("HOME_API_KEY","@HOME_API_KEY@"); //IFTTT用webhookパラメータ define("IFTTT_POST_API_KEY","/with/key/@IFTTT_POST_API_KEY@"); define("IFTTT_POST_API_BASE","https://maker.ifttt.com/trigger/"); //IFTTT用puchLINE define("IFTTT_LINE_KEY","pushLINE"); //google-home-notifier define("GOOGLE_HOME_1",'@GOOGLE_HOME_1@'); define("GOOGLE_HOME_2",'@GOOGLE_HOME_2@'); define("GOOGLE_HOME_3",'@GOOGLE_HOME_3@'); //Windows PC define("WINDOWS_IP","@WINDOWS_IP@"); define("WINDOWS_USER","@WINDOWS_USER@"); define("WINDOWS_PASS","@WINDOWS_PASS@"); define("WINDOWS_MAC","@WINDOWS_MAC@"); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | <?php /************************************************************************* * home-api (function) * 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 *************************************************************************/ function checkTime($startTime, $endTime) { $currentTime = date('H:i'); if(strtotime($startTime) <= strtotime($currentTime) and strtotime($currentTime) <= strtotime($endTime)) { return true; }else{ return false; } } function pushLINE($value1, $value2) { logger("Start pushLINE value1={$value1},value2={$value2}","INFO"); $url = IFTTT_POST_API_BASE.IFTTT_LINE_KEY.IFTTT_POST_API_KEY; $data = array( 'value1' => $value1, 'value2' => $value2 ); $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); $response = curl_exec($curl); $result = json_decode($response, true); curl_close($curl); return $result; } function announce($api_url, $text) { logger("Start announce target={$api_url},text={$text}","INFO"); $message = 'text='.$text; $data = array( 'text' => "$text" ); $path = '/google-home-notifier'; $url = $api_url.$path; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 証明書の検証を行わない curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // curl_execの結果を文字列で返す curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // jsonデータを送信 $response = curl_exec($curl); $result = json_decode($response, true); curl_close($curl); return $result; } function shutdownPC($target_ip, $target_user, $target_pass) { logger("Start shutdown target_ip:".$target_ip." target_user:".$target_user,"INFO"); $shutdown_time = 60; $comment = "シャットダウン開始します。キャンセル「-a」"; $cmd = 'shutdown /s /f /t '.$shutdown_time.' /c '.$comment; $connection = ssh2_connect($target_ip); ssh2_auth_password($connection, $target_user, $target_pass); $stream = ssh2_exec($connection, $cmd); $errorstream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); stream_set_blocking($stream, true); stream_set_blocking($errorstream, true); $ok = stream_get_contents($stream); $ng = stream_get_contents($errorstream); //エラーがあれば表示 echo $ok; echo $ng; } function WakeupPC($MAC) { logger("Start powerOn","INFO"); $cmd = 'sudo /usr/sbin/etherwake '.$MAC; $output = shell_exec($cmd); } function logger($text, $level) { $datetime = date('Y-m-d H:i:s'); $date = date('Ym'); $file_name = __DIR__ . "/log/log-home-{$date}.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); } |