Skip to content

Commit cb8d9b4

Browse files
committed
Added programfromurl command
Former-commit-id: 96dd1f336cbf0d3d430408b29da8015d162df072 [formerly 6e32f6a743e63c0edd103068279019bdad9aa683] Former-commit-id: 2e5904cb3efae1712374372e654566297fdf0fe7
1 parent 7000480 commit cb8d9b4

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

download.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// download.go
2+
package main
3+
4+
import (
5+
"errors"
6+
"fmt"
7+
"io"
8+
"io/ioutil"
9+
"net/http"
10+
"os"
11+
"strings"
12+
)
13+
14+
func downloadFromUrl(url string) (filename string, err error) {
15+
16+
// clean up url
17+
// remove newlines and space at end
18+
url = strings.TrimSpace(url)
19+
20+
// create tmp dir
21+
tmpdir, err := ioutil.TempDir("", "serial-port-json-server")
22+
if err != nil {
23+
return "", errors.New("Could not create temp directory to store downloaded file. Do you have permissions?")
24+
}
25+
tokens := strings.Split(url, "/")
26+
filePrefix := tokens[len(tokens)-1]
27+
fmt.Println("The filePrefix is", filePrefix)
28+
tmpfile, err := ioutil.TempFile(tmpdir, filePrefix)
29+
if err != nil {
30+
fmt.Println("Error creating tempfile. ", err)
31+
return "", errors.New(err.Error() + " Could not create temp file to store downloaded file. Do you have permissions? Are you out of storage space?")
32+
}
33+
34+
fileName := tmpfile.Name()
35+
fmt.Println("Downloading", url, "to", fileName)
36+
37+
// TODO: check file existence first with io.IsExist
38+
output, err := os.Create(fileName)
39+
if err != nil {
40+
fmt.Println("Error while creating", fileName, "-", err)
41+
return fileName, err
42+
}
43+
defer output.Close()
44+
45+
response, err := http.Get(url)
46+
if err != nil {
47+
fmt.Println("Error while downloading", url, "-", err)
48+
return fileName, err
49+
}
50+
defer response.Body.Close()
51+
52+
n, err := io.Copy(output, response.Body)
53+
if err != nil {
54+
fmt.Println("Error while downloading", url, "-", err)
55+
return fileName, err
56+
}
57+
58+
fmt.Println(n, "bytes downloaded.")
59+
60+
return fileName, nil
61+
}

hub.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (h *hub) run() {
5353
h.connections[c] = true
5454
// send supported commands
5555
c.send <- []byte("{\"Version\" : \"" + version + "\"} ")
56-
c.send <- []byte("{\"Commands\" : [\"list\", \"open [portName] [baud] [bufferAlgorithm (optional)]\", \"send [portName] [cmd]\", \"sendnobuf [portName] [cmd]\", \"close [portName]\", \"bufferalgorithms\", \"baudrates\", \"restart\", \"exit\", \"program [portName] [board:name] [$path/to/filename/without/extension]\"]} ")
56+
c.send <- []byte("{\"Commands\" : [\"list\", \"open [portName] [baud] [bufferAlgorithm (optional)]\", \"send [portName] [cmd]\", \"sendnobuf [portName] [cmd]\", \"close [portName]\", \"bufferalgorithms\", \"baudrates\", \"restart\", \"exit\", \"program [portName] [board:name] [$path/to/filename/without/extension]\", \"programfromurl [portName] [board:name] [urlToHexFile]\"]} ")
5757
c.send <- []byte("{\"Hostname\" : \"" + *hostname + "\"} ")
5858
case c := <-h.unregister:
5959
delete(h.connections, c)
@@ -169,14 +169,23 @@ func checkCmd(m []byte) {
169169
go spErr("You did not specify a port to close")
170170
}
171171

172+
} else if strings.HasPrefix(sl, "programfromurl") {
173+
174+
args := strings.Split(s, " ")
175+
if len(args) == 4 {
176+
go spProgramFromUrl(args[1], args[2], args[3])
177+
} else {
178+
go spErr("You did not specify a port, a board to program and/or a URL")
179+
}
180+
172181
} else if strings.HasPrefix(sl, "program") {
173182

174183
args := strings.Split(s, " ")
175184
if len(args) > 3 {
176185
var slice []string = args[3:len(args)]
177186
go spProgram(args[1], args[2], strings.Join(slice, " "))
178187
} else {
179-
go spErr("You did not specify a port, a board to program and a filename")
188+
go spErr("You did not specify a port, a board to program and/or a filename")
180189
}
181190

182191
} else if strings.HasPrefix(sl, "sendjson") {

serial.go

+10
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,16 @@ func spClose(portname string) {
540540
}
541541
}
542542

543+
// Download the file from URL first, store in tmp folder, then pass to spProgram
544+
func spProgramFromUrl(portname string, boardname string, url string) {
545+
filename, err := downloadFromUrl(url)
546+
if err != nil {
547+
spErr(err.Error())
548+
} else {
549+
spProgram(portname, boardname, filename)
550+
}
551+
}
552+
543553
func spProgram(portname string, boardname string, filePath string) {
544554

545555
isFound, flasher, mycmd := assembleCompilerCommand(boardname, portname, filePath)

0 commit comments

Comments
 (0)