Skip to content

Commit

Permalink
Inatial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Raihan Afroz committed Aug 19, 2020
0 parents commit 853e6d3
Show file tree
Hide file tree
Showing 20 changed files with 1,582 additions and 0 deletions.
Empty file added CHANGELOG.md
Empty file.
Empty file added README.md
Empty file.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "rats/zktec",
"description": "ZKTec Laravel Library",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Raihan Afroz",
"email": "you@example.com"
}
],
"autoload": {
"psr-4":{
"Rats\\Zkteco\\": "src"
}
},
"minimum-stability": "dev",
"require": {}
}
72 changes: 72 additions & 0 deletions src/Lib/Helper/Attendance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Rats\Zkteco\Lib\Helper;

use Rats\Zkteco\Lib\ZKTeco;

class Attendance
{
/**
* @param ZKTeco $self
* @return array [uid, id, state, timestamp]
*/
static public function get(ZKTeco $self)
{
$self->_section = __METHOD__;

$command = Util::CMD_ATT_LOG_RRQ;
$command_string = '';

$session = $self->_command($command, $command_string, Util::COMMAND_TYPE_DATA);
if ($session === false) {
return [];
}

$attData = Util::recData($self);

$attendance = [];
if (!empty($attData)) {
$attData = substr($attData, 10);

while (strlen($attData) > 40) {
$u = unpack('H78', substr($attData, 0, 39));

$u1 = hexdec(substr($u[1], 4, 2));
$u2 = hexdec(substr($u[1], 6, 2));
$uid = $u1 + ($u2 * 256);
$id = hex2bin(substr($u[1], 8, 18));
$id = str_replace(chr(0), '', $id);
$state = hexdec(substr($u[1], 56, 2));
$timestamp = Util::decodeTime(hexdec(Util::reverseHex(substr($u[1], 58, 8))));
$type = hexdec(Util::reverseHex(substr($u[1], 66, 2 )));

$attendance[] = [
'uid' => $uid,
'id' => $id,
'state' => $state,
'timestamp' => $timestamp,
'type' => $type
];

$attData = substr($attData, 40);
}

}

return $attendance;
}

/**
* @param ZKTeco $self
* @return bool|mixed
*/
static public function clear(ZKTeco $self)
{
$self->_section = __METHOD__;

$command = Util::CMD_CLEAR_ATT_LOG;
$command_string = '';

return $self->_command($command, $command_string);
}
}
83 changes: 83 additions & 0 deletions src/Lib/Helper/Connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Rats\Zkteco\Lib\Helper;

use Rats\Zkteco\Lib\Helper\Util;
use Rats\Zkteco\Lib\ZKTeco;
use ErrorException;
use Exception;

class Connect
{
/**
* @param ZKTeco $self
* @return bool
*/
static public function connect(ZKTeco $self)
{
$self->_section = __METHOD__;

$command = Util::CMD_CONNECT;
$command_string = '';
$chksum = 0;
$session_id = 0;
$reply_id = -1 + Util::USHRT_MAX;

$buf = Util::createHeader($command, $chksum, $session_id, $reply_id, $command_string);

socket_sendto($self->_zkclient, $buf, strlen($buf), 0, $self->_ip, $self->_port);

try {
@socket_recvfrom($self->_zkclient, $self->_data_recv, 1024, 0, $self->_ip, $self->_port);
if (strlen($self->_data_recv) > 0) {
$u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr($self->_data_recv, 0, 8));

$session = hexdec($u['h6'] . $u['h5']);
if (empty($session)) {
return false;
}

$self->_session_id = $session;
return Util::checkValid($self->_data_recv);
} else {
return false;
}
} catch (ErrorException $e) {
return false;
} catch (Exception $e) {
return false;
}
}

/**
* @param ZKTeco $self
* @return bool
*/
static public function disconnect(ZKTeco $self)
{
$self->_section = __METHOD__;

$command = Util::CMD_EXIT;
$command_string = '';
$chksum = 0;
$session_id = $self->_session_id;

$u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr($self->_data_recv, 0, 8));
$reply_id = hexdec($u['h8'] . $u['h7']);

$buf = Util::createHeader($command, $chksum, $session_id, $reply_id, $command_string);


socket_sendto($self->_zkclient, $buf, strlen($buf), 0, $self->_ip, $self->_port);
try {
@socket_recvfrom($self->_zkclient, $self->_data_recv, 1024, 0, $self->_ip, $self->_port);

$self->_session_id = 0;
return Util::checkValid($self->_data_recv);
} catch (ErrorException $e) {
return false;
} catch (Exception $e) {
return false;
}
}
}
50 changes: 50 additions & 0 deletions src/Lib/Helper/Device.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Rats\Zkteco\Lib\Helper;

use Rats\Zkteco\Lib\ZKTeco;

class Device
{
/**
* @param ZKTeco $self
* @return bool|mixed
*/
static public function name(ZKTeco $self)
{
$self->_section = __METHOD__;

$command = Util::CMD_DEVICE;
$command_string = '~DeviceName';

return $self->_command($command, $command_string);
}

/**
* @param ZKTeco $self
* @return bool|mixed
*/
static public function enable(ZKTeco $self)
{
$self->_section = __METHOD__;

$command = Util::CMD_ENABLE_DEVICE;
$command_string = '';

return $self->_command($command, $command_string);
}

/**
* @param ZKTeco $self
* @return bool|mixed
*/
static public function disable(ZKTeco $self)
{
$self->_section = __METHOD__;

$command = Util::CMD_DISABLE_DEVICE;
$command_string = chr(0) . chr(0);

return $self->_command($command, $command_string);
}
}
23 changes: 23 additions & 0 deletions src/Lib/Helper/Face.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rats\Zkteco\Lib\Helper;

use Rats\Zkteco\Lib\ZKTeco;

class Face
{
/**
* @param ZKTeco $self
* @return bool|mixed
*/
static public function on(ZKTeco $self)
{
$self->_section = __METHOD__;

$command = Util::CMD_DEVICE;
$command_string = 'FaceFunOn';

return $self->_command($command, $command_string);
}
}

Loading

0 comments on commit 853e6d3

Please sign in to comment.