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 attr_reader :valve def self.instance @@instance end def initialize path, options = {} @@instance = self @path = path @options = Defaults.merge(gnode @path).merge(options) puts "options: #{@options.inspect}" # 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}" 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 puts "#{Time.now.iso8601} | ++ Starting servers" EventMachine::threadpool_size = 20 EventMachine::run do puts "#{Time.now.iso8601} | ++ * 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 op, attribute @valve = attribute["value"].to_sym puts " -- can-controls-valve now: #{@valve}" end end __END__ def _fill_settings CcgLogger::LOGGER.info "#{Time.now.iso8601} | ++ Initializing settings ..." my_gom = RestFs::Client.new GOM_ROOT my_res = my_gom.retrieve("#{STATION['device']}:rci_uri") if my_res STATION['rci_uri'] = my_res['attribute']['value'] CcgLogger::LOGGER.info "#{Time.now.iso8601} | ++ setting rci_uri for station #{STATION['device']} => #{STATION['rci_uri'].inspect}" my_port_res = my_gom.retrieve("#{STATION['device']}/hid:port") if my_port_res STATION['port'] = my_port_res['attribute']['value'] else CcgLogger::LOGGER.info "#{Time.now.iso8601} | ++ device's hid:port #{STATION['device']}/hid:port not found in gom" end else CcgLogger::LOGGER.info "#{Time.now.iso8601} | ++ device's rci_uri #{STATION['device']}:rci_uri not found in gom" end end