Skip to content

Various enhancements #3

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

Merged
merged 4 commits into from
Mar 1, 2017
Merged
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: 47 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/http/httptrace"
"os"
"regexp"
"strconv"
Expand All @@ -22,6 +23,8 @@ var (
version = flag.Bool("version", false, "Prints program version")
networkAddress = flag.String("address", "localhost", "The address of the board")
networkPort = flag.String("port", "80", "The board needs to be listening on this port")
username = flag.String("username", "", "Username for authentication")
password = flag.String("password", "", "Password for authentication")
sketchPath = flag.String("sketch", "", "Sketch path")
uploadEndpoint = flag.String("upload", "", "Upload endpoint")
resetEndpoint = flag.String("reset", "", "Upload endpoint")
Expand All @@ -46,6 +49,10 @@ func main() {
os.Exit(0)
}

var httpClient = &http.Client{
Timeout: time.Second * 10,
}

httpheader := "http://"

if *useSsl != "" {
Expand All @@ -68,7 +75,7 @@ func main() {
fmt.Println("Resetting the board")
}

resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil)
resp, err := httpClient.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil)
if err != nil || resp.StatusCode != syncRetCode {
if *verbose {
fmt.Println("Failed to reset the board, upload failed")
Expand All @@ -86,7 +93,7 @@ func main() {
timeout := 0

for timeout < 10 {
resp, err := http.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint)
resp, err := httpClient.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint)
if err != nil {
if *verbose {
fmt.Println("Failed to reset the board, upload failed")
Expand All @@ -108,10 +115,6 @@ func main() {
}

if *uploadEndpoint != "" {
if *verbose {
fmt.Println("Uploading the sketch")
}

f, err := os.Open(*sketchPath)
if err != nil {
if *verbose {
Expand Down Expand Up @@ -139,9 +142,44 @@ func main() {
}
os.Exit(1)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
if *binMode {
req.Header.Set("Content-Type", "application/octet-stream")
} else {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}

if len(*username) > 0 && len(*password) != 0 {
req.SetBasicAuth(*username, *password)
}

if *verbose {
trace := &httptrace.ClientTrace{
ConnectStart: func(network, addr string) {
fmt.Print("Connecting to board ... ")
},
ConnectDone: func(network, addr string, err error) {
if err != nil {
fmt.Println("failed!")
} else {
fmt.Println(" done")
}
},
WroteHeaders: func() {
fmt.Print("Uploading sketch ... ")
},
WroteRequest: func(wri httptrace.WroteRequestInfo) {
fmt.Println(" done")
fmt.Print("Flashing sketch ... ")
},
GotFirstResponseByte: func() {
fmt.Println(" done")
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
}

resp, err := httpClient.Do(req)
if err != nil {
if *verbose {
fmt.Println("Error flashing the sketch")
Expand Down Expand Up @@ -170,7 +208,7 @@ func main() {
fmt.Println("Resetting the board")
}

resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil)
resp, err := httpClient.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil)
if err != nil {
if *verbose {
fmt.Println("Failed to reset the board, please reset maually")
Expand Down