cpee-handlerwrapper-opcua 0.2 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5097543e5a1b74cc025835261dc297194cb4ac7446187ca44a8d694ac86ce032
4
- data.tar.gz: 5aefcf1dc9ffe6d088993a7b64d6ce4a976a552870faad76b0cf9e7a7d37ace6
3
+ metadata.gz: c53decc037d22d3802022844c8483b9e680121f36e28ad05ce3e3250126de6ef
4
+ data.tar.gz: 999c8f9bd81fc344214559f82ab0e53b88212c4a49d37de1deb066ae0cf85905
5
5
  SHA512:
6
- metadata.gz: 7932110d1fb1fde2e2f7fed5f5d958b7b6f9047942e3dd2ab6b01392b51fa292e1395e17ed73e444b32f18b90b280939943a94736781e241264f271e6d9cbc1a
7
- data.tar.gz: 607f6b0b17991b6b0226c7b8b4ba1a188f0a95b458a64e2d8dbd271773fa9448ce6ae3030bf62c1e64a4e4d817d39ad510a06126c3e8d409638a9e9504727556
6
+ metadata.gz: 5f6f022ce8b077f2fa01ed2527f4a007847d97b63982b5334f9febd76f8829d4ff04ac250ac8bb320e786f7bac88ca9e1007c7872a183f45778dc046e000213b
7
+ data.tar.gz: a1560af1df4d1eb928717ba8b6c200c2929266df7af7ba31d0a3a9066c6ee67cd8c3ae1167eaa76a13b373621a8d4e2af18efe7250e66dd2d970bec78326b394
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cpee-handlerwrapper-opcua"
3
- s.version = "0.2"
3
+ s.version = "0.7"
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.license = "LGPL-3.0"
6
6
  s.summary = "OPCUA for CPEE HandlerWrapper. Use opc.tcp-read|write|exececute:// links."
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.email = 'juergen.mangler@gmail.com'
19
19
  s.homepage = 'https://cpee.org//cpee-handlerwrapper-opcua'
20
20
 
21
- s.add_runtime_dependency 'cpee', '~>1.5', '>= 1.5.3'
21
+ s.add_runtime_dependency 'cpee', '~>1.5', '>= 1.5.8'
22
22
  s.add_runtime_dependency 'opcua', '~>0', '>= 0.13'
23
23
  s.add_development_dependency 'rake', '~> 12'
24
24
  s.add_development_dependency 'rake-compiler', '~> 1.0'
@@ -44,127 +44,166 @@ class DefaultHandlerWrapper < WEEL::HandlerWrapperBase
44
44
  @handler_position = position
45
45
  @handler_passthrough = nil
46
46
  @handler_returnValue = nil
47
+ @handler_returnOptions = nil
47
48
  @label = ''
48
49
  end # }}}
49
50
 
50
- def prepare(readonly, endpoints, parameters)
51
- @handler_endpoint = endpoints.is_a?(Array) ? endpoints.map{ |ep| readonly.endpoints[ep] }.compact : readonly.endpoints[endpoints]
52
- parameters[:arguments]&.each do |ele|
53
- if ele.value.is_a?(Proc)
54
- ele.value = readonly.instance_exec &ele.value
51
+ def prepare(readonly, endpoints, parameters, replay=false) #{{{
52
+ if replay
53
+ @handler_endpoint = @controller.attributes[:replayer]
54
+ else
55
+ @handler_endpoint = endpoints.is_a?(Array) ? endpoints.map{ |ep| readonly.endpoints[ep] }.compact : readonly.endpoints[endpoints]
56
+ end
57
+ params = parameters.dup
58
+ params[:arguments] = params[:arguments].dup if params[:arguments]
59
+ params[:arguments]&.map! do |ele|
60
+ t = ele.dup
61
+ if t.value.is_a?(Proc)
62
+ t.value = readonly.instance_exec &t.value
55
63
  end
64
+ t
56
65
  end
57
- parameters
58
- end
66
+ params
67
+ end #}}}
59
68
 
