diff --git a/src/gateway/routes/routes-misc.ts b/src/gateway/routes/routes-misc.ts index 29915e8..2f6034b 100644 --- a/src/gateway/routes/routes-misc.ts +++ b/src/gateway/routes/routes-misc.ts @@ -161,13 +161,33 @@ export function registerMiscRoutes(app: express.Application, deps: MiscRouteDeps if (!apiKey) { res.status(503).json({ error: 'Traffic API key not configured' }); return; } const { minX = '124.0', maxX = '132.0', minY = '33.0', maxY = '39.0', cctvType = '1' } = req.query as Record; - const url = `https://openapi.its.go.kr:9443/cctvInfo?apiKey=${encodeURIComponent(apiKey)}&type=its&cctvType=${cctvType}&minX=${minX}&maxX=${maxX}&minY=${minY}&maxY=${maxY}&getType=json`; + // ITS 국가교통정보는 type=its(국도)와 type=ex(한국도로공사 고속도로)가 따로다. 예전엔 its만 불러서 + // 고속도로 CCTV가 통째로 빠졌다(인천 5개 → its+ex 117개). 기본은 둘 다, type=its|ex로 하나만도 가능. + const want = String((req.query as Record).type || 'all'); + const types = want === 'its' ? ['its'] : want === 'ex' ? ['ex'] : ['its', 'ex']; + const fetchType = async (type: string): Promise => { + const url = `https://openapi.its.go.kr:9443/cctvInfo?apiKey=${encodeURIComponent(apiKey)}&type=${type}&cctvType=${encodeURIComponent(cctvType)}&minX=${encodeURIComponent(minX)}&maxX=${encodeURIComponent(maxX)}&minY=${encodeURIComponent(minY)}&maxY=${encodeURIComponent(maxY)}&getType=json`; + const r = await fetch(url); + if (!r.ok) throw new Error(`ITS API error ${r.status} (${type})`); + const data = await r.json() as any; + return (data?.response?.data || []).map((it: any) => ({ ...it, _src: type })); + }; try { - const r = await fetch(url); - if (!r.ok) { res.status(502).json({ error: `ITS API error ${r.status}` }); return; } - const data = await r.json() as any; - const items: any[] = data?.response?.data || []; + const results = await Promise.allSettled(types.map(fetchType)); + const ok = results.filter((x): x is PromiseFulfilledResult => x.status === 'fulfilled'); + if (!ok.length) { + const first = results[0] as PromiseRejectedResult; + res.status(502).json({ error: String(first?.reason?.message || first?.reason || 'ITS API failed') }); + return; + } + const seen = new Set(); + const items = ok.flatMap(x => x.value).filter(it => { + const k = String(it.cctvurl || `${it.cctvname}|${it.coordx}|${it.coordy}`); + if (seen.has(k)) return false; + seen.add(k); + return true; + }); res.json({ count: items.length, items }); } catch (e: any) { res.status(502).json({ error: String(e?.message || e) }); diff --git a/web-ui/html/traffic-app.html b/web-ui/html/traffic-app.html index 616552e..8b4ba81 100644 --- a/web-ui/html/traffic-app.html +++ b/web-ui/html/traffic-app.html @@ -132,7 +132,7 @@ body{background:var(--bg);color:var(--text);font-family:system-ui,sans-serif;dis
- +