zklib 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 619040938efac5653b65136119f32ad44daa5554
4
+ data.tar.gz: a3cb9affaaba22587fa802dc46c5ec865c8eda14
5
+ SHA512:
6
+ metadata.gz: b9a49652d4bd758ad3dff28816a4472bedfb8d49aaedbcaef553b640232049abe245428a412cb8bfa29103b6786d31de52e83474163ba68a62b86280ef5ac7d0
7
+ data.tar.gz: b6dc8eea2775d6b27621f5fa332c78ae36e54470a76b2142f8f22446252b60781756ca3fbefccb35de03ac311505adfd2e19ec04e4a20fdfde7ca096a66f2ad4
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-version
11
+ .rbenv-gemsets
12
+ .byebug_history
13
+ *~
14
+ *.swp
15
+ *.gem
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at anhtrantuan.hcmc@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zklib.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Anh Tran
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.
@@ -0,0 +1,41 @@
1
+ # Zklib
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zklib`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'zklib'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install zklib
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zklib. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zklib"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,124 @@
1
+ require 'zklib/version'
2
+
3
+ # Libraries
4
+ require 'socket'
5
+ require 'bindata'
6
+
7
+ # Mixins
8
+ require 'zklib/helper'
9
+ require 'zklib/attendance_management'
10
+ require 'zklib/connection_management'
11
+ require 'zklib/data_management'
12
+ require 'zklib/device_management'
13
+ require 'zklib/face_management'
14
+ require 'zklib/pin_management'
15
+ require 'zklib/platform_management'
16
+ require 'zklib/serial_management'
17
+ require 'zklib/ssr_management'
18
+ require 'zklib/time_management'
19
+ require 'zklib/user_management'
20
+ require 'zklib/version_management'
21
+ require 'zklib/work_code_management'
22
+
23
+ class Zklib
24
+ include Helper
25
+ include AttendanceManagement
26
+ include ConnectionManagement
27
+ include DataManagement
28
+ include DeviceManagement
29
+ include FaceManagement
30
+ include PINManagement
31
+ include PlatformManagement
32
+ include SerialManagement
33
+ include SSRManagement
34
+ include TimeManagement
35
+ include UserManagement
36
+ include VersionManagement
37
+ include WorkCodeManagement
38
+
39
+ # Max unsigned short value
40
+ USHRT_MAX = 65535
41
+
42
+
43
+ # Data structures' size
44
+ ATTENDANCE_DATA_SIZE = 40
45
+ USER_DATA_SIZE = 72
46
+
47
+
48
+ # Commands
49
+ CMD_CONNECT = 1000
50
+ CMD_EXIT = 1001
51
+ CMD_ENABLEDEVICE = 1002
52
+ CMD_DISABLEDEVICE = 1003
53
+ CMD_RESTART = 1004
54
+ CMD_POWEROFF = 1005
55
+
56
+ CMD_ACK_OK = 2000
57
+ CMD_ACK_ERROR = 2001
58
+ CMD_ACK_DATA = 2002
59
+
60
+ CMD_PREPARE_DATA = 1500
61
+ CMD_DATA = 1501
62
+ CMD_FREE_DATA = 1502
63
+ CMD_REFRESHDATA = 1013
64
+ CMD_REFRESHOPTION = 1014
65
+
66
+ CMD_USERTEMP_RRQ = 9
67
+ CMD_ATTLOG_RRQ = 13
68
+ CMD_CLEAR_DATA = 14
69
+ CMD_CLEAR_ATTLOG = 15
70
+
71
+ CMD_WRITE_LCD = 66
72
+
73
+ CMD_GET_TIME = 201
74
+ CMD_SET_TIME = 202
75
+
76
+ CMD_VERSION = 1100
77
+ CMD_DEVICE = 11
78
+
79
+ CMD_CLEAR_ADMIN = 20
80
+ CMD_DELETE_USER = 18
81
+ CMD_SET_USER = 8
82
+
83
+
84
+ # User levels
85
+ LEVEL_USER = 0
86
+ LEVEL_ADMIN = 14
87
+
88
+
89
+ # Login states
90
+ LOGIN_BY_PASSWORD = 0
91
+ LOGIN_BY_FINGERPRINT = 1
92
+
93
+
94
+ # Communication states
95
+ STATE_FIRST_PACKET = 1
96
+ STATE_PACKET = 2
97
+ STATE_FINISHED = 3
98
+
99
+
100
+ attr_reader :inport,
101
+ :ip,
102
+ :port
103
+ attr_accessor :data_recv,
104
+ :reply_id,
105
+ :session_id,
106
+ :socket,
107
+ :exttrynumber,
108
+ :id_com
109
+
110
+
111
+ # param options Init options
112
+ # |_ ip Attendance machine IP
113
+ # |_ port Attendance machine UDP port
114
+ # |_ inport Client UDP port
115
+ def initialize(options)
116
+ @ip = options[:ip]
117
+ @port = options[:port]
118
+ @inport = options[:inport]
119
+ @socket = nil
120
+ @reply_id = USHRT_MAX - 1
121
+ @data_recv = ''
122
+ @session_id = 0
123
+ end
124
+ end
@@ -0,0 +1,119 @@
1
+ class Zklib
2
+ module AttendanceManagement
3
+ # Clear attendances
4
+ def clear_attendances
5
+ execute_cmd(
6
+ command: CMD_CLEAR_ATTLOG,
7
+ command_string: '',
8
+ ) do |opts|
9
+ return puts "ERROR: #{options[:error]}" unless opts[:valid]
10
+
11
+ data = opts[:data]
12
+ if data.length > 7
13
+ data.split("\u0000").pop
14
+ else
15
+ puts 'ERROR: Invalid clear attendances response'
16
+ end
17
+ end
18
+ end
19
+
20
+ # Decode attendance data
21
+ #
22
+ # param options
23
+ # |_ data Attendance data to decode
24
+ def decode_attendance_data(options)
25
+ data = options[:data]
26
+
27
+ {
28
+ uid: data[0..3].split("\u0000").pop.to_i,
29
+ id: data[4..7].split("\u0000").pop.to_i,
30
+ state: BinData::Uint8le.read(data[28]).snapshot,
31
+ timestamp: decode_time(seconds: BinData::Uint32le.read(data[29..-1]).snapshot),
32
+ }
33
+ end
34
+
35
+ # Get attendance list size
36
+ def get_attendance_count
37
+ return 0 if BinData::Uint16le.read(data_recv).snapshot != CMD_PREPARE_DATA
38
+
39
+ BinData::Uint32le.read(data_recv[8..-1]).snapshot
40
+ end
41
+
42
+ # Get attendance list from attendance machine
43
+ def get_attendances
44
+ header = create_header(
45
+ command: CMD_ATTLOG_RRQ,
46
+ command_string: '',
47
+ checksum: 0,
48
+ session_id: session_id,
49
+ reply_id: BinData::Uint16le.read(data_recv[6..-1]).snapshot,
50
+ )
51
+
52
+ # Send command
53
+ socket = UDPSocket.new
54
+ socket.bind('0.0.0.0', inport)
55
+ socket.send(header, 0, ip, port)
56
+
57
+ ### START Get response size ###
58
+ self.data_recv = receive_nonblock(socket: socket)[0]
59
+
60
+ return puts 'ERROR: Empty response' unless data_recv && data_recv.length > 0
61
+
62
+ self.session_id = BinData::Uint16le.read(data_recv[3..-1]).snapshot
63
+ total_bytes = get_attendance_count
64
+
65
+ # Stop if attendance list is empty
66
+ if total_bytes <= 0
67
+ socket.close
68
+
69
+ return []
70
+ end
71
+ ### END Get response size ###
72
+
73
+ ### START Get attendance list ###
74
+ bytes_recv = 0
75
+ rem = nil
76
+ offset = 0
77
+ attendance_data_size = 40
78
+ trim_first = 10
79
+ trim_others = 8
80
+ attendances = []
81
+
82
+ while true
83
+ data = receive_nonblock(socket: socket)[0]
84
+
85
+ if bytes_recv == 0
86
+ offset = trim_first
87
+ bytes_recv = 4
88
+ else
89
+ offset = trim_others
90
+ end
91
+
92
+ while(data.length - offset >= attendance_data_size)
93
+ attendance_data = data[offset..-1]
94
+ offset += attendance_data_size
95
+
96
+ if rem && rem.length > 0
97
+ attendance_data.prepend(rem)
98
+ offset -= rem.length
99
+ rem = nil
100
+ end
101
+
102
+ attendances << decode_attendance_data(data: attendance_data)
103
+ bytes_recv += attendance_data_size
104
+
105
+ if bytes_recv == total_bytes
106
+ socket.close
107
+
108
+ return attendances
109
+ end
110
+ end
111
+
112
+ rem = data[offset..-1]
113
+ end
114
+ ### END Get attendance list ###
115
+
116
+ socket.close
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,23 @@
1
+ class Zklib
2
+ module ConnectionManagement
3
+ # Connect to attendance machine
4
+ def connect
5
+ execute_cmd(
6
+ command: CMD_CONNECT,
7
+ command_string: ''
8
+ ) do |opts|
9
+ check_valid(data: opts[:data])
10
+ end
11
+ end
12
+
13
+ # Disconnect from attendance machine
14
+ def disconnect
15
+ execute_cmd(
16
+ command: CMD_EXIT,
17
+ command_string: ''
18
+ ) do |opts|
19
+ check_valid(data: opts[:data])
20
+ end
21
+ end
22
+ end
23
+ end