Skip to content

Commit

Permalink
Merge pull request #40 from ralatsdc/rl/upstream-use-common-environme…
Browse files Browse the repository at this point in the history
…nt-variable-names

Use common environment variable names
  • Loading branch information
robotastic authored Feb 22, 2024
2 parents 27cb9d4 + 59968d2 commit 584c8c2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
10 changes: 5 additions & 5 deletions daisy.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PROJECT_NAME=<PROJECT_NAME>
HOSTNAME=<HOSTNAME>
MQTT_IP=mqtt
HOSTNAME=skyscan-011
AIS_SERIAL_PORT=/dev/serial0
BYTESTRING_OUTPUT_TOPIC=/Multimodal/${HOSTNAME}/AIS/edgetech-daisy/bytestring
JSON_OUTPUT_TOPIC=/Multimodal/${HOSTNAME}/AIS/edgetech-daisy/JSON
LOG_LEVEL="DEBUG"
DAISY_SERIAL_PORT=/dev/serial0
AIS_BYTESTRING_TOPIC=/${PROJECT_NAME}/${HOSTNAME}/AIS_Bytestring/edgetech-daisy/bytestring
CONTINUE_ON_EXCEPTION=False
LOG_LEVEL=INFO
65 changes: 36 additions & 29 deletions daisy/dAISy_pub_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class DAISyPubSub(BaseMQTTPubSub):
def __init__(
self: Any,
hostname: str,
serial_port: str,
bytestring_output_topic: str,
# json_output_topic: str,
daisy_serial_port: str,
ais_bytestring_topic: str,
log_level: str = "INFO",
continue_on_exception: bool = False,
**kwargs: Any,
):
Expand All @@ -44,20 +44,20 @@ def __init__(
Args:
hostname (str): Name of host
serial_port (str): a serial port to subscribe
daisy_serial_port (str): a serial port to subscribe
to. Specified via docker-compose.
bytestring_output_topic (str): MQTT topic on which to
ais_bytestring_topic (str): MQTT topic on which to
publish AIS bytestring data
json_output_topic (str): MQTT topic on which to publish
AIS JSON data
log_level (str): One of 'NOTSET', 'DEBUG', 'INFO', 'WARN',
'WARNING', 'ERROR', 'FATAL', 'CRITICAL'
continue_on_exception (bool): Continue on unhandled
exceptions if True, raise exception if False (the default)
"""
super().__init__(**kwargs)
self.hostname = hostname
self.serial_port = serial_port
self.bytestring_output_topic = bytestring_output_topic
# self.json_output_topic = json_output_topic
self.daisy_serial_port = daisy_serial_port
self.ais_bytestring_topic = ais_bytestring_topic
self.log_level = log_level
self.continue_on_exception = continue_on_exception

# Connect to the MQTT client
Expand All @@ -68,6 +68,17 @@ def __init__(
# Setup the serial connection
self._connect_serial()

# Log configuration parameters
logging.info(
f"""DAISyPubSub initialized with parameters:
hostname = {hostname}
daisy_serial_port = {daisy_serial_port}
ais_bytestring_topic = {ais_bytestring_topic}
log_level = {log_level}
continue_on_exception = {continue_on_exception}
"""
)

def decode_payload(
self, msg: Union[mqtt.MQTTMessage, str], data_payload_type: str
) -> Dict[Any, Any]:
Expand Down Expand Up @@ -99,15 +110,15 @@ def _connect_serial(self: Any) -> None:
"""
# Setup serial connection without blocking
# dAISy default baud is 38400
self.serial = serial.Serial(self.serial_port, timeout=0, baudrate=38400)
logging.info(f"Connected to Serial Bus on {self.serial_port}")
self.serial = serial.Serial(self.daisy_serial_port, timeout=0, baudrate=38400)
logging.info(f"Connected to Serial Bus on {self.daisy_serial_port}")

def _disconnect_serial(self: Any) -> None:
"""Disconnects the serial connection using python's serial
package.
"""
self.serial.close()
logging.info(f"Disconnected from Serial Bus on {self.serial_port}")
logging.info(f"Disconnected from Serial Bus on {self.daisy_serial_port}")

def _send_data(self: Any, data: Dict[str, str]) -> bool:
"""Leverages edgetech-core functionality to publish a JSON
Expand Down Expand Up @@ -136,20 +147,16 @@ def _send_data(self: Any, data: Dict[str, str]) -> bool:
data_payload=data["payload"],
)

# Publish the data as JSON to the topic by type
# if data["type"] == "Binary AIS":
send_data_topic = self.bytestring_output_topic
success = self.publish_to_topic(send_data_topic, out_json)

# elif data["type"] == "Decoded AIS":
# send_data_topic = self.json_output_topic

# Publish the output JSON to the topic
success = self.publish_to_topic(self.ais_bytestring_topic, out_json)
if success:
logging.info(f"Successfully sent data on channel {send_data_topic}: {data}")
logging.info(
f"Successfully sent data on channel {self.ais_bytestring_topic}: {data}"
)
else:
logging.info(f"Failed to send data on channel {send_data_topic}: {data}")

# Return True if successful else False
logging.info(
f"Failed to send data on channel {self.ais_bytestring_topic}: {data}"
)
return success

def process_serial_payload(self, binary_payload: str) -> None:
Expand Down Expand Up @@ -250,11 +257,11 @@ def main(self: Any) -> None:
if __name__ == "__main__":
# Instantiate DAISyPubSub and execute
daisy = DAISyPubSub(
mqtt_ip=os.environ.get("MQTT_IP", ""),
hostname=os.environ.get("HOSTNAME", ""),
serial_port=os.environ.get("AIS_SERIAL_PORT", ""),
bytestring_output_topic=os.environ.get("BYTESTRING_OUTPUT_TOPIC", ""),
# json_output_topic=os.environ.get("JSON_OUTPUT_TOPIC", ""),
mqtt_ip=os.environ.get("MQTT_IP", ""),
daisy_serial_port=os.environ.get("DAISY_SERIAL_PORT", ""),
ais_bytestring_topic=os.environ.get("AIS_BYTESTRING_TOPIC", ""),
log_level=os.environ.get("LOG_LEVEL", "INFO"),
continue_on_exception=ast.literal_eval(
os.environ.get("CONTINUE_ON_EXCEPTION", "False")
),
Expand Down
17 changes: 16 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
version: "3"
services:
mqtt:
image: iqtlabs/edgetech-mqtt:latest
ports:
- "1883:1883"
- "9001:9001"
restart: unless-stopped
logging:
driver: "json-file"
options:
max-size: "10M"
max-file: "10"
daisy:
image: iqtlabs/edgetech-daisy:latest
devices:
Expand All @@ -13,5 +24,9 @@ services:
options:
max-size: "10M"
max-file: "10"
depends_on:
- mqtt
env_file:
- daisy-wo-mqtt.env
- .env
- path: ./daisy.env
required: false

0 comments on commit 584c8c2

Please sign in to comment.