You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

91 lines
4.2 KiB

<?php
date_default_timezone_set("Asia/Taipei");
$db_query = file_get_contents('php://input');
$myfile = fopen("push_earthquake_message.txt", "a") or die("Unable to open file!");
#fwrite($myfile, date("Y-m-d H:i:s") . ": " . $db_query . "\n");
$db_query=json_decode($db_query,true);
#fwrite($myfile, date("Y-m-d H:i:s") . ": " . $db_query["secret_key"] . "\n");
fwrite($myfile, date("Y-m-d H:i:s") . ": " . $db_query["earthquake_id"] . "\n");
fclose($myfile);
if ($db_query["secret_key"] == "masada_earthquake_SS300"){
require_once "database.php";
$earthquake_id = $link->real_escape_string($db_query['earthquake_id']);
# 測試階段只針對指定對象處理(A000000001)
$sql = "INSERT INTO push_message (accountid, message_detail, pushed_time, creater, create_at, source)
SELECT DISTINCT f.customerid AS accountid,
CONCAT('地震訊息:', s.earthquake_type, ' ', s.county, ':最大震度', s.county_level, '級') AS message_detail,
now() AS pushed_time,
'system' AS creater,
TIMESTAMP(f_return_date(s.issue_date, 1), f_return_time(s.issue_time)) AS create_at,
'cwa' as source
FROM facility as f
INNER JOIN (
SELECT DISTINCT earthquake_type, county, county_level, issue_date, issue_time
FROM earthquake_list WHERE earthquake_id = '$earthquake_id'
) AS s ON s.county = f_return_county(f.area COLLATE utf8_general_ci)
WHERE f.customerid = 'A000000001'";
$link->query($sql);
$sql = "SELECT DISTINCT earthquake_type, issue_date, issue_time, longitude, latitude, earthquake_depth, unit_of_depth,
earthquake_level, direction, direction_distence, distence_unit, distence_unit, source_descript,
county, county_level
FROM earthquake_list WHERE earthquake_id = '$earthquake_id'";
$result = $link->query($sql);
$datas = $result->fetch_all(MYSQLI_ASSOC);
mysqli_close($link);
$datas = array_reduce($datas, function ($acc, $row) {
if (!$acc) {
$acc = array(
"message" => sprintf("%s震度%d級", trim($row["earthquake_type"]), $row["earthquake_level"]),
"datetime" => sprintf("%s %s", $row["issue_date"], $row["issue_time"]),
"lng" => $row["longitude"],
"lat" => $row["latitude"],
"address" => sprintf("%s%d%s(%s)", $row["direction"], $row["direction_distence"], $row["distence_unit"], $row["source_descript"]),
"depth" => sprintf("%s%s", $row["earthquake_depth"], $row["unit_of_depth"]),
"county" => array(),
);
}
$acc["county"][] = sprintf("%s最大震度%d級", $row["county"], $row["county_level"]);
return $acc;
}, array());
// file_put_contents("./test.log", print_r($datas, true)."\r\n", FILE_APPEND);
//以下推送到客户端消息
$access_token = array();
$access_token[]="OMzyxk983YviHTp3SI8BQUidgAguITTmCaLNEbKqnTE";
// $access_token[]="blymLDSO21gDaCXA6nJ2jf6fEZTNE0wEwANbLAhKMhM";
$TargetCount = count($access_token);
$Push_Content['message'] = "\n";
$Push_Content['message'] = $Push_Content['message'] . "地震訊息: " . $datas["message"] . "\n";
$Push_Content['message'] = $Push_Content['message'] . "地震時間: " . $datas["datetime"] . "\n";
$Push_Content['message'] = $Push_Content['message'] . "經度: " . $datas["lng"] . "\n";
$Push_Content['message'] = $Push_Content['message'] . "緯度: " . $datas["lat"] . "\n";
$Push_Content['message'] = $Push_Content['message'] . "位置: " . $datas["address"] . "\n";
$Push_Content['message'] = $Push_Content['message'] . "深度: " . $datas["depth"] . "\n";
$Push_Content['message'] = $Push_Content['message'] . implode("\n", $datas["county"]) . "\n";
for ($i=0; $i<$TargetCount; $i++) {
$ch = curl_init("https://notify-api.line.me/api/notify");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($Push_Content));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Bearer '.$access_token[$i]
));
$response_json_str = curl_exec($ch);
curl_close($ch);
// file_put_contents("./test.log", print_r($response_json_str, true)."\r\n", FILE_APPEND);
}
//以上推送到客户端消息
}
?>