Skip to content

Commit f3c4bda

Browse files
authored
Merge pull request #7 from Rione/send-image
Send image
2 parents 35afcd6 + 9a9bb84 commit f3c4bda

13 files changed

Lines changed: 1051 additions & 395 deletions

api.go

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"log"
67
"net"
8+
"os"
9+
"os/exec"
710
"strconv"
811
"strings"
912
"time"
1013
)
1114

15+
var cmd *exec.Cmd
16+
1217
func RunApi(chapi chan bool, MyID uint32) {
18+
// python3 main.py を実行する
19+
cmd = exec.Command("python3", "main.py")
20+
//コマンドを実行
21+
cmd.Start()
1322
//ポートを開く
1423
listener, err := net.Listen("tcp", PORT)
1524
if err != nil {
@@ -117,19 +126,110 @@ func HandleRequest(conn net.Conn) {
117126
return
118127
}
119128

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+
120210
// 200 OKを返す
121211
fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n")
122212
// UTF-8指定
123213
fmt.Fprintf(conn, "Content-Type: application/json; charset=utf-8\r\n\r\n")
124214

125215
// 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+
126224
response := fmt.Sprintf(`{
127225
"VOLT": %f,
128-
"ISHOLDBALL": %t,
226+
"ISDETECTPHOTOSENSOR": %t,
227+
"ISDETECTDRIBBLERSENSOR": %t,
228+
"ISNEWDRIBBLER": %t,
129229
"ERROR": %t,
130230
"ERRORCODE": %d,
131231
"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)
133233

134234
fmt.Fprint(conn, response)
135235

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func kickCheck(chkicker chan bool) {
4141
//ループを行うため、少し待機する
4242
time.Sleep(16 * time.Millisecond)
4343
}
44+
<-chkicker
4445
}
4546

4647
func main() {
@@ -80,6 +81,7 @@ func main() {
8081
netInterfaceAddresses, _ := net.InterfaceAddrs()
8182

8283
ip := "0.0.0.0"
84+
ipCamera := "127.0.0.1"
8385
for _, netInterfaceAddress := range netInterfaceAddresses {
8486
networkIp, ok := netInterfaceAddress.(*net.IPNet)
8587
if ok && !networkIp.IP.IsLoopback() && networkIp.IP.To4() != nil {
@@ -156,6 +158,7 @@ func main() {
156158
chkick := make(chan bool)
157159
chgpio := make(chan bool)
158160
chapi := make(chan bool)
161+
chreceive := make(chan bool)
159162

160163
//各並列処理部分
161164
go RunClient(chclient, MyID, ip)
@@ -164,13 +167,15 @@ func main() {
164167
go kickCheck(chkick)
165168
go RunGPIO(chgpio)
166169
go RunApi(chapi, MyID)
170+
go ReceiveData(chreceive, MyID, ipCamera)
167171

168172
<-chclient
169173
<-chserver
170174
<-chserial
171175
<-chkick
172176
<-chgpio
173177
<-chapi
178+
<-chreceive
174179
}
175180

176181
func CheckError(err error) {

0 commit comments

Comments
 (0)