This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
140 lines (126 loc) · 3.88 KB
/
api.go
File metadata and controls
140 lines (126 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"log"
"net"
"strconv"
"strings"
"time"
)
func RunApi(chapi chan bool, MyID uint32) {
//ポートを開く
listener, err := net.Listen("tcp", PORT)
if err != nil {
log.Fatal(err)
}
defer listener.Close()
//接続を待つ
for {
conn, err := listener.Accept()
if err != nil {
log.Fatal(err)
}
// log
log.Println("Remote API Connected by ", conn.RemoteAddr())
//接続があったら処理を行う
go HandleRequest(conn)
}
}
// 接続があったときの処理
func HandleRequest(conn net.Conn) {
defer conn.Close()
//リクエストを解析
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
log.Println(err)
return
}
// リクエストを解析
// リクエストヘッダーの1行目を取得
request := string(buf)
// リクエストヘッダーの1行目をスペースで区切る
requests := strings.Split(request, " ")
// リクエストヘッダーの1行目からリクエストの種類を取得
requestType := requests[0]
// リクエストの種類がGETでなければエラーを返す
if requestType != "GET" {
fmt.Fprintf(conn, "HTTP/1.1 400 Bad Request\r\n")
fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n")
fmt.Fprintf(conn, "400 Bad Request\r\n")
return
}
// リクエストのパスが"/buzzer"の場合
if strings.Split(requests[1], "/")[1] == "buzzer" {
// tone が指定されていない場合
if len(requests) < 3 {
fmt.Fprintf(conn, "HTTP/1.1 400 Bad Request\r\n")
fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n")
fmt.Fprintf(conn, "400 Bad Request\r\n")
return
}
// buzzer/ の後に tone が指定されている場合
log.Println(strings.Split(requests[1], "/")[1])
tone, err := strconv.Atoi(strings.Split(requests[1], "/")[3])
if err != nil {
fmt.Fprintf(conn, "HTTP/1.1 400 Bad Request\r\n")
fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n")
fmt.Fprintf(conn, "400 Bad Request\r\n")
return
}
duration, err := strconv.Atoi(strings.Split(requests[1], "/")[4])
if err != nil {
fmt.Fprintf(conn, "HTTP/1.1 400 Bad Request\r\n")
fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n")
fmt.Fprintf(conn, "400 Bad Request\r\n")
return
}
// tone が 0 から 12 でない場合
if tone < 0 || tone > 15 {
fmt.Fprintf(conn, "HTTP/1.1 400 Bad Request\r\n")
fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n")
fmt.Fprintf(conn, "400 Bad Request\r\n")
return
}
// duration が 50 から 3000 でない場合
if duration < 50 || duration > 3000 {
fmt.Fprintf(conn, "HTTP/1.1 400 Bad Request\r\n")
fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n")
fmt.Fprintf(conn, "400 Bad Request\r\n")
return
}
// OK と表示
fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n")
fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n")
fmt.Fprintf(conn, "BUZZER OK\r\n")
//ブザーを1秒鳴らす
doBuzzer = true
buzzerTone = tone
buzzerTime = time.Duration(duration) * time.Millisecond
return
}
if strings.Split(requests[1], "/")[1] == "ignorebatterylow" {
// OK と表示
fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n")
fmt.Fprintf(conn, "Content-Type: text/plain; charset=utf-8\r\n\r\n")
fmt.Fprintf(conn, "IGNORE BATTERY LOW OK\r\n")
//アラーム無視をセットする
alarmIgnore = true
return
}
// 200 OKを返す
fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n")
// UTF-8指定
fmt.Fprintf(conn, "Content-Type: application/json; charset=utf-8\r\n\r\n")
// JSON形式で返す
response := fmt.Sprintf(`{
"VOLT": %f,
"PHOTOSENSOR": %d,
"ISHOLDBALL": %t,
"IMUDIR": %d,
"ERROR": %t,
"ERRORCODE": %d,
"ERRORMESSAGE": "%s"
}`, float32(recvdata.Volt)/10.0, recvdata.PhotoSensor, recvdata.IsHoldBall, recvdata.ImuDir, isRobotError, RobotErrorCode, RobotErrorMessage)
fmt.Fprint(conn, response)
}