Skip to content

Commit

Permalink
Fixing linting errors and missing json encoding for payload.
Browse files Browse the repository at this point in the history
  • Loading branch information
freol35241 committed Jun 22, 2023
1 parent e03a1c5 commit 8540970
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This extension provides the necessary tools to be compatible with the dtaa forma

* **canboat2pontos**

Converts output from canboat's `analyzer --json` to pontos format, see [porla-nmea](https://github.com/MO-RISE/porla-nmea). Expects a single argument, the `vessel_id`. Expects input in the form `<epoch> <canboat json output>`.
Converts output from canboat's `analyzer --json` to pontos format, see [porla-nmea](https://github.com/MO-RISE/porla-nmea). Expects a single argument, the `vessel_id`. Expects input in the form `<epoch> <canboat json output>`. Outputs `<topic> <json payload>`, suitable for input to `mqtt-cli`, see [porla-mqtt](https://github.com/MO-RISE/porla-mqtt).

### 3rd-party tools

Expand Down
22 changes: 16 additions & 6 deletions bin/canboat2pontos
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#!/usr/bin/env python3

"""
canboat2pontos
Converting from NMEA2000 data in canboat json format to pontos format.
"""

# pylint: disable=redefined-outer-name

import sys
import json
import logging
import argparse
from typing import List, Dict, Tuple, Any

from parse import compile
import parse

## Handlers

Expand Down Expand Up @@ -70,7 +77,7 @@ HANDLERS = {
129029: handler_129029,
}

PATTERN = compile("{timestamp} {data}")
PATTERN = parse.compile("{timestamp} {data}")


## Parse command-line arguments
Expand All @@ -82,25 +89,28 @@ args = parser.parse_args()

# Define some helper functions
def create_topic(tag: str, index: int = 1, prefix: str = "PONTOS"):
"""Assembling mqtt topic for pontos-hub ingestion"""
return f"{prefix}/{args.vessel_id}/{tag}/{index}"


def create_payload(timestamp: str, value: Any) -> Dict:
return {"timestamp": timestamp, "value": value}
"""Creating JSON payload for pontos-hub ingestion"""
return json.dumps({"timestamp": timestamp, "value": value})


## Processing loop
for line in sys.stdin:
if not (res := PATTERN.parse(line)):
logging.error(
f"Line {line} did not look as expected: '<timestamp> <canboat json output>'"
"Line %s did not look as expected: '<timestamp> <canboat json output>'",
line,
)
continue

try:
data = json.loads(res["data"])
except json.JSONDecodeError:
logging.error(f"Failed to JSON decode data: {data}")
logging.error("Failed to JSON decode data: %s", res["data"])
continue

if pgn := data.get("pgn"):
Expand All @@ -111,4 +121,4 @@ for line in sys.stdin:

sys.stdout.flush()
else:
logging.warning(f"No handler found for PGN: {pgn}")
logging.warning("No handler found for PGN: %s", pgn)
18 changes: 9 additions & 9 deletions tests/pgn_127245_expected_output.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410546.672819', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410546.828808', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410546.973205', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410547.075819', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410547.172865', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410547.322846', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410547.478794', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410547.623891', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {'timestamp': '1687410547.772852', 'value': -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410546.672819", "value": -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410546.828808", "value": -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410546.973205", "value": -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410547.075819", "value": -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410547.172865", "value": -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410547.322846", "value": -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410547.478794", "value": -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410547.623891", "value": -3.8}
PONTOS/test_vessel/steering_angle_deg/1 {"timestamp": "1687410547.772852", "value": -3.8}

0 comments on commit 8540970

Please sign in to comment.