Files
tthcmservices/zklib/src/Connect.php
asansal 5bec63d9cd Add ZK library classes for device interaction and user management
- Implemented Os class for retrieving OS information.
- Added Pin class for getting PIN width.
- Created Platform class for fetching platform details and version.
- Developed SerialNumber class to retrieve device serial number.
- Introduced Ssr class for SSR information retrieval.
- Implemented Time class for setting and getting device time.
- Added User class for user management including setting, getting, clearing, and removing users.
- Created Util class with various utility functions for command handling and data processing.
- Implemented Version class for fetching device version.
- Added WorkCode class for retrieving work code information.
- Set up Composer autoloading for the ZK library.
2025-06-25 17:25:23 +07:00

82 lines
2.2 KiB
PHP

<?php
namespace ZK;
use ZKLib;
use ErrorException;
use Exception;
class Connect
{
/**
* @param ZKLib $self
* @return bool
*/
public function connect(ZKLib $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 ZKLib $self
* @return bool
*/
public function disconnect(ZKLib $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;
}
}
}