<?php
/**
 * 动态下发当前最优语音识别节点（由 asr_monitor.js 每分钟刷新 asr_active.json）
 * 前端以 <script src="asr_server.php"></script> 引入，得到 window.ASR_SERVER
 */
header('Content-Type: application/javascript; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: 0');

$out = array(
    'name' => '', 'host' => '', 'ip' => '',
    'base' => '', 'url' => '', 'latency' => 0, 'updated' => ''
);

$file = __DIR__ . '/asr_active.json';
if (is_file($file)) {
    $data = json_decode(file_get_contents($file), true);
    if (is_array($data) && !empty($data['active']) && !empty($data['active']['url'])) {
        $a = $data['active'];
        $out = array(
            'name'    => isset($a['name']) ? $a['name'] : '',
            'host'    => isset($a['host']) ? $a['host'] : '',
            'ip'      => isset($a['ip']) ? $a['ip'] : '',
            'base'    => isset($a['base']) ? $a['base'] : '',
            'url'     => isset($a['url']) ? $a['url'] : '',
            'latency' => isset($a['latency']) ? $a['latency'] : 0,
            'updated' => isset($data['updated']) ? $data['updated'] : ''
        );
    }
}

echo 'window.ASR_SERVER = '
    . json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
    . ';';
