smartware 0.3.1 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/smartware-monitor +13 -0
- data/lib/smartware.rb +18 -0
- data/lib/smartware/clients/card_reader.rb +6 -25
- data/lib/smartware/clients/cash_acceptor.rb +7 -0
- data/lib/smartware/drivers/cash_acceptor/ccnet.rb +24 -1
- data/lib/smartware/drivers/cash_acceptor/dummy.rb +87 -4
- data/lib/smartware/interfaces/card_reader.rb +95 -44
- data/lib/smartware/interfaces/cash_acceptor.rb +34 -14
- data/lib/smartware/interfaces/interface.rb +31 -9
- data/lib/smartware/interfaces/modem.rb +10 -14
- data/lib/smartware/interfaces/printer.rb +4 -6
- data/lib/smartware/interfaces/watchdog.rb +5 -9
- data/lib/smartware/pub_sub_client.rb +69 -0
- data/lib/smartware/pub_sub_server.rb +49 -0
- data/lib/smartware/service.rb +23 -5
- data/lib/smartware/version.rb +1 -1
- metadata +6 -2
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'smartware'
|
6
|
+
|
7
|
+
EventMachine.run do
|
8
|
+
Smartware.subscribe do |key, *value|
|
9
|
+
puts "#{key}: #{value.map(&:inspect).join(", ")}"
|
10
|
+
end
|
11
|
+
|
12
|
+
puts "Receiving Smartware events."
|
13
|
+
end
|
data/lib/smartware.rb
CHANGED
@@ -6,6 +6,7 @@ require 'redcarpet'
|
|
6
6
|
require 'stringio'
|
7
7
|
require 'eventmachine'
|
8
8
|
require 'serialport'
|
9
|
+
require 'json'
|
9
10
|
|
10
11
|
require 'smartkiosk/common'
|
11
12
|
|
@@ -25,6 +26,8 @@ require 'smartware/interfaces/printer'
|
|
25
26
|
require 'smartware/interfaces/watchdog'
|
26
27
|
require 'smartware/interfaces/card_reader'
|
27
28
|
require 'smartware/connection_monitor'
|
29
|
+
require 'smartware/pub_sub_server'
|
30
|
+
require 'smartware/pub_sub_client'
|
28
31
|
|
29
32
|
module Smartware
|
30
33
|
|
@@ -32,6 +35,10 @@ module Smartware
|
|
32
35
|
yield self
|
33
36
|
end
|
34
37
|
|
38
|
+
def self.card_reader
|
39
|
+
Smartware::Client::CardReader
|
40
|
+
end
|
41
|
+
|
35
42
|
def self.cash_acceptor
|
36
43
|
Smartware::Client::CashAcceptor
|
37
44
|
end
|
@@ -48,4 +55,15 @@ module Smartware
|
|
48
55
|
Smartware::Client::Watchdog
|
49
56
|
end
|
50
57
|
|
58
|
+
def self.subscribe(&block)
|
59
|
+
Smartware::PubSubClient.destroy_static_client
|
60
|
+
client = Smartware::PubSubClient.create_static_client
|
61
|
+
client.receiver = block
|
62
|
+
client.start
|
63
|
+
client
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.unsubscribe
|
67
|
+
Smartware::PubSubClient.destroy_static_client
|
68
|
+
end
|
51
69
|
end
|
@@ -8,13 +8,6 @@ module Smartware
|
|
8
8
|
DRb.start_service
|
9
9
|
@device = DRbObject.new_with_uri('druby://localhost:6004')
|
10
10
|
|
11
|
-
def self.open(limit_min = nil, limit_max = nil)
|
12
|
-
@device.open_session(limit_min, limit_max)
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.close
|
16
|
-
@device.close_session
|
17
|
-
end
|
18
11
|
|
19
12
|
def self.status
|
20
13
|
@device.status
|
@@ -32,28 +25,16 @@ module Smartware
|
|
32
25
|
@device.version
|
33
26
|
end
|
34
27
|
|
35
|
-
def self.
|
36
|
-
@device.
|
37
|
-
end
|
38
|
-
|
39
|
-
def self.start_accepting
|
40
|
-
@device.start_accepting
|
28
|
+
def self.open
|
29
|
+
@device.open
|
41
30
|
end
|
42
31
|
|
43
|
-
def self.
|
44
|
-
@device.
|
45
|
-
end
|
46
|
-
|
47
|
-
def self.eject
|
48
|
-
@device.eject
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.capture
|
52
|
-
@device.capture
|
32
|
+
def self.close
|
33
|
+
@device.close
|
53
34
|
end
|
54
35
|
|
55
|
-
def self.
|
56
|
-
@device.
|
36
|
+
def self.card_inserted
|
37
|
+
@device.card_inserted
|
57
38
|
end
|
58
39
|
end
|
59
40
|
end
|
@@ -66,7 +66,7 @@ module Smartware
|
|
66
66
|
|
67
67
|
attr_reader :bill_types
|
68
68
|
|
69
|
-
attr_accessor :escrow, :stacked, :returned, :status
|
69
|
+
attr_accessor :open, :closed, :escrow, :stacked, :returned, :status
|
70
70
|
attr_accessor :enabled_types
|
71
71
|
|
72
72
|
def initialize(config)
|
@@ -76,6 +76,8 @@ module Smartware
|
|
76
76
|
@connection = EventMachine.attach @io, CCNETConnection
|
77
77
|
@connection.address = 3
|
78
78
|
|
79
|
+
@open = nil
|
80
|
+
@closed = nil
|
79
81
|
@escrow = nil
|
80
82
|
@stacked = nil
|
81
83
|
@returned = nil
|
@@ -188,6 +190,13 @@ module Smartware
|
|
188
190
|
when IDLING
|
189
191
|
if @enabled_types == 0
|
190
192
|
@connection.command(ENABLE_BILL_TYPES, "\x00" * 6) {}
|
193
|
+
|
194
|
+
begin
|
195
|
+
@closed.call
|
196
|
+
rescue => e
|
197
|
+
Logging.logger.error "Error in open: #{e}"
|
198
|
+
e.backtrace.each { |line| Logging.logger.error line }
|
199
|
+
end
|
191
200
|
end
|
192
201
|
|
193
202
|
when UNIT_DISABLED
|
@@ -219,6 +228,13 @@ module Smartware
|
|
219
228
|
]
|
220
229
|
|
221
230
|
@connection.command(ENABLE_BILL_TYPES, mask.pack("C*")) {}
|
231
|
+
|
232
|
+
begin
|
233
|
+
@open.call
|
234
|
+
rescue => e
|
235
|
+
Logging.logger.error "Error in open: #{e}"
|
236
|
+
e.backtrace.each { |line| Logging.logger.error line }
|
237
|
+
end
|
222
238
|
else
|
223
239
|
interval = 0.5
|
224
240
|
end
|
@@ -273,6 +289,13 @@ module Smartware
|
|
273
289
|
EventMachine.add_timer(interval, &method(:poll))
|
274
290
|
end
|
275
291
|
|
292
|
+
def insert_casette
|
293
|
+
|
294
|
+
end
|
295
|
+
|
296
|
+
def eject_casette
|
297
|
+
|
298
|
+
end
|
276
299
|
end
|
277
300
|
end
|
278
301
|
end
|
@@ -6,18 +6,28 @@ module Smartware
|
|
6
6
|
class Dummy
|
7
7
|
attr_reader :bill_types
|
8
8
|
|
9
|
-
attr_accessor :escrow, :stacked, :returned, :status
|
9
|
+
attr_accessor :open, :closed, :escrow, :stacked, :returned, :status
|
10
10
|
attr_accessor :enabled_types
|
11
11
|
|
12
12
|
def initialize(config)
|
13
|
-
@bill_types = []
|
13
|
+
@bill_types = [ 10, 50, 100, 500, 5000 ].map do |value|
|
14
|
+
Interface::CashAcceptor::BillType.new(value, 'RUS')
|
15
|
+
end
|
14
16
|
|
17
|
+
@open = nil
|
18
|
+
@closed = nil
|
15
19
|
@escrow = nil
|
16
20
|
@stacked = nil
|
17
21
|
@returned = nil
|
18
22
|
@status = nil
|
23
|
+
@escrow_bill = nil
|
24
|
+
@casette = true
|
19
25
|
|
20
|
-
@enabled_types =
|
26
|
+
@enabled_types = 0
|
27
|
+
|
28
|
+
EventMachine.add_periodic_timer 0.5, method(:poll)
|
29
|
+
|
30
|
+
@dummy_state = :power_up
|
21
31
|
end
|
22
32
|
|
23
33
|
def model
|
@@ -27,8 +37,81 @@ module Smartware
|
|
27
37
|
def version
|
28
38
|
"1.0"
|
29
39
|
end
|
30
|
-
end
|
31
40
|
|
41
|
+
def accepting?
|
42
|
+
false
|
43
|
+
end
|
44
|
+
|
45
|
+
def insert_casette
|
46
|
+
@casette = true
|
47
|
+
end
|
48
|
+
|
49
|
+
def eject_casette
|
50
|
+
@casette = false
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def poll
|
56
|
+
error = nil
|
57
|
+
|
58
|
+
case @dummy_state
|
59
|
+
when :power_up
|
60
|
+
@dummy_state = :initialize
|
61
|
+
|
62
|
+
when :initialize
|
63
|
+
@dummy_state = :disabled
|
64
|
+
|
65
|
+
when :disabled
|
66
|
+
if !@casette
|
67
|
+
@dummy_state = :no_casette
|
68
|
+
elsif @enabled_types != 0
|
69
|
+
@dummy_state = :idle
|
70
|
+
@open.call
|
71
|
+
end
|
72
|
+
|
73
|
+
when :idle
|
74
|
+
if @enabled_types == 0
|
75
|
+
@dummy_state = :disabled
|
76
|
+
@closed.call
|
77
|
+
elsif !@casette
|
78
|
+
@dummy_state = :no_casette
|
79
|
+
else
|
80
|
+
bill, index = @bill_types.each_with_index
|
81
|
+
.select { |(obj, index)| enabled_types & (1 << index) != 0 }
|
82
|
+
.sample
|
83
|
+
|
84
|
+
|
85
|
+
@dummy_state = :escrow
|
86
|
+
@escrow_bill = bill
|
87
|
+
end
|
88
|
+
|
89
|
+
when :escrow
|
90
|
+
if @escrow.call(@escrow_bill)
|
91
|
+
@dummy_state = :stacking
|
92
|
+
else
|
93
|
+
@dummy_state = :returning
|
94
|
+
end
|
95
|
+
|
96
|
+
when :stacking
|
97
|
+
@dummy_state = :idle
|
98
|
+
@stacked.call @escrow_bill
|
99
|
+
|
100
|
+
when :returning
|
101
|
+
@dummy_state = :idle
|
102
|
+
@return.call @escrow_bill
|
103
|
+
|
104
|
+
when :no_casette
|
105
|
+
error = Interface::CashAcceptor::DROP_CASETTE_OUT_OF_POSITION
|
106
|
+
|
107
|
+
if @casette
|
108
|
+
@dummy_state = :disabled
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
@status.call error
|
113
|
+
end
|
114
|
+
end
|
32
115
|
end
|
33
116
|
end
|
34
117
|
end
|
@@ -18,65 +18,116 @@ module Smartware
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
def initialize(config)
|
21
|
+
def initialize(config, service)
|
22
22
|
super
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
update_status :model, @device.model
|
25
|
+
update_status :version, @device.version
|
26
|
+
update_status :card_inserted, false, nil, nil, nil
|
27
|
+
|
28
|
+
@state = :inactive
|
29
|
+
@open = false
|
30
|
+
@mutex = Mutex.new
|
31
|
+
@cvar = ConditionVariable.new
|
32
|
+
|
33
|
+
schedule_status
|
26
34
|
end
|
27
35
|
|
28
|
-
def
|
29
|
-
@
|
30
|
-
|
31
|
-
|
32
|
-
|
36
|
+
def open
|
37
|
+
@mutex.synchronize do
|
38
|
+
@open = true
|
39
|
+
@cvar.wait(@mutex)
|
40
|
+
end
|
33
41
|
end
|
34
42
|
|
35
|
-
def
|
36
|
-
@
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
@status[:error] = e.code
|
41
|
-
false
|
43
|
+
def close
|
44
|
+
@mutex.synchronize do
|
45
|
+
@open = false
|
46
|
+
@cvar.wait(@mutex)
|
47
|
+
end
|
42
48
|
end
|
43
49
|
|
44
|
-
def
|
45
|
-
@
|
46
|
-
@status[:error] = nil
|
47
|
-
true
|
48
|
-
rescue CardReaderError => e
|
49
|
-
@status[:error] = e.code
|
50
|
-
false
|
50
|
+
def card_inserted
|
51
|
+
@status[:card_inserted]
|
51
52
|
end
|
52
53
|
|
53
|
-
|
54
|
-
@device.eject
|
54
|
+
private
|
55
55
|
|
56
|
-
|
56
|
+
def poll
|
57
|
+
open = nil
|
58
|
+
@mutex.synchronize do
|
59
|
+
open = @open
|
60
|
+
@cvar.broadcast
|
61
|
+
end
|
57
62
|
|
58
|
-
|
59
|
-
|
60
|
-
rescue CardReaderError => e
|
61
|
-
@status[:error] = e.code
|
62
|
-
false
|
63
|
-
end
|
63
|
+
begin
|
64
|
+
status = @device.status
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
66
|
+
case @state
|
67
|
+
when :failure
|
68
|
+
if status == :ready
|
69
|
+
@state = :inactive
|
70
|
+
update_status :error, nil
|
71
|
+
end
|
72
|
+
|
73
|
+
when :inactive
|
74
|
+
if open
|
75
|
+
@device.accepting = true
|
76
|
+
@state = :waiting_card
|
77
|
+
end
|
78
|
+
|
79
|
+
when :waiting_card
|
80
|
+
if !open
|
81
|
+
@device.accepting = false
|
82
|
+
@device.eject rescue nil
|
83
|
+
@state = :waiting_eject
|
73
84
|
|
74
|
-
|
75
|
-
|
85
|
+
elsif status == :card_inserted
|
86
|
+
@state = :card_inside
|
87
|
+
@device.accepting = false
|
76
88
|
|
77
|
-
|
78
|
-
|
79
|
-
|
89
|
+
track1, track2, = @device.read_magstrip
|
90
|
+
if track1.nil? || track2.nil?
|
91
|
+
@device.eject rescue nil
|
92
|
+
@state = :waiting_eject
|
93
|
+
else
|
94
|
+
update_status :card_inserted, true, false, track1, track2
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
when :card_inside
|
99
|
+
if !open || status != :card_inserted
|
100
|
+
update_status :card_inserted, false, nil, nil, nil
|
101
|
+
@device.eject rescue nil
|
102
|
+
@state = :waiting_eject
|
103
|
+
end
|
104
|
+
|
105
|
+
when :waiting_eject
|
106
|
+
if status == :ready || status == :not_ready
|
107
|
+
if open
|
108
|
+
@state = :waiting_card
|
109
|
+
@device.accepting = true
|
110
|
+
else
|
111
|
+
@state = :inactive
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
rescue CardReaderError => e
|
116
|
+
Logging.logger.error "Card reader error: #{e}"
|
117
|
+
|
118
|
+
@device.eject rescue nil
|
119
|
+
@device.accepting = false rescue nil
|
120
|
+
|
121
|
+
@state = :failure
|
122
|
+
update_status :error, e.code
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def schedule_status(ret = nil)
|
127
|
+
EventMachine.add_timer(0.1) do
|
128
|
+
EventMachine.defer(method(:poll),
|
129
|
+
method(:schedule_status))
|
130
|
+
end
|
80
131
|
end
|
81
132
|
end
|
82
133
|
end
|
@@ -33,9 +33,11 @@ module Smartware
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def initialize(config)
|
36
|
+
def initialize(config, service)
|
37
37
|
super
|
38
38
|
|
39
|
+
@device.open = method :device_open
|
40
|
+
@device.closed = method :device_closed
|
39
41
|
@device.escrow = method :escrow
|
40
42
|
@device.stacked = method :stacked
|
41
43
|
@device.returned = method :returned
|
@@ -45,9 +47,8 @@ module Smartware
|
|
45
47
|
@banknotes = {}
|
46
48
|
@banknotes.default = 0
|
47
49
|
|
48
|
-
update_status
|
49
|
-
|
50
|
-
end
|
50
|
+
update_status :casette, false
|
51
|
+
update_status :accepting, false
|
51
52
|
|
52
53
|
Smartware::Logging.logger.info "Cash acceptor monitor started"
|
53
54
|
end
|
@@ -90,7 +91,7 @@ module Smartware
|
|
90
91
|
end
|
91
92
|
|
92
93
|
def banknotes
|
93
|
-
|
94
|
+
@banknotes
|
94
95
|
end
|
95
96
|
|
96
97
|
def cashsum
|
@@ -99,6 +100,14 @@ module Smartware
|
|
99
100
|
end
|
100
101
|
end
|
101
102
|
|
103
|
+
def insert_casette
|
104
|
+
@device.insert_casette
|
105
|
+
end
|
106
|
+
|
107
|
+
def eject_casette
|
108
|
+
@device.eject_casette
|
109
|
+
end
|
110
|
+
|
102
111
|
private
|
103
112
|
|
104
113
|
def limit_satisfied?(sum)
|
@@ -112,11 +121,11 @@ module Smartware
|
|
112
121
|
def stacked(banknote)
|
113
122
|
value = banknote.value
|
114
123
|
|
115
|
-
|
116
|
-
@banknotes[value] += 1
|
117
|
-
end
|
124
|
+
@banknotes[value] += 1
|
118
125
|
|
119
126
|
Smartware::Logging.logger.debug "cash acceptor: bill stacked, #{value}"
|
127
|
+
|
128
|
+
publish_event :stacked, value
|
120
129
|
end
|
121
130
|
|
122
131
|
def returned(banknote)
|
@@ -126,12 +135,23 @@ module Smartware
|
|
126
135
|
end
|
127
136
|
|
128
137
|
def status_changed(error)
|
129
|
-
update_status
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
138
|
+
update_status :error, error
|
139
|
+
update_status :model, @device.model
|
140
|
+
update_status :version, @device.version
|
141
|
+
update_status :casette, error != DROP_CASETTE_OUT_OF_POSITION
|
142
|
+
update_status :cashsum, cashsum
|
143
|
+
end
|
144
|
+
|
145
|
+
def device_open
|
146
|
+
Smartware::Logging.logger.debug "Device acknowleged open"
|
147
|
+
|
148
|
+
update_status :accepting, true
|
149
|
+
end
|
150
|
+
|
151
|
+
def device_closed
|
152
|
+
Smartware::Logging.logger.debug "Device acknowleged close"
|
153
|
+
|
154
|
+
update_status :accepting, false
|
135
155
|
end
|
136
156
|
end
|
137
157
|
end
|
@@ -3,34 +3,47 @@ module Smartware
|
|
3
3
|
class Interface
|
4
4
|
attr_reader :config
|
5
5
|
|
6
|
-
def initialize(config)
|
6
|
+
def initialize(config, service)
|
7
7
|
@config = config
|
8
|
+
@service = service
|
9
|
+
|
8
10
|
@status_mutex = Mutex.new
|
9
11
|
@status = {
|
10
|
-
|
11
|
-
|
12
|
+
error: [ nil ],
|
13
|
+
model: [ '' ],
|
14
|
+
version: [ '' ]
|
12
15
|
}
|
13
16
|
|
14
17
|
iface = @config["name"]
|
15
18
|
driver = @config["driver"]
|
16
19
|
|
17
|
-
|
20
|
+
@iface_id = iface.underscore
|
21
|
+
|
22
|
+
require "smartware/drivers/#{@iface_id}/#{driver.underscore}"
|
18
23
|
|
19
24
|
@device = Smartware::Driver.const_get(iface.to_s)
|
20
25
|
.const_get(driver.to_s)
|
21
26
|
.new(config)
|
22
27
|
end
|
23
28
|
|
29
|
+
def repush_events(connection)
|
30
|
+
@status_mutex.synchronize do
|
31
|
+
@status.each do |key, value|
|
32
|
+
connection.publish_event "#{@iface_id}.#{key}", *value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
24
37
|
def error
|
25
|
-
self.status[:error]
|
38
|
+
self.status[:error][0]
|
26
39
|
end
|
27
40
|
|
28
41
|
def model
|
29
|
-
self.status[:model]
|
42
|
+
self.status[:model][0]
|
30
43
|
end
|
31
44
|
|
32
45
|
def version
|
33
|
-
self.status[:version]
|
46
|
+
self.status[:version][0]
|
34
47
|
end
|
35
48
|
|
36
49
|
def status
|
@@ -39,8 +52,17 @@ module Smartware
|
|
39
52
|
|
40
53
|
protected
|
41
54
|
|
42
|
-
def update_status(
|
43
|
-
@status_mutex.synchronize
|
55
|
+
def update_status(key, *value)
|
56
|
+
@status_mutex.synchronize do
|
57
|
+
if @status[key] != value
|
58
|
+
@status[key] = value
|
59
|
+
publish_event key, *value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def publish_event(key, *data)
|
65
|
+
@service.publish_event "#{@iface_id}.#{key}", *data
|
44
66
|
end
|
45
67
|
end
|
46
68
|
end
|
@@ -3,24 +3,22 @@ module Smartware
|
|
3
3
|
class Modem < Interface
|
4
4
|
MODEM_NOT_AVAILABLE = 1
|
5
5
|
|
6
|
-
def initialize(config)
|
6
|
+
def initialize(config, service)
|
7
7
|
super
|
8
8
|
|
9
|
-
update_status
|
10
|
-
|
11
|
-
@status[:signal_level] = ''
|
12
|
-
end
|
9
|
+
update_status :balance, ''
|
10
|
+
update_status :signal_level, ''
|
13
11
|
|
14
12
|
@session = Thread.new &method(:poll)
|
15
13
|
Smartware::Logging.logger.info 'Modem monitor started'
|
16
14
|
end
|
17
15
|
|
18
16
|
def balance
|
19
|
-
self.status[:balance]
|
17
|
+
self.status[:balance][0]
|
20
18
|
end
|
21
19
|
|
22
20
|
def signal_level
|
23
|
-
self.status[:signal_level]
|
21
|
+
self.status[:signal_level][0]
|
24
22
|
end
|
25
23
|
|
26
24
|
private
|
@@ -30,13 +28,11 @@ module Smartware
|
|
30
28
|
loop do
|
31
29
|
@device.tick
|
32
30
|
|
33
|
-
update_status
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
@status[:balance] = @device.balance
|
39
|
-
end
|
31
|
+
update_status :signal_level, @device.signal_level
|
32
|
+
update_status :model, @device.model
|
33
|
+
update_status :version, @device.version
|
34
|
+
update_status :error, @device.error
|
35
|
+
update_status :balance, @device.balance
|
40
36
|
end
|
41
37
|
rescue => e
|
42
38
|
Smartware::Logging.logger.error e.message
|
@@ -7,7 +7,7 @@ module Smartware
|
|
7
7
|
OUT_OF_PAPER = 3
|
8
8
|
PAPER_NEAR_END = 1000
|
9
9
|
|
10
|
-
def initialize(config)
|
10
|
+
def initialize(config, service)
|
11
11
|
super
|
12
12
|
|
13
13
|
@printer_mutex = Mutex.new
|
@@ -89,11 +89,9 @@ EOS
|
|
89
89
|
|
90
90
|
def query_printer
|
91
91
|
@device.query
|
92
|
-
update_status
|
93
|
-
|
94
|
-
|
95
|
-
@status[:version] = @device.version
|
96
|
-
end
|
92
|
+
update_status :error, @device.error
|
93
|
+
update_status :model, @device.model
|
94
|
+
update_status :version, @device.version
|
97
95
|
end
|
98
96
|
|
99
97
|
def poll
|
@@ -4,22 +4,18 @@ module Smartware
|
|
4
4
|
|
5
5
|
WATCHDOG_NOT_AVAILBLE = 1
|
6
6
|
|
7
|
-
def initialize(config)
|
7
|
+
def initialize(config, service)
|
8
8
|
super
|
9
9
|
|
10
|
-
update_status
|
11
|
-
|
12
|
-
|
13
|
-
@status[:version] = @device.version
|
14
|
-
end
|
10
|
+
update_status :error, nil
|
11
|
+
update_status :model, @device.model
|
12
|
+
update_status :version, @device.version
|
15
13
|
end
|
16
14
|
|
17
15
|
def reboot_modem
|
18
16
|
@device.reboot_modem
|
19
17
|
|
20
|
-
update_status
|
21
|
-
@status[:error] = @device.error
|
22
|
-
end
|
18
|
+
update_status :error, @device.error
|
23
19
|
end
|
24
20
|
end
|
25
21
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "em/protocols/line_protocol"
|
2
|
+
|
3
|
+
module Smartware
|
4
|
+
class PubSubClient
|
5
|
+
class PubSubHandler < EventMachine::Connection
|
6
|
+
include EventMachine::Protocols::LineProtocol
|
7
|
+
|
8
|
+
def initialize(client)
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def unbind
|
13
|
+
@client.broken
|
14
|
+
end
|
15
|
+
|
16
|
+
def receive_line(line)
|
17
|
+
data = JSON.load line
|
18
|
+
|
19
|
+
@client.deliver data["key"], *data["args"]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
@@static_client = nil
|
24
|
+
|
25
|
+
def self.create_static_client
|
26
|
+
@@static_client = PubSubClient.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.destroy_static_client
|
30
|
+
return if @@static_client.nil?
|
31
|
+
@@static_client.stop
|
32
|
+
@@static_client = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_accessor :receiver
|
36
|
+
|
37
|
+
def initialize(host = "localhost", port = 6100)
|
38
|
+
@host = host
|
39
|
+
@port = port
|
40
|
+
@reconnect_timer = nil
|
41
|
+
@connection = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def start
|
45
|
+
attempt if @connection.nil? && @reconnect_timer.nil?
|
46
|
+
end
|
47
|
+
|
48
|
+
def stop
|
49
|
+
@connection.close_connection unless @connection.nil?
|
50
|
+
EventMachine.cancel_timer @reconnect_timer unless @reconnect_timer.nil?
|
51
|
+
end
|
52
|
+
|
53
|
+
def broken
|
54
|
+
@connection = nil
|
55
|
+
@reconnect_timer = EventMachine.add_timer 1, &method(:attempt)
|
56
|
+
end
|
57
|
+
|
58
|
+
def deliver(key, *args)
|
59
|
+
@receiver.call key, *args
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def attempt
|
65
|
+
@reconnect_timer = nil
|
66
|
+
@connection = EventMachine.connect @host, @port, PubSubHandler, self
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Smartware
|
2
|
+
class PubSubServer
|
3
|
+
class PubSubConnection < EventMachine::Connection
|
4
|
+
def initialize(server)
|
5
|
+
@server = server
|
6
|
+
end
|
7
|
+
|
8
|
+
def post_init
|
9
|
+
@server.add_connection self
|
10
|
+
end
|
11
|
+
|
12
|
+
def unbind
|
13
|
+
@server.remove_connection self
|
14
|
+
end
|
15
|
+
|
16
|
+
def receive_data(data)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def publish_event(key, *args)
|
21
|
+
send_data(JSON.dump({ key: key, args: args }) + "\r\n")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_accessor :repush
|
26
|
+
|
27
|
+
def initialize(host, port)
|
28
|
+
@repush = nil
|
29
|
+
@connections = Set.new
|
30
|
+
|
31
|
+
EventMachine.start_server host, port, PubSubConnection, self
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_connection(connection)
|
35
|
+
@connections.add connection
|
36
|
+
@repush.call connection
|
37
|
+
end
|
38
|
+
|
39
|
+
def remove_connection(connection)
|
40
|
+
@connections.delete connection
|
41
|
+
end
|
42
|
+
|
43
|
+
def publish_event(key, *args)
|
44
|
+
@connections.each do |connection|
|
45
|
+
connection.publish_event key, *args
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/smartware/service.rb
CHANGED
@@ -5,15 +5,31 @@ module Smartware
|
|
5
5
|
def initialize(config_file)
|
6
6
|
@config = YAML.load File.read(config_file)
|
7
7
|
@interfaces = []
|
8
|
+
@devices = []
|
8
9
|
end
|
9
10
|
|
10
11
|
def start
|
11
12
|
EventMachine.epoll
|
12
13
|
EventMachine.run do
|
13
|
-
@
|
14
|
-
|
14
|
+
@event_channel = EventMachine::Channel.new
|
15
|
+
@event_channel.subscribe do |(key, args)|
|
16
|
+
@pubsub.publish_event key, *args
|
17
|
+
end
|
18
|
+
|
19
|
+
@pubsub = PubSubServer.new "localhost", (@config["pubsub_port"] || 6100)
|
20
|
+
@pubsub.repush = ->(connection) do
|
21
|
+
@devices.each do |device|
|
22
|
+
device.repush_events connection
|
23
|
+
end
|
24
|
+
end
|
15
25
|
|
16
|
-
|
26
|
+
@config["interfaces"].each do |config|
|
27
|
+
interface = Smartware::Interface.const_get(config['name']).new config, self
|
28
|
+
@devices << interface
|
29
|
+
|
30
|
+
if config.include? "uri"
|
31
|
+
@interfaces << DRb::DRbServer.new(config["uri"], interface)
|
32
|
+
end
|
17
33
|
end
|
18
34
|
|
19
35
|
unless @config["connection_timeout"].nil?
|
@@ -29,13 +45,16 @@ module Smartware
|
|
29
45
|
end
|
30
46
|
end
|
31
47
|
end
|
48
|
+
end
|
32
49
|
|
50
|
+
def publish_event(key, *args)
|
51
|
+
@event_channel.push [key, args]
|
33
52
|
end
|
34
53
|
|
35
54
|
def stop
|
36
55
|
@interfaces.each &:stop_service
|
37
56
|
|
38
|
-
EventMachine.
|
57
|
+
EventMachine.stop
|
39
58
|
end
|
40
59
|
|
41
60
|
def join
|
@@ -45,4 +64,3 @@ module Smartware
|
|
45
64
|
end
|
46
65
|
end
|
47
66
|
end
|
48
|
-
|
data/lib/smartware/version.rb
CHANGED
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.3
|
4
|
+
version: 0.4.3
|
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-02-01 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: smartkiosk-common
|
@@ -146,6 +146,7 @@ email:
|
|
146
146
|
- boris@roundlake.ru
|
147
147
|
executables:
|
148
148
|
- smartware
|
149
|
+
- smartware-monitor
|
149
150
|
- smartware-ppp-helper
|
150
151
|
extensions: []
|
151
152
|
extra_rdoc_files: []
|
@@ -156,6 +157,7 @@ files:
|
|
156
157
|
- README.md
|
157
158
|
- Rakefile
|
158
159
|
- bin/smartware
|
160
|
+
- bin/smartware-monitor
|
159
161
|
- bin/smartware-ppp-helper
|
160
162
|
- config/smartware.yml.sample
|
161
163
|
- lib/smartware.rb
|
@@ -186,6 +188,8 @@ files:
|
|
186
188
|
- lib/smartware/interfaces/watchdog.rb
|
187
189
|
- lib/smartware/logging.rb
|
188
190
|
- lib/smartware/process_manager.rb
|
191
|
+
- lib/smartware/pub_sub_client.rb
|
192
|
+
- lib/smartware/pub_sub_server.rb
|
189
193
|
- lib/smartware/service.rb
|
190
194
|
- lib/smartware/version.rb
|
191
195
|
- smartware.gemspec
|