Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Codex Agent Guide (pyzk)

## Repo overview
- `zk/` is the library package. The main entry point is `ZK` in `zk/base.py`.
- `example/` contains minimal usage scripts.
- `test_machine.py` and `test_backup_restore.py` are hardware-facing CLIs.
- `docs/` is a Sphinx site (ReadTheDocs compatible).

## Key modules
- `zk/base.py`: core protocol, connection handling, and device operations.
- `zk/user.py`: `User` model and packing helpers.
- `zk/finger.py`: `Finger` model and template packing helpers.
- `zk/attendance.py`: `Attendance` model.
- `zk/const.py`: protocol constants and flags.
- `zk/exception.py`: library exceptions.

## Safety notes
- Destructive calls include `clear_data()`, `clear_attendance()`, `poweroff()`, and `restart()`.
- `unlock()` opens the door/relay on some devices. Treat it as a safety-sensitive operation.
- Prefer `disable_device()` before bulk reads/writes to avoid inconsistent data.
- Live capture (`live_capture()`) can hold device state; ensure you exit cleanly.

## Common commands
- Run an example: `python example/get_users.py` (edit IP/port inside the script).
- Basic device probe: `python test_machine.py -a 192.168.1.201`.
- Backup/restore CLI (new): `pyzk-backup --help`.

## Docs build
- Build Sphinx docs: `cd docs && make html`.
- Or: `sphinx-build -b html docs docs/_build/html`.

## Hardware-dependent tests
- Any command that connects to a real device requires a reachable IP/port (default 4370).
- Use `--force-udp` if TCP is unreliable for a specific model.
- Time sync and firmware reads may differ by model/firmware version.

## Conventions
- Keep new code Python 2/3 compatible unless explicitly dropping support.
- Avoid changing protocol details without referencing `docs/_static/Communication_protocol_manual_CMD.pdf`.
39 changes: 39 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Codex Agent Guide (pyzk)

## Repo overview
- `zk/` is the library package. The main entry point is `ZK` in `zk/base.py`.
- `example/` contains minimal usage scripts.
- `test_machine.py` and `test_backup_restore.py` are hardware-facing CLIs.
- `docs/` is a Sphinx site (ReadTheDocs compatible).

## Key modules
- `zk/base.py`: core protocol, connection handling, and device operations.
- `zk/user.py`: `User` model and packing helpers.
- `zk/finger.py`: `Finger` model and template packing helpers.
- `zk/attendance.py`: `Attendance` model.
- `zk/const.py`: protocol constants and flags.
- `zk/exception.py`: library exceptions.

## Safety notes
- Destructive calls include `clear_data()`, `clear_attendance()`, `poweroff()`, and `restart()`.
- `unlock()` opens the door/relay on some devices. Treat it as a safety-sensitive operation.
- Prefer `disable_device()` before bulk reads/writes to avoid inconsistent data.
- Live capture (`live_capture()`) can hold device state; ensure you exit cleanly.

## Common commands
- Run an example: `python example/get_users.py` (edit IP/port inside the script).
- Basic device probe: `python test_machine.py -a 192.168.1.201`.
- Backup/restore CLI (new): `pyzk-backup --help`.

## Docs build
- Build Sphinx docs: `cd docs && make html`.
- Or: `sphinx-build -b html docs docs/_build/html`.

## Hardware-dependent tests
- Any command that connects to a real device requires a reachable IP/port (default 4370).
- Use `--force-udp` if TCP is unreliable for a specific model.
- Time sync and firmware reads may differ by model/firmware version.

## Conventions
- Keep new code Python 2/3 compatible unless explicitly dropping support.
- Avoid changing protocol details without referencing `docs/_static/Communication_protocol_manual_CMD.pdf`.
Comment on lines +1 to +39
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLAUDE.md and AGENTS.md have identical content. Consider consolidating these into a single file or clearly documenting why both are needed. Having duplicate files can lead to maintenance issues where one is updated but not the other.

Copilot uses AI. Check for mistakes.
140 changes: 16 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,136 +335,28 @@ optional arguments:

**Backup/Restore (Users and fingers only!!!)** *(WARNING! destructive test! do it at your own risk!)*

