Skip to content

Commit 2f1f9ee

Browse files
committed
Feature: 신호등 잔여시간(T-데이터)) API 정보 불러오기 기능구현
1 parent d0b15c7 commit 2f1f9ee

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

map/services/v2x.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import requests
2+
import os
3+
from dotenv import load_dotenv
4+
5+
load_dotenv()
6+
7+
V2X_API_KEY = os.getenv("V2X_API_KEY")
8+
9+
def get_signal_phase(intersection_id: str, page=1, rows=20):
10+
url = "http://t-data.seoul.go.kr/apig/apiman-gateway/tapi/v2xSignalPhaseTimingInformation/1.0"
11+
params = {
12+
"apiKey": V2X_API_KEY,
13+
"pageNo": page,
14+
"numOfRows": rows
15+
}
16+
headers = {
17+
"Content-Type": "application/json"
18+
}
19+
payload = {
20+
"intersectionId": intersection_id
21+
}
22+
23+
try:
24+
response = requests.post(url, params=params, headers=headers, json=payload)
25+
if response.status_code == 200:
26+
return response.json()
27+
else:
28+
return {"error": f"신호 API 실패: {response.status_code}"}
29+
except Exception as e:
30+
return {"error": str(e)}

map/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django.urls import path
2-
from .views import TmapRouteView, TrafficLightView
2+
from .views import TmapRouteView, TrafficLightView, SignalPhaseView
33

44
urlpatterns = [
55
path('route/', TmapRouteView.as_view(), name='tmap-route'),
66
path('traffic-lights/', TrafficLightView.as_view(), name='traffic-lights'),
7+
path("signal-phase/", SignalPhaseView.as_view(), name="signal-phase"),
78
]

map/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from rest_framework import status
44
from .services.tmap import get_pedestrian_route
55
from .services.traffic_light import fetch_traffic_lights, convert_tm_to_wgs84, is_within_radius
6-
6+
from .services.v2x import get_signal_phase
77
class TmapRouteView(APIView):
88
def get(self, request):
99
startX = request.query_params.get("startX")
@@ -55,3 +55,12 @@ def get(self, request):
5555
continue
5656

5757
return Response({"total": len(result), "lights": result}, status=status.HTTP_200_OK)
58+
59+
class SignalPhaseView(APIView):
60+
def get(self, request):
61+
intersection_id = request.query_params.get("id")
62+
if not intersection_id:
63+
return Response({"error": "intersectionId를 입력해주세요"}, status=400)
64+
65+
data = get_signal_phase(intersection_id)
66+
return Response(data, status=200)

0 commit comments

Comments
 (0)