Initial private import

This commit is contained in:
seo
2026-06-08 02:33:14 +09:00
commit c0fc076d34
7 changed files with 550 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
<?php
header('Content-Type: application/json; charset=utf-8');
const HA_BASE = 'https://ha.chaegeon.com';
const HA_LONG_LIVED_TOKEN = 'REDACTED_HA_TOKEN';
const POKE_ENTITY_ID = 'counter.kog_jjireugi';
function ha_get_entity_state($entity_id) {
$url = rtrim(HA_BASE, '/') . '/api/states/' . urlencode($entity_id);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . HA_LONG_LIVED_TOKEN,
'Content-Type: application/json'
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
]);
$res = curl_exec($ch);
$errno = curl_errno($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($errno !== 0 || $status < 200 || $status >= 300) {
return null;
}
$json = json_decode($res, true);
// HA는 상태값을 문자열로 줍니다. 숫자면 (string)"12" 형태.
if (!is_array($json) || !isset($json['state'])) return null;
// 숫자로 캐스팅 시도 (실패해도 문자열 반환 가능)
$stateRaw = $json['state'];
if (is_numeric($stateRaw)) {
return (int)$stateRaw;
}
return $stateRaw; // 숫자가 아니면 원본 문자열
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$password = $_POST['password'] ?? '';
if ($password === '0010') {
echo json_encode(['result' => 'ok']);
} else {
echo json_encode(['result' => 'fail']);
}
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (isset($_GET['failMessages'])) {
echo json_encode([
'failMessages' => [
'비밀번호가 틀립니다.',
]
], JSON_UNESCAPED_UNICODE);
exit;
}
if (isset($_GET['pokeCount'])) {
// HA 엔티티 상태 읽기
$count = ha_get_entity_state(POKE_ENTITY_ID);
if ($count === null) {
http_response_code(502);
echo json_encode([
'ok' => false,
'error' => 'failed_to_fetch_from_ha'
], JSON_UNESCAPED_UNICODE);
exit;
}
echo json_encode([
'ok' => true,
'count' => $count
], JSON_UNESCAPED_UNICODE);
exit;
}
http_response_code(404);
echo json_encode(['error' => 'not_found']);
exit;
}
http_response_code(405);
echo json_encode(['error' => 'method_not_allowed']);
?>