Class: PlcAccess::Protocol::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/plc_access/protocol/protocol.rb

Constant Summary collapse

TIMEOUT =
1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Protocol

Returns a new instance of Protocol.



34
35
36
37
# File 'lib/plc_access/protocol/protocol.rb', line 34

def initialize(options = {})
  @logger = Logger.new($stdout)
  self.log_level = options[:log_level] || :info
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



32
33
34
# File 'lib/plc_access/protocol/protocol.rb', line 32

def host
  @host
end

#log_levelObject

Returns the value of attribute log_level.



39
40
41
# File 'lib/plc_access/protocol/protocol.rb', line 39

def log_level
  @log_level
end

#portObject

Returns the value of attribute port.



32
33
34
# File 'lib/plc_access/protocol/protocol.rb', line 32

def port
  @port
end

Instance Method Details

#[](*args) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/plc_access/protocol/protocol.rb', line 122

def [](*args)
  case args.size
  when 1
    # protocol["DM0"]
    # protocol["DM0".."DM9"]
    case args[0]
    when String
      self[args[0], 1].first
    when Range
      self[args[0].first, args[0].count]
    else
      raise ArgumentError, "#{args[0]} must be String or Range."
    end
  when 2
    # protocol["DM0", 10]
    d = device_by_name args[0]
    c = args[1]
    a = []
    if d.bit_device?
      b = available_bits_range(d).last
      until c.zero?
        n_c = [b, c].min
        a += get_bits_from_device(n_c, d)
        d += n_c
        c -= n_c
      end
    else
      b = available_words_range(d).last
      until c.zero?
        n_c = [b, c].min
        a += get_words_from_device(n_c, d)
        d += n_c
        c -= n_c
      end
    end
    a
  else
    raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1 or 2)"
  end
end

#[]=(*args) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/plc_access/protocol/protocol.rb', line 163

def []=(*args)
  case args.size
  when 2
    # protocol["DM0"] = 0
    # protocol["DM0".."DM9"] = [0, 1, .., 9]
    v = args[1]
    v = [v] unless v.is_a? Array
    case args[0]
    when String
      self[args[0], 1] = v
    when Range
      self[args[0].first, args[0].count] = v
    else
      raise ArgumentError, "#{args[1]} must be String or Array."
    end
  when 3
    # protocol["DM0", 10] = [0, 1, .., 9]
    d = device_by_name args[0]
    c = args[1]
    values = args[2]
    values = [values] unless values.is_a? Array
    raise ArgumentError, "Count #{c} is not match #{args[2].size}." unless c == values.size

    a = []
    if d.bit_device?
      values.each_slice(available_bits_range(d).last) do |sv|
        set_bits_to_device(sv, d)
        d += sv.size
      end
    else
      values.each_slice(available_words_range(d).last) do |sv|
        set_words_to_device(sv, d)
        d += sv.size
      end
    end
    a
  else
    raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 2 or 3)"
  end
end

#available_bits_range(_device = nil) ⇒ Object



114
115
116
# File 'lib/plc_access/protocol/protocol.rb', line 114

def available_bits_range(_device = nil)
  -Float::INFINITY..Float::INFINITY
end

#available_words_range(_device = nil) ⇒ Object



118
119
120
# File 'lib/plc_access/protocol/protocol.rb', line 118

def available_words_range(_device = nil)
  -Float::INFINITY..Float::INFINITY
end

#closeObject



64
# File 'lib/plc_access/protocol/protocol.rb', line 64

def close; end

#destination_ipv4Object



204
205
206
# File 'lib/plc_access/protocol/protocol.rb', line 204

def destination_ipv4
  Socket.gethostbyname(host)[3].unpack('C4').join('.')
end

#device_by_name(_name) ⇒ Object



90
91
92
# File 'lib/plc_access/protocol/protocol.rb', line 90

def device_by_name(_name)
  nil
end

#get_bit_from_device(device) ⇒ Object



66
67
68
# File 'lib/plc_access/protocol/protocol.rb', line 66

def get_bit_from_device(device)
  get_bits_from_device(1, device_by_name(device)).first
end

#get_bits_from_device(count, device) ⇒ Object



70
# File 'lib/plc_access/protocol/protocol.rb', line 70

def get_bits_from_device(count, device); end

#get_from_devices(device, count = 1) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/plc_access/protocol/protocol.rb', line 94

def get_from_devices(device, count = 1)
  d = device_by_name device
  if d.bit_device?
    get_bits_from_device count, d
  else
    get_words_from_device count, d
  end
end

#get_word_from_device(device) ⇒ Object



78
79
80
# File 'lib/plc_access/protocol/protocol.rb', line 78

def get_word_from_device(device)
  get_words_from_device(1, device_by_name(device)).first
end

#get_words_from_device(count, device) ⇒ Object



82
# File 'lib/plc_access/protocol/protocol.rb', line 82

def get_words_from_device(count, device); end

#openObject

abstract methods



63
# File 'lib/plc_access/protocol/protocol.rb', line 63

def open; end

#self_ipv4Object



208
209
210
# File 'lib/plc_access/protocol/protocol.rb', line 208

def self_ipv4
  Socket.getaddrinfo(Socket.gethostname, 'echo', Socket::AF_INET)[0][3]
end

#set_bit_to_device(bit, device) ⇒ Object



72
73
74
# File 'lib/plc_access/protocol/protocol.rb', line 72

def set_bit_to_device(bit, device)
  set_bits_to_device [bit], device
end

#set_bits_to_device(bits, device) ⇒ Object



76
# File 'lib/plc_access/protocol/protocol.rb', line 76

def set_bits_to_device(bits, device); end

#set_to_devices(device, values) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/plc_access/protocol/protocol.rb', line 103

def set_to_devices(device, values)
  values = [values] unless values.is_a? Array
  d = device_by_name device
  if d.bit_device?
    values = values.map { |v| case v; when 1 then true; when 0 then false; else; v; end }
    set_bits_to_device values, d
  else
    set_words_to_device values, d
  end
end

#set_word_to_device(word, device) ⇒ Object



84
85
86
# File 'lib/plc_access/protocol/protocol.rb', line 84

def set_word_to_device(word, device)
  set_words_to_device [word], device
end

#set_words_to_device(words, device) ⇒ Object



88
# File 'lib/plc_access/protocol/protocol.rb', line 88

def set_words_to_device(words, device); end