tests/cli_tests/opcua_test.bash
changeset 3549 0af7b6a96c53
parent 3543 6210c08c1c41
child 3718 7841b651d601
equal deleted inserted replaced
3548:da7b461f54b7 3549:0af7b6a96c53
       
     1 #!/bin/bash
       
     2 
       
     3 rm -f ./SRVOK ./PLCOK
       
     4 
       
     5 # Run server
       
     6 $BEREMIZPYTHONPATH - > >(
       
     7     echo "Start SRV loop"
       
     8     while read line; do 
       
     9         # Wait for server to print modified value
       
    10         echo "SRV>> $line"
       
    11         if [[ "$line" == 3.4 ]]; then
       
    12             echo "PLC could write value"
       
    13             touch ./SRVOK
       
    14         fi
       
    15     done
       
    16     echo "End SRV loop"
       
    17 ) << EOF &
       
    18 
       
    19 import sys
       
    20 import time
       
    21 
       
    22 from opcua import ua, Server
       
    23 
       
    24 server = Server()
       
    25 server.set_endpoint("opc.tcp://127.0.0.1:4840/freeopcua/server/")
       
    26 
       
    27 uri = "http://beremiz.github.io"
       
    28 idx = server.register_namespace(uri)
       
    29 
       
    30 objects = server.get_objects_node()
       
    31 
       
    32 testobj = objects.add_object(idx, "TestObject")
       
    33 testvarout = testobj.add_variable(idx, "TestOut", 1.2)
       
    34 testvar = testobj.add_variable(idx, "TestIn", 5.6)
       
    35 testvar.set_writable()
       
    36 
       
    37 server.start()
       
    38 
       
    39 try:
       
    40     while True:
       
    41         time.sleep(1)
       
    42         print testvar.get_value()
       
    43         sys.stdout.flush()
       
    44 finally:
       
    45     server.stop()
       
    46 EOF
       
    47 SERVER_PID=$!
       
    48 
       
    49 # Start PLC with opcua test
       
    50 setsid $BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \
       
    51      --project-home $BEREMIZPATH/tests/projects/opcua_client build transfer run > >(
       
    52 echo "Start PLC loop"
       
    53 while read line; do 
       
    54     # Wait for PLC runtime to output expected value on stdout
       
    55     echo "PLC>> $line"
       
    56     if [[ "$line" == 1.2 ]]; then
       
    57         echo "PLC could read value"
       
    58         touch ./PLCOK
       
    59     fi
       
    60 done
       
    61 echo "End PLC loop"
       
    62 ) &
       
    63 PLC_PID=$!
       
    64 
       
    65 echo all subprocess started, start polling results
       
    66 res=110  # default to ETIMEDOUT
       
    67 c=30
       
    68 while ((c--)); do
       
    69     if [[ -a ./SRVOK && -a ./PLCOK ]]; then
       
    70         echo got results.
       
    71         res=0  # OK success
       
    72         break
       
    73     else
       
    74         echo waiting.... $c
       
    75         sleep 1
       
    76     fi
       
    77 done
       
    78 
       
    79 # Kill PLC and subprocess
       
    80 echo will kill PLC:$PLC_PID and SERVER:$SERVER_PID
       
    81 pkill -s $PLC_PID 
       
    82 kill $SERVER_PID
       
    83 
       
    84 exit $res