def streaming_off(self): self.observer.stop() self.observer = None self.scheduler.stop_thread() self.scheduler = None self.is_streaming = False if self.schd_queue.qsize() > 0: Log.e(self.tag, 'queue is not empty.')
def start_thread(self): if not self.is_on: self.is_on = True self.is_running = True self.start() else: self.is_running = True Log.d(self.tag, 'stream scheduler started.')
def streaming_on(self): if not self.observer: Log.e(self.tag, 'observer not set. it must be set before streaming.') return self.observer.start() self.scheduler.start_thread() self.is_streaming = True
def put(self, d: dict): """ send command를 queue에 삽입 :param d: send command. :type: dict { 'url': remote url, 'post_data': CAS entry data } :return: """ Log.d(self.tag, 'send command has been put in queue. cmd type:', d['url'].split('/')[-1]) self.q.put(d)
def toggle_streaming(self): self.local_dirpath = self.ledt_dirpath.text() self.url = self.ledt_url.text() if self.streamer.is_streaming: # streaming off self.streamer.streaming_off() # enable checkbox self.chbx_basic.setEnabled(True) self.chbx_ird.setEnabled(True) self.chbx_simple.setEnabled(True) # change btn caption self.lbl_state.setText('Streaming Status: Stopped') self.lbl_state.setStyleSheet('QLabel {color: red;}') self.btn_start.setText('START') else: try: # setup self.streamer.setup_streamer(self.local_dirpath, self.url, self.get_flags()) # streaming on self.streamer.streaming_on() except Exception as e: Log.e(self.tag, 'streaming start error! :', e.__class__.__name__) return # disable checkbox self.chbx_basic.setEnabled(False) self.chbx_ird.setEnabled(False) self.chbx_simple.setEnabled(False) # change btn caption self.lbl_state.setText('Streaming Status: Streaming') self.lbl_state.setStyleSheet('QLabel {color: green;}') self.btn_start.setText('STOP') Log.d(self.tag, self.lbl_state.text(), ',', self.local_dirpath)
def send_stream(self, entry, mode): if mode == 'basic': post_data = entry.get_category(category='basic') footer = 'stream' elif mode == 'ird': post_data = entry.get_category(category='ird', str_key_type=True) footer = 'stream_ird' elif mode == 'simple': post_data = entry.get_category(category='simple') footer = 'stream_simple' elif mode == 'file': post_data = entry.get_category( category='simple') # file 모드가 현재 테스트모드로 동작중 footer = 'stream_test' else: Log.e(self.tag, 'invalid send stream mode. abort send stream.') return Log.d(self.tag, 'send stream %s ::' % entry.file_name.split('\\')[-1], str(post_data)[:50] + '...') self.ref_scheduler.put({ 'url': self.url + footer, 'post_data': post_data })
def on_created(self, event): # TODO: process when a file created in the selected directory if event.event_type == 'created': Log.d( self.tag, 'file creation detected. waiting for creating completed for %ds...' % self.wait) time.sleep(self.wait) entry = CasEntry('') # make empty entry for i in range(self.retry): entry = CasEntry(event.src_path) if entry.valid: break else: Log.e( self.tag, 'new file is not valid. retrying...(%d / %d)' % (i + 1, self.retry)) time.sleep(1) if not entry.valid: Log.e(self.tag, 'ISD file parsing error. streaming aborted.') return # TODO: send cas entry if self.flags['basic']: self.send_stream(entry, mode='basic') # 분광 빼고 전부 if self.flags['ird']: self.send_stream( entry, mode='ird', ) # 분광만 if self.flags['simple']: self.send_stream(entry, mode='simple') # 간략 데이터 if self.flags['file']: self.send_stream(entry, mode='file') else: pass
def stop_thread(self): self.is_on = False Log.d(self.tag, 'stream scheduler stopped.')
def run(self): """ queue에 들어있는 send command 하나씩 꺼내서 url로 전송 :return: none """ while self.is_on: if self.q.qsize() > 0: # get one send command dict send_cmd = self.q.get() # validate send command try: url = send_cmd['url'] post_data = send_cmd['post_data'] except KeyError: Log.e(self.tag, 'send command format error:', send_cmd) continue # discard command dict # send to backend server retry = 10 # while resend: i = 0 for i in range(1, retry + 1): try: Log.d(self.tag, 'tried: %d, url: %s' % (i, url)) Log.d(self.tag, 'body:', str(post_data)[:70] + '...') response = requests.post(url, json=post_data) # response = requests.post(url, data=post_data) => data가 아니라 json except Exception as e: Log.e(self.tag, 'streaming error:', e.__class__.__name__) time.sleep(0.5) continue # retry else: try: if response.status_code == 200: Log.e(self.tag, response.text) break # succeed stream else: Log.e( self.tag, 'http post error: %d' % response.status_code) continue # retry except: Log.e(self.tag, 'no response.') continue # retry if i >= 10: Log.d(self.tag, 'tried %d times, reinsert into queue.' % i) self.q.put(send_cmd) # Log.e(self.tag, 'send thread running...') time.sleep(1)