---
- name: Automated Black Diamond C2R Bringup
  tasks:
    - name: Welcome
      shell: read -n1 -s; echo -e "\nLet's go!"
      message: |
        Welcome to the automated L3 bringup!
        Before continue ensure the vehicle is connected to the internet via wired connection
        The vehicle should be a C2R Black Diamond-spec vehicle. If it is not this specification,
        please do not run this script.

        Please disconnect all unconfigured radars and lidars.

        Press any key to start
      stdout: True
      success_when: "True"

    # Check if any unconfigured are on the network because they
    # will interfere with network traffic and their configuration
    - name: Verify all unconfigured sensors are disconnected
      shell: |
        SENSOR_CONNECTED=true
        while [ "$SENSOR_CONNECTED" = true ]
        do
          SENSOR_CONNECTED=false
          NETWORK_DUMP=$(sudo tcpdump -i eth0 -c1000 2>&1)
          if echo "$NETWORK_DUMP" | grep "192.168.1.201\."
          then
            echo -e "Unconfigured lidar is still connected!"
            SENSOR_CONNECTED=true
          fi
          if echo "$NETWORK_DUMP" | grep "192.168.1.2\."
          then
            echo -e "Unconfigured radar is still connected!"
            SENSOR_CONNECTED=true
          fi
        done
        echo -e "\nAll unconfigured sensors have been disconnected "
      message: "Checking for unconfigured sensors ..."
      stdout: True
      filter: "connected"
      success_when: returncode == 0

    - name: Pepwave Self-Test
      shell: ./vehicle/hwsd/pepwave_test/pepwave_test
      message: Performing self-test ...
      stdout: True
      stderr: True
      success_when: returncode == 0

    - name: PCAN Gateway Self-Test
      message: |
        Pinging llc-gateway1 ...
        Pinging llc-gateway2 ...
      shell: |
        ping -c5 -i0.25 -w2 192.168.3.24
        RC=$?
        ping -c5 -i0.25 -w2 192.168.3.25
        exit $(($RC + $?))
      success_when: returncode == 0

    - name: MoFo Ping Test
      shell: |
        echo 192.168.3.8: `ping -c5 -i0.25 -w5 192.168.3.8 | egrep -oh '[0-9]+% packet loss'`
      stdout: True
      stderr: True
      success_when: "sum(map(int, re.findall(': (\\d+)% packet', output))) == 0"

    - name: Network Switch Test
      shell: ./vehicle/bringup/helper/network_switch_test
      stdout: True
      success_when: "not 'FAILED' in output"

    - name: Pull Latest Master in Calibration Repository
      shell: |
        cd /etc/zoox/calibration
        git pull
      stdout: True
      stderr: True
      success_when: returncode == 0

    - name: Camera Test
      shell: ./vehicle/hwsd/camera_test/camera_test
      stdout: True
      filter: Camera
      timeout: 600
      success_when: "not 'Failed to start camera' in output"

    - name: LTE Speed Test
      shell: |
        read -n1 -s
        echo Continuing ...
        curl -o /dev/null -sw 'Download speed: %{speed_download}\n' http://sun-speedtest01.zooxlabs.com/garbage.php?ckSize=25
      stdout: True
      stderr: True
      message: "Please disconnect network cable from vehicle for this test. Press any key to continue ..."
      success_when: "float('0'+re.findall('Download speed: (\\d+\\.\\d+)|$', output)[0]) > 50000" # > 0.5 Mbps
      timeout: 180

    - name: Wired Network Speed Test
      shell: |
        read -n1 -s
        echo Continuing ...
        curl -o /dev/null -sw 'Download speed: %{speed_download}\n' http://sun-speedtest01.zooxlabs.com/garbage.php?ckSize=25
      stdout: True
      message: "Please connect network cable from vehicle for this test. Press any key to continue ..."
      success_when: "float('0'+re.findall('Download speed: (\\d+\\.\\d+)|$', output)[0]) > 50000000" # > 500 Mbps

    - name: Individual Radar Configure
      shell: |
        INDEX=1
        ADDR=(32 33 34 35 36 37 38 39 31 30)
        read -n1 -s
        echo -e "Starting!\n"
        for x in "${ADDR[@]}"
        do
          echo Please connect radar $INDEX. Press ENTER to continue ...
          read -n1 -s
          sleep 2
          if sudo tcpdump -i eth0 -c5000 -q | grep --quiet 192.168.1.2
          then
            echo "Programming radar $INDEX"
            ./vehicle/sensors/radar/brand --id $INDEX
          fi

          if sudo tcpdump -n -c5000 | grep --quiet 192.168.3.$x
          then
            echo -e "Radar $INDEX configured\n"
          else
            echo -e "Radar $INDEX not configured!\n"
          fi
          INDEX=$((INDEX+1))
        done
      message: Please disconnect all radars. Press ENTER to continue ...
      stdout: 'True'
      success_when: "not 'not configured' in output"

    - name: Individual Lidar Configure
      shell: stdbuf -o0 -e0 ./vehicle/bringup/helper/configure_lidar
      message: "Configuring lidars ... "
      stdout: True
      stderr: True
      success_when: "returncode == 0"

    - name: TIB Self-Test
      shell: |
        EXIT_CODES=0
        addr=(188 190 191 192 193 194 195 196 197)
        for i in "${addr[@]}"
        do
          echo 192.168.3.$i: `ping -c5 -i0.25 -w5 192.168.3.$i | egrep -oh '[0-9]+% packet loss'`
        done
      stdout: True
      stderr: True
      success_when: "sum(map(int, re.findall(': (\\d+)% packet', output))) == 0"

    - name: LLC Self-Test
      shell: ./vehicle/bringup/helper/llc_vehicle_test
      stdout: True
      stderr: True
      success_when: "not 'FAILED' in output"

    - name: Installing Puffin
      shell: |
        ssh operator ~/driving/vehicle/puffin/install.sh
      stdout: True
      success_when: returncode == 0

    - name: Run sanity checks
      shell: ./vehicle/sanity_checker/cli
      stdout: True
      stderr: True
      success_when: returncode == 0

    - name: Deploy branch
      shell: |
        echo " "
        echo "What is the name of the calibration branch?"
        read -p " " CAL_BRANCH
        zstart --local --kraken master calibration=$CAL_BRANCH
      message: "Please commit the calibration to the repository"
      success_when: returncode == 0
      stdout: True
      stderr: True
