Kodi Community Forum
Python/Add-On newbie want to control projector. - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32)
+--- Forum: Add-ons (https://forum.kodi.tv/forumdisplay.php?fid=26)
+--- Thread: Python/Add-On newbie want to control projector. (/showthread.php?tid=124041)



Python/Add-On newbie want to control projector. - darkscout - 2012-02-26

I'm looking at controlling my projector with XBMC. It has a horribly old web interface that magically only works in IE6. (The SSL is fubar, etc).

I finally figured out how to get it working through curl and I'd like to make it an addon. I tried System.Exec to run my bash script but it was not happy.

This is what I have right now:
Code:
#!/bin/bash
curl -k -silent https://10.0.0.162/index.html -d "DATA1=Administrator&DATA2" -o /dev/null
if [ "$1" = "on" ] ; then
    curl -k -silent https://10.0.0.162/admin/main.html -d "DATA1=Administrator&DATA2&D1=1&V1=1" -o /dev/null
fi
if [ "$1" = "off" ] ; then
        curl -k -silent https://10.0.0.162/admin/main.html -d "DATA1=Administrator&DATA2&D2=0&V2=1" -o /dev/null
fi
if [ "$1" = "xbmc" ] ; then
        curl -k -silent https://10.0.0.162/admin/main.html -d "DATA1=Administrator&DATA2&D3=3&V3=1" -o /dev/null
fi
if [ "$1" = "wii" ] ; then
        curl -k -silent https://10.0.0.162/admin/main.html -d "DATA1=Administrator&DATA2&D3=5&V3=1" -o /dev/null
fi

I'd like to make it so that my python script will take parameters on what I want the projector to do and wire that to the remote.xml so power will turn the projector on.

Thanks.


- giftie - 2012-02-26

Here is your bash script changed to python:

Code:
import subprocess, sys

if ( __name__ == "__main__" ):
    subprocess.call('curl', '-k', '-silent', 'https://10.0.0.162/index.html', '-d', '"DATA1=Administrator&DATA2"', '-o', '/dev/null')
    if sys.argv[ 1 ]:
        if sys.argv[ 1 ] == "on":
            subprocess.call('curl', '-k', '-silent', 'https://10.0.0.162/admin/main.html', '-d', '"DATA1=Administrator&DATA2&D1=1&V1=1"', '-o', '/dev/null')
        elseif sys.argv[ 1 ] == "off":
            subprocess.call('curl', '-k', '-silent', 'https://10.0.0.162/admin/main.html', '-d', '"DATA1=Administrator&DATA2&D2=0&V2=1"', '-o', '/dev/null')
        elseif sys.argv[ 1 ] == "xbmc":
            subprocess.call('curl', '-k', '-silent', 'https://10.0.0.162/admin/main.html', '-d', '"DATA1=Administrator&DATA2&D3=3&V3=1"', '-o', '/dev/null')
        elseif sys.argv[ 1 ] == "wii":
            subprocess.call('curl', '-k', '-silent', 'https://10.0.0.162/admin/main.html', '-d', '"DATA1=Administrator&DATA2&D3=5&V3=1"', '-o', '/dev/null')
        else:
            print "Argument not recognized"
    else:
        print "Argument not supplied"

Save this as 'projector.py' and store it in the temp folder in xbmc home folder.

To call the script, you need to issue the following command:

Code:
RunScript(special://temp/projector.py,command)
command = on, off, xbmc, wii


- darkscout - 2012-02-27

Thanks. This rocks. Just some changes.

1) For some reason it couldn't find it in temp, I moved it to the 'home' folder. (.xbmc)
2) Some changes to python syntax.

Code:
import subprocess, sys

if ( __name__ == "__main__" ):
    if len(sys.argv)==2:
        subprocess.call(['curl', '-k', '-silent', 'https://10.0.0.162/index.html', '-d', 'DATA1=Administrator&DATA2','-o','/dev/null'])
        if sys.argv[ 1 ] == "on":
            subprocess.call(['curl', '-k', '-silent', 'https://10.0.0.162/admin/main.html', '-d', 'DATA1=Administrator&DATA2&D1=1&V1=1', '-o', '/dev/null'])
        elif sys.argv[ 1 ] == "off":
            subprocess.call(['curl', '-k', '-silent', 'https://10.0.0.162/admin/main.html', '-d', 'DATA1=Administrator&DATA2&D2=0&V2=1', '-o', '/dev/null'])
        elif sys.argv[ 1 ] == "xbmc":
            subprocess.call(['curl', '-k', '-silent', 'https://10.0.0.162/admin/main.html', '-d', 'DATA1=Administrator&DATA2&D3=3&V3=1', '-o', '/dev/null'])
        elif sys.argv[ 1 ] == "wii":
            subprocess.call(['curl', '-k', '-silent', 'https://10.0.0.162/admin/main.html', '-d', 'DATA1=Administrator&DATA2&D3=5&V3=1', '-o', '/dev/null'])
        else:
            print "Argument not recognized"
    else:
        print "Argument not supplied"