示例#1
0
    def _upload_preview_image(self):
        os.makedirs(os.path.join(os.getcwd(), 'preview_images'), 0755)
        raw_image = os.path.join(os.getcwd(), 'preview_images',
                                 self.channel + '_' + self._ip)
        thumbnail = os.path.join(os.getcwd(), 'preview_images',
                                 self.channel + '_' + self._ip)
        upload_client = UploadClient(self._upload_server, self._project_name,
                                     self._ivt.id, self._ivt.login_passwd,
                                     self.channel)
        while True:
            try:
                if self._is_online != self.STATE_OFFLINE:
                    res = requests.get('http://{0}/snapshot_ch=1'.format(
                        self._ip),
                                       stream=True)
                    res.raise_for_status()
                    for data in res.iter_content(1024):
                        with open(raw_image, 'wb') as f:
                            f.write(data)
                    # TODO scale image down

                    # upload image to IVC
                    with open(thumbnail, 'rb') as f:
                        upload_client.upload(f)
            except Exception:
                try:
                    res.close()
                except Exception:
                    pass
                log.exception(
                    'Failed to fetch or upload preview image of {0}'.format(
                        self))
            finally:
                gevent.sleep(self._upload_interval)