· 5 years ago · Jul 21, 2020, 03:02 PM
1?php
2defined('BASEPATH') OR exit('No direct script access allowed');
3
4class Api extends CI_Controller {
5
6 public function __construct() {
7 parent::__construct();
8 $this->load->model('m_admin');
9 $this->load->model('m_api');
10 date_default_timezone_set("asia/jakarta");
11 }
12
13 public function index()
14 {
15 echo "REST API for Device";
16
17 }
18
19 public function getmode(){
20 if (isset($_GET['key']) && isset($_GET['iddev'])) {
21 $key = $this->input->get('key');
22 $cekkey = $this->m_api->getkey();
23
24 if($cekkey[0]->key == $key){
25 $iddev = $this->input->get('iddev');
26
27 $data = $this->m_api->getmode($iddev);
28 if (isset($data)) {
29 $mode = "-";
30 foreach ($data as $key => $value) {
31 $mode = $value->mode;
32 }
33 if ($mode == "-") {
34 echo "*id-device-tidak-ditemukan*";
35 }else{
36 echo "*";
37 echo $mode;
38 echo "*";
39 }
40 }else{
41 echo "*id-device-tidak-ditemukan*";
42 }
43 }else{
44 echo "*salah-secret-key*";
45 }
46 }else{
47 echo "*salah-param*";
48 }
49 }
50
51 public function getmodejson(){
52 if (isset($_GET['key']) && isset($_GET['iddev'])) {
53 $key = $this->input->get('key');
54 $cekkey = $this->m_api->getkey();
55 //print_r($cekkey);
56 if($cekkey[0]->key == $key){
57 $iddev = $this->input->get('iddev');
58
59 $data = $this->m_api->getmode($iddev);
60 if (isset($data)) {
61 $mode = "-";
62 foreach ($data as $key => $value) {
63 $mode = $value->mode;
64 }
65 if ($mode == "-") {
66 $array = array('status' => 'warning', 'mode' => $mode, 'ket' => 'id device tidak ditemukan');
67 echo json_encode($array);
68 }else{
69 $array = array('status' => 'success', 'mode' => $mode, 'ket' => 'berhasil');
70 echo json_encode($array);
71 }
72 }else{
73 $array = array('status' => 'warning', 'mode' => $mode, 'ket' => 'id device tidak ditemukan');
74 echo json_encode($array);
75 }
76 }else{
77 $array = array('status' => 'failed', 'ket' => 'salah secret key');
78 echo json_encode($array);
79 }
80 }else{
81 $array = array('status' => 'failed', 'ket' => 'salah parameter');
82 echo json_encode($array);
83 }
84 }
85
86 public function addcard(){
87 if (isset($_GET['key']) && isset($_GET['iddev']) && isset($_GET['rfid'])) {
88 $key = $this->input->get('key');
89 $cekkey = $this->m_api->getkey();
90
91 if($cekkey[0]->key == $key){
92 $iddev = $this->input->get('iddev');
93 $rfid = $this->input->get('rfid');
94
95 $checkDoubleRFID = $this->m_api->checkRFID($rfid);
96 $z = 0;
97 if (isset($checkDoubleRFID)) {
98 foreach ($checkDoubleRFID as $key => $value) {
99 $z++;
100 }
101 }
102
103 if ($z > 0) {
104 echo "*RFID-sudah-terdaftar*";
105 }else{
106 $device = $this->m_api->getdevice($iddev);
107 $count = 0;
108 foreach ($device as $key => $value) {
109 $count++;
110 }
111 if ($count > 0) {
112 $savedata = array('id_devices' => $iddev, 'uid' => $rfid);
113 if ($this->m_api->insert_rfid($savedata)) {
114 $getlastrfid = $this->m_api->last_rfid();
115 $idrfid = 0;
116 if (isset($getlastrfid)) {
117 foreach ($getlastrfid as $key => $value) {
118 $idrfid = $value->id_rfid;
119 }
120 }
121 if ($idrfid > 0) {
122 $histori = array('id_rfid' => $idrfid, 'keterangan' => 'ADD RFID CARD', 'waktu' => time(), 'id_devices' => $iddev);
123 if ($this->m_api->insert_histori($histori)) {
124 echo "*berhasil-tambah-rfid-card*";
125 }
126 }else{
127 echo "*terjadi-kesalahan*";
128 }
129 }
130 }else{
131 echo "*id-device-tidak-ditemukan*";
132 }
133 }
134
135
136 }else{
137 echo "*salah-secret-key*";
138 }
139 }else{
140 echo "*salah-param*";
141 }
142 }
143
144
145 public function addcardjson(){
146 if (isset($_GET['key']) && isset($_GET['iddev']) && isset($_GET['rfid'])) {
147 $key = $this->input->get('key');
148 $cekkey = $this->m_api->getkey();
149
150 if($cekkey[0]->key == $key){
151 $iddev = $this->input->get('iddev');
152 $rfid = $this->input->get('rfid');
153
154 $checkDoubleRFID = $this->m_api->checkRFID($rfid);
155 $z = 0;
156 if (isset($checkDoubleRFID)) {
157 foreach ($checkDoubleRFID as $key => $value) {
158 $z++;
159 }
160 }
161
162 if ($z > 0) {
163 $notif = array('status' => 'failed', 'ket' => 'RFID sudah terdaftar');
164 echo json_encode($notif);
165 }else{
166 $device = $this->m_api->getdevice($iddev);
167 $count = 0;
168 foreach ($device as $key => $value) {
169 $count++;
170 }
171 if ($count > 0) {
172 $savedata = array('id_devices' => $iddev, 'uid' => $rfid);
173 if ($this->m_api->insert_rfid($savedata)) {
174 $getlastrfid = $this->m_api->last_rfid();
175 $idrfid = 0;
176 if (isset($getlastrfid)) {
177 foreach ($getlastrfid as $key => $value) {
178 $idrfid = $value->id_rfid;
179 }
180 }
181 if ($idrfid > 0) {
182 $histori = array('id_rfid' => $idrfid, 'keterangan' => 'ADD RFID CARD', 'waktu' => time(), 'id_devices' => $iddev);
183 if ($this->m_api->insert_histori($histori)) {
184 $notif = array('status' => 'success', 'ket' => 'berhasil tambah rfid card');
185 echo json_encode($notif);
186 }
187 }else{
188 $notif = array('status' => 'failed', 'ket' => 'terjadi kesalahan');
189 echo json_encode($notif);
190 }
191 }
192 }else{
193 $notif = array('status' => 'failed', 'ket' => 'device tidak ditemukan');
194 echo json_encode($notif);
195 }
196 }
197 }else{
198 $notif = array('status' => 'failed', 'ket' => 'salah secret key');
199 echo json_encode($notif);
200 }
201 }else{
202 $notif = array('status' => 'failed', 'ket' => 'salah parameter');
203 echo json_encode($notif);
204 }
205 }
206
207
208 public function absensi(){
209 if (isset($_GET['key']) && isset($_GET['iddev']) && isset($_GET['rfid'])) {
210 $key = $this->input->get('key');
211 $cekkey = $this->m_api->getkey();
212
213 if($cekkey[0]->key == $key){
214 $iddev = $this->input->get('iddev');
215 $rfid = $this->input->get('rfid');
216
217 $cekrfid = $this->m_api->checkRFID($rfid);
218 $countrfid = 0;
219 $idrfid = 0;
220 foreach ($cekrfid as $key => $value) {
221 $countrfid++;
222 $idrfid = $value->id_rfid;
223 }
224
225 $device = $this->m_api->getdevice($iddev);
226 $count = 0;
227 foreach ($device as $key => $value) {
228 $count++;
229 }
230
231 if ($count > 0) {
232 if ($countrfid > 0) {
233 $waktu = $this->m_api->waktuoperasional();
234 if (isset($waktu)) {
235 foreach ($waktu as $key => $value) {
236 if ($value->id_waktu_operasional == 1) {
237 $masuk = $value->waktu_operasional;
238 }
239 if ($value->id_waktu_operasional == 2) {
240 $keluar = $value->waktu_operasional;
241 }
242 }
243 }else{
244 echo "*error-waktu-operasional*";
245 }
246 if (isset($masuk) && isset($keluar)) {
247 $masuk = explode("-", $masuk);
248 $keluar = explode("-", $keluar);
249 if (isset($masuk[0]) && isset($masuk[1]) && isset($keluar[0]) && isset($keluar[1])) {
250 $masuk1 = strtotime($masuk[0]);
251 $masuk2 = strtotime($masuk[1]);
252 $keluar1 = strtotime($keluar[0]);
253 $keluar2 = strtotime($keluar[1]);
254
255 $absen = false;
256 $ket = "";
257 $respon = "";
258
259 if (time() < $masuk1) {
260 echo "*absensi-diluar-waktu*";
261 }
262 if (time() >= $masuk1 && time() <= $masuk2) {
263 $absen = true;
264 $ket = "masuk";
265 $respon = "*masuk-tepat-waktu*";
266 }
267 if (time() > $masuk2 && time() <= $masuk2 + 3600) { //3600 = 1 jam
268 $absen = true;
269 $ket = "masuk";
270 $respon = "*telat-masuk*";
271 }
272 if (time() > $masuk2 + 3600 && time() < $keluar1 ) { //3600 = 1 jam
273 echo "*absensi-diluar-waktu-masuk-dan-keluar*";
274 }
275 if (time() >= $keluar1 && time() <= $keluar2 + 3600) {
276 $absen = true;
277 $ket = "keluar";
278 $respon = "*keluar*";
279 }
280 if (time() > $keluar2 + 3600) {
281 echo "*absensi-diluar-waktu*";
282 }
283
284 if ($absen) {
285 $today = strtotime("today");
286 $tomorrow = strtotime("tomorrow");
287
288 $datamasuk = $this->m_api->get_absensi("masuk",$today,$tomorrow);
289 $datakeluar = $this->m_api->get_absensi("keluar",$today,$tomorrow);
290
291 $duplicate = 0;
292 if (isset($datamasuk)) {
293 foreach ($datamasuk as $key => $value) {
294 if ($value->id_rfid == $idrfid && $value->keterangan == $ket) {
295 $duplicate++;
296 }
297 }
298 }
299 if (isset($datakeluar)) {
300 foreach ($datakeluar as $key => $value) {
301 if ($value->id_rfid == $idrfid && $value->keterangan == $ket) {
302 $duplicate++;
303 }
304 }
305 }
306
307 if ($duplicate == 0) {
308 $data = array('id_devices' => $iddev, 'id_rfid' => $idrfid,
309 'keterangan' => $ket, 'created_at' => time());
310 if ($this->m_api->insert_absensi($data)) {
311 $histori = array('id_rfid' => $idrfid, 'keterangan' => $ket, 'waktu' => time(), 'id_devices' => $iddev);
312 $this->m_api->insert_histori($histori);
313 echo $respon;
314 }else{
315 echo "*gagal-insert-absensi*";
316 }
317 }else{
318 echo "*sudah-absensi*";
319 }
320 }
321 }
322 }else{
323 echo "*error-waktu-operasional*";
324 }
325 }else{
326 echo "*rfid-tidak-ditemukan*";
327 }
328 }else{
329 echo "*id-device-tidak-ditemukan*";
330 }
331 }else{
332 echo "*salah-secret-key*";
333 }
334 }else{
335 echo "*salah-param*";
336 }
337 }
338
339 public function absensijson(){
340 if (isset($_GET['key']) && isset($_GET['iddev']) && isset($_GET['rfid'])) {
341 $key = $this->input->get('key');
342 $cekkey = $this->m_api->getkey();
343
344 if($cekkey[0]->key == $key){
345 $iddev = $this->input->get('iddev');
346 $rfid = $this->input->get('rfid');
347
348 $cekrfid = $this->m_api->checkRFID($rfid);
349 $countrfid = 0;
350 $idrfid = 0;
351 foreach ($cekrfid as $key => $value) {
352 $countrfid++;
353 $idrfid = $value->id_rfid;
354 }
355
356 $device = $this->m_api->getdevice($iddev);
357 $count = 0;
358 foreach ($device as $key => $value) {
359 $count++;
360 }
361
362 if ($count > 0) {
363 if ($countrfid > 0) {
364 $waktu = $this->m_api->waktuoperasional();
365 if (isset($waktu)) {
366 foreach ($waktu as $key => $value) {
367 if ($value->id_waktu_operasional == 1) {
368 $masuk = $value->waktu_operasional;
369 }
370 if ($value->id_waktu_operasional == 2) {
371 $keluar = $value->waktu_operasional;
372 }
373 }
374 }else{
375 $notif = array('status' => 'failed', 'ket' => 'error waktu operasional');
376 echo json_encode($notif);
377 }
378 if (isset($masuk) && isset($keluar)) {
379 $masuk = explode("-", $masuk);
380 $keluar = explode("-", $keluar);
381 if (isset($masuk[0]) && isset($masuk[1]) && isset($keluar[0]) && isset($keluar[1])) {
382 $masuk1 = strtotime($masuk[0]);
383 $masuk2 = strtotime($masuk[1]);
384 $keluar1 = strtotime($keluar[0]);
385 $keluar2 = strtotime($keluar[1]);
386
387 $absen = false;
388 $ket = "";
389 $respon = "";
390
391 if (time() < $masuk1) {
392 $notif = array('status' => 'failed', 'ket' => 'absensi diluar waktu');
393 echo json_encode($notif);
394 }
395 if (time() >= $masuk1 && time() <= $masuk2) {
396 $absen = true;
397 $ket = "masuk";
398 $respon = "masuk tepat waktu";
399 }
400 if (time() > $masuk2 && time() <= $masuk2 + 3600) { //3600 = 1 jam
401 $absen = true;
402 $ket = "masuk";
403 $respon = "telat masuk";
404 }
405 if (time() > $masuk2 + 3600 && time() < $keluar1 ) { //3600 = 1 jam
406 $notif = array('status' => 'failed', 'ket' => 'absensi diluar waktu masuk dan keluar');
407 echo json_encode($notif);
408 }
409 if (time() >= $keluar1 && time() <= $keluar2 + 3600) {
410 $absen = true;
411 $ket = "keluar";
412 $respon = "keluar";
413 }
414 if (time() > $keluar2 + 3600) {
415 $notif = array('status' => 'failed', 'ket' => 'absensi diluar waktu');
416 echo json_encode($notif);
417 }
418
419 if ($absen) {
420 $today = strtotime("today");
421 $tomorrow = strtotime("tomorrow");
422
423 $datamasuk = $this->m_api->get_absensi("masuk",$today,$tomorrow);
424 $datakeluar = $this->m_api->get_absensi("keluar",$today,$tomorrow);
425
426 $duplicate = 0;
427 if (isset($datamasuk)) {
428 foreach ($datamasuk as $key => $value) {
429 if ($value->id_rfid == $idrfid && $value->keterangan == $ket) {
430 $duplicate++;
431 }
432 }
433 }
434 if (isset($datakeluar)) {
435 foreach ($datakeluar as $key => $value) {
436 if ($value->id_rfid == $idrfid && $value->keterangan == $ket) {
437 $duplicate++;
438 }
439 }
440 }
441
442 if ($duplicate == 0) {
443 $data = array('id_devices' => $iddev, 'id_rfid' => $idrfid,
444 'keterangan' => $ket, 'created_at' => time());
445 if ($this->m_api->insert_absensi($data)) {
446 $histori = array('id_rfid' => $idrfid, 'keterangan' => $ket, 'waktu' => time(), 'id_devices' => $iddev);
447 $this->m_api->insert_histori($histori);
448 $notif = array('status' => 'success', 'ket' => $respon);
449 echo json_encode($notif);
450 }else{
451 $notif = array('status' => 'failed', 'ket' => 'gagal insert absensi');
452 echo json_encode($notif);
453 }
454 }else{
455 $notif = array('status' => 'failed', 'ket' => 'sudah absensi');
456 echo json_encode($notif);
457 }
458 }
459 }
460 }else{
461 $notif = array('status' => 'failed', 'ket' => 'error waktu operasional');
462 echo json_encode($notif);
463 }
464 }else{
465 $notif = array('status' => 'failed', 'ket' => 'rfid tidak ditemukan');
466 echo json_encode($notif);
467 }
468 }else{
469 $notif = array('status' => 'failed', 'ket' => 'id device tidak ditemukan');
470 echo json_encode($notif);
471 }
472 }else{
473 $notif = array('status' => 'failed', 'ket' => 'salah secret key');
474 echo json_encode($notif);
475 }
476 }else{
477 $notif = array('status' => 'failed', 'ket' => 'salah parameter');
478 echo json_encode($notif);
479 }
480 }
481
482
483 public function absensifoto(){
484 if (isset($_POST['key']) && isset($_POST['iddev']) && isset($_POST['rfid']) && isset($_POST['foto'])) {
485 $key = $this->input->post('key');
486 $cekkey = $this->m_api->getkey();
487
488 if($cekkey[0]->key == $key){
489 $iddev = $this->input->post('iddev');
490 $rfid = $this->input->post('rfid');
491 $foto = $this->input->post('foto');
492
493 $cekrfid = $this->m_api->checkRFID($rfid);
494 $countrfid = 0;
495 $idrfid = 0;
496 foreach ($cekrfid as $key => $value) {
497 $countrfid++;
498 $idrfid = $value->id_rfid;
499 }
500
501 $device = $this->m_api->getdevice($iddev);
502 $count = 0;
503 foreach ($device as $key => $value) {
504 $count++;
505 }
506
507 if ($count > 0) {
508 if ($countrfid > 0) {
509 $lastRFID = $this->m_api->lastRFIDfoto($idrfid);
510 if (isset($lastRFID)) {
511 foreach ($lastRFID as $key => $value) {
512 if ($value->foto == "") {
513 $data = array('foto' => $foto);
514 if ($this->m_api->update_absensi($value->id_absensi, $data)) {
515 echo "*berhasil-insert-foto*";
516 }else{
517 echo "*gagal-insert-foto*";
518 }
519 }else{
520 echo "*sudah-ada-foto*";
521 }
522 }
523 }else{
524 echo "*last-absen-error*";
525 }
526 }else{
527 echo "*rfid-tidak-ditemukan*";
528 }
529 }else{
530 echo "*id-device-tidak-ditemukan*";
531 }
532 }else{
533 echo "*salah-secret-key*";
534 }
535 }else{
536 echo "*salah-param*";
537 }
538 }
539
540
541 public function absensifotojson(){
542 if (isset($_POST['key']) && isset($_POST['iddev']) && isset($_POST['rfid']) && isset($_POST['foto'])) {
543 $key = $this->input->post('key');
544 $cekkey = $this->m_api->getkey();
545
546 if($cekkey[0]->key == $key){
547 $iddev = $this->input->post('iddev');
548 $rfid = $this->input->post('rfid');
549 $foto = $this->input->post('foto');
550
551 $cekrfid = $this->m_api->checkRFID($rfid);
552 $countrfid = 0;
553 $idrfid = 0;
554 foreach ($cekrfid as $key => $value) {
555 $countrfid++;
556 $idrfid = $value->id_rfid;
557 }
558
559 $device = $this->m_api->getdevice($iddev);
560 $count = 0;
561 foreach ($device as $key => $value) {
562 $count++;
563 }
564
565 if ($count > 0) {
566 if ($countrfid > 0) {
567 $lastRFID = $this->m_api->lastRFIDfoto($idrfid);
568 if (isset($lastRFID)) {
569 foreach ($lastRFID as $key => $value) {
570 if ($value->foto == "") {
571 $data = array('foto' => $foto);
572 if ($this->m_api->update_absensi($value->id_absensi, $data)) {
573 $notif = array('status' => 'success', 'ket' => 'berhasil insert foto');
574 echo json_encode($notif);
575 }else{
576 $notif = array('status' => 'failed', 'ket' => 'gagal insert foto');
577 echo json_encode($notif);
578 }
579 }else{
580 $notif = array('status' => 'failed', 'ket' => 'sudah ada foto');
581 echo json_encode($notif);
582 }
583 }
584 }else{
585 $notif = array('status' => 'failed', 'ket' => 'last absen error');
586 echo json_encode($notif);
587 }
588 }else{
589 $notif = array('status' => 'failed', 'ket' => 'rfid tidak ditemukan');
590 echo json_encode($notif);
591 }
592 }else{
593 $notif = array('status' => 'failed', 'ket' => 'id device tidak ditemukan');
594 echo json_encode($notif);
595 }
596 }else{
597 $notif = array('status' => 'failed', 'ket' => 'salah secret key');
598 echo json_encode($notif);
599 }
600 }else{
601 $notif = array('status' => 'failed', 'ket' => 'salah parameter');
602 echo json_encode($notif);
603 }
604 }
605
606
607 public function realtimehistori(){
608 $data = $this->m_admin->get_history();
609 echo '<table id="t1" class="table table-bordered table-striped">
610 <thead>
611 <tr>
612 <th style="text-align:center">ID Histori</th>
613 <th style="text-align:center">UID RFID</th>
614 <th style="text-align:center">Keterangan</th>
615 <th style="text-align:center">Nama Device</th>
616 <th style="text-align:center">Waktu</th>
617 </tr>
618 </thead>
619 <tbody>';
620 if(empty($data)){
621 echo '
622 <tr>
623 <td style="text-align:center">Data tidak ditemukan</td>
624 <td style="text-align:center">Data tidak ditemukan</td>
625 <td style="text-align:center">Data tidak ditemukan</td>
626 <td style="text-align:center">Data tidak ditemukan</td>
627 <td style="text-align:center">Data tidak ditemukan</td>
628 </tr>';
629 } else{
630 foreach($data as $row){
631 echo '
632 <tr>
633 <td style="text-align:center"><b class="text-success">'.$row->id_histori.'</b></td>
634 <td style="text-align:center">'.$row->uid.'</td>
635 <td style="text-align:center">'.$row->keterangan.'</td>
636 <td style="text-align:center">'.$row->nama_devices.' ('.$row->id_devices.')</td>
637 <td style="text-align:center">'.date("d M Y, H:i:s",$row->waktu).'</td>
638 </tr>';
639 }
640 }
641 echo '
642 </tbody>
643 </table>';
644 }
645
646}