Compare commits

..

1 Commits

Author SHA1 Message Date
Thomas Klaehn
ec727efc13 Add camera control
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-09-11 08:42:57 +02:00
3 changed files with 28 additions and 19 deletions

View File

@@ -112,9 +112,21 @@ func handle_patch_printerstate(w http.ResponseWriter, r *http.Request) {
return return
} }
if state.State == StateOn { if state.State == StateOn {
octoprint.ReStart() err = octoprint.ReStart()
if err != nil {
logger.Print(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(json.RawMessage(fmt.Sprintf(`{"error": "%s"}`, err.Error())))
return
}
} else { } else {
octoprint.Stop() err := octoprint.Stop()
if err != nil {
logger.Print(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(json.RawMessage(fmt.Sprintf(`{"error": "%s"}`, err.Error())))
return
}
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

View File

@@ -19,7 +19,7 @@ type DockerState struct {
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 -a" status_cmd = "docker ps"
) )
var ( var (
@@ -30,21 +30,22 @@ func init() {
logger.SetFlags(log.Llongfile | log.Ltime) logger.SetFlags(log.Llongfile | log.Ltime)
} }
func Start() { func Start() error {
p := process.NewProcess(start_cmd) p := process.NewProcess(start_cmd)
p.Start() return p.Start()
p.Wait()
} }
func Stop() { func Stop() error {
p := process.NewProcess(stop_cmd) p := process.NewProcess(stop_cmd)
p.Start() return p.Start()
p.Wait()
} }
func ReStart() { func ReStart() error {
Stop() err := Stop()
Start() if err != nil {
logger.Print(err)
}
return Start()
} }
func Status() DockerState { func Status() DockerState {
@@ -57,7 +58,7 @@ func Status() DockerState {
std := <-p.StdoutChannel std := <-p.StdoutChannel
if strings.Contains(std, "octoprint/octoprint") { if strings.Contains(std, "octoprint/octoprint") {
res := strings.Split(std, " ") res := strings.Split(std, " ")
if len(res) < 7 { if len(res) != 7 {
logger.Printf("Unkown number pf parameters (%d vs. 7)", len(res)) logger.Printf("Unkown number pf parameters (%d vs. 7)", len(res))
} else { } else {
state.ContainerID = res[0] state.ContainerID = res[0]
@@ -66,11 +67,7 @@ func Status() DockerState {
state.Created = res[3] state.Created = res[3]
state.Status = res[4] state.Status = res[4]
state.Ports = res[5] state.Ports = res[5]
if len(res) == 7 {
state.Name = res[6] state.Name = res[6]
} else if len(res) == 9 {
state.Name = res[8]
}
} }
} }
} }

View File

@@ -36,7 +36,7 @@ func main() {
apiservice_printer.AddHandler() apiservice_printer.AddHandler()
apiservice_relay.AddHandler() apiservice_relay.AddHandler()
port := ":80" port := ":8083"
http.Handle("/", http.FileServer(http.Dir(webui_path))) http.Handle("/", http.FileServer(http.Dir(webui_path)))
logger.Fatal(http.ListenAndServe(port, nil)) logger.Fatal(http.ListenAndServe(port, nil))
} }