lib/tamashii/agent/master.rb in tamashii-agent-0.2.1 vs lib/tamashii/agent/master.rb in tamashii-agent-0.2.3
- old
+ new
@@ -10,13 +10,14 @@
module Tamashii
module Agent
class Master < Component
attr_reader :serial_number
+ attr_reader :host, :port
def initialize(host, port)
- super()
+ super(self)
logger.info "Starting Tamashii::Agent #{Tamashii::Agent::VERSION} in #{Config.env} mode"
@host = host
@port = port
@serial_number = get_serial_number
logger.info "Serial number: #{@serial_number}"
@@ -40,24 +41,37 @@
end
end
def create_components
@components = {}
- @components[:connection] = create_component(Connection, self, @host, @port)
- @components[:buzzer] = create_component(Buzzer)
- @components[:lcd] = create_component(LCD)
- @components[:card_reader] = create_component(CardReader, self)
+ create_component(Connection, self)
+ create_component(Buzzer, self)
+ create_component(LCD, self)
+ create_component(CardReader, self)
end
def create_component(class_name, *args)
c = class_name.new(*args)
logger.info "Starting component: #{class_name}"
yield c if block_given?
c.run
+ @components[class_name] = c
+
c
end
+ def restart_component(class_name)
+ if old_component = @components[class_name]
+ logger.info "Stopping component: #{class_name}"
+ old_component.stop # TODO: set timeout for stopping?
+ logger.info "Restarting component: #{class_name}"
+ create_component(class_name, self)
+ else
+ logger.error "Rstart component failed: unknown component #{name}"
+ end
+ end
+
# override
def process_event(event)
super
case event.type
when Event::SYSTEM_COMMAND
@@ -72,9 +86,12 @@
when Tamashii::Type::UPDATE
system_update
end
when Event::CONNECTION_NOT_READY
broadcast_event(Event.new(Event::BEEP, "error"))
+ broadcast_event(Event.new(Event::LCD_MESSAGE, "Fatal Error\nConnection Error"))
+ when Event::RESTART_COMPONENT
+ restart_component(event.body)
else
broadcast_event(event)
end
end