pponce / homebridge-script2

Execute custom scripts via HomeKit apps
89 stars 18 forks source link

script2 and adb #8

Closed nicktones closed 6 years ago

nicktones commented 6 years ago

I'm trying to get script2 to work with a adb (android debugger) shell script I have to remotely start a Nvidia Shield. This script works fine when executed in the shell, however doesn't work when executing with script2. When checking the logs, everything looks correct, however the shield doesn't start. Note again that the script works when fired in the shell.

Any help would be gratefully received!

#!/bin/sh

adb kill-server 
adb start-server 
adb connect 192.168.1.21
sleep 5
adb shell "input keyevent KEYCODE_SLEEP"
sleep 3
adb kill-server
pponce commented 6 years ago

Can you share the configuration settings? Things to try: 1.) Insure that the full path of the script file is entered into the configuration settings. 2.) Insure the script is accessible by the homebridge user.

nicktones commented 6 years ago

Absolutely. Here is my configuration...

{
        "accessory": "Script2",
        "name": "Shield",
        "on": "/var/homebridge/shield-wake.sh",
        "off": "/var/homebridge/shield-sleep.sh",
        "state": "/var/homebridge/shield-state.sh",
        "on_value" : "true"
}

Checking the log after flipping the switch shows that the script runs and it shows the exact same output as when running in the shell, however the Shield doesn't power off. I think it's an issue with the 'adb shell..." line of the script when running with Script2.

pponce commented 6 years ago

Where is adb located? Can you try changing the script to use 1.) #!/bin/bash instead of sh.
2.) Putting the full path to adb in your script?

I'm shooting in the dark a bit here. But i noticed in my scripts I cd into the location and then execute the command. Not sure why I did this but perhaps i ran into the same issue.

nicktones commented 6 years ago

Thanks for the point in the right direction. I ended up with this script, which works perfectly with Script2. This will be useful for anyone wanting to use adb and a script to remotely control an Android device.

#!/bin/bash
# Sleep Nvidia Shield Script v1.0

sudo -u pi bash << EOF
/usr/bin/adb kill-server 
/usr/bin/adb start-server 
/usr/bin/adb connect 192.168.1.21
/bin/sleep 5
/usr/bin/adb -s 192.168.1.21:5555 shell "input keyevent KEYCODE_SLEEP"
/bin/sleep 5
/usr/bin/adb kill-server
EOF

This script opens a shell as the default pi user, and passes in the commands to it. Ensuring everything works as it should.

nicktones commented 6 years ago

Ok, so here is the complete solution for anyone that wants to control their Nvidia Shield (or any Android device) through HomeBridge with Script2.

HomeBridge Config Extract

{
        "accessory": "Script2",
        "name": "Shield",
        "on": "/var/homebridge/shield-wake.sh",
        "off": "/var/homebridge/shield-sleep.sh",
        "state": "/var/homebridge/shield-state.sh",
        "on_value" : "ON"
}

shield-wake.sh

#!/bin/bash
# Wake Nvidia Shield Script v1.0

sudo -u pi bash << EOF
/usr/bin/adb kill-server 
/usr/bin/adb start-server 
/usr/bin/adb connect 192.168.1.21
/bin/sleep 3
/usr/bin/adb -s 192.168.1.21:5555 shell "input keyevent KEYCODE_WAKEUP"
/usr/bin/adb kill-server
EOF

shield-sheep.sh

#!/bin/bash
# Sleep Nvidia Shield Script v1.0

sudo -u pi bash << EOF
/usr/bin/adb kill-server 
/usr/bin/adb start-server 
/usr/bin/adb connect 192.168.1.21
/bin/sleep 3
/usr/bin/adb -s 192.168.1.21:5555 shell "input keyevent KEYCODE_SLEEP"
/usr/bin/adb kill-server
EOF

shield-state.sh

#!/bin/bash
# State Nvidia Shield Script v1.0

sudo -u pi bash << EOF
{
/usr/bin/adb kill-server 
/usr/bin/adb start-server 
/usr/bin/adb connect 192.168.1.21
/bin/sleep 3
} 1>/dev/null 2>&1
if /usr/bin/adb shell dumpsys input_method | grep -q mInteractive=true
then
    echo "ON"

else
    echo "OFF"
fi
/usr/bin/adb kill-server
EOF
pponce commented 6 years ago

Glad you figured it out and thanks for the write up. I'm sure it will help others. Do you know what user you run homebridge as? Do you run it as user PI? I my setup i run it as user "homebridge" and i gave user "homebridge" assess to my scripts.

nicktones commented 6 years ago

No problem - hope it helps others.

Yeah, I have it setup the same way. Homebridge runs as the homebridge user. I gave it access to this script, and sudo without password permissions - however I think this one is due to the fact the adb launches its own shell.