Skip to content

Instantly share code, notes, and snippets.

@betzrhodes
Created July 31, 2019 19:34
Show Gist options
  • Save betzrhodes/aa5266cb552b401c191b1a0266a4a4c9 to your computer and use it in GitHub Desktop.
Save betzrhodes/aa5266cb552b401c191b1a0266a4a4c9 to your computer and use it in GitHub Desktop.
Ping Time Example
server.log("AGENT RUNNING...");
// Setup listener for device side ping
device.on("dPing", function(pingStart) {
// Send pong back to device with the starting timestamp
device.send("dPong", pingStart);
})
device.on("aPong", function(pingStart) {
local now = date();
local diffSec = now.time - pingStart.time;
local diffUSec = now.usec - pingStart.usec;
local roundTrip = (diffUSec / 1000) + (diffSec * 1000);
server.log("Round trip agent-dev: " + roundTrip + "ms");
server.log("Estimated agent to device: " + roundTrip/2 + "ms");
})
imp.wakeup(5, function() {
server.log("Sending agent ping");
device.send("aPing", date());
})
server.log("DEVICE RUNNING...");
imp.enableblinkup(true);
server.log(imp.getsoftwareversion());
agent.on("aPing", function(pingStart) {
agent.send("aPong", pingStart);
})
// Setup listener for device side response
agent.on("dPong", function(pingStart) {
local roundTrip = hardware.millis() - pingStart;
server.log("Round trip dev-agent: " + roundTrip + "ms");
server.log("Estimated device to agent: " + roundTrip/2 + "ms");
})
imp.wakeup(1, function() {
// Send device side ping
server.log("Sending Device Ping");
agent.send("dPing", hardware.millis());
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment