Moode Forum
Connecting to database with python - Printable Version

+- Moode Forum (https://moodeaudio.org/forum)
+-- Forum: moOde audio player (https://moodeaudio.org/forum/forumdisplay.php?fid=3)
+--- Forum: Support (https://moodeaudio.org/forum/forumdisplay.php?fid=7)
+--- Thread: Connecting to database with python (/showthread.php?tid=2546)



Connecting to database with python - Max Schmeling - 05-18-2020

I'm on Moode 4.1+Rpi2b and I have just upgraded to Moode 6.5.2 + Rpi4 I run a python script that helps me with power and a button and the script has a few places, where I'm unsure if I need to change anything.

First up is checking if Airplay is on:

Code:
# Airplay state check
def IsAirplayOn():
    
    global ButtonOverride
    
    AirplayOn = False
    try:
        cur = con.cursor()    
        cur.execute('select value from cfg_system where param=\'airplayactv\'')
        data = cur.fetchone()[0]    
        if (Debug): print "SQL = %s" % data
        if data == "1":
            AirplayOn = True

    except SQLite3.Error, e:
        if (Debug): print "Error %s:" % e.args[0]

    if ButtonOverride:
        AirplayOn = False
    
    if Debug: print "Returning IsAirplayOn = {0}".format(AirplayOn)
    return AirplayOn

and

Code:
# Connect to player database, in order to read Airplay status     
try:
        con = SQLite3.connect('/var/local/www/db/moode-sqlite3.db')    
except SQLite3.Error, e:
        if Debug: print "Error %s:" % e.args[0]
        sys.exit(1)

Do I need to change anything here when upgrading?

The last bit of code, that I'm unsure about is this:

Code:
# Setup MDPClient and connect to MPD on localhost
Client = mpd.MPDClient() # create client object
Client.timeout = None
Client.idletimeout = None

while True:
    try:
        Client.connect("localhost", 6600) # connect to localhost:6600
    except:
        continue
    break

Does anyone know if I need to change anything in these three code bits in order to get it woring under moode 6.5.2 ?


RE: Connecting to database with python - TheOldPresbyope - 05-18-2020

@Max Schmeling

What, no "try it and see" test  Tongue

The airplayactv parameter hasn't changed in 6.5.2. I think your code will work unmodified.

However, you ought to consider updating it to Python3. At some point the Debian maintainers will remove the Python 2 stack and I expect the Raspbian maintainers will too.

Regards,
Kent


RE: Connecting to database with python - Max Schmeling - 05-19-2020

(05-18-2020, 04:55 PM)TheOldPresbyope Wrote: @Max Schmeling

What, no "try it and see" test  Tongue

The airplayactv parameter hasn't changed in 6.5.2. I think your code will work unmodified.

However, you ought to consider updating it to Python3. At some point the Debian maintainers will remove the Python 2 stack and I expect the Raspbian maintainers will too.

Regards,
Kent

@TheOldPresbyope 

Thanks. Smile  I just tried updating to P3 and installing python-mpd2. Everything seems to work, but I'll have to check, once I get it in the box with the buttons. I'll report back Smile


RE: Connecting to database with python - Max Schmeling - 05-20-2020

(05-19-2020, 11:06 AM)Max Schmeling Wrote:
(05-18-2020, 04:55 PM)TheOldPresbyope Wrote: @Max Schmeling

What, no "try it and see" test  Tongue

The airplayactv parameter hasn't changed in 6.5.2. I think your code will work unmodified.

However, you ought to consider updating it to Python3. At some point the Debian maintainers will remove the Python 2 stack and I expect the Raspbian maintainers will too.

Regards,
Kent

@TheOldPresbyope 

Thanks. Smile  I just tried updating to P3 and installing python-mpd2. Everything seems to work, but I'll have to check, once I get it in the box with the buttons. I'll report back Smile

Right Confused . Guess I have to install the python-mdp2 with pip3 and not pip. Big Grin Now it is working Smile