```sh
usage: ./test_backup_restore.py [-h] [-a ADDRESS] [-p PORT] [-T TIMEOUT]
[-P PASSWORD] [-f] [-v] [-r]
[filename]

ZK Basic Backup/Restore Tool

positional arguments:
filename backup filename (default [serialnumber].bak)
Use the installed CLI:

optional arguments:
-h, --help show this help message and exit
-a ADDRESS, --address ADDRESS
ZK device Address [192.168.1.201]
-p PORT, --port PORT ZK device port [4370]
-T TIMEOUT, --timeout TIMEOUT
Default [10] seconds (0: disable timeout)
-P PASSWORD, --password PASSWORD
Device code/password
-f, --force-udp Force UDP communication
-v, --verbose Print debug information
-E, --erase clean the device after writting backup!
-r, --restore Restore from backup
-c, --clear-attendance
On Restore, also clears the attendance [default keep
attendance]
```sh
pyzk-backup --help
pyzk-backup -a 192.168.1.201
pyzk-backup -a 192.168.1.201 --restore backup.json.bak
```

The legacy script `test_backup_restore.py` now wraps the same CLI and accepts
the same flags.

To restore on a different device, make sure to specify the `filename`. on restoring, it asks for the serial number of the destination device (to make sure it was correct, as it deletes all data) WARNING. there is no way to restore attendance data, you can keep it or clear it, but once cleared, there is no way to restore it.

# Compatible devices

```
Firmware Version : Ver 6.21 Nov 19 2008
Platform : ZEM500
DeviceName : U580

Firmware Version : Ver 6.60 Apr 9 2010
Platform : ZEM510_TFT
DeviceName : T4-C

Firmware Version : Ver 6.60 Dec 1 2010
Platform : ZEM510_TFT
DeviceName : T4-C

Firmware Version : Ver 6.60 Mar 18 2011
Platform : ZEM600_TFT
DeviceName : iClock260

Platform : ZEM560_TFT
Firmware Version : Ver 6.60 Feb 4 2012
DeviceName :

Firmware Version : Ver 6.60 Oct 29 2012
Platform : ZEM800_TFT
DeviceName : iFace402/ID

Firmware Version : Ver 6.60 Mar 18 2013
Platform : ZEM560
DeviceName : MA300

Firmware Version : Ver 6.60 Dec 27 2014
Platform : ZEM600_TFT
DeviceName : iFace800/ID

Firmware Version : Ver 6.60 Nov 6 2017 (remote tested with correct results)
Platform : ZMM220_TFT
DeviceName : (unknown device) (broken info but at least the important data was read)

Firmware Version : Ver 6.60 Jun 9 2017
Platform : JZ4725_TFT
DeviceName : K20 (latest checked correctly!)

Firmware Version : Ver 6.60 Aug 23 2014
Platform : ZEM600_TFT
DeviceName : VF680 (face device only, but we read the user and attendance list!)

Firmware Version : Ver 6.70 Feb 16 2017
Platform : ZLM30_TFT
DeviceName : RSP10k1 (latest checked correctly!)

Firmware Version : Ver 6.60 Jun 16 2015
Platform : JZ4725_TFT
DeviceName : K14 (tested & verified working as expected.)

Firmware Version : Ver 6.60 Jan 13 2016
Platform : ZMM220_TFT
DeviceName : iFace702 (without voice function, test with encoding='gbk')

Firmware Version : Ver 6.60 Apr 26 2016
Platform : ZMM210_TFT
DeviceName : F18/ID

Firmware Version : Ver 6.60 May 25 2018
Platform : JZ4725_TFT
DeviceName : K40/ID
```



### Latest tested (not really confirmed)

```
Firmware Version : Ver 6.60 Jun 16 2015
Platform : JZ4725_TFT
DeviceName : iClock260

Firmware Version : Ver 6.60 Jun 5 2015
Platform : ZMM200_TFT
DeviceName : iClock3000/ID (Active testing! latest fix)

