Skip to content

Commit

Permalink
Merge pull request #21 from thin-edge/get-info-from-tedge-config
Browse files Browse the repository at this point in the history
replace hardcoded device info with the one from tedge config
  • Loading branch information
jarhodes314 authored Sep 17, 2024
2 parents 7af9a50 + bf2f04e commit 726e2e1
Showing 1 changed file with 32 additions and 4 deletions.
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

0 comments on commit 726e2e1

Please sign in to comment.