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

replace hardcoded device info with the one from tedge config #21

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
36 changes: 32 additions & 4 deletions gittyup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ def get_device_type():
)


def get_topic_root_prefix() -> str:
return (
subprocess.check_output(["tedge", "config", "get", "mqtt.topic_root"])
.decode("utf-8")
.strip()
+ "/"
+ subprocess.check_output(["tedge", "config", "get", "mqtt.device_topic_id"])
.decode("utf-8")
.strip()
)


def get_mqtt_info() -> tuple[str, int]:
return (
subprocess.check_output(["tedge", "config", "get", "mqtt.client.host"])
.decode("utf-8")
.strip(),
int(
subprocess.check_output(["tedge", "config", "get", "mqtt.client.port"])
.decode("utf-8")
.strip()
),
)


class GittyUpClient:
"""GittyUp client

Expand All @@ -40,8 +65,8 @@ class GittyUpClient:

def __init__(self):
self.client = None
self.device_id = "main"
self.root = "te/device/main//"
self.device_id = get_device_type()
self.root = get_topic_root_prefix()
self.sub_topic = f"{self.root}/cmd/device_profile/+"

def connect(self):
Expand All @@ -61,7 +86,9 @@ def connect(self):
client.on_message = self.on_message
client.on_subscribe = self.on_subscribe

logger.debug(f"Trying to connect to the MQTT broker: host=localhost:1883")
(host, port) = get_mqtt_info()

logger.debug(f"Trying to connect to the MQTT broker: host={host}:{port}")

client.will_set(
"te/device/main/service/gittyup/status/health",
Expand All @@ -70,7 +97,7 @@ def connect(self):
retain=True,
)

client.connect("localhost", 1883)
client.connect(host, port)
client.loop_start()

self.client = client
Expand Down Expand Up @@ -256,6 +283,7 @@ def clone_or_pull_repo(repo: GitOpsRepository, clone_dir="repo") -> Optional[str
logger.addHandler(handler)

client = GittyUpClient()

try:
client.connect()
# Wait for connection
Expand Down