Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding Issue with Date Handling in pyzk Package #221

Open
TisSanal opened this issue Sep 6, 2024 · 8 comments
Open

Encoding Issue with Date Handling in pyzk Package #221

TisSanal opened this issue Sep 6, 2024 · 8 comments

Comments

@TisSanal
Copy link

TisSanal commented Sep 6, 2024

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
Copy link

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
Copy link
Author

TisSanal commented Sep 9, 2024

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
Copy link

Farhan-CSE commented Sep 25, 2024

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

Any help would be appreciated.

@devinvento
Copy link

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
Copy link

@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
Copy link

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
Copy link

Farhan-CSE commented Sep 30, 2024

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
Copy link

Farhan-CSE commented Oct 6, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants