author | Edouard Tisserant <edouard@beremiz.fr> |
Wed, 10 Jul 2024 11:10:05 +0200 | |
changeset 3985 | d0c5d77a33af |
parent 3820 | 46f3ca3f0157 |
permissions | -rw-r--r-- |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
1 |
#!/bin/bash |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
2 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
3 |
rm -f ./SRVOK ./PLCOK |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
4 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
5 |
# Run server |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
6 |
$BEREMIZPYTHONPATH - > >( |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
7 |
echo "Start SRV loop" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
8 |
while read line; do |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
9 |
# Wait for server to print modified value |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
10 |
echo "SRV>> $line" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
11 |
if [[ "$line" == 3.4 ]]; then |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
12 |
echo "PLC could write value" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
13 |
touch ./SRVOK |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
14 |
fi |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
15 |
done |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
16 |
echo "End SRV loop" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
17 |
) << EOF & |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
18 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
19 |
import sys |
3718
7841b651d601
Tests: OPCUA tests can now use OPCUA_DEFAULT_HOST environment variable to force host to something else than "127.0.0.1" or "localhost"
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
20 |
import os |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
21 |
import time |
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
22 |
import asyncio |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
23 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
24 |
from asyncua import Server |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
25 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
26 |
async def main(): |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
27 |
server = Server() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
28 |
host = os.environ.get("OPCUA_DEFAULT_HOST", "127.0.0.1") |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
29 |
endpoint = "opc.tcp://"+host+":4840/freeopcua/server/" |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
30 |
await server.init() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
31 |
server.set_endpoint(endpoint) |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
32 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
33 |
uri = "http://beremiz.github.io" |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
34 |
idx = await server.register_namespace(uri) |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
35 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
36 |
objects = server.get_objects_node() |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
37 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
38 |
testobj = await objects.add_object(idx, "TestObject") |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
39 |
testvarout = await testobj.add_variable(idx, "TestOut", 1.2) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
40 |
testvar = await testobj.add_variable(idx, "TestIn", 5.6) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
41 |
await testvar.set_writable() |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
42 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
43 |
await server.start() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
44 |
try: |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
45 |
while True: |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
46 |
await asyncio.sleep(1) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
47 |
print(await testvar.get_value()) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
48 |
sys.stdout.flush() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
49 |
finally: |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
50 |
await server.stop() |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
51 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
52 |
asyncio.run(main()) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
53 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
54 |
EOF |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
55 |
SERVER_PID=$! |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
56 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
57 |
# Start PLC with opcua test |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
58 |
setsid $BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \ |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
59 |
--project-home $BEREMIZPATH/tests/projects/opcua_client build transfer run > >( |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
60 |
echo "Start PLC loop" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
61 |
while read line; do |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
62 |
# Wait for PLC runtime to output expected value on stdout |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
63 |
echo "PLC>> $line" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
64 |
if [[ "$line" == 1.2 ]]; then |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
65 |
echo "PLC could read value" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
66 |
touch ./PLCOK |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
67 |
fi |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
68 |
done |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
69 |
echo "End PLC loop" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
70 |
) & |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
71 |
PLC_PID=$! |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
72 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
73 |
echo all subprocess started, start polling results |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
74 |
res=110 # default to ETIMEDOUT |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
75 |
c=30 |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
76 |
while ((c--)); do |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
77 |
if [[ -a ./SRVOK && -a ./PLCOK ]]; then |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
78 |
echo got results. |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
79 |
res=0 # OK success |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
80 |
break |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
81 |
else |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
82 |
echo waiting.... $c |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
83 |
sleep 1 |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
84 |
fi |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
85 |
done |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
86 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
87 |
# Kill PLC and subprocess |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
88 |
echo will kill PLC:$PLC_PID and SERVER:$SERVER_PID |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
89 |
pkill -s $PLC_PID |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
90 |
kill $SERVER_PID |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
91 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
92 |
exit $res |