-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathFactoryFixture_SensorNodeTesting.agent.nut
123 lines (98 loc) · 4.2 KB
/
FactoryFixture_SensorNodeTesting.agent.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#require "FactoryTools.class.nut:2.1.0"
class SensorNodeFactoryAgent {
constructor(debug = false) {
FactoryTools.isFactoryFirmware(function(isFactoryEnv) {
if (isFactoryEnv) {
FactoryTools.isDeviceUnderTest() ? RunDeviceUnderTest(debug) : RunFactoryFixture(debug);
} else {
server.log("This firmware is not running in the Factory Environment");
}
}.bindenv(this));
}
RunFactoryFixture = class {
debug = null;
constructor(_debug) {
debug = _debug;
// Handle incomming HTTP requests from DUT
http.onrequest(HTTPReqHandler.bindenv(this));
if (debug) server.log("Running Factory Fixture Flow");
}
function HTTPReqHandler(req, res) {
switch (req.method) {
case "POST":
try {
local data = http.jsondecode(req.body);
if ("mac" in data) {
// Send the device’s data to the BlinkUp fixture
device.send("data.to.print", data);
// Confirm successful receipt
res.send(200, "OK");
} else {
// Unexpected request
res.send(404, "Not Found");
}
} catch(err) {
res.send(400, err);
}
break;
default :
// Unexpected request
res.send(404, "Not Found");
}
}
}
RunDeviceUnderTest = class {
debug = null;
testResults = null;
constructor(_debug) {
debug = _debug;
testResults = {"passed" : [], "failed" : []};
device.on("set.label.data", setLabelHandler.bindenv(this));
device.on("test.result", testResultHandler.bindenv(this));
device.on("get.test.results", sendResults.bindenv(this));
device.on("clear.test.resluts", clearResults.bindenv(this));
if (debug) server.log("Running Device Under Test Flow");
}
function clearResults(nada) {
testResults = {"passed" : [], "failed" : []};
}
function sendResults(nada) {
device.send("send.test.results", testResults);
}
function testResultHandler(testResult) {
// In full production may want to add code to push test results to a web service
if ("err" in testResult && testResult.err != null) {
testResults.failed.push(testResult.err);
} else if ("msg" in testResult) {
testResults.passed.push(testResult.msg);
}
}
function setLabelHandler(deviceData) {
// Get the URL of the BlinkUp fixture that configured the unit under test
local fixtureAgentURL = imp.configparams.factory_fixture_url;
if (debug) server.log(fixtureAgentURL);
if (fixtureAgentURL != null) {
// Relay the DUT’s data (MAC, deviceID) to the factory BlinkUp fixture via HTTP
local header = { "content-type" : "application/json" };
local body = http.jsonencode(deviceData);
local req = http.post(fixtureAgentURL, header, body);
// Wait for a response before proceeding, ie. pause operation until
// fixture confirms receipt. We need label printing and UUT’s position
// on the assembly line to stay in sync
local res = req.sendsync();
if (res.statuscode != 200) {
// Issue a simple error here; real firmware would need a more advanced solution
server.error("Problem contacting fixture...");
server.error(res.body);
}
} else {
server.error("Factory Fixture URL not found.");
}
}
}
}
// Runtime
// --------------------------------------
server.log("Agent Running...");
local ENABLE_DEBUG_LOGS = true;
SensorNodeFactoryAgent(ENABLE_DEBUG_LOGS);