Skip to content

Commit

Permalink
gateway: publish sw update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
larsfroelich committed Jan 24, 2025
1 parent 822c40f commit ec4fb13
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions software/gateway/src/modules/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ def start_controller(self, version_to_launch: Optional[str] = None) -> None:
print("[DOCKER-CLIENT][FATAL] No previous version available to build from")
print("[DOCKER-CLIENT][FATAL] Requesting version from ThingsBoard...")
GatewayMqttClient().publish('v1/devices/me/attributes/request/1', '{"sharedKeys":"sw_title,sw_url,sw_version"}')
GatewayMqttClient().publish_sw_state(
version_to_launch,"FAILED",
"No previous version available to build from, requested version info from ThingsBoard")
print("[DOCKER-CLIENT][FATAL] Delaying main loop by 30s...")
sleep(30) # it is unlikely that the version to build will be available immediately
return
print("[DOCKER-CLIENT] Building image for version '" + version_to_launch + "'")
GatewayMqttClient().publish_sw_state(version_to_launch, "DOWNLOADING")
GatewayGitClient().execute_fetch()
commit_hash = GatewayGitClient().get_commit_from_hash_or_tag(version_to_launch)
if commit_hash is None:
Expand All @@ -118,6 +122,7 @@ def start_controller(self, version_to_launch: Optional[str] = None) -> None:
else:
print("[DOCKER-CLIENT][FATAL] Unable to reset to commit " + commit_hash)
return
GatewayMqttClient().publish_sw_state(version_to_launch, "DOWNLOADED")
build_result = self.docker_client.images.build(
path=os.path.join(os.path.dirname(ACROPOLIS_GATEWAY_GIT_PATH), "software/controller"),
dockerfile="./docker/Dockerfile",
Expand All @@ -133,6 +138,7 @@ def start_controller(self, version_to_launch: Optional[str] = None) -> None:
print("[DOCKER-CLIENT][FATAL] Version to launch is 'unknown', requesting version from ThingsBoard...")
GatewayMqttClient().publish('v1/devices/me/attributes/request/1', '{"sharedKeys":"sw_title,sw_url,sw_version"}')

GatewayMqttClient().publish_sw_state(version_to_launch, "UPDATING")
# remove old containers and start the new one
self.last_launched_version = version_to_launch
self.prune_containers()
Expand Down Expand Up @@ -174,4 +180,5 @@ def start_controller(self, version_to_launch: Optional[str] = None) -> None:
},
}
)
GatewayMqttClient().publish_sw_state(version_to_launch, "UPDATED")
print("[DOCKER-CLIENT] Started Acropolis Edge container with version '" + version_to_launch + "'")
10 changes: 9 additions & 1 deletion software/gateway/src/modules/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import time
from queue import Queue
from typing import Any, Optional
from typing import Any, Optional, Union

from paho.mqtt.client import Client

Expand Down Expand Up @@ -75,6 +75,14 @@ def __on_message(self, _client, _userdata, msg) -> None:
"payload": json.loads(msg.payload)
})

def publish_sw_state(self, version: str, state: str, msg : Optional[str]=None) -> None:
self.publish_message(json.dumps({
"current_sw_title": version,
"current_sw_version": version,
"sw_state": state,
"sw_error": msg or ""
}))

def publish_message(self, message: str) -> bool:
return self.publish_message_raw("v1/devices/me/telemetry", message)

Expand Down

0 comments on commit ec4fb13

Please sign in to comment.