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 .PHONY: clean
clean: clean:
go clean go clean
rm -rf bin rm -rf build
-rm $(PROJECT_NAME).tar.gz -rm $(PROJECT_NAME).tar.gz
.PHONY: install .PHONY: install

View File

@@ -15,6 +15,7 @@ type state string
type State struct { type State struct {
State state `json:"state"` State state `json:"state"`
DockerState octoprint.DockerState `json:"octoprint_containerstate"`
} }
const ( const (
@@ -56,6 +57,7 @@ func handle_get_printerstate(w http.ResponseWriter, r *http.Request) {
} else { } else {
res.State = StateOff res.State = StateOff
} }
res.DockerState = octoprint.Status()
tmp, err := json.Marshal(res) tmp, err := json.Marshal(res)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@@ -3,11 +3,23 @@ package octoprint
import ( import (
"log" "log"
"powerswitch/internal/app/process" "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 ( const (
start_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml up -d" 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" stop_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml down"
status_cmd = "docker ps"
) )
var ( var (
@@ -35,3 +47,37 @@ func ReStart() error {
} }
return Start() 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; chk = true;
} }
btn.checked = chk; 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 { } else {
console.log(`Error: ${xhr.status}`); console.log(`Error: ${xhr.status}`);
} }
@@ -82,11 +96,14 @@
</div> --> </div> -->
</div> </div>
<div id="content"> <div id="content">
<div>
<h1>3D Drucker</h1> <h1>3D Drucker</h1>
<label class="switch"> <label class="switch">
<input id="printer_slider" type="checkbox" onchange="check(this, 'printer')"> <input id="printer_slider" type="checkbox" onchange="check(this, 'printer')">
<span class="slider round"></span> <span class="slider round"></span>
</label> </label>
</div>
<div>
<h2>Schalter</h2> <h2>Schalter</h2>
<table> <table>
<tr> <tr>
@@ -109,5 +126,22 @@
</tr> </tr>
</table> </table>
</div> </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> </body>
</html> </html>