Skip to content

Commit 9455c03

Browse files
authored
Merge pull request #360 from nanomad/develop
OsmAnd: Default to knots as a unit of measure
2 parents 4531df3 + 97b82a9 commit 9455c03

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Those parameters can be used to allow the MQTT Gateway to send data to an OsmAnd
8787
|---------------------------|---------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
8888
| --osmand-server-uri | OSMAND_SERVER_URI | The URL of your OsmAnd Server |
8989
| --osmand-device-id | OSMAND_DEVICE_ID | Mapping of VIN to OsmAnd Device Id. Multiple mappings can be provided separated by ',' Example: LSJXXXX=12345-abcdef,LSJYYYY=67890-ghijkl. Defaults to use the car VIN as Device Id if unset |
90+
| --osmand-use-knots | OSMAND_USE_KNOTS | Whether to use knots of kph as a speed unit in OsmAnd messages. Enabled (True) by default to ensure compatibilty with Traccar. | |
9091
| --publish-raw-osmand-data | PUBLISH_RAW_OSMAND_DATA_ENABLED | Publish raw ABRP OSMAND request/response to MQTT. Disabled (False) by default. |
9192

9293
### OpenWB Integration

src/configuration/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self) -> None:
5454
# OsmAnd Integration
5555
self.osmand_device_id_map: dict[str, str] = {}
5656
self.osmand_server_uri: str | None = None
57+
self.osmand_use_knots: bool = True
5758
self.publish_raw_osmand_data: bool = False
5859

5960
@property

src/configuration/parser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,16 @@ def __setup_abrp(args: Namespace, config: Configuration) -> None:
144144

145145
def __setup_osmand(args: Namespace, config: Configuration) -> None:
146146
config.osmand_server_uri = args.osmand_server_uri
147+
147148
if args.osmand_device_id:
148149
cfg_value_to_dict(args.osmand_device_id, config.osmand_device_id_map)
150+
149151
if args.publish_raw_osmand_data is not None:
150152
config.publish_raw_osmand_data = args.publish_raw_osmand_data
151153

154+
if args.osmand_use_knots is not None:
155+
config.osmand_use_knots = args.osmand_use_knots
156+
152157

153158
def __setup_parser() -> argparse.ArgumentParser:
154159
parser = argparse.ArgumentParser(prog="MQTT Gateway")
@@ -434,6 +439,18 @@ def __setup_parser() -> argparse.ArgumentParser:
434439
action=EnvDefault,
435440
envvar="OSMAND_DEVICE_ID",
436441
)
442+
parser.add_argument(
443+
"--osmand-use-knots",
444+
help="Whether to use knots of kph as a speed unit in OsmAnd messages. "
445+
"Enabled (True) by default to ensure compatibilty with Traccar. "
446+
"Environment Variable: OSMAND_USE_KNOTS",
447+
dest="osmand_use_knots",
448+
required=False,
449+
action=EnvDefault,
450+
envvar="OSMAND_USE_KNOTS",
451+
default=True,
452+
type=check_bool,
453+
)
437454
parser.add_argument(
438455
"--publish-raw-osmand-data",
439456
help="Publish raw ABRP OsmAnd request/response to MQTT. Environment Variable: "

src/handlers/vehicle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def __setup_osmand(
116116
return OsmAndApi(
117117
server_uri=self.configuration.osmand_server_uri,
118118
device_id=osmand_device_id,
119+
use_knots=self.configuration.osmand_use_knots,
119120
listener=api_listener,
120121
)
121122

src/integrations/osmand/api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ def __init__(
5050
*,
5151
server_uri: str,
5252
device_id: str,
53+
use_knots: bool,
5354
listener: OsmAndApiListener | None = None,
5455
) -> None:
5556
self.__device_id = device_id
5657
self.__listener = listener
5758
self.__server_uri = server_uri
59+
self.__use_knots = use_knots
5860
self.client = httpx.AsyncClient(
5961
event_hooks={
6062
"request": [self.invoke_request_listener],
@@ -158,12 +160,11 @@ def __extract_basic_vehicle_status(
158160

159161
if basic_vehicle_status.is_parked:
160162
# We assume the vehicle is stationary, we will update it later from GPS if available
161-
data["speed"] = (0.0,)
163+
data["speed"] = 0.0
162164

163165
return data
164166

165-
@staticmethod
166-
def __extract_gps_position(gps_position: GpsPosition) -> dict[str, Any]:
167+
def __extract_gps_position(self, gps_position: GpsPosition) -> dict[str, Any]:
167168
data: dict[str, Any] = {}
168169

169170
# Do not use GPS data if it is not available
@@ -176,7 +177,8 @@ def __extract_gps_position(gps_position: GpsPosition) -> dict[str, Any]:
176177

177178
speed = way_point.speed
178179
if speed is not None and value_in_range(speed, -999, 4500):
179-
data["speed"] = speed / 10
180+
actual_speed = speed * 0.0539956803 if self.__use_knots else speed / 10.0
181+
data["speed"] = actual_speed
180182

181183
heading = way_point.heading
182184
if heading is not None and value_in_range(heading, 0, 360):

0 commit comments

Comments
 (0)