I think this is how it could work - note this is just a quick and dirty code hack. Welcome comment in case I got it all wrong.
So as per your second example… Basically the problem with the Agent is that it will send your UART command to Device… as shown in example device.send("req", cmd)
and then moves on to the deal with the next event. There is no default method where it stops and waits for a reponse as you get with html.
So in this case I would suggest create a dual messaging format where you have some value that is stored and monitored in the Agent Code. To do that I suggest making “cmd” an object and not a string such as:
cmd = {"id": CmdCounter, "UARTmsg": "YOURCOMMAND"}
Then you would need to have a global object or an array in the Agent code that you use to store the CmdCounter value and you will then have another function to monitor when the device returns the CmdCounter back to the agent to close the loop, so to speak. For example:
Global_cmds = {"id": CmdCounter, "ConfirmationReceived": false}
Then in Device code you have a handler for “req” as given in the example agent.on("req", getRequest);
Then in the getRequest function you would have something like:
function getRequest(cmd) { server.log("Requesting"+cmd.UARmsg); AgentCmdID = cmd.id; // where AgentCmdID is declare a global in device code // You need to do this to allow the "id" to be referenced in the other device function that reads the response from the UART serial RX rxBuffer = "" uart.write(soh); uart.write(cmd); uart.write(etx); }
Then in function function dataRx() you would append something like this at the bottom of the function
function dataRx() { ....... else { local AgentMsg = {"id":AgentCmdID, "rxBufferMsg": rxBuffer}; agent.send("resp", AgentMsg ) }
Then in the agent code you would have a response handler:
`
function DeviceMsgResponseHandler(DeviceMsg)
{
// Check that the ID’s match in the Agent global
if (Global_cmds.id == DeviceMsg.id) {
// you have now got a valid response
Global_cmds.ConfirmationReceived = true;
}
}
device.on(“resp”, DeviceMsgResponseHandler);
`
You can also add in time value to track how long this feedback loop takes.
You could then also add in a watchdog function the monitors this time and have a timeout handler to manage the case when the device fails to return a response.