Last active
August 6, 2020 09:23
-
-
Save ElectricImpSampleCode/1bf003a0c51ce9dfae66e820484ded0d to your computer and use it in GitHub Desktop.
Electric Imp imp API server.bless() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *************************************** | |
// ***** SETUP ***** | |
// *************************************** | |
// CONSTANTS | |
// Replace the following string with your server logging endpoint | |
const RESULTS_URL = "YOUR_RESULT_LOGGING_URL"; | |
// *************************************** | |
// ***** DUT AGENT FUNCTIONS ***** | |
// *************************************** | |
// Set up an receiver for "test.result" messages from the device under test (DUT) | |
device.on("test.result", function(data) { | |
local body = http.jsonencode(data, {"compact":true}); | |
local deviceID = data.device_id; | |
local headers = {"Content-Type": "application/json"}; | |
// The following line is helpful for debugging factory firmware during development. | |
server.log(format("Posting message 'test results' for device %s: %s.", deviceID, body)); | |
// Send the test result data to your server | |
http.post(RESULTS_URL, headers, body).sendasync(function(response) { | |
if (response.statuscode >= 300) { | |
// The following line will not be called in a production environment | |
// unless you have enabled logging for the specific device via impCentral | |
server.error(format("Failed posting test results for device %s with response %d:%s.", deviceID, response.statuscode, response.body)); | |
} else { | |
// Success | |
server.log(format("Message 'test results' for device %s: %s has been received.", deviceID, body)); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *************************************** | |
// ** DEVICE UNDER TEST (DUT) FUNCTIONS ** | |
// *************************************** | |
function blessDeviceUnderTest() { | |
// Run any tests you require for you DUT | |
local testResult = testDeviceHardware(); | |
// Attempt to bless this device, and send the result to the Factory Test Results Webhook | |
server.bless(testResult, function(blessSuccess) { | |
// Send identifying info and result to the test results webhook | |
agent.send("test.result", { "device_id": hardware.getdeviceid(), | |
"success": (blessSuccess ? "SUCCEEDED" : "FAILED") }); | |
// Clear the WiFi credentials and factory token then | |
// reboot if blessing completes successfully | |
if (blessSuccess) imp.clearconfiguration(); | |
// The imp will go idle at this point, allowing impOS to install | |
// application code and restart the Squirrel VM if the Production Device Group’s | |
// devices are set to receive application code immediately after blessing | |
// If you are installing your application upon device activation (first | |
// end-user BlinkUp), you can power down the device at this point | |
}); | |
} | |
// *************************************** | |
// ***** YOUR HARDWARE TEST CODE ***** | |
// *************************************** | |
function testDeviceHardware() { | |
// Run your tests here | |
// Returning false will prevent blessing and pass a "FAILED" to the test results webhook | |
return true; | |
} | |
// *************************************** | |
// ***** RUNTIME START ***** | |
// *************************************** | |
// Test and bless the DUT | |
blessDeviceUnderTest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment