python rasbbmc api XAP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • videomanworld
    Automated Home Lurker
    • Apr 2013
    • 9

    python rasbbmc api XAP

    Hello there
    here is a piece of python largely inspired by "xpl chumby" of doghouse for notifications in xbmc. I'm not a programmer, away from, I copy the code here and I'm trying to adapt. XAP works on my Arduinos many functions, dialogue with HomeSeer.

    HomeSeer ascocié the event has this code:
    ------------------------------------------------------script homeseer----------------------------------------------------------------
    Sub Main
    Set xap = hs.Plugin("mcsXap")
    Dim line1
    dim line2
    line1 = hs.DeviceString("[12")
    line2 = hs.DeviceString("Y90")
    xap.SendXapMessage "raspbmc.xap.raspbmc", "xap-osd.display", "display.xbmc", "line1" & Chr(1) & line1 & Chr(0) & "line2" & Chr(1) & line2
    End Sub



    ------------------------------------------code thanks to doghouse for xpl chumby------------------------------------------------

    #!/mnt/usb/python/bin/python
    from xbmcjson import XBMC, PLAYER_VIDEO
    import time
    import datetime
    from threading import Event, Thread
    import re
    import sys, string, select
    import os
    from socket import *
    import warnings

    # instance name
    iName = "raspbmc"
    # Sub routine for sending a heartbeat
    def SendHeartbeat(hbThreadEvent) :
    hbSock = socket(AF_INET,SOCK_DGRAM)
    hbSock.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
    while not hbThreadEvent.isSet():
    msg = "xap-hbeat\n{\nv=12\nhop=1\nuid=FF00CC00\nclass=xap-hbeat.alive\nsource=raspbmc.xap."+iName+"\ninterva l=60\nport="
    msg = msg + str(xapPort) + "\n}\n"
    hbSock.sendto(msg,("255.255.255.255",3639))
    hbThreadEvent.wait(60.0)

    # Sub routine for sending a heartbeat
    def SendEndBeat(hbThreadEvent) :
    hbSock = socket(AF_INET,SOCK_DGRAM)
    hbSock.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
    msg = "xap-hbeat\n{\nv=12\nhop=1\nuid=FF00CC00\nclass=xap-hbeat.alive\nsource=raspbmc.xap."+iName+"\ninterva l=60\nport="
    msg = msg + str(xapPort) + "\n}\n"
    hbSock.sendto(msg,("255.255.255.255",3639))
    hbThreadEvent.wait(60.0)

    # Sub routine for sending info
    def Sendinfo() :
    hbSockinfo = socket(AF_INET,SOCK_DGRAM)
    hbSockinfo.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
    msgbsc = "xap-header\n{\nv=12\nhop=1\nuid=FF00CC01\nclass=xAP-OSD.Display\nsource=raspbmc.xap."+iName+"isplay\n}\nDisplay.xbmc\n{\nLine1=Text to display\nLine2=Text to display\n}\n"
    hbSockinfo.sendto(msgbsc,("255.255.255.255",3639))


    # bsc info

    # Get IP Address - LINUX ONLY
    def getip(ifname):
    import fcntl
    import struct
    s = socket(AF_INET, SOCK_DGRAM)
    return inet_ntoa(fcntl.ioctl(s.fileno(),0x8915,struct.pac k('256s', ifname[:15]))[20:24])

    buff = 1500
    # Main Loop
    def main():
    warnings.filterwarnings("ignore", category=UserWarning, module='urllib2')
    global xapPort
    xapPort = 3639
    # Initialise the socket
    UDPSock = socket(AF_INET,SOCK_DGRAM)
    bound = 0
    while bound == 0 :
    bound = 1
    try :
    addr = ('0.0.0.0',xapPort)
    UDPSock.bind(addr)
    except :
    bound = 0
    xapPort += 1

    # start the heartbeat thread
    hbThreadEvent = Event()
    hbThread = Thread(target=SendHeartbeat, args=(hbThreadEvent,))
    hbThread.start()
    #print "------------start------------------"
    Sendinfo()
    xbmc = XBMC("http://192.168.1.55/jsonrpc", "xbmc", "xbmc")
    xbmc1 = XBMC("http://192.168.1.250/jsonrpc", "xbmc", "xbmc")
    try:
    while True:
    readable, writeable, errored = select.select([UDPSock],[],[],60)
    if len(readable) == 1 :
    data,addr = UDPSock.recvfrom(buff)
    message = str(data)
    message = message.splitlines()
    xaptype = message[0]
    if xaptype == "xap-hbeat":
    continue
    xaptruc = message[1]
    xapversion = message[2:5]
    xaphop = message[3]
    xapuid = message[4]
    #print (xapuid)
    xapclass = message[5].rsplit("=")[1]
    #print (xapclass)
    if xapclass != "xap-osd.display":
    continue
    xapsource = message[6].rsplit("=")[1]
    print (xapsource)
    xaptarget = message[7].rsplit("=")[1]
    print (xaptarget)
    if xaptarget.lower() != "raspbmc.xap."+iName.lower()+"isplay.xbmc" or xaptarget != "raspbmc.xap.raspbmc:display.xbmc":
    print "---------yes-------------"
    bracket = message[8]
    xapcmd = message[9]
    #print (xapcmd)
    bracket1 = message[10]
    xapcommand = message[11].rsplit("=")[1]
    xapcommand1 = message[12].rsplit("=")[1]
    print (xapcommand)
    print (xapcommand1)
    if xapcommand != "":
    xbmc1.GUI.ShowNotification({"title":xapcommand,"me ssage":xapcommand1})
    xbmc.GUI.ShowNotification({"title":xapcommand,"mes sage":xapcommand1})
    elif xapcommand == " ":
    print "---------no-------------"
    else :
    continue
    except KeyboardInterrupt:
    print 'Exiting, please wait...'
    SendEndBeat(hbThreadEvent)
    sys.exit(2)

    # you'll have to change this to your adapter name if using usb ethernet
    LocalIP=getip('eth0')
    main()
Working...
X