Class: Extface::Driver::EltradeTmU220

Inherits:
Extface::Driver show all
Includes:
Extface::Driver::Eltrade::CommandsFp4
Defined in:
app/models/extface/driver/eltrade_tm_u220.rb

Defined Under Namespace

Classes: Frame, PrinterStatus

Constant Summary collapse

NAME =
'Eltrade TM-U220 (Serial)'.freeze
GROUP =
Extface::FISCAL_DRIVER
DEVELOPMENT =

driver is not ready for production (not passing all tests or has major bugs)

true
RAW =

Select driver features

true
false
FISCAL =

POS, slip printers

true
REPORT =

cash registers, fiscal printers

false
RESPONSE_TIMEOUT =

only transmit data that must be parsed by handler, CDR, report devices

3
INVALID_FRAME_RETRIES =

seconds

6
BUSY_MAX_WAIT_CYCLES =

count

60
FLAG_TRUE =

count

"\xff\xff"
FLAG_FALSE =
"\x00\x00"

Constants inherited from Extface::Driver

DRIVER_TYPES

Instance Method Summary collapse

Methods inherited from Extface::Driver

#flush, has_serial_config, #notify, #pre_handle, #pull, #push, #rpush, #set_job

Instance Method Details

#build_packet(cmd, data = "") ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 179

def build_packet(cmd, data = "")
  "".tap() do |packet|
    packet << STRT.b
    packet << 0x00 #address
    packet << sequence_number
    packet << cmd
    packet << data.length
    packet << data.b
    packet << check_sum(packet[2..-1])
  end
end

#build_sale_data(price, text1 = "", text2 = nil, tax_group = 2, qty = 1, percent = nil, neto = nil, number = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 100

def build_sale_data(price, text1 = "", text2 = nil, tax_group = 2, qty = 1, percent = nil, neto = nil, number = nil)
  "".b.tap() do |data|
    price_units = (price * 100).to_i # !FIXME
    price_bytes = "".b
    4.times{ |shift| price_bytes.insert 0, ((price_units >> shift*8) & 0xff).chr }
    data << price_bytes
    qty_units = ((qty || 1) * 1000).to_i # !FIXME
    qty_bytes = "".b
    4.times{ |shift| qty_bytes.insert 0, ((qty_units >> shift*8) & 0xff).chr }
    data << qty_bytes
    data << "\x00".b #number len FIXME
    data << "\xAA\xAA\xAA\xAA\xAA\xAA".b #number FIXME
    text = text1.truncate(20)
    data << text.length.chr
    data << text.ljust(20, " ").b
    data << (tax_group || 2).chr
  end
end

#cancel_doc_session ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 151

def cancel_doc_session
  device.session("Doc cancel") do |s|
    s.notify "Doc Cancel Start"
    # cancel old one by open/close new one
    s.open_receipt
    s.close_receipt
    s.notify "Doc Cancel End"
  end
end

#check_ready! ⇒ Object



161
162
163
164
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 161

def check_ready!
  fsend Info::GET_STATUS
  raise errors.full_messages.join(", ") if errors.any?
end

#check_status ⇒ Object



170
171
172
173
174
175
176
177
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 170

def check_status
  flush
  status = get_printer_status
  #TODO check for:
  #1. sold PLUs dangerously high -> solution PLU Report (0x32)
  #2. open bon and transaction count
  errors.empty?
end

#check_sum(buffer) ⇒ Object



239
240
241
242
243
244
245
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 239

def check_sum(buffer)
  sum = 0
  buffer.each_byte do |byte|
    sum -= byte
  end
  sum & 0xff
end

#close_receipt ⇒ Object



54
55
56
57
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 54

def close_receipt
  fsend Receipt::CLOSE_RECEIPT
  status = get_printer_status
end

#fiscal_test ⇒ Object



94
95
96
97
98
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 94

def fiscal_test
  sale_and_pay_items_session([
    { price: 0.01, text1: "Extface Test" }
  ])
end

#frecv(timeout) ⇒ Object

return RespFrame or nil



230
231
232
233
234
235
236
237
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 230

def frecv(timeout) # return RespFrame or nil
  if frame_bytes = pull(timeout)
    return Frame.new(frame_bytes.b)
  else
    errors.add :base, "No data received from device"
    return nil
  end
end

#fsend(cmd, data = "") ⇒ Object

return data or nil



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 191

