Sha256: d6c9913b0a253fa4ad8ceb8eef79d1a769ac5722ab7b30e656fcc558e7d75d98

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

module Gom
  class SensorPorts < Gom::Remote::Entry

    Defaults = {
      :logfile      => '-',
      :interface    => '0.0.0.0',
      :sensor_port  => 76001,
      :mode         => :udp,
    }

    include OAttr
    oattr :logfile, :interface, :sensor_port, :mode

    def initialize path, options = {}
      @path = path
      @options = Defaults.merge(gnode @path).merge(options)
      puts " -- new sensor port: #{self.inspect}"

      redirect_to logfile
    end

    def listen
      puts " -- listen: #{self.inspect}"
      self.send "listen_#{mode}"
    end

    def listen_udp
      socket = UDPSocket.new
      socket.bind(interface, sensor_port)
      loop do
        val, sender = socket.recvfrom(1024)
        puts "-->#{val}<-- #{sender.inspect}"
        connection.write "#{@path}:current_value", val.to_s
      end
    ensure
      socket.close rescue nil
    end

    def status
      puts @options.inspect
      #t = Net::Telnet::new(
      #  "Host" => device_ip, "Timeout" => 10, "Prompt" => /[$%#>] \z/n
      #)
      #t.login(user, password) { |c| puts c }
      "not implemented"
    end

    def redirect_to logfile
      (@logfile_fd && @logfile_fd.close) rescue nil
      puts " -- redirecting stdout/stderr to: #{logfile}"
      if logfile == '-'
        if @stdout
          $stderr, $stdout = @stdout, @stderr
        end
      else
        @stderr, @stdout = $stdout, $stderr
        @logfile_fd = File.open(logfile, File::WRONLY|File::APPEND|File::CREAT)
        @logfile_fd.sync = true
        $stderr = $stdout = @logfile_fd
      end
      # first line after redirect
      puts " -- daemon logile redirect at #{Time.now}"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gom-sensor-ports-0.1.2 lib/gom-sensor-ports/sensor-ports.rb