smartware 0.1.30 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/smartware +10 -3
- data/config/smartware.yml.sample +10 -0
- data/lib/smartware.rb +19 -0
- data/lib/smartware/clients/printer.rb +8 -2
- data/lib/smartware/clients/watchdog.rb +36 -0
- data/lib/smartware/connection_monitor.rb +25 -0
- data/lib/smartware/drivers/cash_acceptor/ccnet.rb +28 -24
- data/lib/smartware/drivers/cash_acceptor/dummy.rb +3 -3
- data/lib/smartware/drivers/modem/dummy.rb +2 -2
- data/lib/smartware/drivers/modem/standard.rb +3 -3
- data/lib/smartware/drivers/printer/dummy.rb +16 -5
- data/lib/smartware/drivers/printer/esc_pos.rb +272 -0
- data/lib/smartware/drivers/watchdog/dummy.rb +29 -0
- data/lib/smartware/drivers/watchdog/watchdog_daemon.rb +40 -0
- data/lib/smartware/interfaces/cash_acceptor.rb +154 -109
- data/lib/smartware/interfaces/interface.rb +47 -0
- data/lib/smartware/interfaces/modem.rb +29 -58
- data/lib/smartware/interfaces/printer.rb +78 -60
- data/lib/smartware/interfaces/watchdog.rb +26 -0
- data/lib/smartware/process_manager.rb +40 -0
- data/lib/smartware/service.rb +26 -70
- data/lib/smartware/version.rb +1 -1
- data/smartware.gemspec +3 -1
- metadata +44 -4
- data/lib/smartware/drivers/printer/tg24xx.rb +0 -64
@@ -1,80 +1,98 @@
|
|
1
|
-
require 'drb'
|
2
|
-
|
3
1
|
module Smartware
|
4
2
|
module Interface
|
3
|
+
class Printer < Interface
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@queue = []
|
11
|
-
|
12
|
-
def self.configure!(port=nil, driver=nil)
|
13
|
-
@device = Smartware::Driver::Printer.const_get(
|
14
|
-
Smartware::Service.config['printer_driver']).new(
|
15
|
-
Smartware::Service.config['printer_port'])
|
16
|
-
@session.kill if @session and @session.alive?
|
17
|
-
@session = self.start_monitor!
|
18
|
-
Smartware::Logging.logger.info 'Printer monitor started'
|
19
|
-
@configured = true
|
20
|
-
rescue => e
|
21
|
-
@configured = false
|
22
|
-
Smartware::Logging.logger.error "Printer initialization failed: #{e.to_s}"
|
23
|
-
end
|
5
|
+
HARDWARE_ERROR = 1
|
6
|
+
COMMUNICATION_ERROR = 2
|
7
|
+
OUT_OF_PAPER = 3
|
8
|
+
PAPER_NEAR_END = 1000
|
24
9
|
|
25
|
-
def
|
26
|
-
|
27
|
-
end
|
10
|
+
def initialize(config)
|
11
|
+
super
|
28
12
|
|
29
|
-
|
30
|
-
|
13
|
+
@printer_mutex = Mutex.new
|
14
|
+
Thread.new &method(:poll)
|
15
|
+
@markdown = Redcarpet::Markdown.new(@device.new_render)
|
31
16
|
end
|
32
17
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
18
|
+
def test
|
19
|
+
print_text <<-EOS
|
20
|
+
Smartware: **#{Smartware::VERSION}**
|
36
21
|
|
37
|
-
|
38
|
-
|
39
|
-
|
22
|
+
Driver: **#{@config["driver"]}**
|
23
|
+
|
24
|
+
Model: **#{@device.model}**
|
40
25
|
|
41
|
-
|
42
|
-
|
26
|
+
Version: **#{@device.version}**
|
27
|
+
|
28
|
+
EOS
|
43
29
|
end
|
44
30
|
|
45
|
-
def
|
46
|
-
|
31
|
+
def print(file, max_time = 30)
|
32
|
+
File.open(file, "r") do |io|
|
33
|
+
print_text io.read, max_time
|
34
|
+
end
|
47
35
|
end
|
48
36
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
37
|
+
def print_text(text, max_time = 30)
|
38
|
+
Smartware::Logging.logger.info "Started printing"
|
39
|
+
|
40
|
+
started = Time.now
|
41
|
+
|
42
|
+
@printer_mutex.synchronize do
|
43
|
+
query_printer
|
44
|
+
|
45
|
+
loop do
|
46
|
+
case @device.status
|
47
|
+
when :ready, :warning
|
48
|
+
@device.print @markdown.render(text)
|
49
|
+
|
50
|
+
query_printer
|
51
|
+
|
52
|
+
break
|
53
|
+
|
54
|
+
when :transient_error
|
55
|
+
Smartware::Logging.logger.warn "Transient error #{self.error}."
|
56
|
+
|
57
|
+
now = Time.now
|
58
|
+
break if now - started > max_time
|
59
|
+
|
60
|
+
sleep 0.5
|
61
|
+
when :error
|
62
|
+
Smartware::Logging.logger.warn "Error #{self.error}."
|
63
|
+
|
64
|
+
break
|
69
65
|
end
|
70
66
|
end
|
71
67
|
end
|
72
68
|
|
73
|
-
|
69
|
+
Smartware::Logging.logger.info "Completed"
|
70
|
+
|
71
|
+
self.error.nil? || self.error < 1000
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def query_printer
|
77
|
+
@device.query
|
78
|
+
update_status do
|
79
|
+
@status[:error] = @device.error
|
80
|
+
@status[:model] = @device.model
|
81
|
+
@status[:version] = @device.version
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def poll
|
86
|
+
loop do
|
87
|
+
begin
|
88
|
+
@printer_mutex.synchronize &method(:query_printer)
|
89
|
+
sleep 1
|
90
|
+
rescue => e
|
91
|
+
Smartware::Logging.logger.error e.message
|
92
|
+
Smartware::Logging.logger.error e.backtrace.join("\n")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
74
96
|
end
|
75
97
|
end
|
76
98
|
end
|
77
|
-
|
78
|
-
DRb.start_service('druby://localhost:6005', Smartware::Interface::Printer)
|
79
|
-
DRb.thread.join
|
80
|
-
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Smartware
|
2
|
+
module Interface
|
3
|
+
class Watchdog < Interface
|
4
|
+
|
5
|
+
WATCHDOG_NOT_AVAILBLE = 1
|
6
|
+
|
7
|
+
def initialize(config)
|
8
|
+
super
|
9
|
+
|
10
|
+
update_status do
|
11
|
+
@status[:error] = nil
|
12
|
+
@status[:model] = @device.model
|
13
|
+
@status[:version] = @device.version
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def reboot_modem
|
18
|
+
@device.reboot_modem
|
19
|
+
|
20
|
+
update_status do
|
21
|
+
@status[:error] = @device.error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Smartware
|
2
|
+
module ProcessManager
|
3
|
+
def self.track(pid, method)
|
4
|
+
@tracked ||= {}
|
5
|
+
@tracked[pid] = method
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.untrack(pid)
|
9
|
+
@tracked ||= {}
|
10
|
+
@tracked.delete pid
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.handle_sigchld(signal)
|
14
|
+
pids = []
|
15
|
+
@tracked ||= {}
|
16
|
+
|
17
|
+
begin
|
18
|
+
loop do
|
19
|
+
pid = Process.wait(-1, Process::WNOHANG)
|
20
|
+
break if pid.nil?
|
21
|
+
|
22
|
+
pids << pid
|
23
|
+
end
|
24
|
+
rescue
|
25
|
+
end
|
26
|
+
|
27
|
+
pids.each do |pid|
|
28
|
+
tracker = @tracked[pid]
|
29
|
+
unless tracker.nil?
|
30
|
+
@tracked.delete pid
|
31
|
+
tracker.call pid
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.init
|
37
|
+
trap 'CHLD', ProcessManager.method(:handle_sigchld)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/smartware/service.rb
CHANGED
@@ -1,87 +1,43 @@
|
|
1
|
-
require 'thread'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
1
|
module Smartware
|
5
|
-
|
2
|
+
class Service
|
3
|
+
attr_reader :config
|
6
4
|
|
7
|
-
def
|
8
|
-
@config
|
5
|
+
def initialize(config_file)
|
6
|
+
@config = YAML.load File.read(config_file)
|
7
|
+
@interfaces = []
|
9
8
|
end
|
10
9
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
@config = YAML.load File.read(File.expand_path(config_file))
|
15
|
-
|
16
|
-
Smartware::Logging.logger.info "Smartware started at #{Time.now}"
|
17
|
-
|
18
|
-
@threads = %w(modem
|
19
|
-
cash_acceptor
|
20
|
-
printer
|
21
|
-
).inject([]) do |arr, iface|
|
22
|
-
arr << Thread.new do
|
23
|
-
begin
|
24
|
-
driver = Service.config["#{iface}_driver"].downcase
|
25
|
-
|
26
|
-
require "smartware/drivers/#{iface}/#{driver}"
|
27
|
-
require "smartware/interfaces/#{iface}"
|
28
|
-
rescue => e
|
29
|
-
Logging.logger.fatal "During startup of #{iface}:"
|
30
|
-
Logging.logger.fatal e.message
|
31
|
-
Logging.logger.fatal e.backtrace.join("\n")
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
10
|
+
def start
|
11
|
+
@interfaces = @config["interfaces"].map do |config|
|
12
|
+
interface = Smartware::Interface.const_get(config['name']).new config
|
35
13
|
|
36
|
-
|
37
|
-
|
38
|
-
Logging.logger.fatal e.message
|
39
|
-
Logging.logger.fatal e.backtrace.join("\n")
|
40
|
-
end
|
14
|
+
DRb::DRbServer.new config["uri"], interface
|
15
|
+
end
|
41
16
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
17
|
+
unless @config["connection_timeout"].nil?
|
18
|
+
Thread.new do
|
19
|
+
begin
|
20
|
+
monitor = ConnectionMonitor.new @config["connection_timeout"]
|
47
21
|
|
48
|
-
|
22
|
+
monitor.run
|
23
|
+
rescue => e
|
24
|
+
Smartware::Logging.logger.error "Exception in connection monitor thread: #{e}"
|
25
|
+
e.backtrace.each { |line| Smartware::Logging.logger.error line }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
49
29
|
|
50
|
-
module ProcessManager
|
51
|
-
def self.track(pid, method)
|
52
|
-
@tracked ||= {}
|
53
|
-
@tracked[pid] = method
|
54
30
|
end
|
55
31
|
|
56
|
-
def
|
57
|
-
@
|
58
|
-
@tracked.delete pid
|
32
|
+
def stop
|
33
|
+
@interfaces.each &:stop_service
|
59
34
|
end
|
60
35
|
|
61
|
-
def
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
begin
|
66
|
-
loop do
|
67
|
-
pid = Process.wait(-1, Process::WNOHANG)
|
68
|
-
break if pid.nil?
|
69
|
-
|
70
|
-
pids << pid
|
71
|
-
end
|
72
|
-
rescue
|
73
|
-
end
|
74
|
-
|
75
|
-
pids.each do |pid|
|
76
|
-
tracker = @tracked[pid]
|
77
|
-
unless tracker.nil?
|
78
|
-
@tracked.delete pid
|
79
|
-
tracker.call pid
|
80
|
-
end
|
36
|
+
def join
|
37
|
+
@interfaces.each do |server|
|
38
|
+
server.thread.join
|
81
39
|
end
|
82
40
|
end
|
83
41
|
end
|
84
|
-
|
85
|
-
trap 'CHLD', ProcessManager.method(:handle_sigchld)
|
86
42
|
end
|
87
43
|
|
data/lib/smartware/version.rb
CHANGED
data/smartware.gemspec
CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |gem|
|
|
19
19
|
|
20
20
|
gem.add_dependency 'smartkiosk-common', '>= 0.0.2'
|
21
21
|
gem.add_dependency 'serialport'
|
22
|
-
gem.add_dependency 'cmux'
|
22
|
+
gem.add_dependency 'cmux', '>= 0.0.2'
|
23
23
|
gem.add_dependency 'trollop'
|
24
|
+
gem.add_dependency 'activesupport'
|
25
|
+
gem.add_dependency 'redcarpet'
|
24
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smartware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-01-
|
13
|
+
date: 2013-01-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: smartkiosk-common
|
@@ -46,6 +46,22 @@ dependencies:
|
|
46
46
|
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: cmux
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.0.2
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.0.2
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: trollop
|
49
65
|
requirement: !ruby/object:Gem::Requirement
|
50
66
|
none: false
|
51
67
|
requirements:
|
@@ -61,7 +77,23 @@ dependencies:
|
|
61
77
|
- !ruby/object:Gem::Version
|
62
78
|
version: '0'
|
63
79
|
- !ruby/object:Gem::Dependency
|
64
|
-
name:
|
80
|
+
name: activesupport
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
type: :runtime
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: redcarpet
|
65
97
|
requirement: !ruby/object:Gem::Requirement
|
66
98
|
none: false
|
67
99
|
requirements:
|
@@ -93,20 +125,28 @@ files:
|
|
93
125
|
- Rakefile
|
94
126
|
- bin/smartware
|
95
127
|
- bin/smartware-ppp-helper
|
128
|
+
- config/smartware.yml.sample
|
96
129
|
- lib/smartware.rb
|
97
130
|
- lib/smartware/clients/cash_acceptor.rb
|
98
131
|
- lib/smartware/clients/modem.rb
|
99
132
|
- lib/smartware/clients/printer.rb
|
133
|
+
- lib/smartware/clients/watchdog.rb
|
134
|
+
- lib/smartware/connection_monitor.rb
|
100
135
|
- lib/smartware/drivers/cash_acceptor/ccnet.rb
|
101
136
|
- lib/smartware/drivers/cash_acceptor/dummy.rb
|
102
137
|
- lib/smartware/drivers/modem/dummy.rb
|
103
138
|
- lib/smartware/drivers/modem/standard.rb
|
104
139
|
- lib/smartware/drivers/printer/dummy.rb
|
105
|
-
- lib/smartware/drivers/printer/
|
140
|
+
- lib/smartware/drivers/printer/esc_pos.rb
|
141
|
+
- lib/smartware/drivers/watchdog/dummy.rb
|
142
|
+
- lib/smartware/drivers/watchdog/watchdog_daemon.rb
|
106
143
|
- lib/smartware/interfaces/cash_acceptor.rb
|
144
|
+
- lib/smartware/interfaces/interface.rb
|
107
145
|
- lib/smartware/interfaces/modem.rb
|
108
146
|
- lib/smartware/interfaces/printer.rb
|
147
|
+
- lib/smartware/interfaces/watchdog.rb
|
109
148
|
- lib/smartware/logging.rb
|
149
|
+
- lib/smartware/process_manager.rb
|
110
150
|
- lib/smartware/service.rb
|
111
151
|
- lib/smartware/version.rb
|
112
152
|
- smartware.gemspec
|