|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "net/http/httputil" |
| 11 | + "os" |
| 12 | + "regexp" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + networkAddress = flag.String("address", "localhost", "The address of the board") |
| 20 | + networkPort = flag.String("port", "80", "The board needs to be listening on this port") |
| 21 | + sketchPath = flag.String("sketch", "", "Sketch path") |
| 22 | + uploadEndpoint = flag.String("upload", "", "Upload endpoint") |
| 23 | + resetEndpoint = flag.String("reset", "", "Upload endpoint") |
| 24 | + syncEndpoint = flag.String("sync", "", "Upload endpoint") |
| 25 | + verbose = flag.String("v", "true", "Verbose flag") |
| 26 | + quiet = flag.String("q", "", "Quiet flag") |
| 27 | + useSsl = flag.String("ssl", "", "SSL flag") |
| 28 | + syncRet = flag.String("sync_exp", "", "sync expected return code in format code:string") |
| 29 | +) |
| 30 | + |
| 31 | +type Item struct { |
| 32 | + Id int |
| 33 | + Name string |
| 34 | +} |
| 35 | + |
| 36 | +func main() { |
| 37 | + flag.Parse() |
| 38 | + |
| 39 | + httpheader := "http://" |
| 40 | + |
| 41 | + if *useSsl != "" { |
| 42 | + httpheader = "https://" |
| 43 | + } |
| 44 | + |
| 45 | + syncRetCode := 200 |
| 46 | + syncString := "SYNC" |
| 47 | + |
| 48 | + if *syncRet != "" { |
| 49 | + sliceStrRet := strings.Split(*syncRet, ":") |
| 50 | + if len(sliceStrRet) == 2 { |
| 51 | + syncRetCode, _ = strconv.Atoi(sliceStrRet[0]) |
| 52 | + syncString = sliceStrRet[1] |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if *syncEndpoint != "" { |
| 57 | + if *verbose != "" { |
| 58 | + fmt.Println("Resetting the board") |
| 59 | + } |
| 60 | + |
| 61 | + resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil) |
| 62 | + fmt.Println(resp.StatusCode) |
| 63 | + fmt.Println(resp.Status) |
| 64 | + if err != nil || resp.StatusCode != syncRetCode { |
| 65 | + if *verbose != "" { |
| 66 | + fmt.Println("Failed to reset the board, upload failed") |
| 67 | + } |
| 68 | + os.Exit(1) |
| 69 | + } |
| 70 | + defer resp.Body.Close() |
| 71 | + } |
| 72 | + |
| 73 | + if *syncEndpoint != "" { |
| 74 | + if *verbose != "" { |
| 75 | + fmt.Println("Waiting for the upload to start") |
| 76 | + } |
| 77 | + |
| 78 | + timeout := 0 |
| 79 | + |
| 80 | + for timeout < 10 { |
| 81 | + resp, err := http.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint) |
| 82 | + if err != nil { |
| 83 | + if *verbose != "" { |
| 84 | + fmt.Println("Failed to reset the board, upload failed") |
| 85 | + } |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + defer resp.Body.Close() |
| 89 | + |
| 90 | + statusString, err := ioutil.ReadAll(resp.Body) |
| 91 | + |
| 92 | + if strings.Contains(string(statusString), syncString) { |
| 93 | + fmt.Println(string(statusString)) |
| 94 | + break |
| 95 | + } |
| 96 | + |
| 97 | + time.Sleep(1 * time.Second) |
| 98 | + timeout++ |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if *uploadEndpoint != "" { |
| 103 | + if *verbose != "" { |
| 104 | + fmt.Println("Uploading the sketch") |
| 105 | + } |
| 106 | + |
| 107 | + f, err := os.Open(*sketchPath) |
| 108 | + if err != nil { |
| 109 | + if *verbose != "" { |
| 110 | + fmt.Println("Failed to open the sketch") |
| 111 | + } |
| 112 | + os.Exit(1) |
| 113 | + } |
| 114 | + defer f.Close() |
| 115 | + |
| 116 | + str := StreamToString(f) |
| 117 | + re := regexp.MustCompile(`\r?\n`) |
| 118 | + str = re.ReplaceAllString(str, "") |
| 119 | + |
| 120 | + req, err := http.NewRequest("POST", httpheader+*networkAddress+":"+*networkPort+*uploadEndpoint, bytes.NewBufferString(str)) |
| 121 | + if err != nil { |
| 122 | + if *verbose != "" { |
| 123 | + fmt.Println("Error sending sketch file") |
| 124 | + } |
| 125 | + os.Exit(1) |
| 126 | + } |
| 127 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 128 | + |
| 129 | + aaa, _ := httputil.DumpRequestOut(req, true) |
| 130 | + fmt.Println(string(aaa)) |
| 131 | + |
| 132 | + resp, err := http.DefaultClient.Do(req) |
| 133 | + if err != nil { |
| 134 | + if *verbose != "" { |
| 135 | + fmt.Println("Error flashing the sketch") |
| 136 | + } |
| 137 | + os.Exit(1) |
| 138 | + } |
| 139 | + defer resp.Body.Close() |
| 140 | + |
| 141 | + respStr, _ := ioutil.ReadAll(resp.Body) |
| 142 | + |
| 143 | + if resp.StatusCode != 200 { |
| 144 | + if *verbose != "" { |
| 145 | + fmt.Println("Error flashing the sketch:" + string(respStr)) |
| 146 | + } |
| 147 | + os.Exit(1) |
| 148 | + } |
| 149 | + |
| 150 | + if *verbose != "" { |
| 151 | + fmt.Println(string(respStr)) |
| 152 | + fmt.Println("Sketch uploaded successfully") |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if *resetEndpoint != "" { |
| 157 | + if *verbose != "" { |
| 158 | + fmt.Println("Resetting the board") |
| 159 | + } |
| 160 | + |
| 161 | + resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil) |
| 162 | + if err != nil { |
| 163 | + if *verbose != "" { |
| 164 | + fmt.Println("Failed to reset the board, please reset maually") |
| 165 | + } |
| 166 | + os.Exit(0) |
| 167 | + } |
| 168 | + defer resp.Body.Close() |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func StreamToString(stream io.Reader) string { |
| 173 | + buf := new(bytes.Buffer) |
| 174 | + buf.ReadFrom(stream) |
| 175 | + return buf.String() |
| 176 | +} |
0 commit comments