edouard@3543: #!/bin/bash edouard@3543: edouard@3549: rm -f ./SRVOK ./PLCOK edouard@3543: edouard@3631: edouard@3631: yes "" | openssl req -x509 -newkey rsa:2048 -keyout my_private_key.pem -out my_cert.pem \ edouard@3631: -days 355 -nodes -addext "subjectAltName = URI:urn:example.org:FreeOpcUa:python-opcua" edouard@3631: openssl x509 -outform der -in my_cert.pem -out my_cert.der edouard@3631: edouard@3549: # Run server edouard@3549: $BEREMIZPYTHONPATH - > >( edouard@3549: echo "Start SRV loop" edouard@3549: while read line; do edouard@3549: # Wait for server to print modified value edouard@3549: echo "SRV>> $line" edouard@3549: if [[ "$line" == 3.4 ]]; then edouard@3549: echo "PLC could write value" edouard@3549: touch ./SRVOK edouard@3549: fi edouard@3549: done edouard@3549: echo "End SRV loop" edouard@3549: ) << EOF & edouard@3543: edouard@3549: import sys edouard@3549: import time edouard@3549: edouard@3549: from opcua import ua, Server edouard@3549: edouard@3549: server = Server() edouard@3549: server.set_endpoint("opc.tcp://127.0.0.1:4840/freeopcua/server/") edouard@3549: edouard@3631: server.set_security_policy([ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt]) edouard@3631: server.load_certificate("my_cert.der") edouard@3631: server.load_private_key("my_private_key.pem") edouard@3631: edouard@3549: uri = "http://beremiz.github.io" edouard@3549: idx = server.register_namespace(uri) edouard@3549: edouard@3549: objects = server.get_objects_node() edouard@3549: edouard@3549: testobj = objects.add_object(idx, "TestObject") edouard@3549: testvarout = testobj.add_variable(idx, "TestOut", 1.2) edouard@3549: testvar = testobj.add_variable(idx, "TestIn", 5.6) edouard@3549: testvar.set_writable() edouard@3549: edouard@3549: server.start() edouard@3549: edouard@3549: try: edouard@3549: while True: edouard@3549: time.sleep(1) edouard@3549: print testvar.get_value() edouard@3549: sys.stdout.flush() edouard@3549: finally: edouard@3549: server.stop() edouard@3549: EOF edouard@3549: SERVER_PID=$! edouard@3549: edouard@3634: PROJECT_FILES_DIR=$BEREMIZPATH/tests/projects/opcua_client_encrypted/project_files edouard@3634: mkdir $PROJECT_FILES_DIR edouard@3634: cp my_cert.der my_private_key.pem $PROJECT_FILES_DIR edouard@3631: edouard@3549: # Start PLC with opcua test edouard@3549: setsid $BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \ edouard@3631: --project-home $BEREMIZPATH/tests/projects/opcua_client_encrypted build transfer run > >( edouard@3549: echo "Start PLC loop" edouard@3549: while read line; do edouard@3549: # Wait for PLC runtime to output expected value on stdout edouard@3549: echo "PLC>> $line" edouard@3549: if [[ "$line" == 1.2 ]]; then edouard@3549: echo "PLC could read value" edouard@3549: touch ./PLCOK edouard@3549: fi edouard@3549: done edouard@3549: echo "End PLC loop" edouard@3549: ) & edouard@3549: PLC_PID=$! edouard@3549: edouard@3549: echo all subprocess started, start polling results edouard@3549: res=110 # default to ETIMEDOUT edouard@3631: c=45 edouard@3549: while ((c--)); do edouard@3549: if [[ -a ./SRVOK && -a ./PLCOK ]]; then edouard@3549: echo got results. edouard@3549: res=0 # OK success edouard@3549: break edouard@3549: else edouard@3549: echo waiting.... $c edouard@3549: sleep 1 edouard@3543: fi edouard@3543: done edouard@3543: edouard@3549: # Kill PLC and subprocess edouard@3549: echo will kill PLC:$PLC_PID and SERVER:$SERVER_PID edouard@3549: pkill -s $PLC_PID edouard@3549: kill $SERVER_PID edouard@3549: edouard@3549: exit $res