Skip to content

Commit

Permalink
Feature: Add vehicle action handler (#46)
Browse files Browse the repository at this point in the history
* Add new regions (#43)

* Add new regions

* Update README.md

* Feature: Add vehicle action handler (#45)

* Add new regions

* Update README.md

* Add new regions (#43) (#44)

* Add new regions

* Update README.md

* Update vehicle.py

* Update vehicle.py
  • Loading branch information
Ian authored Dec 27, 2021
1 parent 1665061 commit cbeb0c9
Showing 1 changed file with 53 additions and 52 deletions.
105 changes: 53 additions & 52 deletions connectedcar/vehicle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .api import Api
from . import const
import time


class Vehicle(object):
Expand Down Expand Up @@ -333,19 +334,7 @@ def start(self):
"""

job_id = self.api.action(
'PUT',
const.API_URL,
'vehicles/v2/' +
self.vehicle_id +
'/engine/start').json()['commandId']
response = self.api.action(
'GET',
const.API_URL,
'vehicles/' +
self.vehicle_id +
'/engine/start/' +
job_id)
response = self.action_handler('/engine/start/', 'PUT')
return response.json()

def stop(self):
Expand All @@ -359,19 +348,7 @@ def stop(self):
"""

job_id = self.api.action(
'DELETE',
const.API_URL,
'vehicles/v2/' +
self.vehicle_id +
'/engine/start').json()['commandId']
response = self.api.action(
'GET',
const.API_URL,
'vehicles/' +
self.vehicle_id +
'/engine/start/' +
job_id)
response = self.action_handler('/engine/start/', 'DELETE')
return response.json()

def lock(self):
Expand All @@ -385,19 +362,7 @@ def lock(self):
"""

job_id = self.api.action(
'PUT',
const.API_URL,
'vehicles/v2/' +
self.vehicle_id +
'/doors/lock').json()['commandId']
response = self.api.action(
'GET',
const.API_URL,
'vehicles/' +
self.vehicle_id +
'/doors/lock/' +
job_id)
response = self.action_handler('/doors/lock/', 'PUT')
return response.json()

def unlock(self):
Expand All @@ -411,17 +376,53 @@ def unlock(self):
"""

job_id = self.api.action(
'DELETE',
const.API_URL,
'vehicles/v2/' +
self.vehicle_id +
'/doors/lock').json()['commandId']
response = self.api.action(
'GET',
const.API_URL,
'vehicles/' +
self.vehicle_id +
'/doors/lock/' +
job_id)
response = self.action_handler('/doors/lock/', 'DELETE')
return response.json()

""" PUT/DELETE Vehicle.action_handler
Returns:
Response: returns response from the action request to the Ford API
Raises:
Exception
"""
def action_handler(self, context, method):
if (method == "PUT" or method == "DELETE"):
job_id = self.api.action(method, const.API_URL, 'vehicles/v2/' + self.vehicle_id + context).json()['commandId']
if (job_id):
return self.action_status_check(context, job_id)
else:
raise Exception("No job id returned")


""" GET Vehicle.action_status_check
Returns:
Response: returns the current status of the action request
Raises:
Exception
"""
def action_status_check(self, context, job_id):
success = False
attempts = 0
while (not success):
response = self.api.action(
'GET',
const.API_URL,
'vehicles/' +
self.vehicle_id +
context +
job_id)
if (response.json()['status'] == 200):
success = True
return response
else:
attempts += 1
if (attempts >= 30):
raise Exception("Timeout waiting for action to complete")
time.sleep(1)

0 comments on commit cbeb0c9

Please sign in to comment.