
File name
Commit message
Commit date

2024-09-06

2024-09-06

2024-09-06

2024-09-06
from flask_restx import Resource, Api, Namespace, fields,reqparse
from flask import request,jsonify
from flask import Flask, request
import os
from haversine import haversine
from database.database import DB
from datetime import datetime
import pandas as pd
import jwt
paths = os.getcwd()
Action = Namespace(
name="Action",
description="노드 분석을 위해 사용하는 api.",
)
@Action.route('/gps_update')
class fileUpload(Resource):
def post(self):
db = DB()
token = request.headers.get('Authorization')
if not token:
return jsonify({'result': 'fail', 'msg': '토큰이 없습니다.'})
else:
# Decode the token to verify it
decoded_token = jwt.decode(token, "secret", algorithms=['HS256'])
#print(decoded_token)
user_id = decoded_token['id']
data = request.get_json()
if len(data["trip_id"]) !=64:
return jsonify({500 :"ERROR! INVALID TRIP_ID!"})
if len(data["trip_log"]["timestamp"]) == 0:
return jsonify({500 :"ERROR! 'trip_log' is empty!"})
time_stamp_len = len(data["trip_log"]["timestamp"])
latitude_len = len(data["trip_log"]["latitude"])
longitude_len = len(data["trip_log"]["longitude"])
if time_stamp_len != latitude_len or latitude_len != longitude_len:
return jsonify(
{
500: f"ERROR! Mismatching length of data in trip_log! \n timestamp : {time_stamp_len} \n latitude : {latitude_len} \n longitude : {longitude_len}"
}
)
df = pd.DataFrame(data["trip_log"])
df["user_id"] = data["user_id"]
df["trip_id"] = data["trip_id"]
columns = df.columns
data_csv_block = df.to_csv(header=False, index=False)
print(f"recieved : {data}")
# GPS 데이터베이스에 삽입
db.insert_gps_data(data_csv_block, columns)
return jsonify({'result': f'success'})