Module: Zklib::TimeManagement

Included in:
Zklib
Defined in:
lib/zklib/time_management.rb

Instance Method Summary collapse

Instance Method Details

#get_timeObject

Get current time of attendance machine



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/zklib/time_management.rb', line 4

def get_time
  execute_cmd(
    command:        CMD_GET_TIME,
    command_string: ''
  ) do |opts|
    return puts "ERROR: #{options[:error]}" unless opts[:valid]

    data = opts[:data]
    if data.length > 8
      decode_time(seconds: BinData::Uint32le.read(data[8..-1]).snapshot)
    else
      puts 'ERROR: Invalid time response'
    end
  end
end

#set_time(time = Time.now) ⇒ Object

Set current time for attendance machine



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zklib/time_management.rb', line 21

def set_time(time = Time.now)
  seconds        = encode_time(time: time)
  command_buffer = StringIO.new
  binary_writer  = BinData::Uint32le.new

  # Write command string
  binary_writer.value = seconds
  command_buffer.pos  = 0
  binary_writer.write(command_buffer)
  command_string = command_buffer.string

  execute_cmd(
    command:        CMD_SET_TIME,
    command_string: command_string
  ) do |opts|
    return puts "ERROR: #{options[:error]}" unless opts[:valid]

    data = opts[:data]
    if data.length > 7
      data.split("\u0000").pop
    else
      puts 'ERROR: Invalid time response'
    end
  end
end