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