-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (71 loc) · 2.86 KB
/
Copy pathapp.js
File metadata and controls
77 lines (71 loc) · 2.86 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
let selectedDeviceId;
const codeReader = new ZXing.BrowserMultiFormatReader();
let map;
function showFeedback() {
const beep = document.getElementById('beep');
const feedback = document.getElementById('feedback');
beep.play();
feedback.style.display = 'block';
setTimeout(() => {
feedback.style.display = 'none';
}, 1000);
}
function startScan() {
codeReader.decodeOnceFromVideoDevice(selectedDeviceId, 'video').then(result => {
const barcode = result.text;
const timestamp = new Date().toISOString();
navigator.geolocation.getCurrentPosition(pos => {
const latitude = pos.coords.latitude;
const longitude = pos.coords.longitude;
const config = JSON.parse(localStorage.getItem('config'));
const payload = {
barcode,
timestamp,
latitude,
longitude
};
fetch(`https://script.google.com/macros/s/${config.sheetId}/exec`, {
method: 'POST',
body: JSON.stringify(payload)
}).then(res => res.text()).then(txt => {
document.getElementById('output').innerText = `${timestamp}
${barcode}`;
showFeedback();
});
});
});
}
function scanConfig() {
codeReader.decodeOnceFromVideoDevice(selectedDeviceId, 'video').then(result => {
localStorage.setItem('config', result.text);
document.getElementById('output').innerText = 'Konfiguration gespeichert';
});
}
function searchEntries() {
const searchValue = document.getElementById('searchValue').value;
const config = JSON.parse(localStorage.getItem('config'));
fetch(`https://script.google.com/macros/s/${config.sheetId}/exec?search=${encodeURIComponent(searchValue)}`)
.then(res => res.json())
.then(data => {
document.getElementById('output').innerText = `${data.length} Einträge gefunden.`;
if (!map) {
map = L.map('map').setView([0, 0], 2);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap'
}).addTo(map);
}
map.eachLayer(layer => {
if (layer instanceof L.Marker) map.removeLayer(layer);
});
data.forEach(entry => {
if (entry.latitude && entry.longitude) {
L.marker([entry.latitude, entry.longitude]).addTo(map)
.bindPopup(entry.barcode + '<br>' + entry.timestamp);
}
});
});
}
codeReader.listVideoInputDevices().then(videoInputDevices => {
const backCamera = videoInputDevices.find(device => device.label.toLowerCase().includes('back'));
selectedDeviceId = backCamera ? backCamera.deviceId : videoInputDevices[0].deviceId;
});