Hello,

Sorry for bumping this thread, but for people who are still searching
for an easy script to trigger a relay i've created a python script that
runs at boot.
The script checks the powerstate of the player every second (script runs
on a raspberry pi where squeezelite is installed), and if powerstate = 1
then relay is turned on, otherwise the relay is turned off.
It works perfectly. The only thing you need to enter is the mac-adress
of your player and the adress of your server (ip adress or url).

For the start up process i let the script wait 5 minutes before it gets
executed, because if the player and the server are on the same machine,
and the machine is rebooted, it takes a while to start lms.

If there are things that can be changed in order to make the script
better (I'm sure that will be easy, this was my first python script) ,
feel free to do so and post your changes please! :-)


Code:
--------------------
    
  import sys
  import httplib
  import RPi.GPIO as GPIO
  import time
  
  time.sleep (300)
  
  if float(sys.version[:3]) >= 2.6:
  import json
  else:
  # python 2.4 or 2.5 can also import simplejson
  # as working alternative to the json included.
  import simplejson as json
  
  # change to your server
  
  server = "yourserverip"
  playermac = "your player mac adresse"
  
  # This provides http 1.1 keep alive so preventing DNS lookup 
  # For each request.
  
  while True: 
  
        conn = httplib.HTTPConnection(server)
  
        #Make a python dictionary for the request.
  
        data = {
        "id": 1, 
            "method": "slim.request", 
            "params": [
        "", 
                [
            "player", 
                    "count", 
            "?"
                ]
            ]
        }
  
        # So now we convert the dictionary data to a string
        params = json.dumps(data, sort_keys=True, indent=4)
        # Make the keep alive request
        conn.request("POST", "/jsonrpc.js", params)
        httpResponse = conn.getresponse()
        #print "httpResponse.status=%s, httpResponse.reason=%s" % 
(httpResponse.status, httpResponse.reason)
        data = httpResponse.read()
        # Load the JSON string back to python dictionary
        responce = json.loads(data)
        # print the python dictionary as a JSON string.
        # print json.dumps(responce, sort_keys=True, indent=4)
        # Extract the number of players from the python dictionary
        numberplayers = responce['result']["_count"]
        # print "numberplayers=%s" % numberplayers
  
        playerIdentifiers = []
        playerdetected = ""
        for i in range(numberplayers):
            data = { "id":1,
                        "method":"slim.request",
                        "params":[ "", 
                                        ["player","id",i,"?"]
                          ]
                    }
            params = json.dumps(data, sort_keys=True, indent=4)
        #    print params
            conn.request("POST", "/jsonrpc.js", params)
            httpResponse = conn.getresponse()
        #   print "httpResponse.status=%s, httpResponse.reason=%s" % 
(httpResponse.status, httpResponse.reason)
            data = httpResponse.read()
        #    print data
            responce = json.loads(data)
            playerId = responce['result']["_id"]
        #    print playerId
        #    print "id=%s" % playerId
        #    print playermac
        
            if (playerId == playermac):
                playerdetected = playerId
        
        if playerdetected <> "":
        #       print playerdetected
                data = { "id":1,
                        "method":"slim.request",
                        "params":[playerdetected,
                                        ["power","?"]
                                ]               
                        }
                params = json.dumps(data, sort_keys=True, indent=4)
         #       print params
                conn.request("POST", "/jsonrpc.js", params)
                httpResponse = conn.getresponse()
         #       print "httpResponse.status=%s, httpResponse.reason=%s" % 
(httpResponse.status, httpResponse.reason)
                data = httpResponse.read()
         #       print data
                responce = json.loads(data)
                powerstate = responce['result']["_power"]
                if powerstate ==  "1":
  #                     print "player: " + playerdetected + " power state: " + 
powerstate
                        GPIO.setmode(GPIO.BCM)
                        GPIO.setwarnings(False)
                        GPIO.setup(18, GPIO.OUT)
                        GPIO.output(18, True)
                else:
                        powerstate = "0"
  #                     print "player: " + playerdetected + " power state: " + 
powerstate
                        GPIO.setmode(GPIO.BCM)
                        GPIO.setwarnings(False)
                        GPIO.setup(18, GPIO.OUT)
                        GPIO.output(18, False)
        time.sleep (1)
  
--------------------


------------------------------------------------------------------------
RobbeD's Profile: http://forums.slimdevices.com/member.php?userid=56967
View this thread: http://forums.slimdevices.com/showthread.php?t=71077

_______________________________________________
discuss mailing list
[email protected]
http://lists.slimdevices.com/mailman/listinfo/discuss

Reply via email to