Example #1
0
    def start( self ):
        """ """
        #
        # Credentials 
        spotify_username = "******"
    	spotify_password = "******"
        spotify_api_key = "sdsad323wd"
        
        #
        # Playback manager
        self.playback_manager = PlaybackManager( spotify_username, spotify_password, spotify_api_key )
        
        #
        # API manager
        self.api_manager = SpotifyAPIManager()
        
        #
        # Motion subprocess
        def motion_started_cb():
            print "Motion started"
            self.playback_manager.resume_playback()
        
        def motion_stopped_cb():
            print "Motion stopped"
            self.playback_manager.pause_playback()
        
        self.motion_manager = MotionManager( motion_started_cb, motion_stopped_cb )
                
        #
        # General set up 
        
        #
        # Manager set up
        #~self.motion_manager.enable_motion_monitor()
        
        #
        # HTTP server that represents the central controller API
        httpd = ThreadedHTTPServer( ('',8000), ThreadedHTTPServer.HTTPRequestHandler, self )
        
        httpd.register_api_function( 'get_current_playlist', 
                                     self.do_get_current_playlist,
                                     'GET' )

        httpd.register_api_function( 'set_playback_enabled', 
                                     self.do_set_playback_enabled,
                                     'PUT' )
                                  
        httpd.register_api_function( 'set_motion_control_enabled', 
                                     self.do_set_motion_control_enabled,
                                     'PUT' )
                                  
        httpd.register_api_function( 'next_track', 
                                     self.do_next_track,
                                     'PUT' )
                                  
        httpd.register_api_function( 'set_current_playlist', 
                                     self.do_set_current_playlist,
                                     'PUT' )
        
        # unconditional execution: httpd.serve_forever()

        global SIGTERM_SENT
        while not SIGTERM_SENT:
            httpd.handle_request()
        
        #
        # Finish up
        print "Safely stopping"
        self.stop()
Example #2
0
class CentralController( object ):
    """
    A central controller that handles the supporting managers.
    """
    
    def __init__( self ):
        """ """
    
    #
    #
    # Top-level API functions
    #
    def do_set_playback_enabled( self, qargs_dict ):
        """ """
    
    def do_set_motion_control_enabled( self, qargs_dict ):
        #
        # Input handling
        if 'flag' not in qargs_dict:
            raise ArgumentError( "Missing argument: flag" )
        flag = qargs_dict.pop( 'flag' )[0]
        if qargs_dict:
            raise ArgumentError( "Unexpected arguments: " + ','.join(qargs_dict.keys())  )
            
        #
        # Do it
        if flag == 'true':
            self.motion_manager.enable_motion_monitor()
        elif flag == 'false':
            self.motion_manager.disable_motion_monitor()
        else:
            raise ArgumentError( "Cannot understand flag value '%s'" % flag )

    def do_next_track( self, qargs_dict ):
        """ """

    def do_get_current_playlist( self, qargs_dict ):
        """
        Expected args:
        * None
        
        Return data:
        { playlist_name, playlist_index }
        """
        assert len(qargs_dict) == 0
        ret = { 'playlist_name':'test_playlist', 'playlist_index': 33 }
        return ret 
    
    def do_set_current_playlist( self, qargs_dict ):
        """ """
        
        print "do -- set current playlist"
    
    #
    #
    # Controller 
    #
    def start( self ):
        """ """
        #
        # Credentials 
        spotify_username = "******"
    	spotify_password = "******"
        spotify_api_key = "sdsad323wd"
        
        #
        # Playback manager
        self.playback_manager = PlaybackManager( spotify_username, spotify_password, spotify_api_key )
        
        #
        # API manager
        self.api_manager = SpotifyAPIManager()
        
        #
        # Motion subprocess
        def motion_started_cb():
            print "Motion started"
            self.playback_manager.resume_playback()
        
        def motion_stopped_cb():
            print "Motion stopped"
            self.playback_manager.pause_playback()
        
        self.motion_manager = MotionManager( motion_started_cb, motion_stopped_cb )
                
        #
        # General set up 
        
        #
        # Manager set up
        #~self.motion_manager.enable_motion_monitor()
        
        #
        # HTTP server that represents the central controller API
        httpd = ThreadedHTTPServer( ('',8000), ThreadedHTTPServer.HTTPRequestHandler, self )
        
        httpd.register_api_function( 'get_current_playlist', 
                                     self.do_get_current_playlist,
                                     'GET' )

        httpd.register_api_function( 'set_playback_enabled', 
                                     self.do_set_playback_enabled,
                                     'PUT' )
                                  
        httpd.register_api_function( 'set_motion_control_enabled', 
                                     self.do_set_motion_control_enabled,
                                     'PUT' )
                                  
        httpd.register_api_function( 'next_track', 
                                     self.do_next_track,
                                     'PUT' )
                                  
        httpd.register_api_function( 'set_current_playlist', 
                                     self.do_set_current_playlist,
                                     'PUT' )
        
        # unconditional execution: httpd.serve_forever()

        global SIGTERM_SENT
        while not SIGTERM_SENT:
            httpd.handle_request()
        
        #
        # Finish up
        print "Safely stopping"
        self.stop()
    
    def stop( self ):
        print "Sending STOP instruction to managers"
        self.playback_manager.finish()
        self.api_manager.finish()
        self.motion_manager.finish()