def find(): user_id = get_jwt_identity() history = History.select().where(History.user_id == user_id).order_by(History.id.desc()) if history: latest = history[0] responseObj = { 'status': 'success', 'mall': Mall.get_by_id(Floor.get_by_id(Parking.get_by_id(latest.parking_id).floor_id).mall_id).outlet, 'floor': Floor.get_by_id(Parking.get_by_id(latest.parking_id).floor_id).floor, 'parking': Parking.get_by_id(latest.parking_id).parking_num, 'date': latest.created_at.strftime('%A %d %b %Y'), 'time': latest.created_at.strftime('%X %p'), 'id': latest.id } return jsonify(responseObj), 200 else: responseObj = { 'status': 'success', 'message': 'No parking information!', 'mall': 'N/A', 'floor': 'N/A', 'parking': 'N/A', 'date': 'N/A', 'time': 'N/A', 'id': 'N/A' } return jsonify(responseObj), 200
def history(): user_id = get_jwt_identity() current_user = User.get_by_id(user_id) history_obj = History.select().where(History.user_id == current_user.id).order_by(History.id.desc()) history_arr = [] if history_obj: for history in history_obj: history_list = { 'mall': Mall.get_by_id(Floor.get_by_id(Parking.get_by_id(history.parking_id).floor_id).mall_id).outlet, 'floor': Floor.get_by_id(Parking.get_by_id(history.parking_id).floor_id).floor, 'parking': Parking.get_by_id(history.parking_id).parking_num, 'date': history.created_at.strftime('%A %d %b %Y'), 'time': history.created_at.strftime('%X %p'), 'id': history.id } history_arr.append(history_list) responseObj = { 'status': 'success', 'history': history_arr } return jsonify(responseObj), 200 else: responseObj = { 'status': 'success', 'history': history_arr } return jsonify(responseObj), 200
def history_delete(id): user_id = get_jwt_identity() history = History.get_by_id(id) if history.delete_instance(): history_obj = History.select().where(History.user_id == user_id) history_arr = [] if history_obj: for history in history_obj: history_list = { 'mall': Mall.get_by_id(Floor.get_by_id(Parking.get_by_id(history.parking_id).floor_id).mall_id).outlet, 'floor': Floor.get_by_id(Parking.get_by_id(history.parking_id).floor_id).floor, 'parking': Parking.get_by_id(history.parking_id).parking_num, 'date': history.created_at.strftime('%A %d %b %Y'), 'time': history.created_at.strftime('%X %p'), 'id': history.id } history_arr.append(history_list) responseObj= { 'status': 'success', 'message': 'Successfully deleted parking history!', 'history': history_arr } return jsonify(responseObj), 200 else: responseObj = { 'status': 'failed', 'message': 'Failed to delete history!' } return jsonify(responseObj), 400
def run(): try: # During the first run, update prev_status, status and DB correctly # based on the sensor status print("Starting...") prev_status = [False, False, False] status = [False, False, False] for i in range(3): distance = ping(GPIO_TRIGGER[i], GPIO_ECHO[i]) if distance <= 7: GPIO.output(GPIO_LED[i], 1) prev_status[i] = True status[i] = True query = Parking.update(status=True).where( Parking.id == index_to_id(i)) query.execute() else: GPIO.output(GPIO_LED[i], 0) prev_status[i] = False status[i] = False query = Parking.update(status=False).where( Parking.id == index_to_id(i)) query.execute() # Subsequently, only update DB if the status changes while True: for i in range(3): distance = ping(GPIO_TRIGGER[i], GPIO_ECHO[i]) if distance <= 7: GPIO.output(GPIO_LED[i], 1) status[i] = True else: GPIO.output(GPIO_LED[i], 0) status[i] = False print('previous status: ', prev_status) print('current status: ', status) for index, ps in enumerate(prev_status): if status[index] != ps: query = Parking.update(status=status[index]).where( Parking.id == index_to_id(index)) query.execute() prev_status = status.copy() time.sleep(1) except KeyboardInterrupt: print("Program Stopped") GPIO.cleanup()
def layout_id(): user_id = get_jwt_identity() mall_id = request.json.get('mall_id') mall_inst = Mall.get_by_id(mall_id) floors = Floor.select().where(Floor.mall_id == mall_inst.id).order_by(Floor.id.asc()) floor_arr = [] amount_arr = [] parking_arr = [] parking_arr1 = [] parking_arr2 = [] for floor in floors: floor_arr.append(floor.floor) amount_arr.append(' ('+str(floor.count_available())+')') # for parking in floor.parking: # parking_arr.append({"id": parking.id, "status": parking.status}) floor1 = Floor.get_or_none(floor = floor_arr[0]) floor2 = Floor.get_or_none(floor = floor_arr[1]) parking1 = Parking.select().where(Parking.floor_id == floor1.id).order_by(Parking.id.asc()) parking2 = Parking.select().where(Parking.floor_id == floor2.id).order_by(Parking.id.asc()) for i in parking1: parking_arr1.append({"id": i.id, "status": i.status}) for i in parking2: parking_arr2.append({"id": i.id, "status": i.status}) if mall_inst: responseObj = { 'status': 'success', 'user': user_id, 'mall': mall_inst.outlet, 'id': mall_inst.id, 'floor': floor_arr, 'parking1': parking_arr1, 'parking2': parking_arr2, 'amount': amount_arr } return jsonify(responseObj), 200 else: responseObj = { 'status': 'failed', 'message': 'Failed to access the mall layout' } return jsonify(responseObj), 400
def history_add(): user_id = get_jwt_identity() user_inst = User.get_by_id(user_id) parking_id = request.json.get('parking_id') parking_inst = Parking.get_by_id(parking_id) floor_inst = Floor.get_by_id(parking_inst.floor_id) mall_inst = Mall.get_by_id(floor_inst.mall_id) history_inst = History(user_id = user_id, parking_id = parking_id) if history_inst.save(): responseData = client.send_message({ 'from': 'Nexmo', 'to': user_inst.hp_number, 'text': 'RM0.00 EzPark: Your car is parked in Mall: [' + mall_inst.outlet + '], at Floor: [' + floor_inst.floor + '], in Parking Bay: [' + parking_inst.parking_num + ']///' }) # if responseData["messages"][0]["status"] == "0": responseObj = { 'status': 'success', 'message': 'Successfully saved your parking!' } return jsonify(responseObj), 200 # else: # responseObj = { # 'status': 'failed', # 'message': 'Message sent failed' # } # return jsonify(responseObj), 400 else: responseObj = { 'status': 'failed', 'message': 'Parking failed to save!' } return jsonify(responseObj), 400
def count_available(self): from models.parking import Parking return len(Parking.select().where(Parking.floor_id == self.id, Parking.status == False))
def count_total(self): from models.parking import Parking return len(Parking.select().where(Parking.floor_id == self.id))