def resume(self): self.duration = 0 self.runner.resume() self.runner.topics['events'].publish(Event('chat_end', 0)) if self.subscriber: self.subscriber.unregister() self.subscriber = False
def add_turn(self): self.turns += 1 self.last_turn_at = time.time() if self.turns < self.dialog_turns and not ( self.timeout_mode == 'whole' and time.time() - self.started_at >= self.timeout): self.runner.topics['events'].publish(Event('chat', 0)) else: self.resume()
def start(self, run_time): self.runner.pause() self.last_turn_at = time.time() if self.enable_chatbot: self.start_chatbot_session() def input_callback(event): self.respond( self.get_chatbot_response(event.data) if self. enable_chatbot else self.match_response(event.data)) self.subscriber = rospy.Subscriber( '/' + self.runner.robot_name + '/nodes/listen/input', String, input_callback) self.runner.topics['events'].publish(Event('chat', 0)) self.runner.register('speech_events', self.speech_event_callback)
def pause(self): with self.lock: if self.running and not self.paused: self.pause_time = time.time() self.paused = True paused_time = self.get_run_time() self.topics['events'].publish(Event('paused', paused_time)) log_data = { 'performance_report': True, 'performance_id': self.running_performance.get('id', ''), 'performance_time': paused_time, 'performance_action': 'paused' } logger.warn('Pause performance #{} at: {}'.format( log_data['performance_id'], paused_time), extra={'data': log_data}) return True else: return False
def resume(self): success = False with self.lock: if self.running and self.paused: run_time = self.get_run_time() self.paused = False self.start_timestamp = time.time() - run_time self.start_time = 0 self.topics['events'].publish(Event('resume', run_time)) log_data = { 'performance_report': True, 'performance_id': self.running_performance.get('id', ''), 'performance_time': run_time, 'performance_action': 'resume' } logger.warn('Resume performance #{} at: {}'.format( log_data['performance_id'], run_time), extra={'data': log_data}) success = True return success
def worker(self): self.run_condition.acquire() while True: with self.lock: self.paused = False self.running = False self.topics['events'].publish(Event('idle', 0)) self.run_condition.wait() self.topics['events'].publish(Event('running', self.start_time)) with self.lock: if not self.running_performance: continue behavior = True offset = 0 timelines = self.running_performance[ 'timelines'] if 'timelines' in self.running_performance else [ self.running_performance ] for i, timeline in enumerate(timelines): if 'enabled' in timeline and not timeline['enabled']: continue # check if performance is finished without starting running = True self.running_nodes = [ Node.createNode(node, self, self.start_time - offset, timeline.get('id', '')) for node in timeline['nodes'] ] pid = timeline.get('id', '') finished = None run_time = 0 pause = pid and self.get_property(os.path.dirname(pid), 'pause_behavior') # Pause must be either enabled or not set (by default all performances are # pausing behavior if its not set) if (pause or pause is None) and behavior: # Only pause behavior if its already running. Otherwise Pause behavior have no effect behavior_enabled = False try: behavior_enabled = rospy.get_param("/behavior_enabled") except KeyError: pass if behavior_enabled: self.topics['interaction'].publish('btree_off') behavior = False with self.lock: if not self.running: break while running: with self.lock: run_time = self.get_run_time() if not self.running: self.topics['events'].publish( Event('finished', run_time)) break if self.paused: continue running = False # checks if any nodes still running for k, node in enumerate(self.running_nodes): running = node.run(run_time - offset) or running if finished is None: # true if all performance nodes are already finished finished = not running log_data = { 'performance_report': True, 'performance_id': self.running_performance.get('id', '') if self.running_performance else '', 'performance_time': run_time, 'performance_action': 'finished' } if i == len(timelines) - 1: logger.warn('Finished performance #{} at: {}'.format( log_data['performance_id'], run_time), extra={'data': log_data}) offset += self.get_timeline_duration(timeline) with self.lock: autopause = self.autopause and finished is False and i < len( timelines) - 1 if autopause: self.pause() if not behavior: self.topics['interaction'].publish('btree_on') if self.unload_finished: self.unload_finished = False self.unload()
def respond(self, response): self.runner.topics['events'].publish(Event('chat_end', 0)) self.talking = True self.runner.topics['tts'].publish(TTS(response, 'en-US'))