name: $(BuildID)_$(BuildDefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)

schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - master
    
jobs:
- job: Cross_Compile
  pool:
    vmImage: ubuntu-16.04
  steps:
  - script: |
      . build_all/mbed5/setup.sh
    displayName: 'setup'

  - script: |
      . build_all/mbed5/build.sh
    displayName: 'build'
    env:
      MXCHIP_REPO_SOURCE_BRANCH: $(MXCHIP_REPO_SOURCE_BRANCH)

  - script: |
      # Check that this is the right source directory
      ls
      cp mxchip/BUILD/AZ3166/GCC_ARM/mxchip.bin $(Build.ArtifactStagingDirectory)
    displayName: "Copy Files into Artifact repo"

  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifacts: drop'
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)'
      artifactName: 'drop'
      publishLocation: 'Container'
    condition: succeeded()

- job: Execute
  dependsOn: Cross_Compile
  pool:
    name: $(MXCHIPAGENTPOOL)
  steps:
  - task: DownloadBuildArtifacts@0
    displayName: 'Download Build Artifacts'
    inputs:
      artifactName: 'drop'

  - script: |
      echo $(NEWTIN) | sudo -S cp $(System.ArtifactsDirectory)/drop/mxchip.bin /media/newt/$MXCHIP_NGIHTLY_FILE_DESCRIPTOR
    displayName: 'deploy'
    env:
      MXCHIP_NGIHTLY_FILE_DESCRIPTOR: $(MXCHIP_NGIHTLY_FILE_DESCRIPTOR)


  - script: |
      ls
      cd $(Build.SourcesDirectory)/testtools/UART_interface/
      . input.sh
      python3 serial_connect.py -i input.txt -o output.txt -b 115200 -p $(MXCHIP_NIGHTLY_PORT) -m $(MXCHIP_NIGHTLY_FILE_DESCRIPTOR)
      rm input.txt
    env:
      # Most up to date SERIAL_TASKs output can be learned by passing 'help' into input.txt,
      # Current SERIAL_TASK commands:
      # help, 
      # version, 
      # exit (also resets device), 
      # set_wifissid [args],
      # set_wifipwd [args], 
      # set_az_iothub [args], 
      # set_dps_uds [args], 
      # enable_secure [args],
      # send_telemetry [args]

      # Note: SERIAL_TASK can be a single, or several commands sent to device, seperated by \r\n\r\n
      SERIAL_TASK: $(SERIAL_TASK) 
      IOTHUB_CONNECTION_STRING: $(MXCHIP_NIGHTLY_DEVICE_STRING)
    displayName: 'run_test'

  - script: |
      # Check that this is the right source directory
      ls
      cp $(Build.SourcesDirectory)/testtools/UART_interface/output.txt $(Build.ArtifactStagingDirectory)
    displayName: "Copy Files into Artifact repo"

  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifacts: log'
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)'
      artifactName: 'log'
      publishLocation: 'Container'
    condition: succeeded()
    
