-
-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## Connect to OsmAnd API recipient | ||
|
||
Use OsmAnd APR recipient - e.g. [Traccar](https://www.traccar.org) - to track your device | ||
|
||
### Prerequisite | ||
1. A working last version of psa_car_controller | ||
2. Access to an OsmAnd API compatible receiver | ||
|
||
### Procedure | ||
1. Go to your OsmAnd receiver and setup a car which matches your VIN or use a self defined identifier | ||
2. Enable OsmAndAPI for your car in "Control" section | ||
3. stop psa_car_controller | ||
4. edit config.json and check the following: | ||
4.1: | ||
"osmandapi": { | ||
"osmand_enable_vin": [ | ||
"<VIN of you device - should automatically be here once activated in 2.>" | ||
], | ||
"server_uri": "https://<your osmand api endpoint>" | ||
}, | ||
5. If you want to use a self defined identifier (you can skip this if you id should be your VIN): Open cars.json | ||
3.1: set osmand_id to you your chosen device id | ||
|
||
11.4 save the file | ||
|
||
11.5 restart psa_car_controller | ||
6. Enjoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import logging | ||
from datetime import datetime | ||
|
||
import requests | ||
|
||
from psa_car_controller.psacc.model.car import Car | ||
|
||
logger = logging.getLogger(__name__) | ||
TIMEOUT_IN_S = 10 | ||
|
||
|
||
class OsmAndApi: | ||
def __init__(self, server_uri: str = None, osmand_enable_vin=None): | ||
if osmand_enable_vin is None: | ||
osmand_enable_vin = [] | ||
self.__server_uri = server_uri | ||
self.osmand_enable_vin = set(osmand_enable_vin) | ||
self.proxies = None | ||
|
||
def enable_osmand(self, vin, enable): | ||
if enable: | ||
self.osmand_enable_vin.add(vin) | ||
else: | ||
self.osmand_enable_vin.discard(vin) | ||
|
||
def call(self, car: Car, ext_temp: float = None): | ||
try: | ||
if car.vin in self.osmand_enable_vin: | ||
if self.__server_uri is None: | ||
logger.debug("osmandapi: No Server URI set") | ||
return False | ||
|
||
if not car.has_fuel() and not car.has_battery(): | ||
logger.debug("Neither fuel nor battery available") | ||
return False | ||
|
||
data = { | ||
"id": car.get_osmand_id(), | ||
"timestamp": int(datetime.timestamp(car.status.last_position.properties.updated_at)), | ||
"is_parked": not bool(car.status.is_moving()), | ||
"odometer": car.status.timed_odometer.mileage * 1000, | ||
"speed": getattr(car.status.kinetic, "speed", 0.0), | ||
"lat": car.status.last_position.geometry.coordinates[1], | ||
"lon": car.status.last_position.geometry.coordinates[0], | ||
"altitude": car.status.last_position.geometry.coordinates[2] | ||
} | ||
if car.has_fuel: | ||
fuel = car.status.get_energy('Fuel') | ||
data["fuel"] = fuel.level | ||
if car.has_battery(): | ||
energy = car.status.get_energy('Electric') | ||
if energy.battery and energy.battery.health: | ||
data["soh"] = energy.battery.health.resistance | ||
data["soc"] = energy.level | ||
data["batt"] = energy.level | ||
data["power"] = energy.consumption | ||
data["current"] = car.status.battery.current | ||
data["voltage"] = car.status.battery.voltage | ||
data["is_charging"] = energy.charging.status == "InProgress" | ||
data['est_battery_range'] = energy.autonomy | ||
|
||
if ext_temp is not None: | ||
data["ext_temp"] = ext_temp | ||
|
||
response = requests.request("POST", self.__server_uri, params=data, proxies=self.proxies, | ||
verify=self.proxies is None, timeout=TIMEOUT_IN_S) | ||
logger.debug(response.text) | ||
try: | ||
return response.status_code == 200 | ||
except (KeyError): | ||
logger.error("Bad response from OsmAnd API: %s", response.text) | ||
return False | ||
except (AttributeError, IndexError, ValueError): | ||
logger.exception("osmandapi:") | ||
return False | ||
|
||
def __iter__(self): | ||
yield "osmand_enable_vin", list(self.osmand_enable_vin) | ||
yield "server_uri", self.__server_uri |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters