Add status of octoprint container

Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
Thomas Klaehn
2025-09-10 10:01:30 +02:00
parent d760317de2
commit 782e673686
4 changed files with 108 additions and 26 deletions

View File

@@ -22,7 +22,7 @@ service:
.PHONY: clean
clean:
go clean
rm -rf bin
rm -rf build
-rm $(PROJECT_NAME).tar.gz
.PHONY: install

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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 @@
</div> -->
</div>
<div id="content">
<h1>3D Drucker</h1>
<div>
<h1>3D Drucker</h1>
<label class="switch">
<input id="printer_slider" type="checkbox" onchange="check(this, 'printer')">
<span class="slider round"></span>
</label>
<h2>Schalter</h2>
<table>
<tr>
<td>Strom:</td>
<td>
<label class="switch">
<input id="power_slider" type="checkbox" onchange="check(this, 'power')">
<span class="slider round"></span>
</label>
</td>
</tr>
<tr>
<td>Daten:</td>
<td>
<label class="switch">
<input id="data_slider" type="checkbox" onchange="check(this, 'data')">
<span class="slider round"></span>
</label>
</td>
</tr>
</table>
</div>
<div>
<h2>Schalter</h2>
<table>
<tr>
<td>Strom:</td>
<td>
<label class="switch">
<input id="power_slider" type="checkbox" onchange="check(this, 'power')">
<span class="slider round"></span>
</label>
</td>
</tr>
<tr>
<td>Daten:</td>
<td>
<label class="switch">
<input id="data_slider" type="checkbox" onchange="check(this, 'data')">
<span class="slider round"></span>
</label>
</td>
</tr>
</table>
</div>
<div id="containerdiv">
<h2>Octoprint Docker Container</h2>
<table>
<tr>
<td>Name:</td>
<td id="container_name"></td>
</tr>
<tr>
<td>Status:</td>
<td id="container_status"></td>
</tr>
</table>
<a href="http://p4:80" target="_blank">Octoprint</a>
<br>
<a href="http://p4:8080" target="_blank">Camera</a>
</div>
</div>
</body>
</html>