60
- def activity_handle(passthrough, parameters) # {{{
61
- raise "Wrong endpoint" if @handler_endpoint.nil? || @handler_endpoint.empty?
62
- @label = parameters[:label]
63
- @sensors = parameters[:sensors]
64
- @aggregators = parameters[:aggregators]
65
- @costs = parameters[:costs]
66
- @controller.notify("activity/calling", :instance => @controller.instance, :instance_uuid => @controller.uuid, :label => @label, :instance_name => @controller.info, :activity => @handler_position, :passthrough => passthrough, :endpoint => @handler_endpoint, :parameters => parameters, :timestamp => Time.now.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"), :attributes => @controller.attributes_translated)
67
- if passthrough.to_s.empty?
68
- if @handler_endpoint.start_with?('opc.tcp')
69
- if @handler_endpoint =~ /^opc\.tcp-read:\/\/([^\/]+)\/(\d+)\/(.+)/
70
- nid = $3 == $3.to_i.to_s ? $3.to_i : $3
71
- ns = $2
72
- url = 'opc.tcp://' + $1
73
- client = OPCUA::Client.new(url)
74
- if (node = client.get ns.to_i, nid)
75
- result = node.value
76
- else
77
- raise 'invalid nodeid'
78
- end
79
- client.disconnect
80
- callback [Riddl::Parameter::Simple.new('value',result)], {}
81
- elsif @handler_endpoint =~ /^opc\.tcp-write:\/\/([^\/]+)\/(\d+)\/([^\?]+)(\?value=(.*))?/
82
- nid = $3 == $3.to_i.to_s ? $3.to_i : $3
83
- ns = $2
84
- par = $5
85
- url = 'opc.tcp://' + $1
86
- client = OPCUA::Client.new(url)
87
- if (node = client.get ns.to_i, nid)
88
- (parameters[:arguments] || [→(:name => 'value', :value => par)] || []).each do |ele|
89
- what = CPEE::ValueHelper::parse_extended(ele.value)
90
- node.value = what
91
- result = what
92
- end
93
- else
94
- raise 'invalid nodeid'
95
- end
96
- client.disconnect
97
- callback [Riddl::Parameter::Simple.new('value',result)], {}
98
- elsif @handler_endpoint =~ /^opc\.tcp-execute:\/\/([^\/]+)\/(\d+)\/([^\?]+)(\?value=(.*))?/
99
- nid = $3 == $3.to_i.to_s ? $3.to_i : $3
100
- ns = $2
101
- par = $5
102
- url = 'opc.tcp://' + $1
103
- client = OPCUA::Client.new(url)
104
- if (node = client.get ns.to_i, nid)
105
- params = []
106
- (parameters[:arguments] || []).each do |ele|
107
- what = CPEE::ValueHelper::parse_extended(ele.value)
108
- params << what
109
- end
110
- result = node.call *params
111
- else
112
- raise 'invalid nodeid'
113
- end
114
- client.disconnect
115
- callback [Riddl::Parameter::Simple.new('value',result)], {}
116
- end
69
+ def proto_curl(parameters) #{{{
70
+ params = []
71
+ callback = Digest::MD5.hexdigest(Kernel::rand().to_s)
72
+ (parameters[:arguments] || []).each do |s|
73
+ if s.respond_to?(:mimetype)
74
+ params << Riddl::Parameter::Complex.new(s.name.to_s,v.mimetype,v.value)
117
75
  else
118
- params = []
119
- callback = Digest::MD5.hexdigest(Kernel::rand().to_s)
120
- (parameters[:arguments] || []).each do |s|
121
- if s.respond_to?(:mimetype)
122
- params << Riddl::Parameter::Complex.new(s.name.to_s,v.mimetype,v.value)
123
- else
124
- if s.name.to_s =~ /__$/
125
- params << Riddl::Parameter::Simple.new(s.name.to_s.chop.chop,CPEE::ValueHelper::generate(s.value),:query)
126
- else
127
- params << Riddl::Parameter::Simple.new(s.name.to_s,CPEE::ValueHelper::generate(s.value))
128
- end
129
- end
76
+ if s.name.to_s =~ /^__Q_/
77
+ params << Riddl::Parameter::Simple.new(s.name.to_s.sub(/^__Q_/,''),CPEE::ValueHelper::generate(s.value),:query)
78
+ elsif s.name.to_s =~ /^__B_/
79
+ params << Riddl::Parameter::Simple.new(s.name.to_s.sub(/^__B_/,''),CPEE::ValueHelper::generate(s.value),:body)
80
+ elsif s.name.to_s =~ /^__H_/
81
+ params << Riddl::Header.new(s.name.to_s.sub(/^__H_/,''),CPEE::ValueHelper::generate(s.value))
82
+ elsif s.name.to_s =~ /^__C_/
83
+ params << Riddl::Parameter::Complex.new(s.name.to_s.sub(/^__C_/,''),*CPEE::ValueHelper::generate(s.value).split(';',2))
84
+ else
85
+ params << Riddl::Parameter::Simple.new(s.name.to_s,CPEE::ValueHelper::generate(s.value))
130
86
  end
87
+ end
88
+ end
131
89
 
132
- params << Riddl::Header.new("CPEE-BASE",@controller.base_url)
133
- params << Riddl::Header.new("CPEE-INSTANCE",@controller.instance)
134
- params << Riddl::Header.new("CPEE-INSTANCE-URL",@controller.instance_url)
135
- params << Riddl::Header.new("CPEE-INSTANCE-UUID",@controller.uuid)
136
- params << Riddl::Header.new("CPEE-CALLBACK",@controller.instance_url + '/callbacks/' + callback)
137
- params << Riddl::Header.new("CPEE-CALLBACK-ID",callback)
138
- params << Riddl::Header.new("CPEE-ACTIVITY",@handler_position)
139
- params << Riddl::Header.new("CPEE-LABEL",parameters[:label]||'')
140
- @controller.attributes.each do |key,value|
141
- params << Riddl::Header.new("CPEE-ATTR-#{key.to_s.gsub(/_/,'-')}",value)
142
- end
90
+ params << Riddl::Header.new("CPEE-BASE",@controller.base_url)
91
+ params << Riddl::Header.new("CPEE-INSTANCE",@controller.instance)
92
+ params << Riddl::Header.new("CPEE-INSTANCE-URL",@controller.instance_url)
93
+ params << Riddl::Header.new("CPEE-INSTANCE-UUID",@controller.uuid)
94
+ params << Riddl::Header.new("CPEE-CALLBACK",@controller.instance_url + '/callbacks/' + callback)
95
+ params << Riddl::Header.new("CPEE-CALLBACK-ID",callback)
96
+ params << Riddl::Header.new("CPEE-ACTIVITY",@handler_position)
97
+ params << Riddl::Header.new("CPEE-LABEL",@label||'')
98
+ params << Riddl::Header.new("CPEE-REPLAY",@controller.attributes['replayer_args'])
99
+ @controller.attributes.each do |key,value|
100
+ params << Riddl::Header.new("CPEE-ATTR-#{key.to_s.gsub(/_/,'-')}",value)
101
+ end
102
+
103
+ tendpoint = @handler_endpoint.sub(/^http(s)?-(get|put|post|delete):/,'http\\1:')
104
+ type = $2 || parameters[:method] || 'post'
143
105
 
144
- tendpoint = @handler_endpoint.sub(/^http(s)?-(get|put|post|delete):/,'http\\1:')
145
- type = $2 || parameters[:method] || 'post'
106
+ client = Riddl::Client.new(tendpoint)
146
107
 
147
- client = Riddl::Client.new(tendpoint)
108
+ @controller.callbacks[callback] = CPEE::Callback.new("callback activity: #{@handler_position}",self,:callback,nil,nil,:http)
109
+ @handler_passthrough = callback
148
110
 
149
- @controller.callbacks[callback] = CPEE::Callback.new("callback activity: #{@handler_position}",self,:callback,nil,nil,:http)
150
- @handler_passthrough = callback
111
+ status, result, headers = client.request type => params
112
+ if status < 200 || status >= 300
113
+ headers['CPEE_SALVAGE'] = true
114
+ c = result[0]&.value
115
+ c = c.read if c.respond_to? :read
116
+ callback([ Riddl::Parameter::Complex.new('error','application/json',StringIO.new(JSON::generate({ 'status' => status, 'error' => c }))) ], headers)
117
+ else
118
+ if headers['CPEE_INSTANTIATION']
119
+ @controller.notify("task/instantiation", :instance => @controller.instance, :label => @label, :instance_name => @controller.info, :instance_uuid => @controller.uuid, :activity => @handler_position, :endpoint => @handler_endpoint, :received => CPEE::ValueHelper.parse(headers['CPEE_INSTANTIATION']), :timestamp => Time.now.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"), :attributes => @controller.attributes_translated)
120
+ end
121
+ if headers['CPEE_CALLBACK'] && headers['CPEE_CALLBACK'] == 'true' && result.any?
122
+ headers['CPEE_UPDATE'] = true
123
+ callback result, headers
124
+ elsif headers['CPEE_CALLBACK'] && headers['CPEE_CALLBACK'] == 'true' && result.empty?
125
+ # do nothing, later on things will happend
126
+ else
127
+ callback result, headers
128
+ end
129
+ end
130
+ end #}}}
151
131
 
152
- status, result, headers = client.request type => params
153
- if status < 200 || status >= 300
154
- callback([ Riddl::Parameter::Complex.new('error','application/json',StringIO.new(JSON::generate({ 'status' => status, 'error' => result[0].value.read }))) ], 'CPEE_SALVAGE' => true)
132
+ def proto_opcua(parameters)
133
+ if @handler_endpoint =~ /^opc\.tcp-read:\/\/([^\/]+)\/(\d+)\/(.+)/
134
+ nid = $3 == $3.to_i.to_s ? $3.to_i : $3
135
+ ns = $2
136
+ url = 'opc.tcp://' + $1
137
+ begin
138
+ client = OPCUA::Client.new(url)
139
+ if (node = client.get ns.to_i, nid)
140
+ result = node.value
155
141
  else
156
- if headers['CPEE_INSTANTIATION']
157
- @controller.notify("task/instantiation", :instance => @controller.instance, :label => @label, :instance_name => @controller.info, :instance_uuid => @controller.uuid, :activity => @handler_position, :endpoint => @handler_endpoint, :received => CPEE::ValueHelper.parse(headers['CPEE_INSTANTIATION']), :timestamp => Time.now.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"), :attributes => @controller.attributes_translated)
142
+ raise 'invalid nodeid'
143
+ end
144
+ client.disconnect
145
+ callback [Riddl::Parameter::Simple.new('value',result)], {}
146
+ rescue => e
147
+ callback([ Riddl::Parameter::Complex.new('error','application/json',StringIO.new(JSON::generate({ 'status' => 0, 'error' => e.message }))) ], 'CPEE_SALVAGE' => true)
148
+ end
149
+ elsif @handler_endpoint =~ /^opc\.tcp-write:\/\/([^\/]+)\/(\d+)\/([^\?]+)(\?value=(.*))?/
150
+ nid = $3 == $3.to_i.to_s ? $3.to_i : $3
151
+ ns = $2
152
+ par = $5
153
+ url = 'opc.tcp://' + $1
154
+ begin
155
+ client = OPCUA::Client.new(url)
156
+ if (node = client.get ns.to_i, nid)
157
+ (parameters[:arguments] || [→(:name => 'value', :value => par)] || []).each do |ele|
158
+ what = CPEE::ValueHelper::parse_extended(ele.value)
159
+ node.value = what
160
+ result = what
158
161
  end
159
- if headers['CPEE_CALLBACK'] && headers['CPEE_CALLBACK'] == 'true' && result.any?
160
- headers['CPEE_UPDATE'] = true
161
- callback result, headers
162
- elsif headers['CPEE_CALLBACK'] && headers['CPEE_CALLBACK'] == 'true' && result.empty?
163
- # do nothing, later on things will happend
164
- else
165
- callback result
162
+ else
163
+ raise 'invalid nodeid'
164
+ end
165
+ client.disconnect
166
+ callback [Riddl::Parameter::Simple.new('value',result)], {}
167
+ rescue => e
168
+ callback([ Riddl::Parameter::Complex.new('error','application/json',StringIO.new(JSON::generate({ 'status' => 0, 'error' => e.message }))) ], 'CPEE_SALVAGE' => true)
169
+ end
170
+ elsif @handler_endpoint =~ /^opc\.tcp-execute:\/\/([^\/]+)\/(\d+)\/([^\?]+)(\?value=(.*))?/
171
+ nid = $3 == $3.to_i.to_s ? $3.to_i : $3
172
+ ns = $2
173
+ par = $5
174
+ url = 'opc.tcp://' + $1
175
+ begin
176
+ client = OPCUA::Client.new(url)
177
+ if (node = client.get ns.to_i, nid)
178
+ params = []
179
+ (parameters[:arguments] || []).each do |ele|
180
+ what = CPEE::ValueHelper::parse_extended(ele.value)
181
+ params << what
166
182
  end
183
+ result = node.call *params
184
+ else
185
+ raise 'invalid nodeid'
167
186
  end
187
+ client.disconnect
188
+ callback [Riddl::Parameter::Simple.new('value',result)], {}
189
+ rescue => e
190
+ callback([ Riddl::Parameter::Complex.new('error','application/json',StringIO.new(JSON::generate({ 'status' => 0, 'error' => e.message }))) ], 'CPEE_SALVAGE' => true)
191
+ end
192
+ end
193
+ end
194
+
195
+ def activity_handle(passthrough, parameters) # {{{
196
+ raise "Wrong endpoint" if @handler_endpoint.nil? || @handler_endpoint.empty?
197
+ @label = parameters[:label]
198
+ @sensors = parameters.dig(:stream,:sensors)
199
+ @aggregators = parameters.dig(:stream,:aggregators)
200
+ @costs = parameters.dig(:stream,:costs)
201
+ @controller.notify("activity/calling", :instance => @controller.instance, :instance_uuid => @controller.uuid, :label => @label, :instance_name => @controller.info, :activity => @handler_position, :passthrough => passthrough, :endpoint => @handler_endpoint, :parameters => parameters, :timestamp => Time.now.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"), :attributes => @controller.attributes_translated)
202
+ if passthrough.to_s.empty?
203
+ if @handler_endpoint.start_with?('opc.tcp')
204
+ proto_opcua parameters
205
+ else
206
+ proto_curl parameters
168
207
  end
169
208
  else
170
209
  @controller.callbacks[passthrough] = CPEE::Callback.new("callback activity: #{@handler_position}",self,:callback,nil,nil,:http)
@@ -178,6 +217,9 @@ class DefaultHandlerWrapper < WEEL::HandlerWrapperBase
178
217
  def activity_result_value # {{{
179
218
  @handler_returnValue
180
219
  end # }}}
220
+ def activity_result_options # {{{
221
+ @handler_returnOptions
222
+ end # }}}
181
223
 
182
224
  def activity_stop # {{{
183
225
  unless @handler_passthrough.nil?
@@ -283,15 +325,15 @@ class DefaultHandlerWrapper < WEEL::HandlerWrapperBase
283
325
  def callback(result=nil,options={})
284
326
  @controller.notify("activity/receiving", :instance => @controller.instance, :label => @label, :instance_name => @controller.info, :instance_uuid => @controller.uuid, :activity => @handler_position, :endpoint => @handler_endpoint, :received => structurize_result(result), :timestamp => Time.now.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"), :attributes => @controller.attributes_translated, :sensors => @sensors, :aggregators => @aggregators, :costs => @costs)
285
327
  result = simplify_result(result)
328
+ @handler_returnValue = result
329
+ @handler_returnOptions = options
286
330
  if options['CPEE_UPDATE']
287
- @handler_returnValue = result
288
331
  if options['CPEE_UPDATE_STATUS']
289
332
  @controller.notify("activity/status", :instance => @controller.instance, :instance_uuid => @controller.uuid, :activity => @handler_position, :endpoint => @handler_endpoint, :status => options['CPEE_UPDATE_STATUS'], :timestamp => Time.now.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"), :attributes => @controller.attributes_translated)
290
333
  end
291
334
  @handler_continue.continue WEEL::Signal::Again
292
335
  else
293
336
  @controller.callbacks.delete(@handler_passthrough)
294
- @handler_returnValue = result
295
337
  @handler_passthrough = nil
296
338
  if options['CPEE_SALVAGE']
297
339
  @handler_continue.continue WEEL::Signal::Salvage
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpee-handlerwrapper-opcua
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juergen eTM Mangler
8
8
  autorequire:
9
9
  bindir: tools
10
10
  cert_chain: []
11
- date: 2019-10-08 00:00:00.000000000 Z
11
+ date: 2019-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cpee
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '1.5'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 1.5.3
22
+ version: 1.5.8
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '1.5'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 1.5.3
32
+ version: 1.5.8
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: opcua
35
35
  requirement: !ruby/object:Gem::Requirement