Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use common environment variable names #24

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions couchdb-saver.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
PROJECT_NAME=<PROJECT_NAME>
HOSTNAME=<HOSTNAME>
MQTT_IP=mqtt
SENSOR_SAVE_TOPIC=/${PROJECT_NAME}/${HOSTNAME}/Sensor_Save/edgetech-audio-recorder/string
TELEMETRY_TOPIC=/${PROJECT_NAME}/${HOSTNAME}/Telemetry/edgetech-audio-recorder/string
AUDIO_SAVE_TOPIC=/${PROJECT_NAME}/${HOSTNAME}/Audio_Save/edgetech-audio-recorder/string
COUCHDB_ERROR_TOPIC=/${PROJECT_NAME}/${HOSTNAME}/CouchDB_Error/edgetech-audio-recorder/string
COUCHDB_USER=<COUCHDB_USER>
COUCHDB_PASSWORD=<COUCHDB_PASSWORD>
COUCHDB_SERVER_IP_ADDR=<COUCHDB_SERVER_IP_ADDR>
DEVICE_IP=<DEVICE_IP>
LOG_LEVEL=INFO
31 changes: 27 additions & 4 deletions couchdb-saver/couchdb_saver_pub_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import os
import json
import logging
from time import sleep
from typing import Any, Dict

Expand All @@ -23,6 +24,7 @@ class CouchDBSaverPubSub(BaseMQTTPubSub):
BaseMQTTPubSub (BaseMQTTPubSub): parent class written in the EdgeTech Core module
"""

# TODO: Include HOSTNAME?
def __init__(
self: Any,
sensor_save_topic: str,
Expand All @@ -34,6 +36,7 @@ def __init__(
couchdb_server_ip: str,
device_ip: str,
debug: bool = False,
log_level: str = "INFO",
**kwargs: Any,
) -> None:
"""The CouchDBSaverPubSub takes MQTT topics to write data from and CouchDB authentication
Expand All @@ -50,6 +53,8 @@ def __init__(
couchdb_user (str): the username for CouchDB authentication
couchdb_password (str): the password for CouchDB authentication
couchdb_server_ip (str): the IP address for couchDB authentication
log_level (str): One of 'NOTSET', 'DEBUG', 'INFO', 'WARN',
'WARNING', 'ERROR', 'FATAL', 'CRITICAL'
device_ip (str): the IP of the current device
"""
# to override any of the BaseMQTTPubSub attributes
Expand All @@ -65,6 +70,7 @@ def __init__(
self.couchdb_server_ip = couchdb_server_ip
self.device_ip = device_ip
self.debug = debug
self.log_level = log_level

# open schema file for validation
with open("couchdb_saver.schema", "r", encoding="utf-8") as file_pointer:
Expand All @@ -75,6 +81,22 @@ def __init__(
sleep(1)
self.publish_registration("CouchDB Saver Registration")

# Log configuration parameters
logging.info(
f"""CouchDBSaverPubSub initialized with parameters:
sensor_save_topic = {sensor_save_topic}
telemetry_save_topic = {telemetry_save_topic}
audio_save_topic = {audio_save_topic}
couchdb_error_topic = {couchdb_error_topic}
couchdb_user = {couchdb_user}
couchdb_password = {couchdb_password}
couchdb_server_ip = {couchdb_server_ip}
device_ip = {device_ip}
debug = {debug}
log_level = {log_level}
"""
)

def _to_save_callback(
self: Any, _client: mqtt.Client, _userdata: Dict[Any, Any], msg: Any
) -> None:
Expand Down Expand Up @@ -137,14 +159,15 @@ def main(self: Any) -> None:

if __name__ == "__main__":
saver = CouchDBSaverPubSub(
sensor_save_topic=str(os.environ.get("SENSOR_TOPIC")),
telemetry_save_topic=str(os.environ.get("TELEMETRY_TOPIC")),
audio_save_topic=str(os.environ.get("AUDIO_TOPIC")),
mqtt_ip=str(os.environ.get("MQTT_IP")),
sensor_save_topic=str(os.environ.get("SENSOR_SAVE_TOPIC")),
telemetry_save_topic=str(os.environ.get("TELEMETRY_SAVE_TOPIC")),
audio_save_topic=str(os.environ.get("AUDIO_SAVE_TOPIC")),
couchdb_error_topic=str(os.environ.get("COUCHDB_ERROR_TOPIC")),
couchdb_user=str(os.environ.get("COUCHDB_USER")),
couchdb_password=str(os.environ.get("COUCHDB_PASSWORD")),
couchdb_server_ip=str(os.environ.get("COUCHDB_SERVER_IP_ADDR")),
device_ip=str(os.environ.get("DEVICE_IP")),
mqtt_ip=str(os.environ.get("MQTT_IP")),
log_level=str(os.environ.get("LOG_LEVEL")),
)
saver.main()
13 changes: 7 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ services:
build:
context: ./couchdb-saver
dockerfile: ./Dockerfile
restart: unless-stopped
volumes:
- /home/mobian/sensor-data:/sensor-data
restart: unless-stopped
depends_on:
- mqtt
env_file:
- .env
logging:
driver: "json-file"
options:
max-size: "10M"
max-file: "10"
max-file: "10"
depends_on:
- mqtt
env_file:
- path: ./couchdb-saver.env
required: false
10 changes: 0 additions & 10 deletions example.env

This file was deleted.