tamashii-agent 0.3.0 → 0.3.1
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 +4 -4
- data/exe/tamashii-agent +7 -5
- data/lib/tamashii/agent/buzzer.rb +11 -5
- data/lib/tamashii/agent/card_reader.rb +21 -21
- data/lib/tamashii/agent/common.rb +25 -0
- data/lib/tamashii/agent/component.rb +38 -3
- data/lib/tamashii/agent/config.rb +13 -2
- data/lib/tamashii/agent/device/buzzer/base.rb +26 -0
- data/lib/tamashii/agent/device/buzzer/dummy.rb +32 -0
- data/lib/tamashii/agent/device/buzzer/gpio_buzzer.rb +89 -0
- data/lib/tamashii/agent/device/card_reader/base.rb +16 -0
- data/lib/tamashii/agent/device/card_reader/dummy.rb +37 -0
- data/lib/tamashii/agent/device/card_reader/mfrc522_spi.rb +43 -0
- data/lib/tamashii/agent/device/card_reader/pn532_uart.rb +52 -0
- data/lib/tamashii/agent/device/device_base.rb +20 -0
- data/lib/tamashii/agent/device/lcd/base.rb +29 -0
- data/lib/tamashii/agent/device/lcd/dummy.rb +30 -0
- data/lib/tamashii/agent/device/lcd/lcm1602_i2c.rb +133 -0
- data/lib/tamashii/agent/handler/base.rb +2 -2
- data/lib/tamashii/agent/handler/lcd.rb +1 -1
- data/lib/tamashii/agent/handler/remote_response.rb +1 -1
- data/lib/tamashii/agent/lcd.rb +14 -16
- data/lib/tamashii/agent/master.rb +20 -22
- data/lib/tamashii/agent/{connection.rb → networking.rb} +5 -6
- data/lib/tamashii/agent/{connection → networking}/request_observer.rb +5 -5
- data/lib/tamashii/agent/version.rb +1 -1
- data/tamashii-agent.gemspec +4 -3
- metadata +32 -16
- data/lib/tamashii/agent/adapter/base.rb +0 -28
- data/lib/tamashii/agent/adapter/buzzer.rb +0 -21
- data/lib/tamashii/agent/adapter/card_reader.rb +0 -20
- data/lib/tamashii/agent/adapter/lcd.rb +0 -22
- data/lib/tamashii/agent/device/fake_buzzer.rb +0 -30
- data/lib/tamashii/agent/device/fake_card_reader.rb +0 -40
- data/lib/tamashii/agent/device/fake_lcd.rb +0 -31
- data/lib/tamashii/agent/device/lcd.rb +0 -91
- data/lib/tamashii/agent/device/pi_buzzer.rb +0 -70
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'tamashii/agent/device/card_reader/base'
|
2
|
+
|
3
|
+
module Tamashii
|
4
|
+
module Agent
|
5
|
+
module Device
|
6
|
+
module CardReader
|
7
|
+
class Dummy < Base
|
8
|
+
def initialize(*args)
|
9
|
+
super
|
10
|
+
logger.debug "Initialized"
|
11
|
+
@last_time = Time.now
|
12
|
+
end
|
13
|
+
|
14
|
+
def poll_uid
|
15
|
+
if Time.now - @last_time > 2
|
16
|
+
@last_time = Time.now
|
17
|
+
if rand > 0.5
|
18
|
+
uid = Array.new(4){ rand(256)}
|
19
|
+
logger.debug "Fake Card Generated: #{uid}"
|
20
|
+
return uid
|
21
|
+
else
|
22
|
+
return nil
|
23
|
+
end
|
24
|
+
else
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def shutdown
|
30
|
+
logger.debug "Stopped"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'mfrc522'
|
2
|
+
require 'tamashii/agent/device/card_reader/base'
|
3
|
+
|
4
|
+
module Tamashii
|
5
|
+
module Agent
|
6
|
+
module Device
|
7
|
+
module CardReader
|
8
|
+
class Mfrc522Spi < Base
|
9
|
+
|
10
|
+
def initialize(*args)
|
11
|
+
super
|
12
|
+
@reader = MFRC522.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def poll_uid
|
16
|
+
# check antenna
|
17
|
+
return nil unless @reader.picc_request(MFRC522::PICC_REQA)
|
18
|
+
|
19
|
+
# read uid
|
20
|
+
uid = nil
|
21
|
+
begin
|
22
|
+
uid, sak = @reader.picc_select
|
23
|
+
rescue CommunicationError, UnexpectedDataError => e
|
24
|
+
logger.error "Error when selecting card: #{e.message}"
|
25
|
+
uid = :error
|
26
|
+
rescue => e
|
27
|
+
uid = :error
|
28
|
+
logger.error "GemError when selecting card: #{e.message}"
|
29
|
+
ensure
|
30
|
+
@reader.picc_halt
|
31
|
+
end
|
32
|
+
uid
|
33
|
+
end
|
34
|
+
|
35
|
+
def shutdown
|
36
|
+
@reader.shutdown
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'nfc'
|
2
|
+
require 'tamashii/agent/device/card_reader/base'
|
3
|
+
|
4
|
+
module Tamashii
|
5
|
+
module Agent
|
6
|
+
module Device
|
7
|
+
module CardReader
|
8
|
+
class Pn532Uart < Base
|
9
|
+
def initialize(*args)
|
10
|
+
super
|
11
|
+
@ctx = NFC::Context.new
|
12
|
+
@dev = @ctx.open "pn532_uart:#{fetch_path}"
|
13
|
+
@card_type = @options[:card_type] || :felica
|
14
|
+
logger.info "Card type enabled: #{@card_type}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def default_path
|
18
|
+
"/dev/ttyAMA0"
|
19
|
+
end
|
20
|
+
|
21
|
+
def fetch_path
|
22
|
+
if @options.has_key?(:path)
|
23
|
+
path = @options[:path]
|
24
|
+
else
|
25
|
+
path = default_path
|
26
|
+
logger.warn "No path specified. Use default path: #{path}"
|
27
|
+
end
|
28
|
+
path
|
29
|
+
end
|
30
|
+
|
31
|
+
def poll_uid
|
32
|
+
tag = @dev.poll(@card_type)
|
33
|
+
if tag && !tag.is_a?(Integer)
|
34
|
+
return tag.uid
|
35
|
+
else
|
36
|
+
return nil
|
37
|
+
end
|
38
|
+
rescue => e
|
39
|
+
logger.error "Error when reading card: #{e.message}"
|
40
|
+
return :error
|
41
|
+
end
|
42
|
+
|
43
|
+
def shutdown
|
44
|
+
@dev.close
|
45
|
+
@dev = nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'tamashii/agent/common'
|
2
|
+
|
3
|
+
module Tamashii
|
4
|
+
module Agent
|
5
|
+
module Device
|
6
|
+
class DeviceBase
|
7
|
+
include Common::Loggable
|
8
|
+
|
9
|
+
def initialize(component, options = {})
|
10
|
+
@component = component
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def shutdown
|
15
|
+
logger.warn "Device '#{self.class}' does not implement a shutdown method"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'tamashii/agent/device/device_base'
|
2
|
+
|
3
|
+
module Tamashii
|
4
|
+
module Agent
|
5
|
+
module Device
|
6
|
+
module Lcd
|
7
|
+
class Base < DeviceBase
|
8
|
+
# default implementation
|
9
|
+
def print_message(message)
|
10
|
+
lines = message.lines.map{|l| l.delete("\n")}
|
11
|
+
line_count.times.each { |line| print_line(lines[line], line) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def line_count
|
15
|
+
raise NotImplementedError, "line_count"
|
16
|
+
end
|
17
|
+
|
18
|
+
def width
|
19
|
+
raise NotImplementedError, "width"
|
20
|
+
end
|
21
|
+
|
22
|
+
def print_line(message, line)
|
23
|
+
raise NotImplementedError, "print_line"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'tamashii/agent/device/lcd/base'
|
2
|
+
|
3
|
+
module Tamashii
|
4
|
+
module Agent
|
5
|
+
module Device
|
6
|
+
module Lcd
|
7
|
+
class Dummy < Base
|
8
|
+
def line_count
|
9
|
+
2
|
10
|
+
end
|
11
|
+
|
12
|
+
def width
|
13
|
+
16
|
14
|
+
end
|
15
|
+
|
16
|
+
def print_line(message, line)
|
17
|
+
message = '' unless message
|
18
|
+
message = message.ljust(width, ' ')
|
19
|
+
message.split('').take(width).join('')
|
20
|
+
logger.debug "Line #{line}: #{message}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def shutdown
|
24
|
+
logger.debug "Stopped"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'i2c'
|
2
|
+
require 'tamashii/agent/device/lcd/base'
|
3
|
+
|
4
|
+
module Tamashii
|
5
|
+
module Agent
|
6
|
+
module Device
|
7
|
+
module Lcd
|
8
|
+
class Lcm1602I2c < Base
|
9
|
+
WIDTH = 16
|
10
|
+
LINE_COUNT = 2
|
11
|
+
|
12
|
+
OP_CHR = 1
|
13
|
+
OP_CMD = 0
|
14
|
+
|
15
|
+
LINES = [
|
16
|
+
0x80,
|
17
|
+
0xC0
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
BACKLIGHT_ON = 0x08
|
21
|
+
BACKLIGHT_OFF = 0x00
|
22
|
+
|
23
|
+
ENABLE = 0b00000100
|
24
|
+
|
25
|
+
PULSE = 0.0005
|
26
|
+
DELAY = 0.0005
|
27
|
+
|
28
|
+
attr_accessor :backlight
|
29
|
+
|
30
|
+
def width
|
31
|
+
16
|
32
|
+
end
|
33
|
+
|
34
|
+
def line_count
|
35
|
+
2
|
36
|
+
end
|
37
|
+
|
38
|
+
def initialize(*args)
|
39
|
+
super
|
40
|
+
initialize_lcd
|
41
|
+
end
|
42
|
+
|
43
|
+
def default_path
|
44
|
+
'/dev/i2c-1'
|
45
|
+
end
|
46
|
+
|
47
|
+
def default_address
|
48
|
+
0x27
|
49
|
+
end
|
50
|
+
|
51
|
+
def print_line(message, line)
|
52
|
+
write_line(message, LINES[line])
|
53
|
+
end
|
54
|
+
|
55
|
+
def shutdown
|
56
|
+
print_message("")
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def fetch_address
|
62
|
+
if @options.has_key?(:address)
|
63
|
+
address = @options[:address]
|
64
|
+
else
|
65
|
+
address = default_address
|
66
|
+
logger.warn "No address specified. Use default address: #{address.to_s(16)}"
|
67
|
+
end
|
68
|
+
address
|
69
|
+
end
|
70
|
+
|
71
|
+
def fetch_path
|
72
|
+
if @options.has_key?(:path)
|
73
|
+
path = @options[:path]
|
74
|
+
else
|
75
|
+
path = default_path
|
76
|
+
logger.warn "No path specified. Use default path: #{path}"
|
77
|
+
end
|
78
|
+
path
|
79
|
+
end
|
80
|
+
|
81
|
+
def initialize_lcd
|
82
|
+
@lcd = I2C.create(fetch_path)
|
83
|
+
@address = fetch_address
|
84
|
+
@backlight = @options.fetch(:backlight, true)
|
85
|
+
|
86
|
+
byte(0x33, OP_CMD)
|
87
|
+
byte(0x32, OP_CMD)
|
88
|
+
byte(0x06, OP_CMD)
|
89
|
+
byte(0x0C, OP_CMD)
|
90
|
+
byte(0x28, OP_CMD)
|
91
|
+
byte(0x01, OP_CMD)
|
92
|
+
sleep(DELAY)
|
93
|
+
end
|
94
|
+
|
95
|
+
def backlight_mode
|
96
|
+
return BACKLIGHT_ON if @backlight
|
97
|
+
BACKLIGHT_OFF
|
98
|
+
end
|
99
|
+
|
100
|
+
def write_line(message, line)
|
101
|
+
message = '' unless message
|
102
|
+
message = message.ljust(WIDTH, ' ')
|
103
|
+
byte(line, OP_CMD)
|
104
|
+
WIDTH.times.each { |pos| byte(message[pos].ord, OP_CHR) }
|
105
|
+
end
|
106
|
+
|
107
|
+
def write(bits)
|
108
|
+
@lcd.write(@address, bits)
|
109
|
+
end
|
110
|
+
|
111
|
+
def byte(bits, mode)
|
112
|
+
high = mode | (bits & 0xF0) | backlight_mode
|
113
|
+
low = mode | (bits << 4) & 0xF0 | backlight_mode
|
114
|
+
|
115
|
+
write(high)
|
116
|
+
toggle(high)
|
117
|
+
|
118
|
+
write(low)
|
119
|
+
toggle(low)
|
120
|
+
end
|
121
|
+
|
122
|
+
def toggle(bits)
|
123
|
+
sleep(DELAY)
|
124
|
+
write(bits | ENABLE)
|
125
|
+
sleep(PULSE)
|
126
|
+
write(bits & ~ENABLE)
|
127
|
+
sleep(DELAY)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -6,8 +6,8 @@ module Tamashii
|
|
6
6
|
class Base < Tamashii::Handler
|
7
7
|
def initialize(*args, &block)
|
8
8
|
super(*args, &block)
|
9
|
-
@
|
10
|
-
@master =
|
9
|
+
@networking = self.env[:networking]
|
10
|
+
@master = self.env[:master]
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
data/lib/tamashii/agent/lcd.rb
CHANGED
@@ -2,12 +2,10 @@ require 'concurrent'
|
|
2
2
|
|
3
3
|
require 'tamashii/agent/common'
|
4
4
|
require 'tamashii/agent/event'
|
5
|
-
require 'tamashii/agent/adapter/lcd'
|
6
|
-
|
7
5
|
|
8
6
|
module Tamashii
|
9
7
|
module Agent
|
10
|
-
class
|
8
|
+
class Lcd < Component
|
11
9
|
|
12
10
|
class LineAnimator
|
13
11
|
include Common::Loggable
|
@@ -77,10 +75,10 @@ module Tamashii
|
|
77
75
|
end
|
78
76
|
end
|
79
77
|
|
80
|
-
def initialize(master)
|
78
|
+
def initialize(name, master, options = {})
|
81
79
|
super
|
82
|
-
|
83
|
-
@device_line_count = @lcd.
|
80
|
+
@lcd = initialize_device
|
81
|
+
@device_line_count = @lcd.line_count
|
84
82
|
@device_lock = Mutex.new
|
85
83
|
create_line_animators
|
86
84
|
set_idle_text("[Tamashii]\nIdle...")
|
@@ -89,21 +87,21 @@ module Tamashii
|
|
89
87
|
schedule_to_print_idle
|
90
88
|
end
|
91
89
|
|
90
|
+
def default_device_name
|
91
|
+
'Dummy'
|
92
|
+
end
|
93
|
+
|
94
|
+
def get_device_class_name(device_name)
|
95
|
+
"Lcd::#{device_name}"
|
96
|
+
end
|
97
|
+
|
92
98
|
def create_line_animators
|
93
|
-
LineAnimator.line_width = @lcd.
|
99
|
+
LineAnimator.line_width = @lcd.width
|
94
100
|
LineAnimator.handler_print_line = method(:print_line)
|
95
101
|
@line_animators = []
|
96
102
|
@device_line_count.times {|i| @line_animators << LineAnimator.new(i)}
|
97
103
|
end
|
98
104
|
|
99
|
-
def load_lcd_device
|
100
|
-
@lcd = Adapter::LCD.object
|
101
|
-
rescue => e
|
102
|
-
logger.error "Unable to load LCD instance: #{Adapter::LCD.current_class}"
|
103
|
-
logger.error "Use #{Adapter::LCD.fake_class} instead"
|
104
|
-
@lcd = Adapter::LCD.fake_class.new
|
105
|
-
end
|
106
|
-
|
107
105
|
def print_message(message)
|
108
106
|
lines = message.lines.map{|l| l.delete("\n")}
|
109
107
|
@device_line_count.times do |line_count|
|
@@ -185,8 +183,8 @@ module Tamashii
|
|
185
183
|
end
|
186
184
|
|
187
185
|
def clean_up
|
188
|
-
clear_screen
|
189
186
|
super
|
187
|
+
@lcd.shutdown
|
190
188
|
end
|
191
189
|
end
|
192
190
|
end
|