-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
297 lines (263 loc) · 10.6 KB
/
index.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!-- 20240922 weblock追加 -->
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>温度位置情報</title>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
}
.container {
width: 100%;
max-width: 1200px;
margin: 0 auto;
text-align: center;
}
h1 {
text-align: center;
font-size: 1.5em;
}
.info-box {
border: 1px solid #ccc;
padding: 10px;
margin: 10px 0;
text-align: center;
width: 100%;
}
#map {
height: 300px;
width: 100%;
margin: 10px 0;
}
button {
width: 100%;
padding: 15px;
margin-top: 20px;
background-color: #4CAF50;
color: white;
border: none;
font-size: 1.2em;
cursor: pointer;
}
button:disabled {
background-color: #ccc;
}
.header {
display: flex;
justify-content: center;
align-items: center;
margin: 20px 0;
}
.logo {
width: 50px;
height: 50px;
background-color: #ccc;
margin-right: 10px;
}
.drumroll {
margin: 20px 0;
width: 100%;
}
select {
width: 100%;
padding: 10px;
font-size: 1.2em;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<div class="logo">ロゴ</div>
<h1>温度位置情報</h1>
</div>
<div class="drumroll">
<label for="deviceSelect">デバイスIDを選択</label>
<select id="deviceSelect" onchange="updateDeviceId()">
<option value="--">--</option>
<!-- 1〜30のドラムロール -->
<script>
for (let i = 1; i <= 30; i++) {
document.write('<option value="' + i + '">' + i + '</option>');
}
</script>
</select>
</div>
<button id="startButton" onclick="startMeasurement()">測定開始</button>
<div class="info-box" id="measurementInfo" style="display: none;">
<p>温度: <span id="temperature">--</span> °C</p>
<p>緯度: <span id="latitude">--</span></p>
<p>経度: <span id="longitude">--</span></p>
</div>
<div id="map"></div>
<button id="endButton" style="display: none;" onclick="endMeasurement()">測定終了</button>
</div>
<script>
let temperatureCharacteristic;
let map, marker;
// Wake Lock API用の変数
let wakeLock = null;
// ウェイクロックをリクエストする関数
async function requestWakeLock() {
try {
wakeLock = await navigator.wakeLock.request('screen');
wakeLock.addEventListener('release', () => {
console.log('Wake Lock was released');
});
console.log('Wake Lock is active');
} catch (err) {
console.error(`${err.name}, ${err.message}`);
}
}
// ウェイクロックを解放する関数
async function releaseWakeLock() {
if (wakeLock !== null) {
await wakeLock.release();
wakeLock = null;
console.log('Wake Lock released');
}
}
// ページの表示状態が変わったときにウェイクロックを再取得
document.addEventListener('visibilitychange', async () => {
if (wakeLock !== null && document.visibilityState === 'visible') {
await requestWakeLock();
}
});
function updateDeviceId() {
const selectedDeviceId = document.getElementById("deviceSelect").value;
}
async function startMeasurement() {
const selectedDeviceId = document.getElementById("deviceSelect").value;
if (selectedDeviceId === "--") {
alert("デバイスIDを選択してください。");
return;
}
document.getElementById('startButton').style.display = 'none';
document.getElementById('measurementInfo').style.display = 'block';
document.getElementById('endButton').style.display = 'block';
connectBLE(selectedDeviceId);
getLocation();
setInterval(getLocation, 10000); // 1分ごとにGPSを更新
setInterval(uploadData, 10000); // 1分ごとにデータをアップロード
// ウェイクロックをリクエスト
await requestWakeLock();
}
async function endMeasurement() {
document.getElementById('startButton').style.display = 'block';
document.getElementById('measurementInfo').style.display = 'none';
document.getElementById('endButton').style.display = 'none';
// ウェイクロックを解放
await releaseWakeLock();
}
// BLE接続
async function connectBLE(deviceId) {
const deviceName = `M5Stamp_Temp_${deviceId}`;
try {
const device = await navigator.bluetooth.requestDevice({
filters: [{
name: deviceName
}],
optionalServices: [0x1809] // 温度サービスUUIDを指定
});
const server = await device.gatt.connect();
const service = await server.getPrimaryService(0x1809);
// temperatureCharacteristic = await service.getCharacteristic('health_thermometer');
temperatureCharacteristic = await service.getCharacteristic(0x2A1C);
await temperatureCharacteristic.startNotifications();
temperatureCharacteristic.addEventListener('characteristicvaluechanged', handleTemperatureChanged);
alert("BLEデバイスに接続しました。");
} catch (error) {
console.error('BLE接続エラー: ', error);
alert("デバイスの接続に失敗しました。");
}
}
function handleTemperatureChanged(event) {
const value = new TextDecoder().decode(event.target.value);
document.getElementById('temperature').textContent = value;
}
// 現在地を地図の中心にする
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, handleLocationError);
} else {
alert("このブラウザではGPSがサポートされていません。");
}
}
function showPosition(position) {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
document.getElementById('latitude').textContent = lat;
document.getElementById('longitude').textContent = lon;
if (map && marker) {
map.removeLayer(marker);
}
// 現在地を地図の中心に設定
map.setView([lat, lon], 14);
marker = L.marker([lat, lon]).addTo(map);
}
function handleLocationError(error) {
alert(`現在地の取得に失敗しました: ${error.message}`);
}
// async function uploadData() {
// const temp = document.getElementById('temperature').textContent;
// const lat = document.getElementById('latitude').textContent;
// const lon = document.getElementById('longitude').textContent;
// // await fetch('https://webhook.site/', {
// await fetch('http://harvest.soracom.io', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify({
// temp: parseFloat(temp),
// lat: parseFloat(lat),
// lon: parseFloat(lon)
// })
// });
// }
async function uploadData() {
const temp = document.getElementById('temperature').textContent;
const lat = document.getElementById('latitude').textContent;
const lon = document.getElementById('longitude').textContent;
const data = {
"temp": parseFloat(temp),
"lat": parseFloat(lat),
"lon": parseFloat(lon)
};
try {
const response = await fetch('https://asia-northeast1-my-project-test-192507.cloudfunctions.net/proxyFunction', { // デプロイしたCloud FunctionのURLに変更
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.text(); // 必要に応じてJSON.parse(response.text())など
console.log('Success:', result);
} catch (error) {
console.error('Error:', error);
}
}
window.onload = function() {
map = L.map('map').setView([35.681236, 139.767125], 4);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
};
</script>
</body>
</html>