cpee-handlerwrapper-opcua 0.1 → 0.6

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: e9486fd693ad758f8606465a4e1ab91b76630c8c86058b429c0c163b694ef4f0
4
- data.tar.gz: d5dddcb07d26ed7e3f119f7aae9b9ead68b9f89d367e37d69f66d54f86599be4
3
+ metadata.gz: ab1161f5be28bb3a84473aaa83b76fb5a064f184953a30a92dbd6b48a50a451f
4
+ data.tar.gz: 250ee2bf82851194e882349d9dcfef3089305291004745189e2da272797e5a6e
5
5
  SHA512:
6
- metadata.gz: bc9a13724855dd7043bfbe5f2c929f89b043a676e8a3745f385dee8384f2340ebc77621131fdcd963ebe54501aeaefb4eb82945026ff9901ac466480c7dd991a
7
- data.tar.gz: 761142229b1e07199d1eaa40f4cbe50d741523847a3be334fdcc5c5c3bbad51f94488769d42cc2c36933575747c274ed30a12b8d090ccf5fde6a1d4c82bbe42d
6
+ metadata.gz: e9897eabccaa6e3d2258f61d9ec1d9a3d2d657a96c1491b80bd4fd8788593ee8fd6cdc8c7b62a45b9fc9d5675a01088a9b2f60062f6e173cabf6dd2e45c4bd6e
7
+ data.tar.gz: 347988695dab6f0ac3325706c7db7136c61213f46660405d4a6f5d73d7284dd65b2af1199567bb8b1db55a4aec10e821c3e320ab8bcedab94ae9cf6d44af9697
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cpee-handlerwrapper-opcua"
3
- s.version = "0.1"
3
+ s.version = "0.6"
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.0'
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'
@@ -14,7 +14,7 @@
14
14
 
15
15
  require 'opcua/client'
16
16
 
17
- class OPCUAHandlerWrapper < WEEL::HandlerWrapperBase
17
+ class DefaultHandlerWrapper < WEEL::HandlerWrapperBase
18
18
  def self::inform_state_change(arguments,newstate) # {{{
19
19
  controller = arguments[0]
20
20
  controller.serialize_state!
@@ -44,127 +44,166 @@ class OPCUAHandlerWrapper < 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[:sensors]
199
+ @aggregators = parameters[:aggregators]
200
+ @costs = parameters[: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 OPCUAHandlerWrapper < 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 OPCUAHandlerWrapper < 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,35 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpee-handlerwrapper-opcua
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.6'
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-07 00:00:00.000000000 Z
11
+ date: 2019-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cpee
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 1.5.0
20
17
  - - "~>"
21
18
  - !ruby/object:Gem::Version
22
19
  version: '1.5'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.8
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: 1.5.0
30
27
  - - "~>"
31
28
  - !ruby/object:Gem::Version
32
29
  version: '1.5'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.5.8
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: opcua
35
35
  requirement: !ruby/object:Gem::Requirement