kamshory / ZKLibrary

ZKLibrary is PHP library for reading and writing data to attendance device using UDP protocol. This library useful to comunicate between web server and attendance device directly without addition program. This library is implemented in the form of class. So that you can create an object and use it functions.
GNU General Public License v2.0
193 stars 176 forks source link

$zka->setUser() not working properly #131

Open WalidRH opened 4 weeks ago

WalidRH commented 4 weeks ago

hello Does anyone encountered the following issue while adding a new user to the device ?

Sometimes when I try to add a new user to the device, I got a response OK. But when I check the list of users I don't find the recently added user. And the same issue continue if I retried multiple times. but when I restart the device and resend the same user data, there it's added succefully. the Code I used for adding a new user.

`public function setUser($device_ip, $port, Request $request) { $uid = $request['uid']; $userid = $request['userid']; $name = $request['name']; $role = $request['role']; $password = $request['password']; $cardno = $request['cardno'];

    $userValidity = $this->isUserDataValid($uid,$userid,$name,$role,$password,$cardno);

    if($userValidity !== true){
        Log::info($userValidity);
        $error = [
            "error" => $userValidity
        ];
        Log::info($error);
        return response()->json($error, 400);
    }

    $zka = ConnectionUtils::connectDevice($device_ip, $port);
    if ($zka === false) {
        return response()->json("connection refused", 408);
    }
    $zka->disableDevice();

    $result = $zka->setUser($uid,$userid,$name,$password,$role,$cardno);
    Log::info($result);
    if ($result === false){
        $error = [
            "error" => "Error occurred while adding user to the device."
        ];

        return response()->json($error, 502);
    }
    $result = [
        "response" => "User has been successfully added to the device."
    ];
    $zka->enableDevice();
    $zka->disconnect();
    return response()->json($result, 200);
}

    private function isUserDataValid($uid,$userid,$name,$role,$password,$cardno){
    if ($uid > 65535){
        return "uid too large";
    }

    if (strlen($userid) > 9 || !preg_match('/^\d+$/', $userid)){
        return "User ID should contain a maximum of 9 digits.";
    }

    if (strlen($name) > 24){
        return "user name should have less than 24 characters";
    }

    if (strlen($password) > 8 || !preg_match('/^\d+$/', $password)){
        return "password should contain a maximum of 8 digits.";
    }

    if ( $role !== 0 && $role !== 14 ){
        return "user role should either be 0 (for user role), 14 (for admin role).";
    }

    if ( strlen($cardno) > 10 ){
        return "Cardno should contain a maximum of 10 digits.";
    }
    return true;
}`

The problem is that, I don't know HOW or WHEN should I add a user. The device I use F18

{ "device_Version": "Ver 6.60 ", "device_OSVersion": "1", "device_Platform": "ZMM210_TFT", "device_fmVersion": "10", "device_fmEdition": "Ver 6.60 Apr 26 2016", "device_workCode": "0", "device_ssr": "1", "device_pinWidth": "9", "device_deviceName": "F18/ID", }

Deos anyone have an explanation for this issue??