Ejemplo n.º 1
0
def main():
    #Connects to smart space
    
    global ts, sc
    
    try:
        ts = TripleStore()
        connected = True
        
        addEvent("ConnectEvent")

    except:
        print "Cannot connect to smart space."
        return    
       
    if connected:
        registerDevice()
        
        print "Checking for existing connections.."
        qs = node.CreateQueryTransaction(smartSpace)
        result = qs.rdf_query([((None, sc_ns+"connectedTo", ie_ns+deviceID),"uri")])
        for source in result:
            subscribeToSourceEvents(source[0][0])          
    
        print "Subscribing to new connections..."
        rs1 = node.CreateSubscribeTransaction(smartSpace)
        result_rdf1 = rs1.subscribe_rdf([((None, sc_ns+"connectedTo", ie_ns+deviceID), 'uri')], newConnectionHandler())
        
        print "Subscribing to SystemEvents..."
        rs2 = node.CreateSubscribeTransaction(smartSpace)
        result_rdf2 = rs2.subscribe_rdf([((None, "rdf:type", ie_ns+"SystemEvent"), 'uri')], eventHandler())
        
    
                 
#        print "Subscribing to AlarmSetEvents.."
#        rs1 = node.CreateSubscribeTransaction(smartSpace)
#        result_rdf1 = rs1.subscribe_rdf([((None, None, ie_ns+"AlarmSetEvent"), 'uri')], RdfMsgHandler())
#        rs2 = node.CreateSubscribeTransaction(smartSpace)
#        result_rdf2 = rs2.subscribe_rdf([((None, None, ie_ns+"TimeSetEvent"), 'uri')], RdfMsgHandler())

    sc = Server(hostname="127.0.0.1", port=9090, username="******", password="******")
    sc.connect()
    sc.subscribe(SBMsgHandler(),"00:04:20:2A:B0:A5")
    
    print "Logged in: %s" % sc.logged_in
    print "Version: %s" % sc.get_version()

    print "Ctrl+C to exit"

    try:
	    while 42:
	        getPlayer()
            raw_input()
                
    except KeyboardInterrupt: #So use Ctrl+C to exit event loop
        
        print "Unsubscribing RDF subscriptions"
        node.CloseSubscribeTransaction(rs1)
        node.CloseSubscribeTransaction(rs2)
        
        
        sc.unsubscribe()

        print "Leaving smart space.."
        if connected:
            addEvent("DisconnectEvent")
            node.leave(smartSpace)
            print("Exiting..")
            os._exit(1) #this is required because excepting a KeyboardInterrupt makes it difficult to exit the program