Jump to content
C4 Forums | Control4

Help sending commands to C4 from driver B via driver A


Recommended Posts

First, admittedly I am a newbie here...

I have written 2 drivers.

Driver A speaks with C4 just fine.

I want Driver B to send commands to Driver A, that will then simply pass them off to C4.

Follow me here?

Driver B shall not talk directly to C4, but do so via Driver A

Any ideas how to script this?

Thanks in advance :)

Link to comment
Share on other sites


C4:SendToDevice(idDevice, strCommand, tParams)

This will send a devicecommand (a command string and a Lua table of parameters) from this driver ('Driver A') to the driver with Device ID idDevice ('Driver B').

To get the Device ID of 'Driver B', you could make a DEVICE_SELECTOR type property, which would allow the installer to select the device.

In 'Driver B', you will get the sent command via the ExecuteCommand function.

RyanE

Link to comment
Share on other sites

BTW, if these devices have a relationship like a 'gateway' <-> many other devices that talk through the gateway, this is not the right way to do it. i.e. like a Lighting Control driver, etc.

It's better in that case to make a 'network' driver, which has a control binding, and all the 'sub-devices' would also have the same control binding, which would allow them to talk through the 'network' driver.

If you're interested in that info, PM me.

RyanE

Link to comment
Share on other sites

  • 6 years later...

old thread but...

I seem to be unable to get a return value from the C4:SendToDevice function - API docs say "... return value is the value returned by the device driver's ExecuteCommand.  The default value returned is nil.  Return values are limited to string values"

When i check the return value from SendToDevice, it is always of type nil so it has never been initialized.  Otherwise, the API works, triggers Driver B's command - that returns a string (all confirmed by debug statements and log entries)

Thoughts?

I could get the return(s) async but likely calling several drivers so would really prefer to not keep track of all the return calls.

Thanks!

 

Link to comment
Share on other sites

another note - SendToDevice tParams seems to NOT support nested tables (table stored within tParams).  Checks on way in but is dropped in receiving tParams function (with all other k/v intact).  Anyone else seen/confirm this behavior? Took too long to debug this feature...

serializing for now (json)...

Link to comment
Share on other sites

You are correct, SendToDevice tParams does not support nested tables.  You must serialize the data for more complicated structures yourself, as you've found out.

As far as returning the value from a C4:SendToDevice call, the documentation is incorrect.  ExecuteCommand in a driver can't return a value, as it's called asynchronously from the other driver.

You can send a synchronous call to another driver and get the return values back, but there are some limitations.

1) Instead of C4:SendToDevice(idDevice, strCommand, tParams), you use C4:SendUIRequest(idDevice, strCommand, tParams)

2) Instead of function ExecuteCommand(strCommand, tParams) ... in the 'receiving' driver, you use function UIRequest(strCommand, tParams), and you can return a string, but the string must have an 'outer XML' structure to it, due to legacy reasons.  i.e.:

return "<mydata>" .. myJSON .. "</mydata>"

Note, this is synchronous communication, so you can't really wait for asynchronous data from another location (i.e. network, etc.) to come back to reply.

RyanE

Link to comment
Share on other sites

Thank you Ryan - sorry I didn't see your reply until recently.

I moved past the sync/async issue for now but have an upcoming project that will be perfect for your suggestion.

side, side note from above - tParams didn't pass along my integer or floats either so maybe just string are supported...

Thanks again!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.