
--- action.py
+++ action.py
... | ... | @@ -17,6 +17,18 @@ |
17 | 17 |
description="노드 분석을 위해 사용하는 api.", |
18 | 18 |
) |
19 | 19 |
|
20 |
+trip_log_model = Action.model('TripLog', { |
|
21 |
+ 'trip_id': fields.String(required=True, description='The ID of the trip (64 characters)'), |
|
22 |
+ 'trip_distance_m': fields.Float(required=True, description='Total distance traveled in meters'), |
|
23 |
+ 'trip_time_s': fields.Float(required=True, description='Total time of the trip in seconds'), |
|
24 |
+ 'abrupt_start_count': fields.Integer(required=True, description='Count of abrupt starts'), |
|
25 |
+ 'abrupt_stop_count': fields.Integer(required=True, description='Count of abrupt stops'), |
|
26 |
+ 'abrupt_acceleration_count': fields.Integer(required=True, description='Count of abrupt accelerations'), |
|
27 |
+ 'abrupt_deceleration_count': fields.Integer(required=True, description='Count of abrupt decelerations'), |
|
28 |
+ 'helmet_on': fields.Integer(required=True, description='Whether the helmet was worn during the trip, must be 0 or 1 with 1 is the helmet on.'), |
|
29 |
+ 'final_score': fields.Float(required=True, description='The final safety score for the trip') |
|
30 |
+}) |
|
31 |
+ |
|
20 | 32 |
|
21 | 33 |
@Action.route('/gps_update') |
22 | 34 |
class fileUpload(Resource): |
... | ... | @@ -61,5 +73,51 @@ |
61 | 73 |
# GPS 데이터베이스에 삽입 |
62 | 74 |
db.insert_gps_data(data_csv_block, columns) |
63 | 75 |
return jsonify({'result': f'success'}) |
64 |
- |
|
65 | 76 |
|
77 |
+ |
|
78 |
+@Action.route('/trip_and_score_update') |
|
79 |
+class fileUpload(Resource): |
|
80 |
+ def post(self): |
|
81 |
+ db = DB() |
|
82 |
+ token = request.headers.get('Authorization') |
|
83 |
+ if not token: |
|
84 |
+ return jsonify({'result': 'fail', 'msg': '토큰이 없습니다.'}) |
|
85 |
+ else: |
|
86 |
+ # Decode the token to verify it |
|
87 |
+ decoded_token = jwt.decode(token, "secret", algorithms=['HS256']) |
|
88 |
+ # print(decoded_token) |
|
89 |
+ user_id = decoded_token['id'] |
|
90 |
+ |
|
91 |
+ data = request.get_json() |
|
92 |
+ if len(data["trip_id"]) != 64: |
|
93 |
+ return jsonify({500: "ERROR! INVALID TRIP_ID!"}) |
|
94 |
+ |
|
95 |
+ if len(data["trip_log"]["timestamp"]) == 0: |
|
96 |
+ return jsonify({500: "ERROR! 'trip_log' is empty!"}) |
|
97 |
+ |
|
98 |
+ trip_id = data["trip_id"] |
|
99 |
+ trip_distance_m = data["trip_distance_m"] |
|
100 |
+ trip_time_s = data["trip_time_s"] |
|
101 |
+ abrupt_start_count = data["abrupt_start_count"] |
|
102 |
+ abrupt_stop_count = data["abrupt_stop_count"] |
|
103 |
+ abrupt_acceleration_count = data["abrupt_acceleration_count"] |
|
104 |
+ abrupt_deceleration_count = data["abrupt_deceleration_count"] |
|
105 |
+ helmet_on = data["helmet_on"] |
|
106 |
+ final_score = data["final_score"] |
|
107 |
+ |
|
108 |
+ if not (helmet_on == 1) or (helmet_on == 0): |
|
109 |
+ return jsonify({500: f"ERROR! INVALID 'helmet_on'! \n helmet_on : {helmet_on}"}) |
|
110 |
+ |
|
111 |
+ db.insert_trip_data( |
|
112 |
+ user_id, |
|
113 |
+ trip_id, |
|
114 |
+ trip_distance_m, |
|
115 |
+ trip_time_s, |
|
116 |
+ abrupt_start_count, |
|
117 |
+ abrupt_stop_count, |
|
118 |
+ abrupt_acceleration_count, |
|
119 |
+ abrupt_deceleration_count, |
|
120 |
+ helmet_on, |
|
121 |
+ final_score |
|
122 |
+ ) |
|
123 |
+ return jsonify({'result': f'success'})(파일 끝에 줄바꿈 문자 없음) |
--- auth.py
+++ auth.py
... | ... | @@ -6,9 +6,6 @@ |
6 | 6 |
import jwt |
7 | 7 |
|
8 | 8 |
|
9 |
- |
|
10 |
- |
|
11 |
- |
|
12 | 9 |
users = {} |
13 | 10 |
|
14 | 11 |
Auth = Namespace( |
... | ... | @@ -161,8 +158,15 @@ |
161 | 158 |
# Call the login_user method from the UserManager instance |
162 | 159 |
result, status_code = user_manager.login_user(user_data) |
163 | 160 |
|
164 |
- # Return the appropriate response based on the result from UserManager |
|
165 |
- return result, status_code |
|
161 |
+ if result['status'] == 'success': |
|
162 |
+ payload = { |
|
163 |
+ 'id': id, |
|
164 |
+ 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=14) |
|
165 |
+ } |
|
166 |
+ token = jwt.encode(payload, "secret", algorithm='HS256') |
|
167 |
+ return jsonify({'result': 'success', 'token': token}) |
|
168 |
+ else : |
|
169 |
+ return jsonify({'result': 'fail', 'msg': '아이디/비밀번호가 일치하지 않습니다.'}) |
|
166 | 170 |
|
167 | 171 |
|
168 | 172 |
@Auth.route('/secession') |
--- database/database.py
+++ database/database.py
... | ... | @@ -213,7 +213,50 @@ |
213 | 213 |
decrypted_phone = self.decrypt_aes(encrypted_phone, phone_iv) |
214 | 214 |
|
215 | 215 |
return {'status': 'success', 'phone_number': decrypted_phone}, 200 |
216 |
- |
|
216 |
+ |
|
217 |
+ def insert_gps_data(self, csv_block, columns): |
|
218 |
+ cur = self.conn.cursor() |
|
219 |
+ data = StringIO(csv_block) |
|
220 |
+ |
|
221 |
+ # using COPY instead of INSERT to do even less operation per data. |
|
222 |
+ cur.copy_from(data, 'gps_data', sep=',', columns=columns) |
|
223 |
+ self.conn.commit() |
|
224 |
+ cur.close() |
|
225 |
+ return True |
|
226 |
+ |
|
227 |
+ def insert_trip_data( |
|
228 |
+ self, |
|
229 |
+ username, |
|
230 |
+ trip_id, |
|
231 |
+ total_distance_m, |
|
232 |
+ total_time_s, |
|
233 |
+ abrupt_start_count, |
|
234 |
+ abrupt_stop_count, |
|
235 |
+ abrupt_acceleration_count, |
|
236 |
+ abrupt_deceleration_count, |
|
237 |
+ helmet_on, |
|
238 |
+ final_score |
|
239 |
+ ): |
|
240 |
+ |
|
241 |
+ self.cur.execute(f""" |
|
242 |
+ INSERT INTO trip_log (username, trip_id, timestamp, total_distance_m, total_time_s, abrupt_start_count, abrupt_stop_count, |
|
243 |
+ abrupt_acceleration_count, abrupt_deceleration_count, helmet_on, final_score) |
|
244 |
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) |
|
245 |
+ """, ( |
|
246 |
+ username, |
|
247 |
+ trip_id, |
|
248 |
+ datetime.now(), |
|
249 |
+ total_distance_m, |
|
250 |
+ total_time_s, |
|
251 |
+ abrupt_start_count, |
|
252 |
+ abrupt_stop_count, |
|
253 |
+ abrupt_acceleration_count, |
|
254 |
+ abrupt_deceleration_count, |
|
255 |
+ helmet_on, |
|
256 |
+ final_score |
|
257 |
+ ) |
|
258 |
+ ) |
|
259 |
+ |
|
217 | 260 |
def close_connection(self): |
218 | 261 |
cur = self.cur |
219 | 262 |
cur.close() |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?