Skip to content

Programmer #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"

log "github.com/Sirupsen/logrus"
"github.com/arduino/arduino-create-agent/upload"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
Expand Down Expand Up @@ -60,7 +63,7 @@ type Upload struct {
Rewrite string `json:"rewrite"`
Commandline string `json:"commandline"`
Signature string `json:"signature"`
Extra boardExtraInfo `json:"extra"`
Extra upload.Extra `json:"extra"`
Hex []byte `json:"hex"`
Filename string `json:"filename"`
ExtraFiles []AdditionalFile `json:"extrafiles"`
Expand Down Expand Up @@ -123,11 +126,60 @@ func uploadHandler(c *gin.Context) {
data.Board = data.Rewrite
}

go spProgramRW(data.Port, data.Board, filePath, data.Commandline, data.Extra)
go func() {
// Resolve commandline
commandline, err := upload.Resolve(data.Port, data.Board, filePath, data.Commandline, data.Extra, &Tools)
if err != nil {
send(map[string]string{"uploadStatus": "Error", "Msg": err.Error()})
return
}

l := PLogger{Verbose: data.Extra.Verbose}

// Upload
if data.Extra.Network {
send(map[string]string{"uploadStatus": "Starting", "Cmd": "Network"})
err = upload.Network(data.Port, data.Board, filePath, commandline, data.Extra.Auth, l)
} else {
send(map[string]string{"uploadStatus": "Starting", "Cmd": "Serial"})
err = upload.Serial(data.Port, commandline, data.Extra, l)
}

// Handle result
if err != nil {
send(map[string]string{"uploadStatus": "Error", "Msg": err.Error()})
return
}
send(map[string]string{"uploadStatus": "Done", "Flash": "Ok"})
}()

c.String(http.StatusAccepted, "")
}

// PLogger sends the info from the upload to the websocket
type PLogger struct {
Verbose bool
}

// Debug only sends messages if verbose is true
func (l PLogger) Debug(args ...interface{}) {
if l.Verbose {
l.Info(args...)
}
}

// Info always send messages
func (l PLogger) Info(args ...interface{}) {
output := fmt.Sprint(args...)
log.Println(output)
send(map[string]string{"uploadStatus": "Busy", "Msg": output})
}

func send(args map[string]string) {
mapB, _ := json.Marshal(args)
h.broadcastSys <- mapB
}

func verifyCommandLine(input string, signature string) error {
sign, _ := hex.DecodeString(signature)
block, _ := pem.Decode([]byte(*signatureKey))
Expand Down
1 change: 1 addition & 0 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func getPorts() ([]OsSerialPort, error) {
arrPorts := []OsSerialPort{}
go func(results chan *bonjour.ServiceEntry, exitCh chan<- bool) {
for e := range results {
log.Printf("%+v", e)
if e.AddrIPv4 != nil {
arrPorts = append(arrPorts, OsSerialPort{Name: e.AddrIPv4.String(), IdProduct: e.Instance, IdVendor: strings.Join(e.Text[:], " "), NetworkPort: true})
}
Expand Down
21 changes: 11 additions & 10 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ package main
import (
"fmt"

log "github.com/Sirupsen/logrus"
"github.com/kardianos/osext"
//"os"
"os/exec"
//"path"
//"path/filepath"
//"runtime"
//"debug"
"encoding/json"
"io"
"os"
"os/exec"
"runtime"
"runtime/debug"
"strconv"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/arduino/arduino-create-agent/upload"
"github.com/kardianos/osext"
)

type hub struct {
Expand Down Expand Up @@ -168,9 +165,13 @@ func checkCmd(m []byte) {
go spErr("You did not specify a port to close")
}

} else if strings.HasPrefix(sl, "killprogrammer") {
} else if strings.HasPrefix(sl, "killupload") {
// kill the running process (assumes singleton for now)
go spHandlerProgramKill()
go func() {
upload.Kill()
h.broadcastSys <- []byte("{\"uploadStatus\": \"Killed\"}")
log.Println("{\"uploadStatus\": \"Killed\"}")
}()

} else if strings.HasPrefix(sl, "sendjsonraw") {
// will catch sendjsonraw
Expand Down
Loading