rm -f ./CLI_OK ./PLC_OK ./PLC_CONNECTED
export BEREMIZ_APPDATA=`pwd`/AppData
mkdir -p $BEREMIZ_APPDATA
KEYSTORE=$BEREMIZ_APPDATA/keystore
# Set BEREMIZ_LOCAL_HOST to localhost if not already set
: ${BEREMIZ_LOCAL_HOST:=localhost}
# Start runtime one first time to generate PLC PSK
$BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_service.py -s psk.txt -n test_wamp_ID -x 0 &
res=110 # default to ETIMEDOUT
if [[ -a psk.txt ]]; then
if [ "$res" != "0" ] ; then
echo timeout generating PSK.
IFS=':' read -r PLC_wamp_ID PLC_wamp_secret < psk.txt
URI="WAMPS://${BEREMIZ_LOCAL_HOST}:8888/ws#Automation#${PLC_wamp_ID}"
cp -a $BEREMIZPATH/tests/projects/wamp .
sed -i "s,TEST_URI,${URI},g" wamp/beremiz.xml
# Start CLI one first time to generate IDE PSK
IDE_PSK=$KEYSTORE/own/default.psk
$BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \
--project-home wamp connect &
res=110 # default to ETIMEDOUT
if [[ -a $IDE_PSK ]]; then
echo waiting IDE PSK.... $c
if [ "$res" != "0" ] ; then
echo timeout generating IDE PSK.
IFS=':' read -r IDE_wamp_ID IDE_wamp_secret < $IDE_PSK
# Prepare crossbar server configuration
mkdir -p certs/server .crossbar
openssl req -nodes -new -x509 -keyout certs/server/server.key \
-subj "/C=FR/L=Paris/O=Beremiz/OU=server/CN=${BEREMIZ_LOCAL_HOST}" \
-addext "subjectAltName=DNS:${BEREMIZ_LOCAL_HOST}" \
-out certs/server/server.crt
cp certs/server/server.key ./.crossbar/server.key
cp certs/server/server.crt ./.crossbar/server.crt
cat > .crossbar/config.json <<JsonEnd
"id": "automation_router",
"certificate": "server.crt",
"secret": "${IDE_wamp_secret}",
"secret": "${PLC_wamp_secret}",
crossbar start &> crossbar_log.txt &
res=110 # default to ETIMEDOUT
if [[ -a .crossbar/node.pid ]]; then
echo wait for crossbar to start.... $c
if [ "$res" != "0" ] ; then
echo timeout starting crossbar.
# give more time to crossbar
# Prepare runtime Wamp config
cat > wampconf.json <<JsonEnd
"url": "wss://${BEREMIZ_LOCAL_HOST}:8888/ws"
# Re-use self-signed server cert for client
cp .crossbar/server.crt wampTrustStore.crt
# Start Beremiz runtime again, with wamp enabled
$BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_service.py -c wampconf.json -s psk.txt -n test_wamp_ID -x 0 &> >(
# Wait for server to print modified value
if [[ "$line" =~ "WAMP session joined" ]]; then
if [[ "$line" == "PLCobject : PLC started" ]]; then
echo "PLC was programmed"
echo wait for runtime to come up
res=110 # default to ETIMEDOUT
if [[ -a ./PLC_CONNECTED ]]; then
if [ "$res" != "0" ] ; then
echo timeout connecting PLC to crossbar.
# Re-use self-signed server cert for client in test project
cp .crossbar/server.crt $IDE_CERT/${BEREMIZ_LOCAL_HOST}.crt
# Use CLI to build transfer and start PLC
$BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \
--project-home wamp build transfer run &> >(
# Wait for PLC runtime to output expected value on stdout
if [[ "$line" == "PLC installed successfully." ]]; then
echo "CLI did transfer PLC program"
echo all subprocess started, start polling results
res=110 # default to ETIMEDOUT
if [[ -a ./CLI_OK && -a ./PLC_OK ]]; then
# Kill PLC and subprocess
echo will kill PLC:$PLC_PID, SERVER:$SERVER_PID and CLI:$CLI_PID