File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 )}
Original file line number Diff line number Diff line change 11from django .urls import path
2- from .views import TmapRouteView , TrafficLightView
2+ from .views import TmapRouteView , TrafficLightView , SignalPhaseView
33
44urlpatterns = [
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]
Original file line number Diff line number Diff line change 33from rest_framework import status
44from .services .tmap import get_pedestrian_route
55from .services .traffic_light import fetch_traffic_lights , convert_tm_to_wgs84 , is_within_radius
6-
6+ from . services . v2x import get_signal_phase
77class 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 )
You can’t perform that action at this time.
0 commit comments