Class: LadderDrive::LadderDriveConfig

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LadderDriveConfig

Returns a new instance of LadderDriveConfig.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ladder_drive/config.rb', line 59

def initialize options={}
  default = {
    input: "asm/main.asm",
    output: "build/main.hex",
  }
  emulator_default = {
    host: "localhost",
    port: 5555,
    protocol: "emu_protocol",
    keep: [
      ["L0", "L1023"],
      ["H0", "H1023"],
    ],
  }
  raspberrypi_default = {
    host: "localhost",
    port: 5555,
    protocol: "emu_protocol",
    keep: [
      ["L0", "L1023"],
      ["H0", "H1023"],
    ],
    io: {
      inputs: {
        x0: 4,
        x1: 17,
        x2: 27,
        x3: 22,
        x4: 5,
        x5: 6,
        x6: 13,
        x7: 19,
        x8: 26,
      },
      outputs: {
=begin
        y0: 18,
        y1: 23,
        y2: 24,
        y3: 25,
        y4: 12,
        y5: 16,
        y6: 20,
        y7: 21,
=end
      }
    },
  }

  @config = default.merge options
  @config[:plc] ||= {}
  @config[:plc][:emulator] = @config[:plc][:emulator] ? emulator_default.merge(@config[:plc][:emulator]) : emulator_default
  @config[:plc][:raspberrypi] = @config[:plc][:raspberrypi] ? emulator_default.merge(@config[:plc][:raspberrypi]) : raspberrypi_default

  @config[:default] ||= {}

  @targets = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/ladder_drive/config.rb', line 137

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

Class Method Details

.defaultObject



41
42
43
44
45
# File 'lib/ladder_drive/config.rb', line 41

def default
  @config ||= begin
    load File.join("config", "plc.yml")
  end
end

.load(path) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ladder_drive/config.rb', line 47

def load path
  h = {}
  if File.exist?(path)
    erb = ERB.new File.read(path)
    h = YAML.load(erb.result(binding))
    h = JSON.parse(h.to_json, symbolize_names: true)
  end
  new h || {}
end

Instance Method Details

#[](key) ⇒ Object



118
119
120
# File 'lib/ladder_drive/config.rb', line 118

def [] key
  @config[key]
end

#target(name = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ladder_drive/config.rb', line 122

def target name=nil
  name ||= (@config[:default][:target] || :emulator)
  name = name.to_sym if name.is_a? String
  target = @targets[name]
  unless target
    h = @config[:plc][name]
    unless h.nil? || h.empty?
      h = {name:name}.merge h
      target = LadderDriveConfigTarget.new h
      @targets[name] = target
    end
  end
  target
end