Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-iqt committed Nov 21, 2023
1 parent de42587 commit 529cb87
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 160 deletions.
87 changes: 45 additions & 42 deletions gamutrf/mqtt_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(
self.compass = compass
self.gps_server = gps_server
self.mqttc = None
self.heading = 'no heading'
self.heading = "no heading"
self.use_external_gps = use_external_gps
self.use_external_heading = use_external_heading
self.external_gps_server = external_gps_server
Expand All @@ -37,16 +37,16 @@ def __init__(
def log(path, prefix, start_time, record_args):
try:
with open(
os.path.join(path, f'mqtt-{prefix}-{start_time}.log'),
'a+',
encoding='utf-8',
os.path.join(path, f"mqtt-{prefix}-{start_time}.log"),
"a+",
encoding="utf-8",
) as f:
f.write(f'{json.dumps(record_args)}\n')
f.write(f"{json.dumps(record_args)}\n")
except FileNotFoundError as err:
logging.error(f'could not write to mqtt rssi log: {err}')
logging.error(f"could not write to mqtt rssi log: {err}")

def connect(self):
logging.info(f'connecting to {self.mqtt_server}')
logging.info(f"connecting to {self.mqtt_server}")
self.mqttc = mqtt.Client()
self.mqttc.connect(self.mqtt_server)
self.mqttc.loop_start()
Expand All @@ -57,37 +57,41 @@ def get_heading(self):
self.heading = float(
json.loads(
httpx.get(
f'http://{self.external_gps_server}:{self.external_gps_server_port}/heading'
f"http://{self.external_gps_server}:{self.external_gps_server_port}/heading"
).text
)['heading']
)["heading"]
)
except Exception as err:
logging.error('could not update external heading: %s', err)
logging.error("could not update external heading: %s", err)
else:
try:
self.heading = str(
float(
httpx.get(f'http://{self.gps_server}:8000/v1/heading').text)
float(httpx.get(f"http://{self.gps_server}:8000/v1/heading").text)
)
except Exception as err:
logging.error('could not update heading: %s', err)
logging.error("could not update heading: %s", err)

def add_gps(self, publish_args):
if not self.gps_server and not self.external_gps_server:
logging.error('no gps_server or external_gps_server found')
logging.error("no gps_server or external_gps_server found")
return publish_args
if self.external_gps_server and not self.use_external_gps and not self.gps_server:
if (
self.external_gps_server
and not self.use_external_gps
and not self.gps_server
):
logging.error(
'only external_gps_server found, but no use_external_gps flag')
"only external_gps_server found, but no use_external_gps flag"
)
return publish_args
publish_args.update(
{
'position': [0, 0],
'altitude': None,
'gps_time': None,
'map_url': None,
'heading': self.heading,
'gps': 'no fix',
"position": [0, 0],
"altitude": None,
"gps_time": None,
"map_url": None,
"heading": self.heading,
"gps": "no fix",
}
)

Expand All @@ -96,26 +100,26 @@ def add_gps(self, publish_args):
try:
self.external_gps_msg = json.loads(
httpx.get(
f'http://{self.external_gps_server}:{self.external_gps_server_port}/gps-data'
f"http://{self.external_gps_server}:{self.external_gps_server_port}/gps-data"
).text
)

publish_args.update(
{
'position': (
self.external_gps_msg['latitude'],
self.external_gps_msg['longitude'],
"position": (
self.external_gps_msg["latitude"],
self.external_gps_msg["longitude"],
),
'altitude': self.external_gps_msg['altitude'],
'gps_time': self.external_gps_msg['time_usec'],
'map_url': None,
'heading': self.heading,
'gps': 'fix',
"altitude": self.external_gps_msg["altitude"],
"gps_time": self.external_gps_msg["time_usec"],
"map_url": None,
"heading": self.heading,
"gps": "fix",
}
)

except Exception as err:
logging.error('could not update with external GPS: %s', err)
logging.error("could not update with external GPS: %s", err)

# Use internal GPIO GPS
else:
Expand All @@ -127,16 +131,16 @@ def add_gps(self, publish_args):
packet = gpsd.get_current()
publish_args.update(
{
'position': packet.position(),
'altitude': packet.altitude(),
'gps_time': packet.get_time().timestamp(),
'map_url': packet.map_url(),
'heading': self.heading,
'gps': 'fix',
"position": packet.position(),
"altitude": packet.altitude(),
"gps_time": packet.get_time().timestamp(),
"map_url": packet.map_url(),
"heading": self.heading,
"gps": "fix",
}
)
except (BrokenPipeError, gpsd.NoFixError, AttributeError) as err:
logging.error('could not update with GPS: %s', err)
logging.error("could not update with GPS: %s", err)
return publish_args

def publish(self, publish_path, publish_args):
Expand All @@ -146,13 +150,12 @@ def publish(self, publish_path, publish_args):
if self.mqttc is None:
self.connect()
publish_args = self.add_gps(publish_args)
publish_args['name'] = self.name
publish_args["name"] = self.name
self.mqttc.publish(publish_path, json.dumps(publish_args))
except (
socket.gaierror,
ConnectionRefusedError,
mqtt.WebsocketConnectionError,
ValueError,
) as err:
logging.error(
f'failed to publish to MQTT {self.mqtt_server}: {err}')
logging.error(f"failed to publish to MQTT {self.mqtt_server}: {err}")
Loading

0 comments on commit 529cb87

Please sign in to comment.