require 'rubygems' require 'eventmachine' require 'rest_fs/client' require 'settings' require 'server' class CcgRunner < Gom::Remote::Entry Defaults = { :logfile => '-', :port => nil, :valve => :closed, } include OAttr oattr :incoming_osc_port, :device, :valve, :logfile def self.instance @@instance end def initialize path, options = {} @@instance = self @path = path @options = Defaults.merge(gnode @path).merge(options) puts "options: #{@options.inspect}" (Ccg::logger logfile) # FIXIT: legacy! pushing config values from GOM to global variable!!! STATION['port'] = Integer(incoming_osc_port) STATION['device'] = device STATION['rci_uri'] = device_rci_uri puts " -- STATION info: #{STATION.inspect}" @options[:valve] = valve.to_sym # type fixing the GOM string init_gnp end def device_rci_uri @device_rci_uri ||= connection.read "#{device}:rci_uri.txt" end def run #fill_settings port = Integer(incoming_osc_port) if port.nil? raise "NOT Starting servers since port cannot be determined" end Ccg.logger.info "Starting servers" EventMachine::threadpool_size = 20 EventMachine::run do Ccg.logger.info "UdpServer (port: #{port}) ..." EventMachine::open_datagram_socket('0.0.0.0', port, UdpServer) # TODO place http server component for GNP callbacks here... end end private def init_gnp @sub = Subscription.new( "#{@path}:valve", :name => "ccg", :operations => [:update, :create] ) @sub.callback = lambda { |*args| valve_update(*args) } connection.subscribe @sub end def valve_update _, attribute @options[:valve] = attribute["value"].to_sym puts " -- can-controls-valve now: #{valve}" end end