Class: StarEthernet::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/star_ethernet/printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Printer

Returns a new instance of Printer.



12
13
14
15
# File 'lib/star_ethernet/printer.rb', line 12

def initialize(host)
  @host = host
  @status = StarEthernet::Status.new(self)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/star_ethernet/printer.rb', line 10

def host
  @host
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/star_ethernet/printer.rb', line 10

def status
  @status
end

Instance Method Details

#current_statusObject



96
97
98
# File 'lib/star_ethernet/printer.rb', line 96

def current_status
  @status.current_status
end

#fetch_status(purpose = '') ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/star_ethernet/printer.rb', line 78

def fetch_status(purpose = '')
  begin
    socket = socket(StarEthernet.configuration.status_acquisition_port)
    socket.print(StarEthernet::Command.status_acquisition)
    asb_status = socket.read(StarEthernet.configuration.asb_status_size)
    @status.set_status(asb_status, purpose)
    socket.close
    @status.current_status
  rescue => e
      raise StarEthernet::StatusFetchFailed.new(e.full_message)
    end
end


17
18
19
20
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/star_ethernet/printer.rb', line 17

def print(data)
  begin
    socket = socket(StarEthernet.configuration.raw_socket_print_port)

    if StarEthernet.configuration.nsb
      nbs_status = socket.read(StarEthernet.configuration.asb_status_size)
      @status.set_status(nbs_status, 'receive nbs status')
    end

    socket.print(StarEthernet::Command.initialize_print_sequence)

    fetch_status('fetch etb status for checking counting up after initializing print sequence')

    if current_status.offline?
      raise StarEthernet::PrinterOffline.new(@status.full_messages)
    end

    socket.print(StarEthernet::Command.update_asb_etb_status)

    StarEthernet.configuration.retry_counts.times do |index|
      sleep StarEthernet.configuration.fetch_time_interval
      fetch_status('fetch etb status for reserve etb count before printing')

      break if @status.etb_incremented?
      if index == StarEthernet.configuration.retry_counts - 1
        raise StarEthernet::EtbCountUpFailed.new(@status.full_messages)
      end
    end

    socket.print(StarEthernet::Command.start_document)

    socket.print(data)

    socket.print(StarEthernet::Command.update_asb_etb_status)

    socket.print(StarEthernet::Command.end_document)

    StarEthernet.configuration.retry_counts.times do |index|
      sleep StarEthernet.configuration.fetch_time_interval
      fetch_status('fetch etb status for checking print success')

      break if @status.etb_incremented?
      if index == StarEthernet.configuration.retry_counts - 1
        raise StarEthernet::PrintFailed.new(@status.full_messages)
      end
    end
  ensure
    socket.close
  end
end

#rebootObject



91
92
93
94
# File 'lib/star_ethernet/printer.rb', line 91

def reboot
  telnet.cmd('String' => '98', 'Match' => /(.)*Selection: /)
  telnet.cmd('String' => '2', 'Match' => /(.)*Selection: /)
end

#send_command(data) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/star_ethernet/printer.rb', line 68

def send_command(data)
  socket = socket(StarEthernet.configuration.raw_socket_print_port)
  if StarEthernet.configuration.nsb
    nbs_status = socket.read(StarEthernet.configuration.asb_status_size)
    @status.set_status(nbs_status)
  end
  socket.print(data)
  socket.close
end

#set_custom_socket(&socket) ⇒ Object



106
107
108
# File 'lib/star_ethernet/printer.rb', line 106

def set_custom_socket(&socket)
  @custom_socket = socket
end

#socket(port) ⇒ Object



100
101
102
103
104
# File 'lib/star_ethernet/printer.rb', line 100

def socket(port)
  @custom_socket ?
    @custom_socket.call(@host, port) :
    TCPSocket.new(host, port)
end

#telnetObject



110
111
112
113
114
115
116
# File 'lib/star_ethernet/printer.rb', line 110

def telnet
  telnet = Net::Telnet.new('Host' => @host, 'Port' => StarEthernet.configuration.telnet_port)
  telnet.waitfor(/(.)*login: /)
  telnet.cmd('String' => 'root', 'Match' => /(.)*Password: /)
  telnet.cmd('String' => 'public', 'Match' => /(.)*Selection: /)
  telnet
end