Firmware Version : Ver 6.70 Jul 12 2013
Platform : ZEM600_TFT
DeviceName : iClock880-H/ID (Active testing! latest fix)
```

### Not Working (needs more tests, more information)

```
Firmware Version : Ver 6.4.1 (build 99) (display version 2012-08-31)
Platform :
DeviceName : iClock260 (no capture data - probably similar problem as the latest TESTED)
```

If you have another version tested and it worked, please inform me to update this list!
See `docs/compatible_devices.rst` for the canonical list and the submission template.

# Todo
# Roadmap

* Create better documentation
* ~~Finger template downloader & uploader~~
* HTTP Rest api
* ~~Create real time api (if possible)~~
* and much more ...
- Create better documentation (in progress)
- Finger template downloader & uploader (in progress, via `pyzk-backup`)
- HTTP REST API (spec only)
- Real-time API (spec only)
- Expand compatible devices list and contribution guide (in progress)
- Future ideas are tracked in `ROADMAP.md`
22 changes: 22 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# pyzk Roadmap

This roadmap tracks the next steps for pyzk. It is intentionally high level
and may change as new devices or protocol details are discovered.

## Now

- Complete the Sphinx documentation with real usage guides.
- Publish a supported backup/restore CLI (`pyzk-backup`) and helper API.
- Expand the compatible devices list and add a contribution template.

## Next

- Ship an HTTP REST API server (implementation of the spec in `docs/api_design.rst`).
- Provide a real-time streaming API (SSE or WebSocket) for live events.
- Add more example scripts for common tasks (backup, sync time, unlock door).

## Later

- Add automated compatibility tests for known devices.
- Expand device support for newer firmware families.
- Optional plugins for exporting data to common systems (CSV, database).
139 changes: 139 additions & 0 deletions docs/api_design.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
.. toctree::
:caption: HTTP & Real-Time API (Spec)
:name: api_design

###############################
HTTP & Real-Time API (Spec Only)
###############################

Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The document states 'This is a forward-looking design spec' and 'It does not include an implementation yet' (lines 9-10), but this could be confusing for users who might expect this to be part of the current release. Consider adding a clear 'Status: Planning/Design Only' header or moving this to a separate planning document to avoid confusion.

Suggested change
Status
------
**Status: Planning / design only – not implemented in the current release.**

Copilot uses AI. Check for mistakes.
This is a forward-looking design spec for a REST and real-time API built on top
of pyzk. It does **not** include an implementation yet.

Goals
-----

- Provide a simple HTTP interface for device operations and data extraction.
- Support real-time events via server-sent events (SSE).
- Keep device state in a managed connection pool.

Recommended stack
-----------------

- **FastAPI** for HTTP
- **Uvicorn** as the ASGI server
- **SSE** for live capture streaming

Connection lifecycle
--------------------

The API should manage device connections with explicit connect/disconnect calls
and a short idle timeout. Each connection is keyed by ``device_id`` or
``ip:port``.

Proposed endpoints
------------------

Health
^^^^^^

- ``GET /health`` -> ``{ "status": "ok" }``

Devices
^^^^^^^

- ``POST /devices/connect``

Request:

.. code-block:: json

{
"ip": "192.168.1.201",
"port": 4370,
"timeout": 10,
"password": 0,
"force_udp": false,
"encoding": "UTF-8"
}

Response:

.. code-block:: json

{
"device_id": "192.168.1.201:4370",
"serial": "0000000001",
"fp_version": "10",
"platform": "ZEM600_TFT"
}

- ``POST /devices/disconnect``

Request:

.. code-block:: json

{ "device_id": "192.168.1.201:4370" }

Users
^^^^^

- ``GET /devices/{device_id}/users``
- ``POST /devices/{device_id}/users``
- ``DELETE /devices/{device_id}/users/{uid}``

Attendance
^^^^^^^^^^

- ``GET /devices/{device_id}/attendance``
- ``DELETE /devices/{device_id}/attendance``

Templates
^^^^^^^^^

- ``GET /devices/{device_id}/templates``
- ``GET /devices/{device_id}/templates/{uid}/{fid}``

Real-time events (SSE)
^^^^^^^^^^^^^^^^^^^^^^

- ``GET /devices/{device_id}/live``

SSE event payload:

.. code-block:: json

{
"type": "attendance",
"user_id": "1001",
"timestamp": "2024-06-01T12:00:00",
"status": 0,
"punch": 0
}

Error model
-----------

All errors should return:

.. code-block:: json

{
"error": {
"code": "DEVICE_OFFLINE",
"message": "Device did not respond"
}
}

Security
--------

- Prefer network isolation and IP allowlists.
- Consider an API key or JWT for remote calls.
- The server should never expose device passwords in responses.

Open questions
--------------

- How to handle multiple concurrent clients reading attendance.
- Whether to expose raw template data or require an opt-in flag.
Loading
Loading