pyzk icon indicating copy to clipboard operation
pyzk copied to clipboard

ZKTeco Mini TA, invalid timestap on get_attendance()

Open musahcoding opened this issue 9 months ago • 2 comments

Describe the bug In Mini TA model, When we download the attendance the timestamp comes randomly as 2000 Jan 1 12:00 or any other random date such 2063, and some other timestamps are correct. Example of var_dump is:

#0 list(2) 
    [0] => object(Attendance) (5)
        uid => int(1) 
        user_id => str(3) "166"
        timestamp => object(datetime) (2024-05-03 17:11:38)
        status => int(16) 
        punch => int(255) 
    [1] => object(Attendance) (5)
        uid => int(255) 
        user_id => str(0) ""
        timestamp => object(datetime) (2000-01-01 00:00:00)
        status => int(0) 
        punch => int(0)

PS: the same code work on k70 and k60 models.

To Reproduce Just to call connection.get_attendance() after using the face recognition of the device, we get the partially invalid timestamps.

Expected behavior All the timestamps are correct and reflect the actual punch in time.

Capture Data

System (please complete the following information):

  • OS: Ubuntu 22.04
  • Python version 3.10

Additional context N/A

musahcoding avatar May 03 '24 13:05 musahcoding

without a PCAP file and debug data, I'm not sure, but I suspect it's again ol' #56 please review if you already have the fixed code (the one with uid, status, punch, timestamp = unpack('<HBB4s', attendance_data.ljust(8, b'\x00')[:8]) instead of uid, status, timestamp, punch = unpack('HB4sB', attendance_data.ljust(8, b'\x00')[:8]) ) and switch accordingly...

if it works, please share which one works for you... maybe we can finally commit this one

kurenai-ryu avatar May 04 '24 03:05 kurenai-ryu

Unfortunately we do not have that device in hand, as the client replaced them already. Once we get another of the same model, I will test and let you know. Thanks

musahcoding avatar May 18 '24 11:05 musahcoding

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

Code Snippet Related to the Issue: def __decode_time(self, t): """Decode a timestamp retrieved from the timeclock""" t = t.encode('hex') t = int(self.__reverse_hex(t), 16) if self.verbose: print ("decode from %s "% format(t, '04x')) t = unpack("<I", t)[0]

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.

TisSanal avatar Sep 06 '24 12:09 TisSanal

without a PCAP file and debug data, I'm not sure, but I suspect it's again ol' #56 please review if you already have the fixed code (the one with uid, status, punch, timestamp = unpack('<HBB4s', attendance_data.ljust(8, b'\x00')[:8]) instead of uid, status, timestamp, punch = unpack('HB4sB', attendance_data.ljust(8, b'\x00')[:8]) ) and switch accordingly...

if it works, please share which one works for you... maybe we can finally commit this one

"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."

code :


 else:
            while len(attendance_data) >= 40:
                uid, user_id, status, timestamp, punch, space = unpack('<H24sB4sB8s',
                                                                       attendance_data.ljust(40, b'\x00')[:40])
                if self.verbose: print(codecs.encode(attendance_data[:40], 'hex'))
                user_id = (user_id.split(b'\x00')[0]).decode(errors='ignore')
                timestamp = self.__decode_time(timestamp)

                attendance = Attendance(user_id, timestamp, status, punch, uid)
                attendances.append(attendance)
                attendance_data = attendance_data[40:]

TisSanal avatar Sep 09 '24 09:09 TisSanal

did you test #56 ? please confirm if it fixes for you,

otherwise please post the pcap file and the expected date range

kurenai-ryu avatar Sep 09 '24 11:09 kurenai-ryu