module JamRuby
  class MachineExtra < ActiveRecord::Base

    @@log = Logging.logger[MachineExtra]

    belongs_to :machine_fingerprint, :class_name => "JamRuby::MachineFingerprint"

    def self.create(machine_fingerprint, data)
      me = MachineExtra.new
      me.machine_fingerprint = machine_fingerprint
      me.mac_address = data[:mac]
      me.mac_name = data[:name]
      me.upstate = data[:upstate]
      me.ipaddr_0 = data[:ipaddr_0]
      me.ipaddr_1 = data[:ipaddr_1]
      me.ipaddr_2 = data[:ipaddr_2]
      me.ipaddr_3 = data[:ipaddr_3]
      me.ipaddr_4 = data[:ipaddr_4]
      me.ipaddr_5 = data[:ipaddr_5]
      me.save

      unless me.save
        @@log.error("unable to create machine extra: #{me.errors.inspect}")
      end
    end

    def admin_url
      APP_CONFIG.admin_root_url + "/admin/machine_extras/" + id
    end

    def to_s
      "#{mac_address} #{mac_name} #{upstate ? 'UP' : 'DOWN'} #{ipaddr_0} #{ipaddr_1} #{ipaddr_2} #{ipaddr_3} #{ipaddr_4} #{ipaddr_5}"
    end
  end
end