|
| 1 | +import threading |
| 2 | +from .constants import * |
| 3 | + |
| 4 | + |
| 5 | +class ParkingLot: |
| 6 | + # singleton ParkingLot to ensure only one object of ParkingLot in the system, |
| 7 | + # all entrance panels will use this object to create new parking ticket: get_new_parking_ticket(), |
| 8 | + # similarly exit panels will also use this object to close parking tickets |
| 9 | + instance = None |
| 10 | + |
| 11 | + class __OnlyOne: |
| 12 | + def __init__(self, name, address): |
| 13 | + # 1. initialize variables: read name, address and parking_rate from database |
| 14 | + # 2. initialize parking floors: read the parking floor map from database, |
| 15 | + # this map should tell how many parking spots are there on each floor. This |
| 16 | + # should also initialize max spot counts too. |
| 17 | + # 3. initialize parking spot counts by reading all active tickets from database |
| 18 | + # 4. initialize entrance and exit panels: read from database |
| 19 | + |
| 20 | + self.__name = name |
| 21 | + self.__address = address |
| 22 | + self.__parking_rate = ParkingRate() |
| 23 | + |
| 24 | + self.__compact_spot_count = 0 |
| 25 | + self.__large_spot_count = 0 |
| 26 | + self.__motorbike_spot_count = 0 |
| 27 | + self.__electric_spot_count = 0 |
| 28 | + self.__max_compact_count = 0 |
| 29 | + self.__max_large_count = 0 |
| 30 | + self.__max_motorbike_count = 0 |
| 31 | + self.__max_electric_count = 0 |
| 32 | + |
| 33 | + self.__entrance_panels = {} |
| 34 | + self.__exit_panels = {} |
| 35 | + self.__parking_floors = {} |
| 36 | + |
| 37 | + # all active parking tickets, identified by their ticket_number |
| 38 | + self.__active_tickets = {} |
| 39 | + |
| 40 | + self.__lock = threading.Lock() |
| 41 | + |
| 42 | + def __init__(self, name, address): |
| 43 | + if not ParkingLot.instance: |
| 44 | + ParkingLot.instance = ParkingLot.__OnlyOne(name, address) |
| 45 | + else: |
| 46 | + ParkingLot.instance.__name = name |
| 47 | + ParkingLot.instance.__address = address |
| 48 | + |
| 49 | + def __getattr__(self, name): |
| 50 | + return getattr(self.instance, name) |
| 51 | + |
| 52 | + def get_new_parking_ticket(self, vehicle): |
| 53 | + if self.is_full(vehicle.get_type()): |
| 54 | + raise Exception('Parking full!') |
| 55 | + # synchronizing to allow multiple entrances panels to issue a new |
| 56 | + # parking ticket without interfering with each other |
| 57 | + self.__lock.acquire() |
| 58 | + ticket = ParkingTicket() |
| 59 | + vehicle.assign_ticket(ticket) |
| 60 | + ticket.save_in_DB() |
| 61 | + # if the ticket is successfully saved in the database, we can increment the parking spot count |
| 62 | + self.__increment_spot_count(vehicle.get_type()) |
| 63 | + self.__active_tickets.put(ticket.get_ticket_number(), ticket) |
| 64 | + self.__lock.release() |
| 65 | + return ticket |
| 66 | + |
| 67 | + def is_full(self, type): |
| 68 | + # trucks and vans can only be parked in LargeSpot |
| 69 | + if type == VehicleType.Truck or type == VehicleType.Van: |
| 70 | + return self.__large_spot_count >= self.__max_large_count |
| 71 | + |
| 72 | + # motorbikes can only be parked at motorbike spots |
| 73 | + if type == VehicleType.Motorbike: |
| 74 | + return self.__motorbike_spot_count >= self.__max_motorbike_count |
| 75 | + |
| 76 | + # cars can be parked at compact or large spots |
| 77 | + if type == VehicleType.Car: |
| 78 | + return (self.__compact_spot_count + self.__large_spot_count) >= (self.__max_compact_count + self.__max_large_count) |
| 79 | + |
| 80 | + # electric car can be parked at compact, large or electric spots |
| 81 | + return (self.__compact_spot_count + self.__large_spot_count + self.__electric_spot_count) >= (self.__max_compact_count + self.__max_large_count |
| 82 | + + self.__max_electric_count) |
| 83 | + |
| 84 | + # increment the parking spot count based on the vehicle type |
| 85 | + def increment_spot_count(self, type): |
| 86 | + large_spot_count = 0 |
| 87 | + motorbike_spot_count = 0 |
| 88 | + compact_spot_count = 0 |
| 89 | + electric_spot_count = 0 |
| 90 | + if type == VehicleType.Truck or type == VehicleType.Van: |
| 91 | + large_spot_count += 1 |
| 92 | + elif type == VehicleType.Motorbike: |
| 93 | + motorbike_spot_count += 1 |
| 94 | + elif type == VehicleType.Car: |
| 95 | + if self.__compact_spot_count < self.__max_compact_count: |
| 96 | + compact_spot_count += 1 |
| 97 | + else: |
| 98 | + large_spot_count += 1 |
| 99 | + else: # electric car |
| 100 | + if self.__electric_spot_count < self.__max_electric_count: |
| 101 | + electric_spot_count += 1 |
| 102 | + elif self.__compact_spot_count < self.__max_compact_count: |
| 103 | + compact_spot_count += 1 |
| 104 | + else: |
| 105 | + large_spot_count += 1 |
| 106 | + |
| 107 | + def is_full(self): |
| 108 | + for key in self.__parking_floors: |
| 109 | + if not self.__parking_floors.get(key).is_full(): |
| 110 | + return False |
| 111 | + return True |
| 112 | + |
| 113 | + def add_parking_floor(self, floor): |
| 114 | + # store in database |
| 115 | + None |
| 116 | + |
| 117 | + def add_entrance_panel(self, entrance_panel): |
| 118 | + # store in database |
| 119 | + None |
| 120 | + |
| 121 | + def add_exit_panel(self, exit_panel): |
| 122 | + # store in database |
| 123 | + None |
| 124 | + |
0 commit comments