Skip to content

Commit

Permalink
tried to imlement "send poi" feature, but it's not really working...
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n3rva committed Oct 7, 2018
1 parent 518b1a4 commit 2fbf795
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
17 changes: 16 additions & 1 deletion bimmer_connected/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import time
from bimmer_connected.account import ConnectedDriveAccount
from bimmer_connected.country_selector import get_region_from_name, valid_regions
from bimmer_connected.vehicle import VehicleViewDirection
from bimmer_connected.vehicle import VehicleViewDirection, PointOfInterest

FINGERPRINT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'vehicle_fingerprint')
Expand Down Expand Up @@ -41,6 +41,14 @@ def main() -> None:
image_parser.add_argument('vin', help='vehicle identification number')
image_parser.set_defaults(func=image)

sendpoi_parser = subparsers.add_parser('sendpoi', description='send a point of interest to the vehicle')
_add_default_arguments(sendpoi_parser )
sendpoi_parser.add_argument('vin', help='vehicle identification number')
sendpoi_parser.add_argument('latitude', help='latitude of the POI', type=float)
sendpoi_parser.add_argument('longitude', help='longitude of the POI', type=float)
sendpoi_parser.add_argument('name', help='(optional) name of the POI', nargs='?', default=None)
sendpoi_parser.set_defaults(func=send_poi)

args = parser.parse_args()
args.func(args)

Expand Down Expand Up @@ -106,6 +114,13 @@ def image(args) -> None:
print('vehicle image saved to image.png')


def send_poi(args) -> None:
account = ConnectedDriveAccount(args.username, args.password, get_region_from_name(args.region))
vehicle = account.get_vehicle(args.vin)
poi = PointOfInterest(args.latitude, args.longitude, name=args.name)
vehicle.send_poi(poi)


def _add_default_arguments(parser: argparse.ArgumentParser):
"""Add the default arguments username, password, region to the parser."""
parser.add_argument('username', help='Connected Drive user name')
Expand Down
1 change: 1 addition & 0 deletions bimmer_connected/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
REMOTE_SERVICE_STATUS_URL = VEHICLE_VIN_URL + '/serviceExecutionStatus?serviceType={service_type}'
REMOTE_SERVICE_URL = VEHICLE_VIN_URL + "/executeService"
VEHICLE_IMAGE_URL = VEHICLE_VIN_URL + "/image?width={width}&height={height}&view={view}"
VEHICLE_POI_URL = VEHICLE_VIN_URL + '/sendpoi'
52 changes: 50 additions & 2 deletions bimmer_connected/vehicle.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Models state and remote services of one vehicle."""
from enum import Enum
import logging
from typing import List
from typing import List, Dict
import json

from bimmer_connected.state import VehicleState, WINDOWS, LIDS
from bimmer_connected.remote_services import RemoteServices
from bimmer_connected.const import VEHICLE_IMAGE_URL
from bimmer_connected.const import VEHICLE_IMAGE_URL, VEHICLE_POI_URL

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -52,6 +53,41 @@ class LscType(Enum):
LSC_PHEV = 'LSC_PHEV'


class PointOfInterest:
"""Point of interest to be sent to the vehicle.
The latitude/longitude of a POI are mandatory, all other attributes are optional. CamelCase attribute names are
used here so that we do not have to convert the names between the attributes and the keys as expected on the server.
"""

def __init__(self, latitude: float, longitude: float, * , name: str = None):
"""Constructor.
:arg latitude: latitude of the POI
:arg longitude: longitude of the POI
:arg name: name of the POI (Optional)
"""
self.latitude = latitude # type: float
self.longitude = longitude # type: float
self.name = name # type: str
self.additionalInfo = None # type: str
self.street = "" # type: str
self.city = "" # type: str
self.postalCode = "" # type: str
self.country = "" # type: str
self.website = None # type: str
self.phoneNumbers = None # type: List[str]

@property
def as_json(self) -> Dict:
"""Convert to a dictionary so that it can be sent to the server."""
result = {
'lat': self.latitude,
'lon': self.longitude,
}
return {'data': result}


class ConnectedDriveVehicle:
"""Models state and remote services of one vehicle.
Expand Down Expand Up @@ -181,3 +217,15 @@ def set_observer_position(self, latitude: float, longitude: float) -> None:
raise ValueError('Either latitude AND longitude are set or none of them. You cannot set only one of them!')
self.observer_latitude = latitude
self.observer_longitude = longitude

def send_poi(self, poi: PointOfInterest) -> None:
"""Send a point of interest to the vehicle."""
url = VEHICLE_POI_URL.format(
vin=self.vin,
server=self._account.server_url
)
header = self._account.request_header
# the accept field of the header needs to be updated as we want a png not the usual JSON
header['Content-Type'] = 'application/x-www-form-urlencoded'
response = self._account.send_request(url, headers=header, data=poi.as_json, post=True)
print(response.content)

0 comments on commit 2fbf795

Please sign in to comment.