local junit = require("modules/junit")
local logger = require("modules/logger")

local flashCommand = "cd /home/qoitech/myCoolProject && make -j flash > /dev/null 2>&1" -- Command to flash the device
local preFlashDelay = 250          -- Delay after power on until flash command is executed
local postFlashDelay = 5000        -- Delay after power off after flash is completed
local recordDuration = 3100        -- Duration to record
local projectFile = "result.otii"  -- File to save Otii project to
local junitFile = "junit.xml"      -- File to save JUnit xml report to

        name       = name of test
        channel    = name of channel to verify (me, ae)
        target     = expected value in SI unit (i.e. Joules for energy)
        start      = start of the measurement in ms
        stop       = end of the measurement in ms
        lowMargin  = how many percent below target value is acceptable, set to -1 to skip check
        highMargin = how many percent above target value is acceptable, set to -1 to skip check

tests = {{name="Boot",        channel="me", target=0.021,   start=290,  stop=820,  lowMargin=3,   highMargin=3},
         {name="Idle",        channel="me", target=0.00007, start=2500, stop=3000, lowMargin=50,  highMargin=0},
         {name="Advertising", channel="me", target=0.0032,  start=900,  stop=1750, lowMargin=-1,   highMargin=-1}

-- Add tests to junit module
for _,test in ipairs(tests) do
    junit.addTest(,,, test.start, test.stop, test.lowMargin, test.highMargin)

-- Open device and project
local devices = otii.get_devices("Arc")
assert(#devices > 0, "No available devices")
local device = otii.open_device(devices[1].id)
assert(device ~= nil, "Cannot open device")

local project = otii.get_active_project()
if project == nil then
   project = otii.create_project()
assert(project ~= nil, "Cannot open/create project")

-- Enable main channel and set range to low (auto), it also enables the energy channel (me)
device:enable_channel("mc", true)

-- Flash the device with latest software

device:set_main_voltage(3.3)             -- Set output voltage to 3.3 V
device:enable_main(true)                 -- Turn on output voltage
device:enable_5v(true)                   -- Enable 5V in expansion port to power switch board
device:set_gpo(1, true)                  -- Set GPO1 to high, connects the debugger on the switch board

otii.msleep(preFlashDelay);                            -- Sleep before flashing starts to make sure things are up
otii.writeln("\nFlashing device...")
local flashResult = os.execute(flashCommand)           -- Execute the flash command to flash the device
assert(flashResult == true, "Failed to flash device")

device:set_gpo(1, false)                               -- Set GPO1 low, disconnect the debugger on the switch board
device:enable_5v(false)                                -- Disable power to switch board

-- Make a new recording on the new firmware after an initial delay after flashing
otii.writeln("Wait " .. postFlashDelay/1000.0 .. " seconds after power was cut.")

otii.writeln("Starting " .. recordDuration/1000.0 .. " seconds recording, turning power on.")

local rec_id = project:get_last_recording_id()

-- Evaluate the tests defined above, returns a table with a pass indicator and individual test information
local testResults = junit.evaluate(project, device, rec_id)


otii.writeln("Total: " .. .. ", passed: " .. testResults.passed .. ", failed: " .. testResults.failed .. ", disabled: " .. testResults.disabled)
for _,test in ipairs(testResults.tests) do
    if test.state == "failed" then

-- Save the project for reference
assert(project:save(projectFile), "Failed to save project as " .. projectFile)

-- Save an JUnit xml file
generated by LDoc 1.4.6 Last updated 2021-10-13 21:46:39