From 782e6736865f99e0881f1df0752eebd18d6cfb3b Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Wed, 10 Sep 2025 10:01:30 +0200 Subject: [PATCH] Add status of octoprint container Signed-off-by: Thomas Klaehn --- Makefile | 2 +- src/internal/apiservice/printer/printer.go | 4 +- .../app/process/octoprint/octoprint.go | 50 +++++++++++- webui/index.html | 78 +++++++++++++------ 4 files changed, 108 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 7f6f6fc..443ca4c 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ service: .PHONY: clean clean: go clean - rm -rf bin + rm -rf build -rm $(PROJECT_NAME).tar.gz .PHONY: install diff --git a/src/internal/apiservice/printer/printer.go b/src/internal/apiservice/printer/printer.go index f6db7c4..859a16e 100644 --- a/src/internal/apiservice/printer/printer.go +++ b/src/internal/apiservice/printer/printer.go @@ -14,7 +14,8 @@ import ( type state string type State struct { - State state `json:"state"` + State state `json:"state"` + DockerState octoprint.DockerState `json:"octoprint_containerstate"` } const ( @@ -56,6 +57,7 @@ func handle_get_printerstate(w http.ResponseWriter, r *http.Request) { } else { res.State = StateOff } + res.DockerState = octoprint.Status() tmp, err := json.Marshal(res) if err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/src/internal/app/process/octoprint/octoprint.go b/src/internal/app/process/octoprint/octoprint.go index 7fe8d74..81d69d7 100644 --- a/src/internal/app/process/octoprint/octoprint.go +++ b/src/internal/app/process/octoprint/octoprint.go @@ -3,11 +3,23 @@ package octoprint import ( "log" "powerswitch/internal/app/process" + "strings" ) +type DockerState struct { + ContainerID string `json:"container_id"` + Image string `json:"image"` + Command string `json:"command"` + Created string `json:"created"` + Status string `json:"status"` + Ports string `json:"ports"` + Name string `json:"name"` +} + const ( - start_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml up -d" - stop_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml down" + start_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml up -d" + stop_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml down" + status_cmd = "docker ps" ) var ( @@ -35,3 +47,37 @@ func ReStart() error { } return Start() } + +func Status() DockerState { + var state DockerState + state.Status = "off" + p := process.NewProcess(status_cmd) + p.Start() + go func() { + for { + std := <-p.StdoutChannel + if strings.Contains(std, "octoprint/octoprint") { + res := strings.Split(std, " ") + if len(res) != 7 { + logger.Printf("Unkown number pf parameters (%d vs. 7)", len(res)) + } else { + state.ContainerID = res[0] + state.Image = res[1] + state.Command = res[2] + state.Created = res[3] + state.Status = res[4] + state.Ports = res[5] + state.Name = res[6] + } + } + } + }() + go func() { + for { + err := <-p.StderrChannel + logger.Print(err) + } + }() + p.Wait() + return state +} diff --git a/webui/index.html b/webui/index.html index 923f1ed..f8f9d5d 100644 --- a/webui/index.html +++ b/webui/index.html @@ -33,6 +33,20 @@ chk = true; } btn.checked = chk; + if(target === "printer_slider") { + let containerstate = state.octoprint_containerstate; + let container_name = document.getElementById("container_name"); + container_name.innerHTML = containerstate.name; + let container_status = document.getElementById("container_status"); + container_status.innerHTML = containerstate.status; + + let div_container = document.getElementById("containerdiv"); + if(containerstate.status === "off") { + div_container.style.visibility = "hidden"; + } else { + div_container.style.visibility = "visible"; + } + } } else { console.log(`Error: ${xhr.status}`); } @@ -82,32 +96,52 @@ -->
-

3D Drucker

+
+

3D Drucker

-

Schalter

- - - - - - - - - -
Strom: - -
Daten: - -
+
+
+

Schalter

+ + + + + + + + + +
Strom: + +
Daten: + +
+
+
+

Octoprint Docker Container

+ + + + + + + + + +
Name:
Status:
+ Octoprint +
+ Camera +