-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathemployees_rfid.php
165 lines (162 loc) · 6.71 KB
/
employees_rfid.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
<?php
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
____________________________________
/ Si necesitas ayuda, contáctame en \
\ https://parzibyte.me /
------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Creado por Parzibyte (https://parzibyte.me).
------------------------------------------------------------------------------------------------
Si el código es útil para ti, puedes agradecerme siguiéndome: https://parzibyte.me/blog/sigueme/
Y compartiendo mi blog con tus amigos
También tengo canal de YouTube: https://www.youtube.com/channel/UCroP4BTWjfM0CkGB6AFUoBg?sub_confirmation=1
------------------------------------------------------------------------------------------------
*/ ?>
<?php
include_once "header.php";
include_once "nav.php";
?>
<div class="row" id="app">
<div class="col-12">
<h1 class="text-center">RFID Pairing</h1>
</div>
<div class="col-12">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>
Employee
</th>
<th>
RFID serial
</th>
<th>
Actions
</th>
</tr>
</thead>
<tbody>
<tr v-for="employee in employees">
<td>{{employee.name}}</td>
<td>
<h4 v-if="employee.rfid_serial"><span class="badge badge-success"><i class="fa fa-check"></i> Assigned ({{employee.rfid_serial}})</span></h4>
<h4 v-else-if="employee.waiting"><span class="badge badge-warning"><i class="fa fa-clock"></i> Waiting... Please read a RFID card</span></h4>
<h4 v-else><span class="badge badge-primary"><i class="fa fa-times"></i> Not assigned</span></h4>
</td>
<td>
<button @click="removeRfidCard(employee.rfid_serial)" v-if="employee.rfid_serial" class="btn btn-danger">Remove</button>
<button v-else-if="employee.waiting" @click="cancelWaitingForPairing" class="btn btn-warning">Cancel</button>
<button @click="assignRfidCard(employee)" v-else class="btn btn-info">Assign</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="js/vue.min.js"></script>
<script src="js/vue-toasted.min.js"></script>
<script>
Vue.use(Toasted);
let shouldCheck = true;
const CHECK_PAIRING_EMPLOYEE_INTERVAL = 1000;
new Vue({
el: "#app",
data: () => ({
employees: [],
date: "",
}),
async mounted() {
await this.setReaderForReading();
await this.refreshEmployeesList();
},
methods: {
async removeRfidCard(rfidSerial) {
await fetch("./remove_rfid_card.php?rfid_serial=" + rfidSerial);
this.$toasted.show("RFID removed", {
position: "top-left",
duration: 1000,
});
await this.refreshEmployeesList();
},
async cancelWaitingForPairing() {
shouldCheck = false;
await this.setReaderForReading();
},
async setReaderForReading() {
await fetch("./set_reader_for_reading.php");
},
async assignRfidCard(employee) {
shouldCheck = true;
const employeeId = employee.id;
employee.waiting = true;
await fetch("./set_reader_for_pairing.php?employee_id=" + employeeId);
this.checkIfEmployeeHasJustAssignedRfid(employee);
},
async checkIfEmployeeHasJustAssignedRfid(employee) {
const r = await fetch("./get_employee_rfid_serial_by_id.php?employee_id=" + employee.id);
const serial = await r.json();
if (!shouldCheck) {
employee.waiting = false;
return;
}
if (serial) {
this.$toasted.show("RFID assigned!", {
position: "top-left",
duration: 1000,
});
await this.setReaderForReading();
await this.refreshEmployeesList();
} else {
setTimeout(() => {
this.checkIfEmployeeHasJustAssignedRfid(employee);
}, CHECK_PAIRING_EMPLOYEE_INTERVAL);
}
},
async refreshEmployeesList() {
// Get all employees
let response = await fetch("./get_employees_ajax.php");
let employees = await response.json();
// Set rfid_serial by default: null
let employeeDictionary = {};
employees = employees.map((employee, index) => {
employeeDictionary[employee.id] = index;
return {
id: employee.id,
name: employee.name,
rfid_serial: null,
waiting: false,
}
});
// Get RFID data, if any
response = await fetch(`./get_employees_with_rfid.php`);
let rfidData = await response.json();
// Refresh rfid data in each employee, if any
rfidData.forEach(rfidDetail => {
let employeeId = rfidDetail.employee_id;
if (employeeId in employeeDictionary) {
let index = employeeDictionary[employeeId];
employees[index].rfid_serial = rfidDetail.rfid_serial;
}
});
// Let Vue do its magic ;)
this.employees = employees;
}
},
});
</script>
<?php
include_once "footer.php";