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@3718: import os edouard@3549: import time edouard@3820: import asyncio edouard@3549: edouard@3820: from asyncua import ua, Server edouard@3820: from asyncua.server.users import User, UserRole edouard@3549: edouard@3820: # Asyncua can't work without (over)simple shared cerificates/privkey. edouard@3820: # No user is involved in that case, but asyncua needs it. edouard@3820: # Over permessive User Manager hereafter helps cuting that corner. edouard@3820: class AllAdminUserManager: edouard@3820: def get_user(self, iserver, username=None, password=None, certificate=None): edouard@3820: return User(role=UserRole.Admin) edouard@3549: edouard@3820: async def main(): edouard@3820: server = Server(user_manager=AllAdminUserManager()) edouard@3820: host = os.environ.get("OPCUA_DEFAULT_HOST", "127.0.0.1") edouard@3820: endpoint = "opc.tcp://"+host+":4840/freeopcua/server/" edouard@3820: await server.init() edouard@3820: server.set_endpoint(endpoint) edouard@3631: edouard@3820: server.set_security_policy([ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt]) edouard@3820: await server.load_certificate("my_cert.der") edouard@3820: await server.load_private_key("my_private_key.pem") edouard@3549: edouard@3820: uri = "http://beremiz.github.io" edouard@3820: idx = await server.register_namespace(uri) edouard@3549: edouard@3820: objects = server.get_objects_node() edouard@3549: edouard@3820: testobj = await objects.add_object(idx, "TestObject") edouard@3820: testvarout = await testobj.add_variable(idx, "TestOut", 1.2) edouard@3820: testvar = await testobj.add_variable(idx, "TestIn", 5.6) edouard@3820: await testvar.set_writable() edouard@3549: edouard@3820: await server.start() edouard@3820: edouard@3820: try: edouard@3820: while True: edouard@3820: await asyncio.sleep(1) edouard@3820: print(await testvar.get_value()) edouard@3820: sys.stdout.flush() edouard@3820: finally: edouard@3820: await server.stop() edouard@3820: edouard@3820: asyncio.run(main()) edouard@3820: 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