2020-10-29 20:28:17 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os/exec"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const startCmdDelay = time.Second
|
|
|
|
|
|
|
|
|
|
var startedCmd *exec.Cmd
|
2020-10-30 15:21:19 +01:00
|
|
|
var serialPortStartedCmd *exec.Cmd
|
2020-10-29 20:28:17 +01:00
|
|
|
|
|
|
|
|
func doStartCmd() {
|
|
|
|
|
c := strings.Split(runCmd, " ")
|
|
|
|
|
startedCmd = exec.Command(c[0], c[1:]...)
|
|
|
|
|
err := startedCmd.Start()
|
|
|
|
|
if err == nil {
|
|
|
|
|
log.Print("cmd started: ", runCmd)
|
|
|
|
|
} else {
|
2020-10-30 15:21:19 +01:00
|
|
|
log.Error("error starting ", runCmd, ": ", err)
|
2020-10-29 20:28:17 +01:00
|
|
|
startedCmd = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func startCmdIfNeeded() {
|
|
|
|
|
if startedCmd != nil || runCmd == "-" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.AfterFunc(startCmdDelay, doStartCmd)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 15:21:19 +01:00
|
|
|
func doSerialPortStartCmd() {
|
|
|
|
|
c := strings.Split(runCmdOnSerialPortCreated, " ")
|
|
|
|
|
serialPortStartedCmd = exec.Command(c[0], c[1:]...)
|
|
|
|
|
err := serialPortStartedCmd.Start()
|
|
|
|
|
if err == nil {
|
|
|
|
|
log.Print("cmd started: ", runCmdOnSerialPortCreated)
|
|
|
|
|
} else {
|
|
|
|
|
log.Error("error starting ", runCmdOnSerialPortCreated, ": ", err)
|
|
|
|
|
serialPortStartedCmd = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func startSerialPortCmdIfNeeded() {
|
|
|
|
|
if serialPortStartedCmd != nil || runCmdOnSerialPortCreated == "-" {
|
2020-10-29 20:28:17 +01:00
|
|
|
return
|
|
|
|
|
}
|
2020-10-30 15:21:19 +01:00
|
|
|
|
|
|
|
|
time.AfterFunc(startCmdDelay, doSerialPortStartCmd)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func stopCmd() {
|
|
|
|
|
if startedCmd != nil {
|
|
|
|
|
if err := startedCmd.Process.Kill(); err != nil {
|
|
|
|
|
log.Error("failed to stop cmd ", runCmd, ": ", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if serialPortStartedCmd != nil {
|
|
|
|
|
if err := serialPortStartedCmd.Process.Kill(); err != nil {
|
|
|
|
|
log.Error("failed to stop cmd ", runCmdOnSerialPortCreated, ": ", err)
|
|
|
|
|
}
|
2020-10-29 20:28:17 +01:00
|
|
|
}
|
|
|
|
|
}
|