fananimi / pyzk

Unofficial library of zkteco fingerprint attendance machine
GNU General Public License v2.0
505 stars 337 forks source link

Encoding Issue with Date Handling in pyzk Package #221

Open TisSanal opened 2 months ago

TisSanal commented 2 months ago

We are using the pyzk library for our ZKTeco integration and have encountered an issue with date encoding. The library is returning dates that are not valid, and we are receiving errors due to incorrect date formats.

Issue Description:

Problem: The pyzk library is producing invalid dates such as 2025 2 30 21 12 20 (February 30th, which is not a valid date). We are also seeing dates like 2000 1 1 0 0 0 which seems incorrect as well. Error Message: "Failed to connect to the device: day is out of range for month" Sample Output: 2000 1 1 0 0 0 2024 8 9 9 49 50 2025 2 30 21 12 20

Impact: This issue results in errors related to date handling, which affects the overall functionality and data integrity in our application.

We kindly request your assistance in addressing this encoding problem. If there is an updated version of the pyzk library or specific guidance on resolving this issue, please provide us with the necessary information.

Thank you for your support and prompt attention to this matter.

devinvento commented 2 months ago

go to pyzk folder and open base.py then goto line number 334 and overwrite the code

    try:
        d = datetime(year, month, day, hour, minute, second)
    except:
        d=None
TisSanal commented 2 months ago

go to pyzk folder and open base.py then goto line number 334 and overwrite the code

    try:
        d = datetime(year, month, day, hour, minute, second)
    except:
        d=None

"The issue occurs during the unpacking process with unpack('<H24sB4sB8s', attendance_data.ljust(40, b'\x00')[:40]), particularly when decoding the date. The unpacked data does not align correctly, leading to incorrect date values."

Farhan-CSE commented 2 months ago

We have also been facing the same issue with our attendance device. Has there been any workaround solution ?

Any help would be appreciated.

devinvento commented 2 months ago

We have also been facing the same issue with our attendance device. Has there been any workaround solution ?

Any help would be appreciated.

what your device model ? and whats unpack bytes return from device?

Farhan-CSE commented 2 months ago

@devinvento our device model is ZKTeco Speedface V5L and the byte unpacked is such as below : b'10111\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04%\x91/\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff255\x00\x00\x00\x00\x00\x06\x0010111\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04d\x91/\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff255\x00\x00\x00\x00\x00\x07\x0010111\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\xd2\x91/\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff255\x00\x00\x00\x00\x00\x08\x0010111\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x0e\x92/\x01\x00\x00\x00\x00\x00\x00\x00\x00\xff255\x00\x00\x00\x00\x00\t\x0010111\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04L\x92/\x01\x00\x00\x00\x00\x00\x00\x00\x00\xff255\x00\x00\x00\x00\x00'

devinvento commented 2 months ago

goto base.py then goto def get_attendance(self): method and add new section after elif record_size == 16:

    # ZKTeco SpeedFace-V5L Multi-Biometrical Reader
    elif record_size == 49:
        while len(attendance_data) >= 49:
            try:
                # Debug: print raw data before unpacking
                # print(f"Raw data (hex): {codecs.encode(attendance_data[:49], 'hex')}")

                # Adjust format to unpack 49 bytes
                uid, user_id, status, timestamp, punch, additional_data = unpack('<H24sB4sB12s5x', attendance_data[:49])

                # Print unpacked values
                # print(f"Unpacked data: uid={uid}, user_id={user_id}, status={status}, timestamp={timestamp}, punch={punch}, additional_data={additional_data}")

                # Further processing...
                user_id = (user_id.split(b'\x00')[0]).decode(errors='ignore')
                timestamp = self.__decode_time(timestamp)
                # print(f"Decoded timestamp: {timestamp}")
                # print(f"Additional Data (hex): {codecs.encode(additional_data, 'hex')}")
                # print(f"Additional Data (decoded): {additional_data.decode(errors='ignore')}")

                # Create attendance record
                attendance = Attendance(user_id, timestamp, status, punch, uid)
                attendances.append(attendance)

                # Move to the next part of the data
                attendance_data = attendance_data[49:]

            except Exception as e:
                print(f"Unpacking error: {e}")
Farhan-CSE commented 2 months ago

goto base.py then goto def get_attendance(self): method and add new section after elif record_size == 16:

    # ZKTeco SpeedFace-V5L Multi-Biometrical Reader
    elif record_size == 49:
        while len(attendance_data) >= 49:
            try:
                # Debug: print raw data before unpacking
                # print(f"Raw data (hex): {codecs.encode(attendance_data[:49], 'hex')}")

                # Adjust format to unpack 49 bytes
                uid, user_id, status, timestamp, punch, additional_data = unpack('<H24sB4sB12s5x', attendance_data[:49])

                # Print unpacked values
                # print(f"Unpacked data: uid={uid}, user_id={user_id}, status={status}, timestamp={timestamp}, punch={punch}, additional_data={additional_data}")

                # Further processing...
                user_id = (user_id.split(b'\x00')[0]).decode(errors='ignore')
                timestamp = self.__decode_time(timestamp)
                # print(f"Decoded timestamp: {timestamp}")
                # print(f"Additional Data (hex): {codecs.encode(additional_data, 'hex')}")
                # print(f"Additional Data (decoded): {additional_data.decode(errors='ignore')}")

                # Create attendance record
                attendance = Attendance(user_id, timestamp, status, punch, uid)
                attendances.append(attendance)

                # Move to the next part of the data
                attendance_data = attendance_data[49:]

            except Exception as e:
                print(f"Unpacking error: {e}")

The Problem is not solved and persists @devinvento. Do you have any other suggestion ?

Farhan-CSE commented 1 month ago

@TisSanal I have found the issue to be occurring due to attendance device firmware version (Hardware problem). Kindly, inform your vendor to update the firmware version of the device and this issue will be solved hopefully. In our case this was the solution to - incorrect date formats and unknown populated user ids.

cc: @devinvento