-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrScanner.php
243 lines (207 loc) · 6.35 KB
/
qrScanner.php
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
<title>Match Data</title>
<html lang="en">
<?php include('navbar.php'); ?>
<body class="bg-body">
<div class="container row-offcanvas row-offcanvas-left">
<div class="well column col-lg-12 col-sm-12 col-xs-12" id="content">
<div class="row pt-3 pb-3 mb-3">
<!-- Left column -->
<div class="col-lg-12 col-sm-12 col-xs-12 gx-3">
<div class="card">
<div class="card-body">
<div class="row">
<select id="cameraSelect" class="form-select form-select mb-3" aria-label=".form-select-lg example">
</select>
<button id="submitData" type="button" class="btn btn-success">Upload Data</button>
</div>
<br>
<div class="row pt-3 pb-3 mb-3">
<div id="interactive" class="viewport">
<video autoplay="true" id="camera">
</div>
</div>
<br>
<div class="table-responsive">
<table id='verificationTable' class="table table-hover">
<thead>
<tr>
<th scope="col">Scouter</th>
<th scope="col">Match</th>
<th scope="col">Team</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody id='verificationTableBody'>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<?php include("footer.php"); ?>
<script type="text/javascript" src="js/zxing.min.js"></script>
<script>
var scannedData = {};
var scannedCount = 0;
function alertSuccessfulScan() {
// try {window.navigator.vibrate(200);}
// catch (exception) {}
$("#content").addClass("bg-success");
var timeoutFunction = setTimeout(function () { $("#content").removeClass("bg-success"); }, 500);
}
function setDefaultDeviceID(id) {
localStorage.setItem("cameraDefaultID", id);
}
function getDefaultDeviceID(id) {
var defaultId = localStorage.getItem("cameraDefaultID");
if (defaultId == null) {
return id;
}
return defaultId;
}
function validateQrList(dataList) {
/* Do more validation. */
return dataList.length == 16;
}
function qrListToKey(dataObj) {
return dataObj["matchNumber"] + "_" + dataObj["teamNumber"];
}
function addQrData(dataObj) {
console.log(dataObj);
var matchKey = qrListToKey(dataObj);
if (!scannedData.hasOwnProperty(matchKey)){
scannedData[matchKey] = dataObj;
scannedCount++;
$('#submitData').html(`Submit Data: ${scannedCount}`);
var rows =
[`<tr id='${matchKey}_row'>`,
` <td>${dataObj['scout']}</td>`,
` <td>${dataObj['matchNumber']}</td>`,
` <td>${dataObj['teamNumber']}</td>`,
` <td><button id='${matchKey}_delete' value='${matchKey}' type='button' class='btn btn-danger deleteRowButton'>Delete</button></td>`,
`</tr>`].join('');
$('#verificationTableBody').append(rows);
$(`#${matchKey}_delete`).on('click', function(event){
removeQrData($(this).val());
});
}
}
function removeQrData(dataKey) {
console.log(dataKey);
if (scannedData.hasOwnProperty(dataKey)) {
delete scannedData[dataKey];
--scannedCount;
$("#submitData").html(`Submit Data: ${scannedCount}`);
$(`#${dataKey}_row`).remove();
}
}
function clearData(){
$("#verificationTableBody").html("");
scannedCount = 0;
scannedData = {};
$("#submitData").html(`Submit Data: ${scannedCount}`);
}
function submitData(){
var dataList = []
for (const [key, value] of Object.entries(scannedData)){
dataList.push(value);
}
$.post('writeAPI.php', {
'writeDataList': JSON.stringify(dataList)
}, function(data){
data = JSON.parse(data);
if (data['success']){
alert('Data Successfully Submitted! Clearing Data.')
clearData();
}
else {
alert('Data not submitted!');
}
})
}
function uncompressDataList(dataList){
var out = {};
out['scout'] = dataList[0];
out['matchNumber'] = dataList[1];
out['teamNumber'] = dataList[2];
if(dataList[3] == true){
out['autoMobility'] = 1;
}else{
out['autoMobility'] = 0;
}
out['autoAmpNote'] = dataList[4];
out['autoSpeakerNote'] = dataList[5];
out["autoPath"] = '';
out['teleopAmpNote'] = dataList[7];
out['teleopSpeaker'] = dataList[8];
out['teleopSpeakerAmplified'] = dataList[9];
out['teleopTrap'] = dataList[10];
out['climb'] = dataList[11];
if(dataList[12] == true){
out['climbSpotlighted'] = 1;
}else{
out['climbSpotlighted'] = 0;
}
if(dataList[13] == true){
out['climbHarmony'] = 1;
}else{
out['climbHarmony'] = 0;
}
out['cannedComments'] = dataList[14];
out['textComments'] = '';
return out;
}
function scanCamera(reader, id) {
reader.decodeFromInputVideoDeviceContinuously(id, 'camera', (result, err) => {
if (result) {
console.log(result.text);
var dataList = JSON.parse(result.text);
console.log(dataList);
console.log("scanCamera: dataList = "+dataList);
if (validateQrList(dataList)) {
console.log('valid');
var uncompressedList = uncompressDataList(dataList);
alertSuccessfulScan();
addQrData(uncompressedList);
}
else {
alert("Error! Check QR code.");
}
}
});
}
function createCameraSelect(reader) {
reader.getVideoInputDevices().then((videoInputDevices) => {
// Creates drop down menu to switch between cameras
var initial_id = null;
if (videoInputDevices.length >= 1) {
videoInputDevices.forEach((element) => {
if (initial_id == null) {
initial_id = element.deviceId;
}
$("#cameraSelect").append($('<option>', { value: element.deviceId, text: element.label }));
});
}
// Creates default camera scanner based on saved data
scanCamera(reader, getDefaultDeviceID(initial_id));
// Binds drop down on change to select new camera when necessary
$("#cameraSelect").change(function () {
var selCamID = $("#cameraSelect").val();
scanCamera(reader, selCamID);
setDefaultDeviceID(selCamID);
});
});
}
$(document).ready(function () {
const reader = new ZXing.BrowserQRCodeReader();
createCameraSelect(reader);
});
$('#submitData').on('click', function(){
submitData();
});
</script>
</html>