Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Electric Imp imp API server.bless() example
// ***************************************
// ***** 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));
}
});
});
// ***************************************
// ** 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