def get(self):

        url = options.curr_url
        quality = options.curr_quality

        if url and quality:

            yield options.stop_stream()
            res, msg = yield options.start_stream(url, quality)

            if res:
                logger.info("Restarted stream via REST")

                self.write({
                    "status": "restarted",
                    "url": url
                })
            else:
                logger.error("Error restarting stream via REST")

                self.set_status(500)
                self.write({
                    "status": "error",
                    "msg": msg
                })
        else:
            logger.warning("Could not restart stream because no url is set.")

            self.set_status(400)
            self.write({
                "status": "error",
                "msg": "no url currently set"
            })
    def get(self):
        url = str(self.get_query_argument("url"))
        quality = str(self.get_query_argument("quality", "best"))

        yield options.stop_stream()

        if url:
            res, msg = yield options.start_stream(url, quality)
            options.publish_state(url, quality, update_desired=True)

            if res:
                logger.info("Started stream via REST")

                self.write({
                    "status": "started",
                    "url": url
                })
            else:
                logger.error("Error starting stream via REST")

                self.set_status(500)
                self.write({
                    "status": "error",
                    "msg": msg
                })
        else:
            self.write({
                "status": "stopped",
                "url": url
            })
            options.publish_state(url, quality, update_desired=True)
    def __resolve_state(self):
        # This coroutine actually diffs the shadow state with the actual state and starts the stream if needed.
        # If the stream was start successfully then the new reported state is published.

        if self.shadow is None:
            logger.error("__resolve_state called before shadow was set.")
            return

        logger.debug(self.shadow)

        def get_field(name, default=None):
            return self.shadow['state'].get(
                'desired', self.shadow['state'].get('reported',
                                                    {})).get(name, default)

        # Get url and quality from shadow 'desired' or 'reported'
        shadow_url = get_field("url")
        quality = get_field("quality", "best")

        if shadow_url is None:
            logger.error("No 'url' state found in thing shadow")
            return

        if options.curr_url != shadow_url or options.curr_quality != quality:
            if shadow_url:
                logger.info("old url: '{0}', new url: '{1}'".format(
                    options.curr_url, shadow_url))

                yield options.stop_stream()
                res, res_msg = yield options.start_stream(shadow_url, quality)

                if res:
                    logger.info("Started stream via MQTT")
                    self.publish_state(shadow_url, quality)
                else:
                    logger.error("Error starting stream: {0}".format(res_msg))
                    #TODO revert to previous stream on error

            else:
                logger.warning("Stopping stream because shadow url is empty.")
                yield options.stop_stream()
                self.publish_state(shadow_url, quality)
        else:
            logger.info("shadow and state are in sync, no action.")
    def __resolve_state(self):
        # This coroutine actually diffs the shadow state with the actual state and starts the stream if needed.
        # If the stream was start successfully then the new reported state is published.

        if self.shadow is None:
            logger.error("__resolve_state called before shadow was set.")
            return

        logger.debug(self.shadow)

        def get_field(name, default=None):
            return self.shadow['state'].get('desired', self.shadow['state'].get('reported', {})).get(name, default)

        # Get url and quality from shadow 'desired' or 'reported'
        shadow_url = get_field("url")
        quality = get_field("quality", "best")

        if shadow_url is None:
            logger.error("No 'url' state found in thing shadow")
            return

        if options.curr_url != shadow_url or options.curr_quality != quality:
            if shadow_url:
                logger.info("old url: '{0}', new url: '{1}'".format(options.curr_url, shadow_url))

                yield options.stop_stream()
                res, res_msg = yield options.start_stream(shadow_url, quality)

                if res:
                    logger.info("Started stream via MQTT")
                    self.publish_state(shadow_url, quality)
                else:
                    logger.error("Error starting stream: {0}".format(res_msg))
                    #TODO revert to previous stream on error

            else:
                logger.warning("Stopping stream because shadow url is empty.")
                yield options.stop_stream()
                self.publish_state(shadow_url, quality)
        else:
            logger.info("shadow and state are in sync, no action.")