Skip to content

Commit 188337f

Browse files
matteosuppofacchinm
authored andcommitted
Use the new programming package
1 parent 6f5fe36 commit 188337f

File tree

5 files changed

+63
-25
lines changed

5 files changed

+63
-25
lines changed

conn.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/sha256"
1010
"crypto/x509"
1111
"encoding/hex"
12+
"encoding/json"
1213
"encoding/pem"
1314
"errors"
1415
"io/ioutil"
@@ -124,11 +125,39 @@ func uploadHandler(c *gin.Context) {
124125
data.Board = data.Rewrite
125126
}
126127

127-
go programmer.Do(data.Port, data.Board, filePath, data.Commandline, data.Extra, nil)
128+
go func() {
129+
// Resolve commandline
130+
commandline, err := programmer.Resolve(data.Port, data.Board, filePath, data.Commandline, data.Extra, &Tools)
131+
if err != nil {
132+
133+
return
134+
}
135+
136+
// Upload
137+
if data.Extra.Network {
138+
send(map[string]string{"ProgrammerStatus": "Starting", "Cmd": "Network"})
139+
err = programmer.Network(data.Port, data.Board, filePath, commandline, data.Extra.Auth, nil)
140+
} else {
141+
send(map[string]string{"ProgrammerStatus": "Starting", "Cmd": "Serial"})
142+
err = programmer.Serial(data.Port, commandline, data.Extra, nil)
143+
}
144+
145+
// Handle result
146+
if err != nil {
147+
send(map[string]string{"ProgrammerStatus": "Error", "Msg": err.Error()})
148+
return
149+
}
150+
send(map[string]string{"ProgrammerStatus": "Done", "Flash": "Ok"})
151+
}()
128152

129153
c.String(http.StatusAccepted, "")
130154
}
131155

156+
func send(args map[string]string) {
157+
mapB, _ := json.Marshal(args)
158+
h.broadcastSys <- mapB
159+
}
160+
132161
func verifyCommandLine(input string, signature string) error {
133162
sign, _ := hex.DecodeString(signature)
134163
block, _ := pem.Decode([]byte(*signatureKey))

hub.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
log "github.com/Sirupsen/logrus"
7+
"github.com/arduino/arduino-create-agent/programmer"
78
"github.com/kardianos/osext"
89
//"os"
910
"os/exec"
@@ -170,7 +171,7 @@ func checkCmd(m []byte) {
170171

171172
} else if strings.HasPrefix(sl, "killprogrammer") {
172173
// kill the running process (assumes singleton for now)
173-
go spHandlerProgramKill()
174+
go programmer.Kill()
174175

175176
} else if strings.HasPrefix(sl, "sendjsonraw") {
176177
// will catch sendjsonraw

programmer/programmer.go

+5-21
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,8 @@ import (
2323
"github.com/sfreiberg/simplessh"
2424
)
2525

26-
type logger interface {
27-
Debug(args ...interface{})
28-
Info(args ...interface{})
29-
}
30-
31-
func debug(l logger, args ...interface{}) {
32-
if l != nil {
33-
l.Debug(args...)
34-
}
35-
}
36-
37-
func info(l logger, args ...interface{}) {
38-
if l != nil {
39-
l.Info(args...)
40-
}
41-
}
42-
43-
// locater can return the location of a tool in the system
44-
type locater interface {
45-
GetLocation(command string) (string, error)
46-
}
26+
// Busy tells wether the programmer is doing something
27+
var Busy = false
4728

4829
// Auth contains username and password used for a network upload
4930
type Auth struct {
@@ -111,6 +92,9 @@ func Network(port, board, file, commandline string, auth Auth, l logger) error {
11192
return err
11293
}
11394

95+
// Kill stops any upload process as soon as possible
96+
func Kill() {}
97+
11498
// Serial performs a serial upload
11599
func Serial(port, commandline string, extra Extra, l logger) error {
116100
// some boards needs to be resetted

programmer/utils.go

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package programmer
22

3+
type logger interface {
4+
Debug(args ...interface{})
5+
Info(args ...interface{})
6+
}
7+
8+
func debug(l logger, args ...interface{}) {
9+
if l != nil {
10+
l.Debug(args...)
11+
}
12+
}
13+
14+
func info(l logger, args ...interface{}) {
15+
if l != nil {
16+
l.Info(args...)
17+
}
18+
}
19+
20+
// locater can return the location of a tool in the system
21+
type locater interface {
22+
GetLocation(command string) (string, error)
23+
}
24+
325
// differ returns the first item that differ between the two input slices
426
func differ(slice1 []string, slice2 []string) string {
527
m := map[string]int{}

serial.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package main
55
import (
66
"encoding/json"
77
"fmt"
8-
log "github.com/Sirupsen/logrus"
98
"regexp"
109
"runtime/debug"
1110
"strconv"
1211
"strings"
1312
"time"
13+
14+
log "github.com/Sirupsen/logrus"
15+
"github.com/arduino/arduino-create-agent/programmer"
1416
)
1517

1618
type writeRequest struct {
@@ -453,7 +455,7 @@ func discoverLoop() {
453455

454456
go func() {
455457
for {
456-
if !compiling {
458+
if !programmer.Busy {
457459
spListDual(false)
458460
}
459461
time.Sleep(2 * time.Second)

0 commit comments

Comments
 (0)