Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
osie
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nikola Balog
osie
Commits
52c805fd
Commit
52c805fd
authored
May 28, 2024
by
Philipp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Accessing webportal as client not server.
parent
31045f10
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
116 additions
and
42 deletions
+116
-42
opcua-to-http-gw/opcua-to-http-gw.py
opcua-to-http-gw/opcua-to-http-gw.py
+116
-42
No files found.
opcua-to-http-gw/opcua-to-http-gw.py
View file @
52c805fd
...
@@ -5,6 +5,7 @@ Basic OPC UA <-> HTTP gateway server.
...
@@ -5,6 +5,7 @@ Basic OPC UA <-> HTTP gateway server.
import
sys
import
sys
import
asyncio
import
asyncio
from
asyncua
import
Client
,
ua
import
asyncua
import
asyncua
from
dataclasses
import
dataclass
,
field
from
dataclasses
import
dataclass
,
field
import
json
import
json
...
@@ -13,6 +14,7 @@ import urllib
...
@@ -13,6 +14,7 @@ import urllib
import
argparse
import
argparse
import
logging
import
logging
import
__main__
import
__main__
import
random
# command line handling
# command line handling
parser
=
argparse
.
ArgumentParser
(
description
=
'Run OPCUA Server.'
)
parser
=
argparse
.
ArgumentParser
(
description
=
'Run OPCUA Server.'
)
...
@@ -35,6 +37,9 @@ erp5_url = args.erp5_url
...
@@ -35,6 +37,9 @@ erp5_url = args.erp5_url
erp5_username
=
args
.
erp5_username
erp5_username
=
args
.
erp5_username
erp5_password
=
args
.
erp5_password
erp5_password
=
args
.
erp5_password
ERP5_REQUEST_API
=
"ERP5Site_handleOPCUARequest"
ERP5_REQUEST_API
=
"ERP5Site_handleOPCUARequest"
read_backend
=
False
bool_server
=
False
bool_client
=
True
# ERP5 backend storage for OPCUA Document
# ERP5 backend storage for OPCUA Document
@
dataclass
(
frozen
=
True
)
@
dataclass
(
frozen
=
True
)
...
@@ -83,12 +88,61 @@ class InternalSession(asyncua.server.internal_session.InternalSession):
...
@@ -83,12 +88,61 @@ class InternalSession(asyncua.server.internal_session.InternalSession):
# Start OPCUA Server
# Start OPCUA Server
async
def
main
():
async
def
main
():
_logger
=
logging
.
getLogger
(
__name__
)
_logger
=
logging
.
getLogger
(
__name__
)
if
bool_client
:
#VariantType=<VariantType.Boolean
#VariantType=<VariantType.Int64
# Example of creating and sending data to the HTTP backend
def
send_custom_data
(
node
,
val
,
data_type
):
custom_data
=
{
'node'
:
node
,
'val'
:
val
,
'data'
:
data_type
}
# Send the data to the ERP5 backend
erp5_handler
.
call
(
http_method
=
"POST"
,
**
custom_data
)
_logger
.
info
(
"Sent custom data to ERP5 backend"
)
# FHI Server
ipv6
=
"2001:67c:1254:108:404e::a"
url
=
f"opc.tcp://[
{
ipv6
}
]:
{
port
}
/PwDC"
_logger
.
info
(
"Connecting to %s ..."
,
url
)
async
with
Client
(
url
=
url
,
timeout
=
2
)
as
client
:
while
True
:
await
asyncio
.
sleep
(
1
)
# Get the root node
root
=
client
.
get_root_node
()
# Get the objects node
objects
=
await
root
.
get_child
(
"0:Objects"
)
# Get the Powerfuse object
powerfuse_obj
=
await
objects
.
get_child
(
"1:Powerfuse"
)
# Get the variables
machine_status_rot
=
await
powerfuse_obj
.
get_child
(
"1:Powerfuse_Maschinenstatus_Status_Rot"
)
machine_status_gelb
=
await
powerfuse_obj
.
get_child
(
"1:Powerfuse_Maschinenstatus_Status_Gelb"
)
# Read values
value_rot
=
await
machine_status_rot
.
read_value
()
value_gelb
=
await
machine_status_gelb
.
read_value
()
_logger
.
info
(
f"Initial values: Rot=
{
value_rot
}
, Gelb=
{
value_gelb
}
"
)
# Write values
await
machine_status_rot
.
write_value
(
True
)
await
machine_status_gelb
.
write_value
(
False
)
# Start sending custom data in the background
send_custom_data
(
node
=
"Powerfuse_Maschinenstatus_Status_Rot"
,
val
=
value_rot
,
data_type
=
"VariantType=<VariantType.String"
)
if
bool_server
:
# setup our server
# setup our server
server
=
asyncua
.
Server
()
server
=
asyncua
.
Server
()
await
server
.
init
()
await
server
.
init
()
if
bool
(
int
(
ipv6_enabled
)):
if
bool
(
int
(
ipv6_enabled
)):
# todo change name of /freeopcua/server
_logger
.
debug
(
f"Setting endpoint to: opc.tcp://[
{
ipv6
}
]:
{
port
}
/freeopcua/server/"
)
server
.
set_endpoint
(
f"opc.tcp://[
{
ipv6
}
]:
{
port
}
/freeopcua/server/"
)
server
.
set_endpoint
(
f"opc.tcp://[
{
ipv6
}
]:
{
port
}
/freeopcua/server/"
)
#else:
#else:
# server.set_endpoint(f"opc.tcp://{ipv4}:{port}/freeopcua/server/")
# server.set_endpoint(f"opc.tcp://{ipv4}:{port}/freeopcua/server/")
...
@@ -97,6 +151,8 @@ async def main():
...
@@ -97,6 +151,8 @@ async def main():
await
server
.
import_xml
(
xml
)
await
server
.
import_xml
(
xml
)
# read previous state as saved in ERP5 backend
# read previous state as saved in ERP5 backend
#todo: BadNodeIdUnknown is due to the fact that when the server is started the node is not yet created - first create node than read it from backend
if
read_backend
:
erp5_json
=
erp5_handler
.
call
(
http_method
=
"GET"
).
json
()
erp5_json
=
erp5_handler
.
call
(
http_method
=
"GET"
).
json
()
_logger
.
error
(
erp5_json
)
_logger
.
error
(
erp5_json
)
for
k
,
v
in
erp5_json
.
items
():
for
k
,
v
in
erp5_json
.
items
():
...
@@ -118,9 +174,27 @@ async def main():
...
@@ -118,9 +174,27 @@ async def main():
server
.
iserver
.
create_session
=
create_session
server
.
iserver
.
create_session
=
create_session
# Example of creating and sending data to the HTTP backend
async
def
send_custom_data
():
while
True
:
# Create your custom data
custom_data
=
{
'node'
:
'ns=2;s=MyCustomNode'
,
'val'
:
random
.
randint
(
0
,
19
),
'data'
:
'VariantType=<VariantType.Int64'
}
# Send the data to the ERP5 backend
erp5_handler
.
call
(
http_method
=
"POST"
,
**
custom_data
)
_logger
.
info
(
"Sent custom data to ERP5 backend"
)
await
asyncio
.
sleep
(
10
)
# Wait for 10 seconds before sending next data
# Start sending custom data in the background
asyncio
.
create_task
(
send_custom_data
())
# start OPCUA server
# start OPCUA server
_logger
.
info
(
"Starting server!"
)
_logger
.
info
(
"Starting server!"
)
async
with
server
:
async
with
server
:
# End creating nodes for
while
True
:
while
True
:
await
asyncio
.
sleep
(
1
)
await
asyncio
.
sleep
(
1
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment