예제 #1
0
    def send_request(self,
                     request,
                     on_response=None,
                     response_timeout=_default_response_timeout,
                     **kwargs):
        """
        Send a request message on the backend
        Params:
            request     -- The request, either a dict, a string/bytes UTF-8 JSON,
                           or a platypush.message.request.Request object.

            on_response -- Response handler, takes a platypush.message.response.Response
                           as argument. If set, the method will wait for a
                           response before exiting (default: None)
            response_timeout -- If on_response is set, the backend will raise
                                an exception if the response isn't received
                                within this number of seconds (default: 5)
        """

        request = Request.build(request)
        assert isinstance(request, Request)

        request.origin = self.device_id

        if on_response and response_timeout != 0:
            self._setup_response_handler(request, on_response,
                                         response_timeout)

        self.send_message(request, **kwargs)
예제 #2
0
    def send_request(self,
                     request,
                     on_response=None,
                     response_timeout=_default_response_timeout,
                     **kwargs):
        """
        Send a request message on the backend.

        :param request: The request, either a dict, a string/bytes UTF-8 JSON, or a platypush.message.request.Request object.

        :param on_response: Optional callback that will be called when a response is received. If set, this method will synchronously wait for a response before exiting.
        :type on_response: function

        :param response_timeout: If on_response is set, the backend will raise an exception if the response isn't received within this number of seconds (default: None)
        :type response_timeout: float
        """

        request = Request.build(request)
        assert isinstance(request, Request)

        request.origin = self.device_id

        if on_response and response_timeout != 0:
            self._setup_response_handler(request, on_response,
                                         response_timeout)

        self.send_message(request, **kwargs)
예제 #3
0
    def send_request(self,
                     target,
                     action,
                     backend=None,
                     timeout=default_response_wait_timeout,
                     **kwargs):
        """
        Sends a message on a backend and optionally waits for an answer.
        Params:
            target  -- Target node
            action  -- Action to be executed in the form plugin.path.method
                    (e.g. shell.exec or music.mpd.play)
            backend -- Name of the backend that will process the request and get
                    the response (e.g. 'pushbullet' or 'kafka') (default: whichever
                    backend marked as pusher=true in your config.yaml)
            timeout -- Response receive timeout in seconds
                    - Pusher Default: 5 seconds
                    - If timeout == 0 or None: Pusher exits without waiting for a response
            **kwargs    -- Optional key-valued arguments for the action method
                        (e.g. cmd='echo ping' or groups="['Living Room']")
        """
        def _timeout_hndl(signum, frame):
            """ Default response timeout handle: raise RuntimeError and exit """

        if not backend: backend = self.backend

        req = Request.build({
            'target': target,
            'action': action,
            'args': kwargs,
        })

        self.backend_instance = self.get_backend(backend)
        self.backend_instance.send_request(req,
                                           on_response=self.on_response,
                                           response_timeout=timeout)