Update index.html
Browse files- index.html +50 -91
index.html
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>
|
| 7 |
<style>
|
| 8 |
body {
|
| 9 |
font-family: Arial, sans-serif;
|
|
@@ -11,12 +11,12 @@
|
|
| 11 |
#container {
|
| 12 |
margin: 50px;
|
| 13 |
}
|
| 14 |
-
#output, #
|
| 15 |
margin-top: 10px;
|
| 16 |
padding: 10px;
|
| 17 |
border: 1px solid #ccc;
|
| 18 |
background-color: #f9f9f9;
|
| 19 |
-
width:
|
| 20 |
}
|
| 21 |
#status {
|
| 22 |
margin-top: 10px;
|
|
@@ -38,11 +38,12 @@
|
|
| 38 |
</head>
|
| 39 |
<body>
|
| 40 |
<div id="container">
|
| 41 |
-
<h1>
|
| 42 |
-
<p>
|
| 43 |
-
<input type="text" id="
|
| 44 |
-
<
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
<div id="controls">
|
| 48 |
<button onclick="startRecording()">記録開始</button>
|
|
@@ -61,6 +62,7 @@
|
|
| 61 |
<div id="current-flags">
|
| 62 |
<strong>現在のフラグ:</strong> なし
|
| 63 |
</div>
|
|
|
|
| 64 |
</div>
|
| 65 |
|
| 66 |
<div id="export">
|
|
@@ -73,17 +75,13 @@
|
|
| 73 |
<div id="status">Status: Not connected</div>
|
| 74 |
<div id="output">最新サーバーデータがここに表示されます</div>
|
| 75 |
<div id="client-sensor">クライアントサイドデータ: 加速度と回転率がここに表示されます</div>
|
| 76 |
-
<div id="ping">Ping: 計測中...</div>
|
| 77 |
<div id="client-refresh">クライアント リフレッシュレート: 計測中...</div>
|
| 78 |
-
<div id="server-refresh">サーバー リフレッシュレート: 計測中...</div>
|
| 79 |
<div id="error">エラー: なし</div>
|
| 80 |
</div>
|
| 81 |
|
| 82 |
<script>
|
| 83 |
-
|
| 84 |
-
let
|
| 85 |
-
let lastClientUpdate = null;
|
| 86 |
-
let clientRefreshRate = 0;
|
| 87 |
|
| 88 |
// センサー記録用
|
| 89 |
let isRecording = false;
|
|
@@ -122,72 +120,18 @@
|
|
| 122 |
if (flags.hasOwnProperty(flag)) {
|
| 123 |
flags[flag] = !flags[flag];
|
| 124 |
updateFlagButtons();
|
| 125 |
-
sendFlagsViaWebSocket();
|
| 126 |
}
|
| 127 |
}
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
};
|
| 139 |
-
|
| 140 |
-
websocket.onmessage = function(event) {
|
| 141 |
-
// チェック: PING応答か通常のメッセージか
|
| 142 |
-
if (event.data === 'pong') {
|
| 143 |
-
let endTime = new Date().getTime();
|
| 144 |
-
let pingTime = endTime - startTimePing;
|
| 145 |
-
document.getElementById('ping').innerHTML = "Ping: " + pingTime + " ms";
|
| 146 |
-
return;
|
| 147 |
-
}
|
| 148 |
-
|
| 149 |
-
document.getElementById('output').innerHTML = "最新サーバーデータ: " + event.data;
|
| 150 |
-
|
| 151 |
-
// サーバーリフレッシュレートの計算
|
| 152 |
-
let currentTime = new Date().getTime();
|
| 153 |
-
if (lastMessageTime !== null) {
|
| 154 |
-
let serverRefreshRate = (currentTime - lastMessageTime) / 1000;
|
| 155 |
-
document.getElementById('server-refresh').innerHTML = "サーバー リフレッシュレート: " + serverRefreshRate.toFixed(2) + "秒";
|
| 156 |
-
}
|
| 157 |
-
lastMessageTime = currentTime;
|
| 158 |
-
|
| 159 |
-
// クライアントリフレッシュレートの計算
|
| 160 |
-
if (lastClientUpdate !== null) {
|
| 161 |
-
clientRefreshRate = (currentTime - lastClientUpdate) / 1000;
|
| 162 |
-
document.getElementById('client-refresh').innerHTML = "クライアント リフレッシュレート: " + clientRefreshRate.toFixed(2) + "秒";
|
| 163 |
-
}
|
| 164 |
-
lastClientUpdate = currentTime;
|
| 165 |
-
};
|
| 166 |
-
|
| 167 |
-
websocket.onclose = function() {
|
| 168 |
-
document.getElementById('status').innerHTML = "Status: Disconnected";
|
| 169 |
-
};
|
| 170 |
-
|
| 171 |
-
websocket.onerror = function(error) {
|
| 172 |
-
handleError(error);
|
| 173 |
-
};
|
| 174 |
-
} catch (error) {
|
| 175 |
-
handleError(error);
|
| 176 |
-
}
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
// PINGを送信して応答時間を計測
|
| 180 |
-
let startTimePing;
|
| 181 |
-
function startPing() {
|
| 182 |
-
try {
|
| 183 |
-
setInterval(function() {
|
| 184 |
-
if (websocket.readyState === WebSocket.OPEN) {
|
| 185 |
-
startTimePing = new Date().getTime();
|
| 186 |
-
websocket.send('ping');
|
| 187 |
-
}
|
| 188 |
-
}, 5000); // 5秒ごとにPINGを送信
|
| 189 |
-
} catch (error) {
|
| 190 |
-
handleError(error);
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|
|
@@ -247,8 +191,8 @@
|
|
| 247 |
sensorData.push(sensorInfo);
|
| 248 |
}
|
| 249 |
|
| 250 |
-
// フラグを
|
| 251 |
-
|
| 252 |
});
|
| 253 |
} catch (error) {
|
| 254 |
handleError(error);
|
|
@@ -302,14 +246,12 @@
|
|
| 302 |
return;
|
| 303 |
}
|
| 304 |
|
| 305 |
-
// 送信先のサーバーURLを指定
|
| 306 |
-
let serverUrl = prompt("データを送信するサーバーのURLを入力してください(例: https://example.com/api/sensor-data):");
|
| 307 |
if (!serverUrl) {
|
| 308 |
-
alert("サーバーURL
|
| 309 |
return;
|
| 310 |
}
|
| 311 |
|
| 312 |
-
fetch(serverUrl, {
|
| 313 |
method: 'POST',
|
| 314 |
headers: {
|
| 315 |
'Content-Type': 'application/json'
|
|
@@ -319,6 +261,7 @@
|
|
| 319 |
.then(response => {
|
| 320 |
if (response.ok) {
|
| 321 |
alert("データが正常に送信されました。");
|
|
|
|
| 322 |
} else {
|
| 323 |
alert("データの送信に失敗しました。ステータスコード: " + response.status);
|
| 324 |
}
|
|
@@ -328,15 +271,31 @@
|
|
| 328 |
});
|
| 329 |
}
|
| 330 |
|
| 331 |
-
//
|
| 332 |
-
function
|
| 333 |
-
if (
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
flags: { ...flags }
|
| 337 |
-
};
|
| 338 |
-
websocket.send(JSON.stringify(flagsPayload));
|
| 339 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
}
|
| 341 |
|
| 342 |
// エラーを画面とコンソールに出力
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>HTTP with Client-Side Sensor Data and Flags</title>
|
| 7 |
<style>
|
| 8 |
body {
|
| 9 |
font-family: Arial, sans-serif;
|
|
|
|
| 11 |
#container {
|
| 12 |
margin: 50px;
|
| 13 |
}
|
| 14 |
+
#output, #client-refresh, #client-sensor, #controls, #flags, #export, #send-data, #send-flags {
|
| 15 |
margin-top: 10px;
|
| 16 |
padding: 10px;
|
| 17 |
border: 1px solid #ccc;
|
| 18 |
background-color: #f9f9f9;
|
| 19 |
+
width: 320px;
|
| 20 |
}
|
| 21 |
#status {
|
| 22 |
margin-top: 10px;
|
|
|
|
| 38 |
</head>
|
| 39 |
<body>
|
| 40 |
<div id="container">
|
| 41 |
+
<h1>HTTP & Client-Side Sensor Data with Flags</h1>
|
| 42 |
+
<p>データを送信するサーバーのURLを指定してください(例: https://example.com/api/):</p>
|
| 43 |
+
<input type="text" id="server-url" placeholder="https://example.com/api/">
|
| 44 |
+
<div id="send-flags">
|
| 45 |
+
<button onclick="setServerUrl()">設定</button>
|
| 46 |
+
</div>
|
| 47 |
|
| 48 |
<div id="controls">
|
| 49 |
<button onclick="startRecording()">記録開始</button>
|
|
|
|
| 62 |
<div id="current-flags">
|
| 63 |
<strong>現在のフラグ:</strong> なし
|
| 64 |
</div>
|
| 65 |
+
<button onclick="sendFlagsToServer()">フラグを送信</button>
|
| 66 |
</div>
|
| 67 |
|
| 68 |
<div id="export">
|
|
|
|
| 75 |
<div id="status">Status: Not connected</div>
|
| 76 |
<div id="output">最新サーバーデータがここに表示されます</div>
|
| 77 |
<div id="client-sensor">クライアントサイドデータ: 加速度と回転率がここに表示されます</div>
|
|
|
|
| 78 |
<div id="client-refresh">クライアント リフレッシュレート: 計測中...</div>
|
|
|
|
| 79 |
<div id="error">エラー: なし</div>
|
| 80 |
</div>
|
| 81 |
|
| 82 |
<script>
|
| 83 |
+
// サーバーURL
|
| 84 |
+
let serverUrl = '';
|
|
|
|
|
|
|
| 85 |
|
| 86 |
// センサー記録用
|
| 87 |
let isRecording = false;
|
|
|
|
| 120 |
if (flags.hasOwnProperty(flag)) {
|
| 121 |
flags[flag] = !flags[flag];
|
| 122 |
updateFlagButtons();
|
|
|
|
| 123 |
}
|
| 124 |
}
|
| 125 |
|
| 126 |
+
// サーバーURLを設定
|
| 127 |
+
function setServerUrl() {
|
| 128 |
+
const inputUrl = document.getElementById('server-url').value.trim();
|
| 129 |
+
if (inputUrl) {
|
| 130 |
+
serverUrl = inputUrl.endsWith('/') ? inputUrl : inputUrl + '/';
|
| 131 |
+
document.getElementById('status').innerHTML = `Status: Server URL set to ${serverUrl}`;
|
| 132 |
+
document.getElementById('error').innerHTML = "エラー: なし";
|
| 133 |
+
} else {
|
| 134 |
+
alert("有効なサーバーURLを入力してください。");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
|
|
|
| 191 |
sensorData.push(sensorInfo);
|
| 192 |
}
|
| 193 |
|
| 194 |
+
// フラグをサーバーに送信
|
| 195 |
+
sendFlagsToServer();
|
| 196 |
});
|
| 197 |
} catch (error) {
|
| 198 |
handleError(error);
|
|
|
|
| 246 |
return;
|
| 247 |
}
|
| 248 |
|
|
|
|
|
|
|
| 249 |
if (!serverUrl) {
|
| 250 |
+
alert("サーバーURLを設定してください。");
|
| 251 |
return;
|
| 252 |
}
|
| 253 |
|
| 254 |
+
fetch(`${serverUrl}sensor-data`, {
|
| 255 |
method: 'POST',
|
| 256 |
headers: {
|
| 257 |
'Content-Type': 'application/json'
|
|
|
|
| 261 |
.then(response => {
|
| 262 |
if (response.ok) {
|
| 263 |
alert("データが正常に送信されました。");
|
| 264 |
+
document.getElementById('output').innerHTML = "データがサーバーに送信されました。";
|
| 265 |
} else {
|
| 266 |
alert("データの送信に失敗しました。ステータスコード: " + response.status);
|
| 267 |
}
|
|
|
|
| 271 |
});
|
| 272 |
}
|
| 273 |
|
| 274 |
+
// フラグをサーバーに送信
|
| 275 |
+
function sendFlagsToServer() {
|
| 276 |
+
if (!serverUrl) {
|
| 277 |
+
console.warn("サーバーURLが設定されていません。フラグを送信できません。");
|
| 278 |
+
return;
|
|
|
|
|
|
|
|
|
|
| 279 |
}
|
| 280 |
+
|
| 281 |
+
// フラグ情報を送信
|
| 282 |
+
fetch(`${serverUrl}flags`, {
|
| 283 |
+
method: 'POST',
|
| 284 |
+
headers: {
|
| 285 |
+
'Content-Type': 'application/json'
|
| 286 |
+
},
|
| 287 |
+
body: JSON.stringify({ flags: { ...flags } })
|
| 288 |
+
})
|
| 289 |
+
.then(response => {
|
| 290 |
+
if (response.ok) {
|
| 291 |
+
console.log("フラグが正常に送信されました。");
|
| 292 |
+
} else {
|
| 293 |
+
console.error("フラグの送信に失敗しました。ステータスコード: " + response.status);
|
| 294 |
+
}
|
| 295 |
+
})
|
| 296 |
+
.catch(error => {
|
| 297 |
+
handleError(error);
|
| 298 |
+
});
|
| 299 |
}
|
| 300 |
|
| 301 |
// エラーを画面とコンソールに出力
|