-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreserve_bus.py
207 lines (171 loc) · 6.56 KB
/
reserve_bus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymongo
from datetime import datetime
# DB 연결
client = pymongo.MongoClient("mongodb+srv://dbUser:1234@cluster0.3fk5bkx.mongodb.net/?retryWrites=true&w=majority")
db = client.deagu_bus
col_info = db.reserveBus_info # 예약버스 정보
col_bus = db.bus_state # 현재 버스 상태
col_reserve = db.reservation # 예약 내역
# 모든 예약버스 노선의 정류장 반환
def return_all_route_list():
res = []
search = col_info.find({'type': 'route'})
for i in search:
res.append({'route': i['route'], 'forward': i['forward'], 'BS_list': i['BS_list']})
return res
# 유효한 시간인지 확인
def check_date(element):
today = datetime.now()
if (element["year"] < 2000 + int(today.strftime('%y'))):
return False
if (element['year'] > 2000 + int(today.strftime('%y'))):
return True
if (element['month'] < int(today.strftime('%m'))):
return False
if (element['month'] > int(today.strftime('%m'))):
return True
if (element['day'] < int(today.strftime('%d'))):
return False
if (element['day'] > int(today.strftime('%d'))):
return True
if (element['hour'] < int(today.strftime('%H'))):
return False
if (element['hour'] > int(today.strftime('%H'))):
return True
if (element['min'] > int(today.strftime('%M'))):
return False
return True
# 예약내역 불러오기
def return_reservation(user_id, user_pw):
result = []
flag = False
search = col_reserve.find({'user_id': user_id, 'user_pw': user_pw})
for i in search:
flag = True
if (check_date(i) == False):
i['reserve_vaild'] = False
col_reserve.delete_one({'_id': i['_id']})
col_reserve.insert_one(i)
result.append(i)
if (flag):
return result
return False
# DB doc -> 도착까지 남은 시간(1시간 이내), 도착 정보 반환
def return_remain_time(element):
today = datetime.now()
# print(element)
str_time = str(element['hour']) + ":" + str(element['min']) + " 도착"
if (element['year'] != today.year):
return str_time
if (element['month'] != today.month):
return str_time
if (element['day'] != today.day):
return str_time
gap = element['hour'] * 60 + element['min'] - (int(today.strftime('%H')) * 60 + int(today.strftime('%M')))
if (gap < 60):
return str(gap) + "분(" + str_time + ")"
else:
return str_time
# bus_id -> 도착까지 남은 시간(1시간 이내), 도착 정보 반환
def return_remain_time_id(bus_id, BS_on):
element = col_bus.find_one({"bus_id": bus_id, "BS_off": BS_on})
today = datetime.now()
# print(element)
str_time = str(element['hour']) + ":" + str(element['min']) + " 도착"
if (element['year'] != today.year):
return str_time
if (element['month'] != today.month):
return str_time
if (element['day'] != today.day):
return str_time
gap = element['hour'] * 60 + element['min'] - (int(today.strftime('%H')) * 60 + int(today.strftime('%M')))
if (gap < 60):
return str(gap) + "분(" + str_time + ")"
else:
return str_time
# 예약 취소
def cancle_reserve(reserve_id):
res = col_reserve.find_one({'_id': reserve_id})
res['reserve_vaild'] = False
# print(res)
col_reserve.delete_one({'_id': reserve_id})
col_reserve.insert_one(res)
BS_find = res['BS_on']
seat_num = 1 << (res['seat'] - 1)
while True:
search = col_bus.find_one({"bus_id": res['bus_id'], "BS_on": BS_find})
search["available"] = search["available"] | seat_num
# print(search)
# print(bin(search['available']))
col_bus.delete_one({"bus_id": res['bus_id'], "BS_on": BS_find})
col_bus.insert_one(search)
if search["BS_off"] == res["BS_off"]:
break
BS_find = search["BS_off"]
# 예약하기
def reserve_seat(user_id, user_pw, bus_id, BS_on, BS_off, seat):
BS_find = BS_on
seat_num = ~(1 << (seat - 1))
start = col_bus.find_one({"bus_id": bus_id, "BS_on": BS_find})
element = {'reserve_valid': True, 'user_id': user_id, 'user_pw': user_pw, 'route': start['route'], 'bus_id': bus_id,
'BS_on': BS_on, 'BS_off': BS_off, 'seat': seat,
'year': start['year'], 'month': start['month'], 'day': start['day'],
'hour': start['hour'], 'min': start['min']}
col_reserve.insert_one(element)
while True:
search = col_bus.find_one({"bus_id": bus_id, "BS_on": BS_find})
search["available"] = search["available"] & seat_num
# print(bin(search['available']))
col_bus.delete_one({"bus_id": bus_id, "BS_on": BS_find})
col_bus.insert_one(search)
if search["BS_off"] == BS_off:
break
BS_find = search["BS_off"]
# 해당 버스에서 예약가능한 좌석 알아내기
def count_avail(bus_id, BS_on, BS_off, max):
BS_find = BS_on
avail = (1 << max) - 1
while True:
search = col_bus.find_one({"bus_id": bus_id, "BS_on": BS_find})
and_res = avail & search["available"]
avail = and_res
if search["BS_off"] == BS_off:
break
BS_find = search["BS_off"]
count = 0
temp = avail
while temp:
if temp & 1:
count += 1
temp = temp >> 1
return avail, count
# bus_id와 출발/도착 정류장 -> 좌석 현황 리스트
def return_seat_status(bus_id, BS_on, BS_off):
avail_result, count_result = count_avail(bus_id, BS_on, BS_off, 26)
avail_list = []
for j in range(0, 26):
if (avail_result & (1 << j)):
avail_list.append(True)
else:
avail_list.append(False)
return avail_list
# 버스노선, 방향, 타는/내리는 정류장 -> 버스 목록 찾기
def search_bus(route, forward, BS_on, BS_off):
result = []
search = col_bus.find({"route": route, "BS_on": BS_on, "forward": forward})
for i in search:
if check_date(i):
# print(i)
avail_result, count_result = count_avail(i["bus_id"], BS_on, BS_off, i['max_num'])
avail_list = []
for j in range(0, i["max_num"]):
if (avail_result & (1 << j)):
avail_list.append(True)
else:
avail_list.append(False)
result.append(
{"route": i["route"], "bus_id": i["bus_id"], "max_num": i["max_num"], "available": count_result,
"seat_state": avail_list, 'time': return_remain_time(i)})
return result