Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
Rework into arbitrary command exectutor
Browse files Browse the repository at this point in the history
  • Loading branch information
wilriker committed Dec 9, 2019
1 parent 960f66c commit 9bd4646
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 59 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Description
This is a small extension to [DuetSoftwareFramework](https://github.com/christhamm/DuetSoftwareFramework)
to execute arbitrary system commands when a user-defined `M-Code` is encountered.

An example usage would be to execute system shutdown on the SBC when a e.g. `M7722` is run.

# Usage
```
$ ./shutdownsbc --help
Usage of ./shutdownsbc:
-shutdownMCode int
Code that will initiate shutdown of the SBC (default 7722)
$ ./execonmcode --help
Usage of ./execonmcode:
-command string
Command to execute
-mCode int
Code that will initiate execution of the command (default 7722)
-socketPath string
Path to socket (default "/var/run/duet.sock")
Path to socket (default "/var/run/duet.sock")
```
35 changes: 35 additions & 0 deletions cmd/eom/execonmcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"flag"
"log"

"github.com/wilriker/execonmcode"
"github.com/wilriker/goduetapiclient/connection"
)

type settings struct {
socketPath string
mCode int64
command string
}

func main() {
s := settings{}

flag.StringVar(&s.socketPath, "socketPath", connection.DefaultSocketPath, "Path to socket")
flag.Int64Var(&s.mCode, "mCode", 7722, "Code that will initiate execution of the command")
flag.StringVar(&s.command, "command", "", "Command to execute")
flag.Parse()

if s.mCode < 0 {
log.Fatal("--mCode must be >= 0")
}

if s.command == "" {
log.Fatal("--command must not be empty")
}

e := execonmcode.NewExecutor(s.socketPath, s.command, s.mCode)
e.Run()
}
65 changes: 65 additions & 0 deletions executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package execonmcode

import (
"log"
"os/exec"
"strings"

"github.com/wilriker/goduetapiclient/connection"
"github.com/wilriker/goduetapiclient/connection/initmessages"
"github.com/wilriker/goduetapiclient/types"
)

type Executor struct {
socketPath string
mCode int64
command string
args []string
}

func NewExecutor(socketPath, command string, mCode int64) *Executor {
s := strings.Split(command, " ")
a := []string{}
if len(s) > 1 {
a = s[1:]
}
c := s[0]
return &Executor{
socketPath: socketPath,
command: c,
args: a,
mCode: mCode,
}
}

func (e *Executor) Run() {

ic := connection.InterceptConnection{}
err := ic.Connect(initmessages.InterceptionModePre, e.socketPath)
if err != nil {
log.Fatal(err)
}
defer ic.Close()

for {
c, err := ic.ReceiveCode()
if err != nil {
log.Println("Error:", err)
continue
}
if c.Type == types.MCode && c.MajorNumber != nil && *c.MajorNumber == e.mCode {
cmd := exec.Command(e.command, e.args...)
err := cmd.Run()
if err != nil {
err = ic.ResolveCode(types.Error, err.Error())
} else {
err = ic.ResolveCode(types.Success, "")
}
if err != nil {
log.Println("Error:", err)
}
} else {
ic.IgnoreCode()
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/wilriker/dsfext-shutdownsbc
module github.com/wilriker/execonmcode

go 1.13

Expand Down
52 changes: 0 additions & 52 deletions shutdownsbc.go

This file was deleted.

2 changes: 1 addition & 1 deletion shutdownsbc.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ After=duetcontrolserver.service
Requires=duetcontrolserver.service

[Service]
ExecStart=/usr/local/bin/shutdownsbc
ExecStart=/usr/local/bin/execonmcode --command "poweroff"
Restart=always
RestartSec=10

Expand Down

0 comments on commit 9bd4646

Please sign in to comment.