Add status of octoprint container
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
		
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
									
									
									
									
								
							@@ -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
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,7 +14,8 @@ import (
 | 
				
			|||||||
type state string
 | 
					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)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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,32 +96,52 @@
 | 
				
			|||||||
            </div> -->
 | 
					            </div> -->
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
        <div id="content">
 | 
					        <div id="content">
 | 
				
			||||||
            <h1>3D Drucker</h1>
 | 
					            <div>
 | 
				
			||||||
 | 
					                <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>
 | 
				
			||||||
            <h2>Schalter</h2>
 | 
					            </div>
 | 
				
			||||||
            <table>
 | 
					            <div>
 | 
				
			||||||
                <tr>
 | 
					                <h2>Schalter</h2>
 | 
				
			||||||
                    <td>Strom:</td>
 | 
					                <table>
 | 
				
			||||||
                    <td>
 | 
					                    <tr>
 | 
				
			||||||
                        <label class="switch">
 | 
					                        <td>Strom:</td>
 | 
				
			||||||
                            <input id="power_slider" type="checkbox" onchange="check(this, 'power')">
 | 
					                        <td>
 | 
				
			||||||
                            <span class="slider round"></span>
 | 
					                            <label class="switch">
 | 
				
			||||||
                        </label>
 | 
					                                <input id="power_slider" type="checkbox" onchange="check(this, 'power')">
 | 
				
			||||||
                    </td>
 | 
					                                <span class="slider round"></span>
 | 
				
			||||||
                </tr>
 | 
					                            </label>
 | 
				
			||||||
                <tr>
 | 
					                        </td>
 | 
				
			||||||
                    <td>Daten:</td>
 | 
					                    </tr>
 | 
				
			||||||
                    <td>
 | 
					                    <tr>
 | 
				
			||||||
                        <label class="switch">
 | 
					                        <td>Daten:</td>
 | 
				
			||||||
                            <input id="data_slider" type="checkbox" onchange="check(this, 'data')">
 | 
					                        <td>
 | 
				
			||||||
                            <span class="slider round"></span>
 | 
					                            <label class="switch">
 | 
				
			||||||
                        </label>
 | 
					                                <input id="data_slider" type="checkbox" onchange="check(this, 'data')">
 | 
				
			||||||
                    </td>
 | 
					                                <span class="slider round"></span>
 | 
				
			||||||
                </tr>
 | 
					                            </label>
 | 
				
			||||||
            </table>
 | 
					                        </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>
 | 
					        </div>
 | 
				
			||||||
    </body>
 | 
					    </body>
 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user