|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "log" |
6 | 7 | "net" |
| 8 | + "os" |
| 9 | + "os/exec" |
7 | 10 | "strconv" |
8 | 11 | "strings" |
9 | 12 | "time" |
10 | 13 | ) |
11 | 14 |
|
| 15 | +var cmd *exec.Cmd |
| 16 | + |
12 | 17 | func RunApi(chapi chan bool, MyID uint32) { |
| 18 | + // python3 main.py を実行する |
| 19 | + cmd = exec.Command("python3", "main.py") |
| 20 | + //コマンドを実行 |
| 21 | + cmd.Start() |
13 | 22 | //ポートを開く |
14 | 23 | listener, err := net.Listen("tcp", PORT) |
15 | 24 | if err != nil { |
@@ -117,19 +126,110 @@ func HandleRequest(conn net.Conn) { |
117 | 126 | return |
118 | 127 | } |
119 | 128 |
|
| 129 | + if strings.Split(requests[1], "/")[1] == "image" { |
| 130 | + |
| 131 | + response, err := json.Marshal(imageResponse.Frame) |
| 132 | + if err != nil { |
| 133 | + fmt.Fprintf(conn, "HTTP/1.1 500 Internal Server Error\r\n") |
| 134 | + fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n") |
| 135 | + fmt.Fprintf(conn, "500 Internal Server Error\r\n") |
| 136 | + return |
| 137 | + } |
| 138 | + // HTTP レスポンスを返す |
| 139 | + fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n") |
| 140 | + fmt.Fprintf(conn, "Content-Type: application/json\r\n") |
| 141 | + fmt.Fprintf(conn, "Content-Length: %d\r\n", len(response)) |
| 142 | + fmt.Fprintf(conn, "\r\n") |
| 143 | + fmt.Fprintf(conn, "%s", response) |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + if strings.Split(requests[1], "/")[1] == "changeadjustment" { |
| 148 | + // /changeadjustment/1,120,100/15,255,255/150/0.2これを受け取る |
| 149 | + // /120,100,15をとる |
| 150 | + minThreshold := strings.Split(requests[1], "/")[2] |
| 151 | + maxThreshold := strings.Split(requests[1], "/")[3] |
| 152 | + ballDetectRadius, err := strconv.Atoi(strings.Split(requests[1], "/")[4]) |
| 153 | + if err != nil { |
| 154 | + fmt.Fprintf(conn, "HTTP/1.1 400 Bad Request\r\n") |
| 155 | + fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n") |
| 156 | + fmt.Fprintf(conn, "400 Bad Request\r\n") |
| 157 | + } |
| 158 | + circularityThreshold, err := strconv.ParseFloat(strings.Split(requests[1], "/")[5], 32) |
| 159 | + if err != nil { |
| 160 | + fmt.Fprintf(conn, "HTTP/1.1 400 Bad Request\r\n") |
| 161 | + fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n") |
| 162 | + fmt.Fprintf(conn, "400 Bad Request\r\n") |
| 163 | + } |
| 164 | + // OK と表示 |
| 165 | + fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n") |
| 166 | + fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n") |
| 167 | + fmt.Fprintf(conn, "CHANGE ADJUSTMENT OK\r\n") |
| 168 | + // /changeadjustment/1,120,100/15,255,255/150/0.2を受け取ったら、jsonファイルを変更する |
| 169 | + // jsonファイルを変更する |
| 170 | + |
| 171 | + os.Remove("threshold.json") |
| 172 | + file, err := os.Create("threshold.json") |
| 173 | + if err != nil { |
| 174 | + log.Println(err) |
| 175 | + } |
| 176 | + defer file.Close() |
| 177 | + // jsonファイルに書き込む |
| 178 | + data := Adjustment{Min_Threshold: minThreshold, Max_Threshold: maxThreshold, Ball_Detect_Radius: ballDetectRadius, Circularity_Threshold: float32(circularityThreshold)} |
| 179 | + jsonData, err := json.Marshal(data) |
| 180 | + if err != nil { |
| 181 | + fmt.Fprintf(conn, "HTTP/1.1 500 Internal Server Error\r\n") |
| 182 | + fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n") |
| 183 | + fmt.Fprintf(conn, "500 Internal Server Error\r\n") |
| 184 | + } |
| 185 | + _, err = file.Write(jsonData) |
| 186 | + if err != nil { |
| 187 | + fmt.Fprintf(conn, "HTTP/1.1 500 Internal Server Error\r\n") |
| 188 | + fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n") |
| 189 | + fmt.Fprintf(conn, "500 Internal Server Error\r\n") |
| 190 | + } |
| 191 | + // jsonファイルを閉じる |
| 192 | + err = file.Close() |
| 193 | + if err != nil { |
| 194 | + fmt.Fprintf(conn, "HTTP/1.1 500 Internal Server Error\r\n") |
| 195 | + fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n") |
| 196 | + fmt.Fprintf(conn, "500 Internal Server Error\r\n") |
| 197 | + } |
| 198 | + |
| 199 | + cmd.Process.Kill() |
| 200 | + cmd = exec.Command("python3", "main.py") |
| 201 | + |
| 202 | + err = cmd.Start() |
| 203 | + if err != nil { |
| 204 | + log.Println(err) |
| 205 | + } |
| 206 | + |
| 207 | + return |
| 208 | + } |
| 209 | + |
120 | 210 | // 200 OKを返す |
121 | 211 | fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n") |
122 | 212 | // UTF-8指定 |
123 | 213 | fmt.Fprintf(conn, "Content-Type: application/json; charset=utf-8\r\n\r\n") |
124 | 214 |
|
125 | 215 | // JSON形式で返す |
| 216 | + |
| 217 | + // 左から1ビットだけを取り出す |
| 218 | + detectPhotoSensor := 0b10000000&recvdata.SensorInformation != 0 |
| 219 | + // 左から2ビット目だけを取り出す |
| 220 | + detectDribblerSensor := 0b01000000&recvdata.SensorInformation != 0 |
| 221 | + // 左から3ビット目だけを取り出す |
| 222 | + isNewDribbler := 0b00100000&recvdata.SensorInformation != 0 |
| 223 | + |
126 | 224 | response := fmt.Sprintf(`{ |
127 | 225 | "VOLT": %f, |
128 | | - "ISHOLDBALL": %t, |
| 226 | + "ISDETECTPHOTOSENSOR": %t, |
| 227 | + "ISDETECTDRIBBLERSENSOR": %t, |
| 228 | + "ISNEWDRIBBLER": %t, |
129 | 229 | "ERROR": %t, |
130 | 230 | "ERRORCODE": %d, |
131 | 231 | "ERRORMESSAGE": "%s" |
132 | | - }`, float32(recvdata.Volt)/10.0, recvdata.IsDetectPhotosensor, isRobotError, RobotErrorCode, RobotErrorMessage) |
| 232 | + }`, float32(recvdata.Volt)/10.0, detectPhotoSensor, detectDribblerSensor, isNewDribbler, isRobotError, RobotErrorCode, RobotErrorMessage) |
133 | 233 |
|
134 | 234 | fmt.Fprint(conn, response) |
135 | 235 |
|
|
0 commit comments