def fsend(cmd, data = "") #return data or nil
  result = false
  status_invalid_responses = 0
  BUSY_MAX_WAIT_CYCLES.times do |retries|
    errors.clear
    push build_packet(Info::GET_STATUS)
    if stat_frame = frecv(RESPONSE_TIMEOUT)
      if stat_frame.valid?
        break if stat_frame.ready?
      else
        status_invalid_responses -= 1
        unless status_invalid_responses < INVALID_FRAME_RETRIES
          errors.add :base, "#{INVALID_FRAME_RETRIES} Broken Packets Received. Abort!"
        end
      end
    end
    errors.add :base, "#{BUSY_MAX_WAIT_CYCLES} Busy Packets Received. Abort!"
  end
  return(result) if errors.any?
  
  packet_data = build_packet(cmd, data)
  INVALID_FRAME_RETRIES.times do |retries|
    errors.clear
    push packet_data
    if resp = frecv(RESPONSE_TIMEOUT)
      if resp.valid?
        result = resp.data
        break
      else
        resp.errors.full_messages.each do |msg|
          errors.add :base, msg
        end
      end
    end
    errors.add :base, "#{INVALID_FRAME_RETRIES} Broken Packets Received. Abort!"
  end
  return result
end

#get_printer_status ⇒ Object



166
167
168
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 166

def get_printer_status
  PrinterStatus.new(fsend(Info::GET_PRINTER_STATUS))
end

#handle(buffer) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 31

def handle(buffer)
  bytes_processed = 0
  if frame_match = buffer.match(/\xAA\x55.{3}(.{1}).*/nm) #m Treat \x0a as a character matched by .
    len = frame_match.captures.first.ord
    skip = frame_match.pre_match.length
    bytes_processed = skip + 7 + len # 6 pre + 1 check sum
    if bytes_processed <= buffer.length #packet in buffer
      rpush buffer[skip..bytes_processed]
    else
      bytes_processed = skip #not whole packet, just remove trail
    end
  end
  return bytes_processed
end

#non_fiscal_test ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 80

def non_fiscal_test
  device.session("Non Fiscal Text") do |s|
    s.notify "Printing Non Fiscal Text"
    s.open_receipt Receipt::Variant::START_COMMENT_RECEIPT
    s.send_comment "********************************"
    s.send_comment "Extface Print Test".center(32)
    s.send_comment "********************************"
    s.send_comment ""
    s.send_comment "Driver: " + "#{self.class::NAME}".truncate(24)
    s.close_receipt
    s.notify "Printing finished"
  end
end

#open_receipt(variant = nil) ⇒ Object



46
47
48
49
50
51
52
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 46

def open_receipt(variant = nil)
  fsend Receipt::OPEN_RECEIPT
  unless variant.blank?
    fsend Receipt::PRINT_RECEIPT, variant
  end
  status = get_printer_status
end

#sale_and_pay_items_session(items = [], operator = "1", password = "1") ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 119

def sale_and_pay_items_session(items = [], operator = "1", password = "1")
  device.session("Fiscal Doc") do |s|
    s.notify "Fiscal Doc Start"
    s.open_receipt
    items.each do |item|
      s.send_plu build_sale_data(item[:price], item[:text1], nil, item[:tax_group], item[:qty], nil, nil, item[:number])
      s.send_comment(item[:text2]) unless item[:text2].blank?
    end
    s.send_payment
    s.close_receipt
    s.notify "Fiscal Doc End"
  end
end

#send_comment(text) ⇒ Object



59
60
61
62
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 59

def send_comment(text)
  fsend Receipt::PRINT_RECEIPT, Receipt::Variant::COMMENT + text
  status = get_printer_status
end

#send_payment(type_num = 0, value = nil) ⇒ Object

0, 1, 2, 3



69
70
71
72
73
74
75
76
77
78
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 69

def send_payment(type_num = 0, value = nil) # 0, 1, 2, 3
  value_bytes = "\x00\x00\x00\x00" # recalculate
  unless value.nil?
    value_units = (value * 100).to_i # !FIXME
    value_bytes = "".b
    4.times{ |shift| value_bytes.insert 0, ((value_units >> shift*8) & 0xff).chr }
  end
  fsend Receipt::PRINT_RECEIPT, "" << (9 + type_num).chr << value_bytes
  status = get_printer_status
end

#send_plu(plu_data) ⇒ Object



64
65
66
67
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 64

def send_plu(plu_data)
  fsend Receipt::PRINT_RECEIPT, Receipt::Variant::PLU + plu_data
  status = get_printer_status
end

#x_report_session ⇒ Object



142
143
144
145
146
147
148
149
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 142

def x_report_session
  device.session("Z Report") do |s|
    s.notify "X Report Start"
    s.fsend Reports::DAILY_REPORT, FLAG_FALSE
    status = s.get_printer_status
    s.notify "X Report End"
  end
end

#z_report_session ⇒ Object



133
134
135
136
137
138
139
140
# File 'app/models/extface/driver/eltrade_tm_u220.rb', line 133

def z_report_session
  device.session("Z Report") do |s|
    s.notify "Z Report Start"
    s.fsend Reports::DAILY_REPORT, FLAG_TRUE
    status = s.get_printer_status
    s.notify "Z Report End"
  end
end