Skip to content

Commit

Permalink
Vehicles Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
athuler committed Aug 18, 2024
1 parent ee3c31c commit 2c1556d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

### Added

- Testing for every transportation system
- `Vehicle` object
- `TransportationSystem.getVehicles()` which returns a list of `Vehicle` objects

### Changed

- Fixed `AttributeError: 'list' object has no attribute 'items'` error thrown in `TransportationSystem.getRoutes()`
Expand Down
131 changes: 100 additions & 31 deletions passiogo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,65 @@ def getSystemAlerts(

return(allAlerts)

def getVehicles(
self,
paramDigit = 2
) -> list["Vehicle"]:
"""
Gets all currently running buses.
=========
s0: system from which to get content
paramDigit:
0: Error
>=1: Valid
"""


# Initialize & Send Request
url = BASE_URL+"/mapGetData.php?getBuses="+str(paramDigit)
body = {
"s0" : str(self.id),
"sA" : 1
}
vehicles = sendApiRequest(url, body)

# Handle Request Error
if(vehicles == None):
return(None)

allVehicles = []
for vehicleId, vehicle in vehicles["buses"].items():
if vehicleId == '-1':
continue

vehicle = vehicle[0]

for key in ["busId", "busName", "busType", "calculatedCourse", "routeId", "route", "color", "created", "latitude", "longitude", "speed", "paxLoad100", "outOfService", "more", "tripId"]:
if key not in vehicle:
vehicle[key] = None


allVehicles.append(Vehicle(
id = vehicle["busId"],
name = vehicle["busName"],
type = vehicle["busType"],
system = self,
calculatedCourse = vehicle["calculatedCourse"],
routeId = vehicle["routeId"],
routeName = vehicle["route"],
color = vehicle["color"],
created = vehicle["created"],
latitude = vehicle["latitude"],
longitude = vehicle["longitude"],
speed = vehicle["speed"],
paxLoad = vehicle["paxLoad100"],
outOfService = vehicle["outOfService"],
more = vehicle["more"],
tripId = vehicle["tripId"],
))

return(allVehicles)


def getSystems(
appVersion = 2,
Expand Down Expand Up @@ -494,6 +553,7 @@ def getStops(self):

return(stopsForRoute)


### Stops ###

class Stop:
Expand Down Expand Up @@ -521,7 +581,6 @@ def __init__(
self.radius = radius
self.system = system



### System Alerts ###

Expand Down Expand Up @@ -588,38 +647,49 @@ def __init__(
self.fromF = fromF
self.fromOk = fromOk
self.toOk = toOk




### Vehicles ###

class Vehicle:

def __init__(
self,
id: str = None,
name: str = None,
type: str = None,
system: TransportationSystem = None,
calculatedCourse: int = None,
routeId: str = None,
routeName: str = None,
color: str = None,
created: str = None,
latitude: float = None,
longitude: float = None,
speed: float = None,
paxLoad: float = None,
outOfService: int = None,
more: str = None,
tripId: str = None,
):
self.id = id
self.name = name
self.type = type
self.system = system
self.calculatedCourse = calculatedCourse
self.routeId = routeId
self.routeName = routeName
self.color = color
self.created = created
self.longitude = latitude
self.speed = speed
self.paxLoad = paxLoad
self.outOfService = outOfService
self.more = more
self.tripId = tripId


def getBuses(
systemSelected,
paramDigit = 2
):
"""
Gets all currently running buses.
=========
s0: system from which to get content
paramDigit:
0: Error
>=1: Valid
"""


# Initialize & Send Request
url = BASE_URL+"/mapGetData.php?getBuses="+str(paramDigit)
body = {
"s0" : str(systemSelected),
"sA" : 1
}
buses = sendApiRequest(url, body)

# Handle Request Error
if(buses == None):
return(None)

return(buses)


### Live Timings ###
Expand Down Expand Up @@ -650,12 +720,11 @@ def handleWsError(wsapp, error):

def handleWsClose(wsapp, close_status_code, close_msg):
wsapp.close()
...


def subscribeWS(
wsapp,
userId = 1068
userId
):

subscriptionMsg = {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_getAllStops(system):
def test_getSystemAlerts(system):
system.getSystemAlerts()


def test_getBuses():
passiogo.getBuses(1068)
@pytest.mark.parametrize("system", pytest.allSystems, ids=ids)
def test_getVehicles(system):
system.getVehicles()

0 comments on commit 2c1556d

Please sign in to comment.