lib/tamashii/agent/connection.rb in tamashii-agent-0.2.1 vs lib/tamashii/agent/connection.rb in tamashii-agent-0.2.3

- old
+ new

@@ -74,23 +74,23 @@ end attr_reader :url attr_reader :master - def initialize(master, host, port) - super() - @master = master - @url = "#{Config.use_ssl ? "wss" : "ws"}://#{host}:#{port}/#{Config.entry_point}" + def initialize(master) + super + @host = @master.host + @port = @master.port + @url = "#{Config.use_ssl ? "wss" : "ws"}://#{@host}:#{@port}/#{Config.entry_point}" self.reset - @host = host - @port = port @tag = 0 @future_ivar_pool = Concurrent::Map.new @driver_lock = Mutex.new + @last_error_report_time = Time.now setup_resolver end def create_selector @selector = NIO::Selector.new @@ -216,10 +216,14 @@ end end def try_create_socket logger.info "try to open socket..." + if Time.now - @last_error_report_time > 5.0 + @master.send_event(Event.new(Event::LCD_MESSAGE, "Initializing\nConnection...")) + @last_error_report_time = Time.now + end if Config.use_ssl OpenSSL::SSL::SSLSocket.new(TCPSocket.new(@host, @port)).connect else TCPSocket.new(@host, @port) end @@ -248,10 +252,11 @@ if pkt.body == Packet::STRING_TRUE @tag = pkt.tag self.auth_success else logger.error "Authentication failed. Delay for 3 seconds" + @master.send_event(Event.new(Event::LCD_MESSAGE, "Fatal Error\nAuth Failed")) sleep 3 end else logger.error "Authentication error: Not an authentication result packet" end @@ -266,15 +271,19 @@ # override def process_event(event) case event.type when Event::CARD_DATA - id = event.body - wrapped_body = { - id: id, - ev_body: event.body - }.to_json - new_remote_request(id, Type::RFID_NUMBER, wrapped_body) + if self.ready? + id = event.body + wrapped_body = { + id: id, + ev_body: event.body + }.to_json + new_remote_request(id, Type::RFID_NUMBER, wrapped_body) + else + @master.send_event(Event.new(Event::CONNECTION_NOT_READY, "Connection not ready for #{event.type}:#{event.body}")) + end end end def schedule_task_runner(id, ev_type, ev_body, start_time, times) logger.debug "Schedule send attemp #{id} : #{times + 1} time(s)"