Class: PlcAccess::Protocol::Protocol
- Inherits:
-
Object
- Object
- PlcAccess::Protocol::Protocol
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
#host ⇒ Object
Returns the value of attribute host.
32
33
34
|
# File 'lib/plc_access/protocol/protocol.rb', line 32
def host
@host
end
|
#log_level ⇒ Object
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
|
#port ⇒ Object
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
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
162
163
164
165
|
# File 'lib/plc_access/protocol/protocol.rb', line 126
def [](*args)
case args.size
when 1
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
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
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
203
204
205
206
207
|
# File 'lib/plc_access/protocol/protocol.rb', line 167
def []=(*args)
case args.size
when 2
v = args[1]
v = [v] unless v.is_a? Array
case args[0]
when String
self[args[0], v.size] = v
when Range
self[args[0].first, args[0].count] = v
else
raise ArgumentError, "#{args[1]} must be String or Array."
end
when 3
d = device_by_name args[0]
c = args[1]
values = args[2]
values = [values] unless values.is_a? Array
values += [0] * (c - values.size) if values.size < c
values = values[0, c] if values.size > c
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
118
119
120
|
# File 'lib/plc_access/protocol/protocol.rb', line 118
def available_bits_range(_device = nil)
-Float::INFINITY..Float::INFINITY
end
|
#available_words_range(_device = nil) ⇒ Object
122
123
124
|
# File 'lib/plc_access/protocol/protocol.rb', line 122
def available_words_range(_device = nil)
-Float::INFINITY..Float::INFINITY
end
|
#close ⇒ Object
68
|
# File 'lib/plc_access/protocol/protocol.rb', line 68
def close; end
|
#destination_ipv4 ⇒ Object
209
210
211
|
# File 'lib/plc_access/protocol/protocol.rb', line 209
def destination_ipv4
Socket.gethostbyname(host)[3].unpack('C4').join('.')
end
|
#device_by_name(_name) ⇒ Object
94
95
96
|
# File 'lib/plc_access/protocol/protocol.rb', line 94
def device_by_name(_name)
nil
end
|
#get_bit_from_device(device) ⇒ Object
70
71
72
|
# File 'lib/plc_access/protocol/protocol.rb', line 70
def get_bit_from_device(device)
get_bits_from_device(1, device_by_name(device)).first
end
|
#get_bits_from_device(count, device) ⇒ Object
74
|
# File 'lib/plc_access/protocol/protocol.rb', line 74
def get_bits_from_device(count, device); end
|
#get_from_devices(device, count = 1) ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/plc_access/protocol/protocol.rb', line 98
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
82
83
84
|
# File 'lib/plc_access/protocol/protocol.rb', line 82
def get_word_from_device(device)
get_words_from_device(1, device_by_name(device)).first
end
|
#get_words_from_device(count, device) ⇒ Object
86
|
# File 'lib/plc_access/protocol/protocol.rb', line 86
def get_words_from_device(count, device); end
|
#open ⇒ Object
67
|
# File 'lib/plc_access/protocol/protocol.rb', line 67
def open; end
|
#self_ipv4 ⇒ Object
213
214
215
|
# File 'lib/plc_access/protocol/protocol.rb', line 213
def self_ipv4
Socket.getaddrinfo(Socket.gethostname, 'echo', Socket::AF_INET)[0][3]
end
|
#set_bit_to_device(bit, device) ⇒ Object
76
77
78
|
# File 'lib/plc_access/protocol/protocol.rb', line 76
def set_bit_to_device(bit, device)
set_bits_to_device [bit], device
end
|
#set_bits_to_device(bits, device) ⇒ Object
80
|
# File 'lib/plc_access/protocol/protocol.rb', line 80
def set_bits_to_device(bits, device); end
|
#set_to_devices(device, values) ⇒ Object
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/plc_access/protocol/protocol.rb', line 107
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
88
89
90
|
# File 'lib/plc_access/protocol/protocol.rb', line 88
def set_word_to_device(word, device)
set_words_to_device [word], device
end
|
#set_words_to_device(words, device) ⇒ Object
92
|
# File 'lib/plc_access/protocol/protocol.rb', line 92
def set_words_to_device(words, device); end
|
#string_endian ⇒ Object
61
62
63
|
# File 'lib/plc_access/protocol/protocol.rb', line 61
def string_endian
:big
end
|