rbzk 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05c19d8aa64a72f6ca80b430058d5956f2c8250a59c60fb26548c9f546d62c3f
4
+ data.tar.gz: 3ddd33169741c8f2df4046e9add74e21b4f8e4faccc8b5beb25994ac4b546cdc
5
+ SHA512:
6
+ metadata.gz: 296b1eba45da43e51ef8e228fec546ad6b14894f1279219e66ff8f546469bb81e3a848f857d30b352a6a9db201762eee41a438ff606d041fe2e574e254840eab
7
+ data.tar.gz: 6132ea677b868d354db28fc26e32087b933234985a7aaccfc029da1f220bd1655865350b086ef1cc8756f6f190ba41162ef5deaa36f8aa55c8f947d1076b9d38
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2023-05-04
4
+
5
+ - Initial release
6
+ - Basic ZK protocol implementation
7
+ - Support for connecting to ZK devices (both UDP and TCP)
8
+ - Support for getting users and attendance logs
9
+ - Support for getting fingerprint templates
10
+ - Support for basic device operations (restart, poweroff, test voice)
11
+ - Support for getting and setting device time
12
+ - Support for clearing data and attendance logs
13
+ - Connection status checking with connected? method
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Khaled AbuShqear
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,213 @@
1
+ # RBZK
2
+
3
+ A Ruby implementation of the ZK protocol for fingerprint and biometric attendance devices.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rbzk'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install rbzk
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Basic Connection
28
+
29
+ ```ruby
30
+ require 'rbzk'
31
+
32
+ # Create a new ZK instance
33
+ zk = RBZK::ZK.new('192.168.1.201', port: 4370)
34
+
35
+ # Connect to the device
36
+ begin
37
+ conn = zk.connect
38
+
39
+ # Check if connected successfully
40
+ if conn.connected?
41
+ puts "Connected successfully!"
42
+ else
43
+ puts "Connection failed!"
44
+ exit 1
45
+ end
46
+
47
+ # Disable the device to ensure exclusive access
48
+ puts 'Disabling device...'
49
+ conn.disable_device
50
+
51
+ # Your operations here...
52
+
53
+ # Re-enable the device when done
54
+ puts 'Enabling device...'
55
+ conn.enable_device
56
+ rescue => e
57
+ puts "Error: #{e.message}"
58
+ ensure
59
+ # Always disconnect when done
60
+ conn.disconnect if conn && conn.connected?
61
+ end
62
+ ```
63
+
64
+ ### Getting Users
65
+
66
+ ```ruby
67
+ require 'rbzk'
68
+
69
+ zk = RBZK::ZK.new('192.168.1.201')
70
+ begin
71
+ conn = zk.connect
72
+ conn.disable_device
73
+
74
+ puts '--- Get Users ---'
75
+ users = conn.get_users
76
+ users.each do |user|
77
+ privilege = 'User'
78
+ privilege = 'Admin' if user.privilege == RBZK::Constants::USER_ADMIN
79
+
80
+ puts "UID: #{user.uid}"
81
+ puts "Name: #{user.name}"
82
+ puts "Privilege: #{privilege}"
83
+ puts "Password: #{user.password}"
84
+ puts "Group ID: #{user.group_id}"
85
+ puts "User ID: #{user.user_id}"
86
+ puts "---"
87
+ end
88
+
89
+ conn.enable_device
90
+ rescue => e
91
+ puts "Error: #{e.message}"
92
+ ensure
93
+ conn.disconnect if conn && conn.connected?
94
+ end
95
+ ```
96
+
97
+ ### Getting Attendance Logs
98
+
99
+ ```ruby
100
+ require 'rbzk'
101
+
102
+ zk = RBZK::ZK.new('192.168.1.201')
103
+ begin
104
+ conn = zk.connect
105
+ conn.disable_device
106
+
107
+ puts '--- Get Attendance Logs ---'
108
+ logs = conn.get_attendance_logs
109
+ logs.each do |log|
110
+ puts "User ID: #{log.user_id}"
111
+ puts "Timestamp: #{log.timestamp}"
112
+ puts "Status: #{log.status}"
113
+ puts "Punch: #{log.punch}"
114
+ puts "UID: #{log.uid}"
115
+ puts "---"
116
+ end
117
+
118
+ conn.enable_device
119
+ rescue => e
120
+ puts "Error: #{e.message}"
121
+ ensure
122
+ conn.disconnect if conn && conn.connected?
123
+ end
124
+ ```
125
+
126
+ ### Device Operations
127
+
128
+ ```ruby
129
+ require 'rbzk'
130
+
131
+ zk = RBZK::ZK.new('192.168.1.201')
132
+ begin
133
+ conn = zk.connect
134
+
135
+ # Get device time
136
+ time = conn.get_time
137
+ puts "Device time: #{time}"
138
+
139
+ # Set device time to current time
140
+ conn.set_time
141
+
142
+ # Get device info
143
+ info = conn.get_free_sizes
144
+ puts "Users: #{info[:users]}"
145
+ puts "Fingers: #{info[:fingers]}"
146
+ puts "Capacity: #{info[:capacity]}"
147
+ puts "Logs: #{info[:logs]}"
148
+ puts "Passwords: #{info[:passwords]}"
149
+
150
+ # Test the device voice
151
+ conn.test_voice
152
+
153
+ rescue => e
154
+ puts "Error: #{e.message}"
155
+ ensure
156
+ conn.disconnect if conn && conn.connected?
157
+ end
158
+ ```
159
+
160
+ ### Getting Fingerprint Templates
161
+
162
+ ```ruby
163
+ require 'rbzk'
164
+
165
+ zk = RBZK::ZK.new('192.168.1.201')
166
+ begin
167
+ conn = zk.connect
168
+ conn.disable_device
169
+
170
+ puts '--- Get Fingerprint Templates ---'
171
+ templates = conn.get_templates
172
+ templates.each do |template|
173
+ puts "UID: #{template.uid}"
174
+ puts "Finger ID: #{template.fid}"
175
+ puts "Valid: #{template.valid == 1 ? 'Yes' : 'No'}"
176
+ puts "Template size: #{template.template.size} bytes"
177
+ puts "---"
178
+ end
179
+
180
+ # Get a specific user's template
181
+ if templates.any?
182
+ user_template = conn.get_user_template(templates.first.uid, templates.first.fid)
183
+ puts "Got specific template for user #{user_template.uid}, finger #{user_template.fid}"
184
+ end
185
+
186
+ conn.enable_device
187
+ rescue => e
188
+ puts "Error: #{e.message}"
189
+ ensure
190
+ conn.disconnect if conn && conn.connected?
191
+ end
192
+ ```
193
+
194
+ ### Complete Example
195
+
196
+ See the `examples/complete_example.rb` file for a comprehensive example that demonstrates all the main functionality of the gem.
197
+
198
+ ## Development
199
+
200
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
201
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
202
+
203
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
204
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
205
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
206
+
207
+ ## Contributing
208
+
209
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shqear93/rbzk.
210
+
211
+ ## License
212
+
213
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBZK
4
+ class Attendance
5
+ attr_accessor :user_id, :timestamp, :status, :punch, :uid
6
+
7
+ def initialize(user_id, timestamp, status, punch, uid)
8
+ @user_id = user_id
9
+ @timestamp = timestamp
10
+ @status = status
11
+ @punch = punch
12
+ @uid = uid
13
+ end
14
+
15
+ def to_s
16
+ "#{@user_id} #{@timestamp} #{@status} #{@punch} #{@uid}"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBZK
4
+ module Constants
5
+ USHRT_MAX = 65535
6
+
7
+ # Command codes
8
+ CMD_DB_RRQ = 7 # Read in some kind of data from the machine
9
+ CMD_USER_WRQ = 8 # Upload the user information (from PC to terminal)
10
+ CMD_USERTEMP_RRQ = 9 # Read some fingerprint template or some kind of data entirely
11
+ CMD_USERTEMP_WRQ = 10 # Upload some fingerprint template
12
+ CMD_OPTIONS_RRQ = 11 # Read in the machine some configuration parameter
13
+ CMD_OPTIONS_WRQ = 12 # Set machines configuration parameter
14
+ CMD_ATTLOG_RRQ = 13 # Read all attendance record
15
+ CMD_CLEAR_DATA = 14 # clear Data
16
+ CMD_CLEAR_ATTLOG = 15 # Clear attendance records
17
+ CMD_DELETE_USER = 18 # Delete some user
18
+ CMD_DELETE_USERTEMP = 19 # Delete some fingerprint template
19
+ CMD_CLEAR_ADMIN = 20 # Cancel the manager
20
+ CMD_USERGRP_RRQ = 21 # Read the user grouping
21
+ CMD_USERGRP_WRQ = 22 # Set users grouping
22
+ CMD_USERTZ_RRQ = 23 # Read the user Time Zone set
23
+ CMD_USERTZ_WRQ = 24 # Write the user Time Zone set
24
+ CMD_GRPTZ_RRQ = 25 # Read the group Time Zone set
25
+ CMD_GRPTZ_WRQ = 26 # Write the group Time Zone set
26
+ CMD_TZ_RRQ = 27 # Read Time Zone set
27
+ CMD_TZ_WRQ = 28 # Write the Time Zone
28
+ CMD_ULG_RRQ = 29 # Read unlocks combination
29
+ CMD_ULG_WRQ = 30 # write unlocks combination
30
+ CMD_UNLOCK = 31 # unlock
31
+ CMD_CLEAR_ACC = 32 # Restores Access Control set to the default condition
32
+ CMD_CLEAR_OPLOG = 33 # Delete attendance machines all attendance record
33
+ CMD_OPLOG_RRQ = 34 # Read manages the record
34
+ CMD_GET_FREE_SIZES = 50 # Obtain machines condition, like user recording number and so on
35
+ CMD_ENABLE_CLOCK = 57 # Ensure the machine to be at the normal work condition
36
+ CMD_STARTVERIFY = 60 # Ensure the machine to be at the authentication condition
37
+ CMD_STARTENROLL = 61 # Start to enroll some user, ensure the machine to be at the registration user condition
38
+ CMD_CANCELCAPTURE = 62 # Make the machine to be at the waiting order status
39
+ CMD_STATE_RRQ = 64 # Gain the machine the condition
40
+ CMD_WRITE_LCD = 66 # Write LCD
41
+ CMD_CLEAR_LCD = 67 # Clear the LCD captions (clear screen)
42
+ CMD_GET_PINWIDTH = 69 # Obtain the length of user's serial number
43
+ CMD_SMS_WRQ = 70 # Upload the short message
44
+ CMD_SMS_RRQ = 71 # Download the short message
45
+ CMD_DELETE_SMS = 72 # Delete the short message
46
+ CMD_UDATA_WRQ = 73 # Set user's short message
47
+ CMD_DELETE_UDATA = 74 # Delete user's short message
48
+ CMD_DOORSTATE_RRQ = 75 # Obtain the door condition
49
+ CMD_WRITE_MIFARE = 76 # Write the Mifare card
50
+ CMD_EMPTY_MIFARE = 78 # Clear the Mifare card
51
+ CMD_PREPARE_BUFFER = 80 # Prepare buffer for data transfer
52
+ CMD_READFILE_DATA = 81 # Read data from buffer
53
+ CMD_GET_USERTEMP = 88 # Get an specific user template (uid, fid)
54
+ CMD_SAVE_USERTEMPS = 110 # Save user and multiple templates
55
+ CMD_DEL_USER_TEMP = 134 # Delete an specific user template (uid, fid)
56
+
57
+ CMD_GET_TIME = 201 # Obtain the machine time
58
+ CMD_SET_TIME = 202 # Set machines time
59
+ CMD_REG_EVENT = 500 # Register the event
60
+
61
+ CMD_CONNECT = 1000 # Connections requests
62
+ CMD_EXIT = 1001 # Disconnection requests
63
+ CMD_ENABLEDEVICE = 1002 # Ensure the machine to be at the normal work condition
64
+ CMD_DISABLEDEVICE = 1003 # Make the machine to be at the shut-down condition
65
+ CMD_RESTART = 1004 # Restart the machine
66
+ CMD_POWEROFF = 1005 # Shut-down power source
67
+ CMD_SLEEP = 1006 # Ensure the machine to be at the idle state
68
+ CMD_RESUME = 1007 # Awakens the sleep machine (temporarily not to support)
69
+ CMD_CAPTUREFINGER = 1009 # Captures fingerprints picture
70
+ CMD_TEST_TEMP = 1011 # Test some fingerprint exists or does not
71
+ CMD_CAPTUREIMAGE = 1012 # Capture the entire image
72
+ CMD_REFRESHDATA = 1013 # Refresh the machine interior data
73
+ CMD_REFRESHOPTION = 1014 # Refresh the configuration parameter
74
+ CMD_TESTVOICE = 1017 # Play voice
75
+ CMD_GET_VERSION = 1100 # Obtain the firmware edition
76
+ CMD_CHANGE_SPEED = 1101 # Change transmission speed
77
+ CMD_AUTH = 1102 # Connections authorizations
78
+ CMD_PREPARE_DATA = 1500 # Prepares to transmit the data
79
+ CMD_DATA = 1501 # Transmit a data packet
80
+ CMD_FREE_DATA = 1502 # Clear machines opened buffer
81
+ CMD_PREPARE_BUFFER = 1503 # Initialize buffer for partial reads
82
+ CMD_READ_BUFFER = 1504 # Ready a partial chunk of data from buffer
83
+
84
+ CMD_ACK_OK = 2000 # Return value for order perform successfully
85
+ CMD_ACK_ERROR = 2001 # Return value for order perform failed
86
+ CMD_ACK_DATA = 2002 # Return data
87
+ CMD_ACK_RETRY = 2003 # Registered event occurred
88
+ CMD_ACK_REPEAT = 2004 # Not available
89
+ CMD_ACK_UNAUTH = 2005 # Connection unauthorized
90
+
91
+ CMD_ACK_UNKNOWN = 0xffff # Unknown order
92
+ CMD_ACK_ERROR_CMD = 0xfffd # Order false
93
+ CMD_ACK_ERROR_INIT = 0xfffc # Not Initialized
94
+ CMD_ACK_ERROR_DATA = 0xfffb # Not available
95
+
96
+ # Event flags
97
+ EF_ATTLOG = 1 # Be real-time to verify successfully
98
+ EF_FINGER = (1 << 1) # Be real-time to press fingerprint
99
+ EF_ENROLLUSER = (1 << 2) # Be real-time to enroll user
100
+ EF_ENROLLFINGER = (1 << 3) # Be real-time to enroll fingerprint
101
+ EF_BUTTON = (1 << 4) # Be real-time to press button
102
+ EF_UNLOCK = (1 << 5) # Be real-time to unlock
103
+ EF_VERIFY = (1 << 7) # Be real-time to verify fingerprint
104
+ EF_FPFTR = (1 << 8) # Be real-time capture fingerprint minutia
105
+ EF_ALARM = (1 << 9) # Alarm signal
106
+
107
+ # User types
108
+ USER_DEFAULT = 0
109
+ USER_ENROLLER = 2
110
+ USER_MANAGER = 6
111
+ USER_ADMIN = 14
112
+
113
+ # Data flags
114
+ FCT_ATTLOG = 1
115
+ FCT_WORKCODE = 8
116
+ FCT_FINGERTMP = 2
117
+ FCT_OPLOG = 4
118
+ FCT_USER = 5
119
+ FCT_SMS = 6
120
+ FCT_UDATA = 7
121
+
122
+ # Machine constants
123
+ # These values are in little-endian format when packed
124
+ # 0x5050 = 'PP' in ASCII when packed as '<H'
125
+ MACHINE_PREPARE_DATA_1 = 20560 # 0x5050 = 'P' + 'P'*256 in little-endian
126
+ # 0x7D82 = 0x827D in little-endian format
127
+ MACHINE_PREPARE_DATA_2 = 32130 # 0x7D82
128
+ end
129
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBZK
4
+ class ZKError < StandardError; end
5
+
6
+ class ZKNetworkError < ZKError
7
+ def initialize(msg = "Network error")
8
+ super
9
+ end
10
+ end
11
+
12
+ class ZKErrorConnection < ZKError
13
+ def initialize(msg = "Connection error")
14
+ super
15
+ end
16
+ end
17
+
18
+ class ZKErrorResponse < ZKError
19
+ def initialize(msg = "Invalid response")
20
+ super
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBZK
4
+ class Finger
5
+ attr_accessor :uid, :fid, :valid, :template
6
+
7
+ def initialize(uid, fid, valid, template = "")
8
+ @uid = uid
9
+ @fid = fid
10
+ @valid = valid
11
+ @template = template
12
+ end
13
+
14
+ def to_s
15
+ "#{@uid} #{@fid} #{@valid} #{@template.length}"
16
+ end
17
+ end
18
+ end
data/lib/rbzk/user.rb ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBZK
4
+ class User
5
+ attr_accessor :uid, :user_id, :name, :privilege, :password, :group_id, :card
6
+
7
+ @@encoding = "UTF-8"
8
+
9
+ def self.encoding=(encoding)
10
+ @@encoding = encoding
11
+ end
12
+
13
+ def self.encoding
14
+ @@encoding
15
+ end
16
+
17
+ # Match Python's User constructor exactly
18
+ # In Python:
19
+ # def __init__(self, uid, name, privilege, password='', group_id='', user_id='', card=0):
20
+ def initialize(uid = 0, name = "", privilege = 0, password = "", group_id = "", user_id = "", card = 0)
21
+ @uid = uid
22
+ @name = name
23
+ @privilege = privilege
24
+ @password = password
25
+ @group_id = group_id
26
+ @user_id = user_id
27
+ @card = card
28
+ end
29
+
30
+ def to_s
31
+ "#{@uid} #{@user_id} #{@name} #{@privilege} #{@password} #{@group_id} #{@card}"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBZK
4
+ VERSION = "0.1.0"
5
+ end