Class: LadderDrive::LadderDriveConfigTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/ladder_drive/config_target.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LadderDriveConfigTarget

Returns a new instance of LadderDriveConfigTarget.



48
49
50
51
# File 'lib/ladder_drive/config_target.rb', line 48

def initialize options={}
  @target_info = options
  ObjectSpace.define_finalizer(self, self.class.finalize)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/ladder_drive/config_target.rb', line 73

def method_missing(name, *args)
  name = name.to_s unless name.is_a? String
  case name.to_s
  when /(.*)=$/
    @target_info[$1.to_sym] = args.first
  else
    @target_info[name.to_sym]
  end
end

Class Method Details

.finalizeObject



39
40
41
42
43
44
# File 'lib/ladder_drive/config_target.rb', line 39

def finalize
  proc {
    EmuPlcServer.terminate
    RaspberrypiPlcServer.terminate
  }
end

Instance Method Details

#protocolObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ladder_drive/config_target.rb', line 53

def protocol
  @protocol ||= begin
    p = eval("#{@target_info[:protocol].camelize}.new")
    p.host = @target_info[:host] if @target_info[:host]
    p.port = @target_info[:port] if @target_info[:port]
    p.log_level = @target_info[:log_level] if @target_info[:log_level]
    p
  rescue
    nil
  end
end

#runObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ladder_drive/config_target.rb', line 83

def run
  case self.name
  when :emulator
    Plc::Emulator::EmuPlcServer.launch @target_info
  when :raspberrypi
    Plc::Raspberrypi::RaspberrypiPlcServer.launch @target_info
  else
    # DO NOTHIN
    # Actual device is running independently.
  end
end

#uploaderObject



65
66
67
68
69
70
71
# File 'lib/ladder_drive/config_target.rb', line 65

def uploader
  @uploader ||= begin
    u = Uploader.new
    u.protocol = self.protocol
    u
  end
end