zklib-ts 1.0.0-development

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Gustavo Paz <gusti.paz@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,101 @@
1
+ <h1 align="center">zklib ts</h1>
2
+ <p align="center">An unofficial library that provides a robust solution for Node.js developers to interface with ZKTeco Devices.</p>
3
+ <p align="center">
4
+ <img src="https://img.shields.io/badge/node-latest-green?style=flat-square"/>
5
+ <img src="https://img.shields.io/badge/TypeScript-latest-blue?style=flat-square"/>
6
+ <img src="https://img.shields.io/badge/Jest-latest-red?style=flat-square"/>
7
+ <img src="https://img.shields.io/badge/npm-red?style=flat-square"/>
8
+ </p>
9
+ <p align="center">
10
+ <b style="color:red;">Disclaimer</b>
11
+ <b>⚠️ This repository is not recommended for use in production. ⚠️</b>
12
+ This repository is currently in development and may contain bugs or incomplete features. Use at your own risk and do not deploy to a production environment
13
+ </p>
14
+
15
+ ## 📋 **Index**
16
+ 1. [⚙️ Usage](#-usage)
17
+ 2. [🛠️ Testing](#-testing)
18
+ 3. [🗄️Alternatives](#-alternatives)
19
+ 4. [📄 Documentation](#-documentation)
20
+
21
+ ## 🛠️ **Usage**
22
+ create a connection. <b>constructor</b> receives `(ip, timeout, inport, port, comm_key) `
23
+ ```js
24
+ import Zklib from 'zklib'
25
+
26
+ const zkInstance = new Zklib("10.0.0.10",10000,10000,4370,0)
27
+
28
+ await zkInstance.createSocket()
29
+
30
+ ```
31
+ Get <b>all</b> users:
32
+ ```js
33
+
34
+ const users = await zkInstance.getUsers()
35
+
36
+ ```
37
+ Get <b>all</b> attendances:
38
+ ```js
39
+
40
+ const attendances = await zkInstance.getAttendances()
41
+
42
+ ```
43
+ get All templates
44
+ ```js
45
+ const templates = await zkInstance.getTemplates()
46
+ ```
47
+
48
+ save user templates. receives a `User` instance class and an array of `Finger` class. currently only save one template per call.
49
+ ```js
50
+ const templates = await zkInstance.saveUserTemplate(user, templates)
51
+ ```
52
+
53
+
54
+ enrollUser: receives a user `user_id` and finger ID `fid` where `0 <= fid <= 9`
55
+ ```js
56
+ await zkInstance.enrollUser(50,5)
57
+ ```
58
+ delete template. receives user id `uid` and finger id where `0 <= fid <= 9`
59
+ ```js
60
+ await zkInstance.deleteTemplate(50,5)
61
+ ```
62
+ <b>Check the Testing section for more functionalities coverage.</b>
63
+
64
+ ## 🛠️ **Testing**
65
+
66
+ The repo uses Jest. There is a mock file for test without having a phisical device connected.
67
+
68
+ for testing your phisical device first create .env file in root directory with the values down below:
69
+ ```
70
+ DEVICE_IP=10.10.10.1
71
+ DEVICE_PORT=4370
72
+ DEVICE_PASSWORD=1234
73
+ ```
74
+ and then run tests:
75
+ ```
76
+ npm t
77
+ ```
78
+
79
+ for testing especific file after "npm t" type some name that matches a test file ...
80
+ for example the next command will execute "Generic.test.ts"
81
+ ```
82
+ npm t Generic
83
+ ```
84
+
85
+ ## 🗄️ **Alternatives**
86
+ #### Javascript
87
+ - [caobo171/node-zklib](https://github.com/caobo171/node-zklib)
88
+ - [conding-libs/zkteco-js](https://github.com/coding-libs/zkteco-js)
89
+ #### Python:
90
+ - [dnaextrim/python_zklib](https://github.com/dnaextrim/python_zklib)
91
+ - [fananimi/pyzk](https://github.com/fananimi/pyzk)
92
+ #### ☕ Java:
93
+ - [mkhoudary/ZKTeco4J](https://github.com/mkhoudary/ZKTeco4J)
94
+
95
+ ## 📄 **Documentation**
96
+ - [adrobinoga/zk-protocol](https://github.com/adrobinoga/zk-protocol)
97
+
98
+
99
+ ## License
100
+
101
+ The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Error types for device communication
3
+ */
4
+ export declare const ERROR_TYPES: {
5
+ readonly ECONNRESET: "ECONNRESET";
6
+ readonly ECONNREFUSED: "ECONNREFUSED";
7
+ readonly EADDRINUSE: "EADDRINUSE";
8
+ readonly ETIMEDOUT: "ETIMEDOUT";
9
+ };
10
+ type ErrorType = keyof typeof ERROR_TYPES;
11
+ interface ErrorInfo {
12
+ message: string;
13
+ code?: ErrorType | string;
14
+ }
15
+ interface ZkErrorDetails {
16
+ err: {
17
+ message: string;
18
+ code?: string;
19
+ };
20
+ ip: string;
21
+ command: string | number;
22
+ }
23
+ /**
24
+ * Custom error class for device communication errors
25
+ */
26
+ export declare class ZkError {
27
+ private err;
28
+ private ip;
29
+ private command;
30
+ /**
31
+ * Creates a new ZkError instance
32
+ * @param err The error object
33
+ * @param command The command that caused the error
34
+ * @param ip The IP address of the device
35
+ */
36
+ constructor(err: ErrorInfo, command: number | string, ip: string);
37
+ /**
38
+ * Gets a user-friendly error message
39
+ * @returns A formatted error message
40
+ */
41
+ toast(): string;
42
+ /**
43
+ * Gets detailed error information
44
+ * @returns An object containing error details
45
+ */
46
+ getError(): ZkErrorDetails;
47
+ }
48
+ declare const _default: {
49
+ ZkError: typeof ZkError;
50
+ ERROR_TYPES: {
51
+ readonly ECONNRESET: "ECONNRESET";
52
+ readonly ECONNREFUSED: "ECONNREFUSED";
53
+ readonly EADDRINUSE: "EADDRINUSE";
54
+ readonly ETIMEDOUT: "ETIMEDOUT";
55
+ };
56
+ };
57
+ export default _default;
@@ -0,0 +1,110 @@
1
+ export declare const COMMANDS: {
2
+ readonly CMD_CONNECT: 1000;
3
+ readonly CMD_EXIT: 1001;
4
+ readonly CMD_ENABLEDEVICE: 1002;
5
+ readonly CMD_DISABLEDEVICE: 1003;
6
+ readonly CMD_RESTART: 1004;
7
+ readonly CMD_POWEROFF: 1005;
8
+ readonly CMD_SLEEP: 1006;
9
+ readonly CMD_RESUME: 1007;
10
+ readonly CMD_CAPTUREFINGER: 1009;
11
+ readonly CMD_TEST_TEMP: 1011;
12
+ readonly CMD_CAPTUREIMAGE: 1012;
13
+ readonly CMD_REFRESHDATA: 1013;
14
+ readonly CMD_REFRESHOPTION: 1014;
15
+ readonly CMD_TESTVOICE: 1017;
16
+ readonly CMD_GET_VERSION: 1100;
17
+ readonly CMD_CHANGE_SPEED: 1101;
18
+ readonly CMD_AUTH: 1102;
19
+ readonly CMD_PREPARE_DATA: 1500;
20
+ readonly CMD_DATA: 1501;
21
+ readonly CMD_FREE_DATA: 1502;
22
+ readonly CMD_DATA_WRRQ: 1503;
23
+ readonly CMD_DATA_RDY: 1504;
24
+ readonly CMD_DB_RRQ: 7;
25
+ readonly CMD_USER_WRQ: 8;
26
+ readonly CMD_USERTEMP_RRQ: 9;
27
+ readonly CMD_USERTEMP_WRQ: 10;
28
+ readonly CMD_OPTIONS_RRQ: 11;
29
+ readonly CMD_OPTIONS_WRQ: 12;
30
+ readonly CMD_ATTLOG_RRQ: 13;
31
+ readonly CMD_CLEAR_DATA: 14;
32
+ readonly CMD_CLEAR_ATTLOG: 15;
33
+ readonly CMD_DELETE_USER: 18;
34
+ readonly CMD_DELETE_USERTEMP: 19;
35
+ readonly CMD_CLEAR_ADMIN: 20;
36
+ readonly CMD_USERGRP_RRQ: 21;
37
+ readonly CMD_USERGRP_WRQ: 22;
38
+ readonly CMD_USERTZ_RRQ: 23;
39
+ readonly CMD_USERTZ_WRQ: 24;
40
+ readonly CMD_GRPTZ_RRQ: 25;
41
+ readonly CMD_GRPTZ_WRQ: 26;
42
+ readonly CMD_TZ_RRQ: 27;
43
+ readonly CMD_TZ_WRQ: 28;
44
+ readonly CMD_ULG_RRQ: 29;
45
+ readonly CMD_ULG_WRQ: 30;
46
+ readonly CMD_UNLOCK: 31;
47
+ readonly CMD_CLEAR_ACC: 32;
48
+ readonly CMD_CLEAR_OPLOG: 33;
49
+ readonly CMD_OPLOG_RRQ: 34;
50
+ readonly CMD_GET_FREE_SIZES: 50;
51
+ readonly CMD_ENABLE_CLOCK: 57;
52
+ readonly CMD_STARTVERIFY: 60;
53
+ readonly CMD_STARTENROLL: 61;
54
+ readonly CMD_CANCELCAPTURE: 62;
55
+ readonly CMD_STATE_RRQ: 64;
56
+ readonly CMD_WRITE_LCD: 66;
57
+ readonly CMD_CLEAR_LCD: 67;
58
+ readonly CMD_GET_PINWIDTH: 69;
59
+ readonly CMD_SMS_WRQ: 70;
60
+ readonly CMD_SMS_RRQ: 71;
61
+ readonly CMD_DELETE_SMS: 72;
62
+ readonly CMD_UDATA_WRQ: 73;
63
+ readonly CMD_DELETE_UDATA: 74;
64
+ readonly CMD_DOORSTATE_RRQ: 75;
65
+ readonly CMD_WRITE_MIFARE: 76;
66
+ readonly CMD_EMPTY_MIFARE: 78;
67
+ readonly CMD_VERIFY_WRQ: 79;
68
+ readonly CMD_VERIFY_RRQ: 80;
69
+ readonly CMD_TMP_WRITE: 87;
70
+ readonly CMD_GET_USERTEMP: 88;
71
+ readonly CMD_CHECKSUM_BUFFER: 119;
72
+ readonly CMD_DEL_FPTMP: 134;
73
+ readonly CMD_GET_TIME: 201;
74
+ readonly CMD_SET_TIME: 202;
75
+ readonly CMD_REG_EVENT: 500;
76
+ readonly CMD_ACK_OK: 2000;
77
+ readonly CMD_ACK_ERROR: 2001;
78
+ readonly CMD_ACK_DATA: 2002;
79
+ readonly CMD_ACK_RETRY: 2003;
80
+ readonly CMD_ACK_REPEAT: 2004;
81
+ readonly CMD_ACK_UNAUTH: 2005;
82
+ readonly CMD_ACK_UNKNOWN: 65535;
83
+ readonly CMD_ACK_ERROR_CMD: 65533;
84
+ readonly CMD_ACK_ERROR_INIT: 65532;
85
+ readonly CMD_ACK_ERROR_DATA: 65531;
86
+ readonly EF_ATTLOG: 1;
87
+ readonly EF_FINGER: 2;
88
+ readonly EF_ENROLLUSER: 4;
89
+ readonly EF_ENROLLFINGER: 8;
90
+ readonly EF_BUTTON: 16;
91
+ readonly EF_UNLOCK: 32;
92
+ readonly EF_VERIFY: 128;
93
+ readonly EF_FPFTR: 256;
94
+ readonly EF_ALARM: 512;
95
+ };
96
+ export type CommandKeys = keyof typeof COMMANDS;
97
+ export type CommandValues = typeof COMMANDS[CommandKeys];
98
+ export declare const USHRT_MAX: number;
99
+ export declare const MAX_CHUNK: number;
100
+ export declare const MACHINE_PREPARE_DATA_1: number;
101
+ export declare const MACHINE_PREPARE_DATA_2: number;
102
+ interface RequestData {
103
+ DISABLE_DEVICE: Buffer;
104
+ GET_REAL_TIME_EVENT: Buffer;
105
+ GET_ATTENDANCE_LOGS: Buffer;
106
+ GET_USERS: Buffer;
107
+ GET_TEMPLATES: Buffer;
108
+ }
109
+ export declare const REQUEST_DATA: RequestData;
110
+ export {};
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Represents an Attendance Records
3
+ */
4
+ export declare class Attendance {
5
+ private sn;
6
+ private user_id;
7
+ private record_time;
8
+ private type?;
9
+ private state?;
10
+ private ip?;
11
+ constructor(sn: number, user_id: string, record_time: Date, type?: number, state?: number);
12
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Represents a fingerprint template with associated metadata
3
+ */
4
+ export declare class Finger {
5
+ uid: number;
6
+ fid: number;
7
+ valid: number;
8
+ template: Buffer;
9
+ size: number;
10
+ readonly mark: string;
11
+ /**
12
+ * Creates a new Finger instance
13
+ * @param uid User internal reference
14
+ * @param fid Finger ID (value >= 0 && value <= 9)
15
+ * @param valid Flag indicating 0 = invalid | 1 = valid | 3 = duress
16
+ * @param template Fingerprint template data buffer
17
+ */
18
+ constructor(uid: number, fid: number, valid: number, template: Buffer);
19
+ /**
20
+ * Packs the fingerprint data with metadata into a Buffer
21
+ * @returns Buffer containing packed fingerprint data
22
+ */
23
+ repack(): Buffer;
24
+ /**
25
+ * Packs only the fingerprint template data into a Buffer
26
+ * @returns Buffer containing just the template data
27
+ */
28
+ repackOnly(): Buffer;
29
+ /**
30
+ * Compares this fingerprint with another for equality
31
+ * @param other Another Finger instance to compare with
32
+ * @returns true if all properties and template data match
33
+ */
34
+ equals(other: Finger): boolean;
35
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Represents a User as is from ZkDevice and contain methods
3
+ * */
4
+ export declare class User {
5
+ uid: number;
6
+ name: string;
7
+ privilege: number;
8
+ password: string;
9
+ group_id: string | number;
10
+ user_id: string;
11
+ card: number;
12
+ /**
13
+ * Creates a new User instance
14
+ * @param uid User ID
15
+ * @param name User name
16
+ * @param privilege Privilege level
17
+ * @param password User password (default: "")
18
+ * @param group_id Group ID (default: "")
19
+ * @param user_id Alternate user ID (default: "")
20
+ * @param card Card number (default: 0)
21
+ */
22
+ constructor(uid: number, name: string, privilege: number, password?: string, group_id?: string | number, user_id?: string, card?: number);
23
+ private ensureEncoding;
24
+ repack29(): Buffer;
25
+ repack73(): Buffer;
26
+ }
@@ -0,0 +1,5 @@
1
+ declare const _default: {
2
+ encode: (date: any) => any;
3
+ decode: (time: any) => Date;
4
+ };
5
+ export default _default;
@@ -0,0 +1,40 @@
1
+ import { User } from '../models/User';
2
+ import { Attendance } from "../models/Attendance";
3
+ export interface DeviceInfo {
4
+ userCounts: number;
5
+ logCounts: number;
6
+ logCapacity: number;
7
+ }
8
+ export type UserData28 = Omit<User, 'password' | 'group_id' | 'card' | 'repack29' | 'repack73'>;
9
+ export interface RecordData16 {
10
+ record_time: Date;
11
+ user_id: string;
12
+ ip?: string;
13
+ }
14
+ export type RealTimeLog = RecordData16;
15
+ interface UDPHeader {
16
+ commandId: number;
17
+ checkSum: number;
18
+ sessionId: number;
19
+ replyId: number;
20
+ }
21
+ interface TCPHeader extends UDPHeader {
22
+ payloadSize: number;
23
+ }
24
+ export declare const createUDPHeader: (command: number, sessionId: number, replyId: number, data: any) => Buffer;
25
+ export declare const createTCPHeader: (command: number, sessionId: number, replyId: number, data: any) => Buffer;
26
+ export declare const removeTcpHeader: (buf: Buffer) => Buffer;
27
+ export declare const parseTimeToDate: (time: number) => Date;
28
+ export declare const decodeUserData28: (userData: Buffer) => UserData28;
29
+ export declare const decodeUserData72: (userData: Buffer) => User;
30
+ export declare const decodeRecordData40: (recordData: Buffer) => Attendance;
31
+ export declare const decodeRecordData16: (recordData: Buffer) => RecordData16;
32
+ export declare const decodeRecordRealTimeLog18: (recordData: Buffer) => RealTimeLog;
33
+ export declare const decodeRecordRealTimeLog52: (recordData: Buffer) => RealTimeLog;
34
+ export declare const decodeUDPHeader: (header: Buffer) => UDPHeader;
35
+ export declare const decodeTCPHeader: (header: Buffer) => TCPHeader;
36
+ export declare const exportErrorMessage: (commandValue: number) => string;
37
+ export declare const checkNotEventTCP: (data: Buffer) => boolean;
38
+ export declare const checkNotEventUDP: (data: Buffer) => boolean;
39
+ export declare const authKey: (comKey: number, sessionId: number) => number[];
40
+ export {};