I'm playing with bringing an old Griffin PowerMate back to life.
I've got the input side of it working using the code posted in another thread, and now I want to try setting the LED brightness.
However, I can't figure out how to modify the sample code in the analyzer window to do this.
The provided function (from the default analyzer code) looks like this:
async function executeBTTCommand(targetDevice, commandName, commandInput) { ... }
Internally, this calls bttSendDataToDevice()...
It is not clear to me what to do with the sample code, and I cannot find any docs or an example of a preset that does this.
Can somebody point me to a working example that sends data back to a generic device?
I found a way to way to make this work, but it doesn't use the sample code suggested by the device analyzer at all!
I tried adding the BTT action "Send Data To Generic Device" to my trigger, and then once I got this to do something on the device, I copied the json for the trigger and used it to make a JS object in my code, then sent it to BTT by using trigger_action(). Something like this:
// The "40" at byte 5 is the Griffin brightness,
// set this to anything from 00 to FF
let dataString = '4101010040000000'
let targetDevice = '8567E070-7DB1-49BA-9628-4E06D3FAE65C'; // my Griffin, got this from the device analyzer
let trigger_obj = {
"BTTActionCategory" : 0,
"BTTPredefinedActionType" : 379,
"BTTPredefinedActionName" : "Send Data To Generic Device",
"BTTAdditionalActionData" : {
"BTTActionSendDataData" : dataString,
"BTTActionSendDataResponseType" : -1,
"BTTActionSendDataMaxPayloadSize" : 8,
"BTTActionSendDataTargetDevice" : targetDevice,
"BTTActionSendDataReportType" : 0
},
"BTTEnabled2" : 1,
}
let trigger_json = JSON.stringify(trigger_obj);
let deviceResponse = await trigger_action({json: trigger_json, wait_for_reply: true});
@Andreas_Hegenberg - is this the right way to send data to a generic device?
Honestly I have never tried this because I didn't have a device to test with, but there is a function that can be called directly from the analyzer script.
bttSendDataToDevice
It should take the same parameters as in the action and you can use it instead of your custom action code.
bttSendDataToDevice(
{
"BTTActionSendDataData" : dataString,
"BTTActionSendDataResponseType" : -1,
"BTTActionSendDataMaxPayloadSize" : 8,
"BTTActionSendDataTargetDevice" : targetDevice,
"BTTActionSendDataReportType" : 0
}
)
The available commands in the executeBTTCommand function need to be defined here:
![image|600x500](upload://mpBdPiTdDiNnQUzeho9gm1HcYDa. Yjpeg)
The idea is that a device analyzer configuration can provide such commands that can be reused when sharing the device analyzer setup with others.
The commands a device provides will automatically show up in the predefined action selector in the generic device section:
Hi Andreas,
Thanks for the reponse!
I've set things up as you advised (I think!), and I can see that the command provided by the device is now being called.
However, I think that the call to bttSendDataToDevice is failing, because the log statement after this call does not get executed. (Also, the Griffin LED does not change brightness as expected.)
Can you advise as to whether this is a bug in my device analyzer code or maybe there is some bug in bttSendDataToDevice?
As I said in my previous message, I have observed the Griffin LED change brightness when I make a direct JS call to trigger_action() with the right
arguments to "Send Data To Generic Device". I think that I am using the same arguments in the executeBTTCommand().
I'm attaching a preset that includes the code for this as well as a floating menu that triggers the LED brightness change both ways (direct call to trigger_action, and use of the device command).
See the screenshots below, especially the log from the analyzer. I don't know how to help debug this further, please let me know what would be useful.
If you look at the screenshots you can see that the data being sent to the device should be the same in both cases.
A couple of comments:
-
I notice that console.log() does not appear to work inside the analyzer (see the line under the case statement). If this is not hard to add, it would make debugging generic device setups easier.
-
It's great that the command action provided by the device allows the user to specify an input. On the device analyzer side though, the input shows up as a dict like this:
{"BTTActionGenericDeviceCommandInput":"128"}
As a user trying to write the analyzer code, this just adds extra complexity. Can you consider passing the input to the executeBTTCommand function as a string instead?
Screenshot 1: call directly using trigger_action. (Floating menu top button)
(script result shows data sent to device)
Screenshot 2: call using device command action.
(Floating menu third button pressed while device analyzer was open)
(log window shows data sent to device)
PowerMate_1.bttpreset (208.9 KB)
I'll check it and see whether I can find anything! Thanks for trying this functionality!
I recently updated to 4.747
I decided today to try running the test again. I just noticed that if the device analyzer window is not open, then the calls using the device command action work as expected, so it seems that having the device analyzer window open is somehow causing things to fail.
I don't know if I tested it with the device analyzer closed when I made the screenshots above.
And in case it is helpful, the device command action
- works with the BTT config window closed
- works with the main BTT config window open but with no device analyzer window open
- works with the main BTT config window and with the device analyzer window for a different generic device open
Oh that is very interesting, thanks for figuring this out! I'll fix it. (I have not yet had time to look into this, I will try to setup some simple device I can test with)