smartware 0.1.30 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,80 +1,98 @@
1
- require 'drb'
2
-
3
1
  module Smartware
4
2
  module Interface
3
+ class Printer < Interface
5
4
 
6
- module Printer
7
-
8
- @configured = false
9
- @status = {:error => ''}
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 self.configured?
26
- @configured
27
- end
10
+ def initialize(config)
11
+ super
28
12
 
29
- def self.error
30
- @status[:error]
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 self.model
34
- @status[:model]
35
- end
18
+ def test
19
+ print_text <<-EOS
20
+ Smartware: **#{Smartware::VERSION}**
36
21
 
37
- def self.version
38
- @status[:version]
39
- end
22
+ Driver: **#{@config["driver"]}**
23
+
24
+ Model: **#{@device.model}**
40
25
 
41
- def self.print(filepath)
42
- @queue << filepath unless filepath.nil?
26
+ Version: **#{@device.version}**
27
+
28
+ EOS
43
29
  end
44
30
 
45
- def self.test
46
- @queue << '/usr/share/cups/data/testprint'
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
- private
50
- def self.start_monitor!
51
- t = Thread.new do
52
- loop do
53
- begin
54
- if @queue.empty?
55
- @status[:error] = @device.error || ''
56
- @status[:model] = @device.model
57
- @status[:version] = @device.version
58
- else
59
- `lpr #{@queue[0]}`
60
- Smartware::Logging.logger.info "Printed #{@queue[0]}"
61
- @queue.shift
62
- sleep 5
63
- end
64
- rescue => e
65
- Smartware::Logging.logger.error e.message
66
- Smartware::Logging.logger.error e.backtrace.join("\n")
67
- end
68
- sleep 0.2
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
- self.configure!
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
@@ -1,87 +1,43 @@
1
- require 'thread'
2
- require 'yaml'
3
-
4
1
  module Smartware
5
- module Service
2
+ class Service
3
+ attr_reader :config
6
4
 
7
- def self.config
8
- @config
5
+ def initialize(config_file)
6
+ @config = YAML.load File.read(config_file)
7
+ @interfaces = []
9
8
  end
10
9
 
11
- def self.start(config_file)
12
- $stdout.sync = true
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
- @threads.map(&:join)
37
- rescue => e
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
- def self.stop
43
- @threads.map(&:kill)
44
- Logging.logger.info "Smartware shutdown at #{Time.now}"
45
- exit 0
46
- end
17
+ unless @config["connection_timeout"].nil?
18
+ Thread.new do
19
+ begin
20
+ monitor = ConnectionMonitor.new @config["connection_timeout"]
47
21
 
48
- end
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 self.untrack(pid)
57
- @tracked ||= {}
58
- @tracked.delete pid
32
+ def stop
33
+ @interfaces.each &:stop_service
59
34
  end
60
35
 
61
- def self.handle_sigchld(signal)
62
- pids = []
63
- @tracked ||= {}
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
 
@@ -1,3 +1,3 @@
1
1
  module Smartware
2
- VERSION = "0.1.30"
2
+ VERSION = "0.2"
3
3
  end
@@ -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.1.30
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-14 00:00:00.000000000 Z
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: trollop
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/tg24xx.rb
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