star_ethernet 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afba0de930da0bd1b2ffff35864267d0c44c15bb9621d7b8b0590d3574077f78
4
- data.tar.gz: 5aa6048f10b0fb2c70b2043e1a97b2f659571c1604d5d5b9989b14426141ca82
3
+ metadata.gz: e938547b2a7d713bbf3c3836d34dff3f74657c478ed1485c4c20e4bfd809d237
4
+ data.tar.gz: bfc8e7fcea483a1277a2f1fd19266864e48c95842f4e14107d8134731b1eddd8
5
5
  SHA512:
6
- metadata.gz: 42abb8b909fdedb537c0e27a54822bf180456175da03d15b420a90b02b06f7bb01965fb9234657013be24295e61650c91f8d6d47babdcab889663990632bfa46
7
- data.tar.gz: fb5ebd3c1296742ce7c87e074bb93a8d4e21b88e0618598ba39f82d3f39f802645d7c7fea8458b9dc554589b754b75aa58e686fe3239cba2cc9b60804a5d0bf6
6
+ metadata.gz: 826a49de39fc81da15c921acfb42714c05dd7e8e05daab6183a0ccc0515b1dcf99961a74a638f87095ea0b25ad78c65a3339559c592d66c92cc073269f7fb4db
7
+ data.tar.gz: 56fc58ba54b5ed7343236beaf96ee5dcd75c6765745ef0cde2926b1f9da0d8a8946b53bf5dc3ca2400e3bb7fcf0f6e29606c56edd9ec0ba48007349fe9811f1c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- star_ethernet (0.1.2)
4
+ star_ethernet (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,25 @@
1
1
  module StarEthernet
2
2
  module Command
3
+ def self.status_acquisition
4
+ ([0x32] + 50.times.map { 0x00 }).pack('C*')
5
+ end
6
+
7
+ def self.initialize_print_sequence
8
+ [
9
+ self.quit_raster_mode,
10
+ self.send_print_end_counter_initialize(0x04, 0x00,0x00),
11
+ ].join
12
+ end
13
+
14
+ def self.start_document
15
+ self.send_print_end_counter_initialize(0x03, 0x00, 0x00)
16
+ end
17
+
18
+ def self.end_document
19
+ self.send_print_end_counter_initialize(0x04, 0x00, 0x00)
20
+ end
21
+
22
+
3
23
  # Standard Command
4
24
  ## Font style and Character Set
5
25
  def self.select_font(n)
@@ -0,0 +1,13 @@
1
+ module StarEthernet
2
+ class PrinterError < StandardError
3
+ def initialize(statuses_message)
4
+ super(statuses_message)
5
+ end
6
+ end
7
+
8
+ class EtbCountUpFailed < PrinterError; end
9
+
10
+ class PrintFailed < EtbCountUpFailed; end
11
+
12
+ class PrinterOffline < PrinterError; end
13
+ end
@@ -1,43 +1,111 @@
1
1
  require 'socket'
2
2
 
3
3
  require 'star_ethernet/status'
4
+ require 'star_ethernet/exceptions'
4
5
 
5
6
  module StarEthernet
6
7
  class Printer
7
8
  RAW_SOCKET_PRINT_PORT = 9100
8
9
  STATUS_ACQUISITION_PORT = 9101
10
+ ASB_STATUS_SIZE = 9
11
+ FETCH_TIME_INTERVAL = 0.1
12
+ RETRY_COUNTS = 300
9
13
 
10
- attr_reader :status
14
+ attr_reader :status, :host
11
15
 
12
- def initialize(host)
13
- @host = host
14
- @status = StarEthernet::Status.new
16
+ def initialize(host, nsb: false, asb: false, interval: FETCH_TIME_INTERVAL, retry_counts: RETRY_COUNTS)
17
+ @host, @nsb, @asb, @interval, @retry_counts = host, nsb, asb, interval, retry_counts
18
+ @status = StarEthernet::Status.new(self)
15
19
  end
16
20
 
17
21
  def print(data)
22
+ begin
23
+ socket = socket(RAW_SOCKET_PRINT_PORT)
24
+
25
+ if @nsb
26
+ nbs_status = socket.read(ASB_STATUS_SIZE)
27
+ @status.set_status(nbs_status, 'receive nbs status')
28
+ end
29
+
30
+ socket.print(StarEthernet::Command.initialize_print_sequence)
31
+
32
+ fetch_status('fetch etb status for checking counting up after initializing print sequence')
33
+
34
+ if current_status.offline?
35
+ raise StarEthernet::PrinterOffline.new(@status.full_messages)
36
+ end
37
+
38
+ socket.print(StarEthernet::Command.update_asb_etb_status)
39
+
40
+ @retry_counts.times do |index|
41
+ sleep @interval
42
+ fetch_status('fetch etb status for reserve etb count before printing')
43
+
44
+ break if etb_incremented?
45
+ if index == @retry_counts - 1
46
+ raise StarEthernet::EtbCountUpFailed.new(@status.full_messages)
47
+ end
48
+ end
49
+
50
+ socket.print(StarEthernet::Command.start_document)
51
+
52
+ socket.print(data)
53
+
54
+ socket.print(StarEthernet::Command.update_asb_etb_status)
55
+
56
+ socket.print(StarEthernet::Command.end_document)
57
+
58
+ @retry_counts.times do |index|
59
+ sleep @interval
60
+ fetch_status('fetch etb status for checking print success')
61
+ break if etb_incremented?
62
+ if index == @retry_counts - 1
63
+ raise StarEthernet::PrintFailed.new(@status.full_messages)
64
+ end
65
+ end
66
+ ensure
67
+ socket.close
68
+ end
69
+ end
70
+
71
+ def send_command(data)
18
72
  socket = socket(RAW_SOCKET_PRINT_PORT)
19
- nbs_status = socket.read(11)
20
- @status.set_status(nbs_status)
73
+ if @nsb
74
+ nbs_status = socket.read(ASB_STATUS_SIZE)
75
+ @status.set_status(nbs_status)
76
+ end
21
77
  socket.print(data)
22
- socket.close_write
23
78
  socket.close
24
79
  end
25
80
 
26
- def current_status
81
+ def fetch_status(purpose = '')
82
+ socket = socket(STATUS_ACQUISITION_PORT)
83
+ socket.print(StarEthernet::Command.status_acquisition)
84
+ asb_status = socket.read(ASB_STATUS_SIZE)
85
+ @status.set_status(asb_status, purpose)
86
+ socket.close
27
87
  @status.current_status
28
88
  end
29
89
 
30
- def fetch_status
31
- socket = socket(STATUS_ACQUISITION_PORT)
32
- cmd = [0x32] + 50.times.map { 0x00 }
33
- socket.print(cmd.pack('C*'))
34
- asb_status = socket.read(11)
35
- @status.set_status(asb_status)
36
- socket.close
90
+ def etb_incremented?
91
+ current_etb == previous_etb + 1
92
+ end
93
+
94
+ def current_etb
95
+ current_status.etb
96
+ end
97
+
98
+ def previous_etb
99
+ previous_status.etb
100
+ end
101
+
102
+ def current_status
37
103
  @status.current_status
38
104
  end
39
105
 
40
- private
106
+ def previous_status
107
+ @status.previous_status
108
+ end
41
109
 
42
110
  def socket(port)
43
111
  TCPSocket.new(@host, port)
@@ -2,17 +2,32 @@ require 'star_ethernet/status_item'
2
2
 
3
3
  module StarEthernet
4
4
  class StarEthernet::Status
5
- def initialize
6
- @statuses = []
5
+ attr_reader :status_items
6
+
7
+ def initialize(printer)
8
+ @printer = printer
9
+ @status_items = []
7
10
  end
8
11
 
9
12
  def current_status
10
- @statuses.first
13
+ @status_items.last
14
+ end
15
+
16
+ def previous_status
17
+ @status_items[-2]
11
18
  end
12
19
 
13
- def set_status(status_data)
20
+ def set_status(status_data, purpose = '')
14
21
  current_status = StatusItem.decode_status(status_data)
15
- @statuses.push(current_status)
22
+ current_status.purpose = purpose
23
+ @status_items.push(current_status)
24
+ end
25
+
26
+ def full_messages
27
+ <<~"EOS"
28
+ #{@printer.host} printer's statuses are
29
+ #{@status_items.map{ |status_item| status_item.message }.join("\n")}
30
+ EOS
16
31
  end
17
32
  end
18
33
  end
@@ -1,5 +1,34 @@
1
1
  module StarEthernet
2
- module StatusItem
2
+ class StatusItem
3
+ attr_reader :statuses, :time
4
+ attr_writer :purpose
5
+ attr_accessor :etb
6
+
7
+ def initialize(statuses: [])
8
+ @statuses = statuses
9
+ @etb = nil
10
+ @time = Time.now
11
+ @purpose = nil
12
+ end
13
+
14
+ def offline?
15
+ @statuses.include?(PrinterStatus::Offline)
16
+ end
17
+
18
+ def append_status(status)
19
+ @statuses.push(status)
20
+ end
21
+
22
+ def message
23
+ msg = @statuses.empty? ?
24
+ "[#{@time.to_s}] Normal status" :
25
+ "[#{@time.to_s}] #{@statuses.map{ |status| status.to_s }.join(', ')}"
26
+ msg += " etb:#{@etb}"
27
+ msg += " (#{@purpose})" if @purpose
28
+ msg
29
+ end
30
+
31
+
3
32
  class HeaderStatus
4
33
  # todo
5
34
  end
@@ -38,10 +67,8 @@ module StarEthernet
38
67
  class StateWherePaperIsPulledOut; end
39
68
  end
40
69
 
41
- class NormalStatus; end
42
-
43
70
  def self.decode_status(status_raw_data)
44
- status_items = []
71
+ status_item = StatusItem.new
45
72
 
46
73
  status_data = status_raw_data.unpack('H*').first
47
74
 
@@ -49,37 +76,38 @@ module StarEthernet
49
76
  byte2 = status_data[2..3].hex # todo version status
50
77
 
51
78
  byte3 = status_data[4..5].hex
52
- status_items.push(PrinterStatus::OfflineBySwitchInput) if byte3 & 0b01000000 > 0
53
- status_items.push(PrinterStatus::CoverOpen) if byte3 & 0b00100000 > 0
54
- status_items.push(PrinterStatus::Offline) if byte3 & 0b00001000 > 0
55
- status_items.push(PrinterStatus::ConversionSwitchClosed) if byte3 & 0b00000100 > 0
56
- status_items.push(PrinterStatus::EtbCommandExecuted) if byte3 & 0b00000010 > 0
79
+ status_item.append_status(PrinterStatus::OfflineBySwitchInput) if byte3 & 0b01000000 > 0
80
+ status_item.append_status(PrinterStatus::CoverOpen) if byte3 & 0b00100000 > 0
81
+ status_item.append_status(PrinterStatus::Offline) if byte3 & 0b00001000 > 0
82
+ status_item.append_status(PrinterStatus::ConversionSwitchClosed) if byte3 & 0b00000100 > 0
83
+ status_item.append_status(PrinterStatus::EtbCommandExecuted) if byte3 & 0b00000010 > 0
57
84
 
58
85
  byte4 = status_data[6..7].hex
59
- status_items.push(ErrorStatus::StoppedByHighHeadTemperature) if byte4 & 0b01000000 > 0
60
- status_items.push(ErrorStatus::NonRecoverableError) if byte4 & 0b00100000 > 0
61
- status_items.push(ErrorStatus::AutoCutterError) if byte4 & 0b00001000 > 0
62
- status_items.push(ErrorStatus::MechanicalError) if byte4 & 0b00000100 > 0
86
+ status_item.append_status(ErrorStatus::StoppedByHighHeadTemperature) if byte4 & 0b01000000 > 0
87
+ status_item.append_status(ErrorStatus::NonRecoverableError) if byte4 & 0b00100000 > 0
88
+ status_item.append_status(ErrorStatus::AutoCutterError) if byte4 & 0b00001000 > 0
89
+ status_item.append_status(ErrorStatus::MechanicalError) if byte4 & 0b00000100 > 0
63
90
 
64
91
  byte5 = status_data[8..9].hex
65
- status_items.push(ErrorStatus::ReceiveBufferOverflow) if byte5 & 0b01000000 > 0
66
- status_items.push(ErrorStatus::CommandError) if byte5 & 0b00100000 > 0
67
- status_items.push(ErrorStatus::BmError) if byte5 & 0b00001000 > 0
68
- status_items.push(ErrorStatus::PresenterPageJamError) if byte5 & 0b00000100 > 0
69
- status_items.push(ErrorStatus::HeadUpError) if byte5 & 0b00000010 > 0
92
+ status_item.append_status(ErrorStatus::ReceiveBufferOverflow) if byte5 & 0b01000000 > 0
93
+ status_item.append_status(ErrorStatus::CommandError) if byte5 & 0b00100000 > 0
94
+ status_item.append_status(ErrorStatus::BmError) if byte5 & 0b00001000 > 0
95
+ status_item.append_status(ErrorStatus::PresenterPageJamError) if byte5 & 0b00000100 > 0
96
+ status_item.append_status(ErrorStatus::HeadUpError) if byte5 & 0b00000010 > 0
70
97
 
71
98
  byte6 = status_data[10..11].hex
72
- status_items.push(SensorStatus::PaperEnd) if byte6 & 0b00001000 > 0
73
- status_items.push(SensorStatus::PaperNearInsideEnd) if byte6 & 0b00000100 > 0
74
- status_items.push(SensorStatus::PaperNearOutsideEnd) if byte6 & 0b00000010 > 0
99
+ status_item.append_status(SensorStatus::PaperEnd) if byte6 & 0b00001000 > 0
100
+ status_item.append_status(SensorStatus::PaperNearInsideEnd) if byte6 & 0b00000100 > 0
101
+ status_item.append_status(SensorStatus::PaperNearOutsideEnd) if byte6 & 0b00000010 > 0
75
102
 
76
103
  byte7 = status_data[12..13].hex # todo
77
- byte8 = status_data[14..15].hex # todo
78
- byte9 = status_data[16..17].hex # todo
79
104
 
80
- status_items.push(NormalStatus) if status_items.empty?
105
+ byte8 = status_data[14..15].hex
106
+ status_item.etb = ((byte8 >> 1) & 0b00000111) + ((byte8 >> 2) & 0b00011000)
107
+
108
+ byte9 = status_data[16..17].hex # todo
81
109
 
82
- status_items
110
+ status_item
83
111
  end
84
112
  end
85
113
  end
@@ -1,3 +1,3 @@
1
1
  module StarEthernet
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/star_ethernet.rb CHANGED
@@ -3,6 +3,7 @@ require 'star_ethernet/printer'
3
3
  require 'star_ethernet/status'
4
4
  require 'star_ethernet/status_item'
5
5
  require 'star_ethernet/version'
6
+ require 'star_ethernet/exceptions'
6
7
 
7
8
  module StarEthernet
8
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: star_ethernet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shinsuke IMAI
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-19 00:00:00.000000000 Z
11
+ date: 2019-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,6 +77,7 @@ files:
77
77
  - bin/setup
78
78
  - lib/star_ethernet.rb
79
79
  - lib/star_ethernet/command.rb
80
+ - lib/star_ethernet/exceptions.rb
80
81
  - lib/star_ethernet/printer.rb
81
82
  - lib/star_ethernet/status.rb
82
83
  - lib/star_ethernet/status_item.rb