Zklib in ERPNext

Hi, were you able to fix this? Also are you looking for integration of one device or multiple devices across many locations like outlets in different areas of the city/country etc.

you need to run in bench folder
source env/bin/activate
and install base and zlib using the pip comand
pip install base
which is zklib required libray

3 Likes

@Muzzy

I written a custom method to pull data from machine and saved the data in a new doctype in ERPNext

1 Like

@hereabdulla Can you share your code for us?

1 Like

@hereabdulla Can you share it or some hints plz?

def daily_punch_details():
    from zk import ZK, const
    conn = None
    zk = ZK(machine, port=4370,timeout=5)
    try:
        conn = zk.connect()
        attendance = conn.get_attendance()
        curdate = datetime.now().date()
        for att in attendance:
            date = att.timestamp.date()
            if date == curdate:
                mtime = att.timestamp.time()
                mtimef = timedelta(
                    hours=mtime.hour, minutes=mtime.minute, seconds=mtime.second)
                userid = att.user_id
                already_exist = False
                pd_id = frappe.db.get_value("Punch Details", {
                    "employee_id": userid, "attendance_date": date})
                if pd_id:
                    pd = frappe.get_doc("Punch Details", pd_id)
                    for i in pd.timetable:
                        if i.punch_time == mtimef:
                            already_exist = True
                    if not already_exist:
                        pd.append("timetable", {
                            "punch_time": str(mtime)
                        })
                        pd.save(ignore_permissions=True)
                else:
                    pd = frappe.new_doc("Punch Details")
                    pd.employee_id = userid
                    pd.attendance_date = date
                    pd.ip = machine
                    pd.machine_sno = zk.get_serialnumber()
                    pd.append("timetable", {
                        "punch_time": mtime
                    })
                    pd.insert()
                    pd.save(ignore_permissions=True)
    except Exception, e:
        print "Process terminate : {}".format(e)
    finally:
        if conn:
            conn.disconnect()

@MaysaaSafadi for your reference

1 Like

This method will be executed on schedule (daily or hour) or manually?

@magic-overflow thats ur decision if you have only one in and out you can use cron to run this two time

1 Like

Thanks a lot @hereabdulla
But I have some issue with the libraries provided, with zklib lib. ,zk.connect() always get False
and with pyzk lib. , I got an TCP packet invalid Error

Is there a one without problems?

@MaysaaSafadi first ur machine have to support with either pyzk or zklib

It is ZKTeco, I think it supports them, with pyzk, I got data from it such as users, but it get one user then TCP packet invalid Error

share the screenshot of ur code

and here the ouput

Connecting to device …
Disabling device …
Firmware Version: : Ver 6.60 Jun 9 2017

  • UID #1
    Name : 123456
    Privilege : User
    Password :
    Group ID :
    User ID : 1
    Voice Test …
    Process terminate : TCP packet invalid
    Traceback (most recent call last):
    File “/usr/lib/python2.7/runpy.py”, line 174, in _run_module_as_main
    main”, fname, loader, pkg_name)
    File “/usr/lib/python2.7/runpy.py”, line 72, in _run_code
    exec code in run_globals
    File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 94, in
    main()
    File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 18, in main
    click.Group(commands=commands)(prog_name=‘bench’)
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py”, line 722, in call
    return self.main(*args, **kwargs)
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py”, line 697, in main
    rv = self.invoke(ctx)
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py”, line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py”, line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py”, line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py”, line 535, in invoke
    return callback(*args, **kwargs)
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/decorators.py”, line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
    File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/init.py”, line 24, in _func
    ret = f(frappe._dict(ctx.obj), *args, **kwargs)
    File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/utils.py”, line 117, in execute
    ret = frappe.get_attr(method)(*args, **kwargs)
    File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 919, in get_attr
    return getattr(get_module(modulename), methodname)
    File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 704, in get_module
    return importlib.import_module(modulename)
    File “/usr/lib/python2.7/importlib/init.py”, line 37, in import_module
    import(name)
    File “/home/frappe/frappe-bench/apps/erpnext/erpnext/hr/basictest.py”, line 37, in
    conn.disconnect()
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/zk/base.py”, line 350, in disconnect
    cmd_response = self.__send_command(const.CMD_EXIT)
    File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/zk/base.py”, line 224, in __send_command
    raise ZKNetworkError(str(e))
    zk.exception.ZKNetworkError: TCP packet invalid

Hi @hereabdulla,
Plz, can u tell me which the model number of your device, or the list of supported devices from the library?
Thanks
Maysaa

im getting error

Process terminate : timed out

does the device have to be on the same network with ERPNext instance?

@Sibghat were you able to solve this?

actually the pyzk library that gets installed by using ‘pip install pyzk’ is an old version and is only compatible with python2… if you try to use that in python3 you would get the above error…

you could solve this by installing the latest version manually from its git repo: GitHub - fananimi/pyzk: Unofficial library of zkteco fingerprint attendance machine

or by running ‘pip install git+git://github.com/fananimi/pyzk@master#egg=pyzk

1 Like

I am still getting import error and yes I upgraded to python 3

Hii users

I installed biometric attendance app its giving error when i am doing check connection

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 61, in application
response = frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 56, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 1028, in call
return fn(*args, **newargs)
File “/home/frappe/frappe-bench/apps/biometric_attendance/biometric_attendance/biometric_attendance/utils.py”, line 29, in check_connection
frappe.throw(e)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 353, in throw
msgprint(msg, raise_exception=exc, title=title, indicator=‘red’)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 338, in msgprint
message_log.append(json.dumps(out))
File “/usr/lib/python2.7/json/init.py”, line 244, in dumps
return _default_encoder.encode(obj)
File “/usr/lib/python2.7/json/encoder.py”, line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File “/usr/lib/python2.7/json/encoder.py”, line 270, in iterencode
return _iterencode(o, 0)
File “/usr/lib/python2.7/json/encoder.py”, line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ZKNetworkError(u"can’t reach device (ping 192.168.1.26)",) is not JSON serializable

Install python3.6+ and git (python versions below 3.6 is NOT supported)

The app needs python 3.6+.

How to activate env in frappe