<commit_before>require 'lifx/seen'
require 'lifx/timers'
require 'lifx/observable'
require 'lifx/gateway_connection'

module LIFX
  class Site
    include Seen
    include Timers
    include Logging
    include Observable
    
    attr_reader :id, :gateways, :tag_manager

    def initialize(id)
      @id            = id
      @gateways      = {}
      @gateways_mutex = Mutex.new
      @threads       = []
      @threads << defer_lights_discovery
      @threads << initialize_timer_thread
    end

    def write(message)
      message.path.site_id = id
      @gateways.values.each do |gateway|
        gateway.write(message)
      end
    end

    def handle_message(message, ip, transport)
      logger.debug("<- #{self} #{transport}: #{message}")
      payload = message.payload
      case payload
      when Protocol::Device::StatePanGateway
        @gateways_mutex.synchronize do
          @gateways[message.device_id] ||= GatewayConnection.new
          @gateways[message.device_id].handle_message(message, ip, transport)
          @gateways[message.device_id].add_observer(self) do |**args|
            notify_observers(**args)
          end
        end
      else
        @message_handler.call(*args) if @message_handler
      end
      seen!
    end

    def flush(**options)
      @gateways.values.map do |gateway|
        Thread.new do
          gateway.flush(**options)
        end
      end.each(&:join)
    end
    
    def to_s
      %Q{#<LIFX::Site id=#{id}>}
    end
    alias_method :inspect, :to_s

    def stop
      @threads.each do |thread|
        Thread.kill(thread)
      end
      @gateways.values.each do |gateway|
        gateway.close
      end
    end

    protected

    def defer_lights_discovery
      # We wait a bit so the TCP transport has a chance to connect
      Thread.new do
        while gateways.empty?
          sleep 0.1
        end
        initialize_lights
      end
    end

    LIGHT_STATE_REQUEST_INTERVAL = 30
    STALE_LIGHT_CHECK_INTERVAL   = 5
    def initialize_lights
      timers.every(LIGHT_STATE_REQUEST_INTERVAL) do
        scan_lights
      end.fire
    end

    def scan_lights
      write(Message.new(path: ProtocolPath.new(tagged: true), payload: Protocol::Light::Get.new))
    end
  end
end
<commit_msg>Remove some dead code<commit_after>require 'lifx/seen'
require 'lifx/timers'
require 'lifx/observable'
require 'lifx/gateway_connection'

module LIFX
  class Site
    include Seen
    include Timers
    include Logging
    include Observable
    
    attr_reader :id, :gateways, :tag_manager

    def initialize(id)
      @id            = id
      @gateways      = {}
      @gateways_mutex = Mutex.new
      @threads       = []
      @threads << defer_lights_discovery
      @threads << initialize_timer_thread
    end

    def write(message)
      message.path.site_id = id
      @gateways.values.each do |gateway|
        gateway.write(message)
      end
    end

    def handle_message(message, ip, transport)
      logger.debug("<- #{self} #{transport}: #{message}")
      payload = message.payload
      case payload
      when Protocol::Device::StatePanGateway
        @gateways_mutex.synchronize do
          @gateways[message.device_id] ||= GatewayConnection.new
          @gateways[message.device_id].handle_message(message, ip, transport)
          @gateways[message.device_id].add_observer(self) do |**args|
            notify_observers(**args)
          end
        end
      end
      seen!
    end

    def flush(**options)
      @gateways.values.map do |gateway|
        Thread.new do
          gateway.flush(**options)
        end
      end.each(&:join)
    end
    
    def to_s
      %Q{#<LIFX::Site id=#{id}>}
    end
    alias_method :inspect, :to_s

    def stop
      @threads.each do |thread|
        Thread.kill(thread)
      end
      @gateways.values.each do |gateway|
        gateway.close
      end
    end

    protected

    def defer_lights_discovery
      # We wait a bit so the TCP transport has a chance to connect
      Thread.new do
        while gateways.empty?
          sleep 0.1
        end
        initialize_lights
      end
    end

    LIGHT_STATE_REQUEST_INTERVAL = 30
    def initialize_lights
      timers.every(LIGHT_STATE_REQUEST_INTERVAL) do
        scan_lights
      end.fire
    end

    def scan_lights
      write(Message.new(path: ProtocolPath.new(tagged: true), payload: Protocol::Light::Get.new))
    end
  end
end

{'fraction_non_alphanumeric': 0.08759288448547624, 'fraction_numerical': 0.002026570592208962, 'mean_word_length': 2.1773962804005724, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3777964', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1345, 'n_words': 317}