def set_state(self, thing, state, path): ''' Publish a given state to a given path on the blackboard ''' data = {} data['event'] = 'set_object_state' data['thing_guid'] = thing.get_guid() data['state'] = state TopicClient.get_instance().publish(path + 'blackboard', data, False)
def set_importance(self, thing, importance, path): ''' Publish a given level of importance to a given path on the blackboard ''' data = {} data['event'] = 'set_object_importance' data['thing_guid'] = thing.get_guid() data['importance'] = importance TopicClient.get_instance().publish(path + 'blackboard', data, False)
def send_data(self, sensor, data): ''' Send off the given data blob to the corresonding sensor topic ''' if self.is_registered(sensor): TopicClient.get_instance().publish_binary( 'sensor-proxy-' + sensor.get_sensor_id(), data, False) else: print "SendData() invoked on unregistered sensor: " + sensor.get_sensor_id( )
def on_gesture_done(self, gesture, error): ''' Publish a message to the blackboard when a gesture is executed ''' data = {} data['event'] = 'execute_done' data['gestureId'] = gesture.get_gesture_id() data['instanceId'] = gesture.get_instance_id() data['error'] = error TopicClient.get_instance().publish('gesture-manager', data, False)
def remove_agent(self, agent): ''' Remove an agent from this society ''' if agent.get_agent_id in self.agent_map: self.overrides_map.remove(agent.get_agent_id()) self.agent_map.remove(agent.get_agent_id()) data = {} data['event'] = 'remove_agent_proxy' data['agentId'] = agent.get_agent_id() TopicClient.get_instance().publish('agent-society', data, False)
def remove_sensor(self, sensor): ''' Remove the given sensor from this manager ''' if sensor.get_sensor_id() in self.sensor_map: self.sensor_map.remove(sensor.get_sensor_id()) self.override_map.remove(sensor.get_sensor_id()) data = {} data['event'] = 'remove_sensor_proxy' data['sensorId'] = sensor.get_sensor_id() TopicClient.get_instance().publish('sensor-manager', data, False)
def run(self): try: headers = [('selfId', ''), ('token', '')] topic = TopicClient.start_instance('127.0.0.1', 9443, headers) TopicClient.get_instance().setHeaders("", "") TopicClient.get_instance().set_callback(self.on_connected); topic.start() except KeyboardInterrupt: thread.exit()
def add_thing(self, thing, path): ''' Add a concept to this blackboard, which will be automatically connected to other concepts to produce a goal in the end ''' data = {} data['event'] = 'add_object' data['type'] = thing.get_type() data['thing'] = thing.serialize() if thing.get_parent(): data['parent'] = thing.get_parent() TopicClient.get_instance().publish(path + 'blackboard', data, False)
def remove_gesture(self, gesture): ''' Remove a gesture from this manager's set of gestures ''' if gesture.get_gesture_id() in self.gestures_map: if gesture.on_stop(): data = {} self.gestures_map.remove(gesture.get_gesture_id()) self.overrides_map.remove(gesture.get_gesture_id()) data['event'] = 'remove_gesture_proxy' data['gestureId'] = gesture.get_gesture_id() data['instanceId'] = gesture.get_instance_id() TopicClient.get_instance().publish('gesture-manager', data, False)
def add_agent(self, agent, override): ''' Add an agent to this society. The society takes ownership of the agent ''' if agent.get_agent_id not in self.agent_map: data = {} data['event'] = 'add_agent_proxy' data['agentId'] = agent.get_agent_id() data['name'] = agent.get_agent_name() data['override'] = override TopicClient.get_instance().publish('agent-society', data, False) self.agent_map[agent.get_agent_id()] = agent self.overrides_map[agent.get_agent_id()] = override
def add_gesture(self, gesture, override): ''' Add a gesture to the set of gestures available to this manager ''' if gesture.get_gesture_id() not in self.gestures_map: if gesture.on_start(): data = {} data['event'] = 'add_gesture_proxy' data['gestureId'] = gesture.get_gesture_id() data['instanceId'] = gesture.get_instance_id() data['override'] = override TopicClient.get_instance().publish('gesture-manager', data, False) self.gestures_map[gesture.get_gesture_id()] = gesture self.overrides_map[gesture.get_gesture_id()] = override print "adding gesture id: " + gesture.get_gesture_id()
def run(self): try: self_id = str(uuid.uuid4()) print("self id: " + self_id) headers = [('selfId', self_id), ('token', '')] topic = TopicClient.start_instance('127.0.0.1', 9443, headers) TopicClient.get_instance().setHeaders(self_id, "") TopicClient.get_instance().set_callback(self.on_connected) topic.start() except KeyboardInterrupt: exit() except ConnectionRefusedError: print("connection error: ")
def add_sensor(self, sensor, override): ''' Add a given sensor to this manager. The manager takes ownership of the object if accepted ''' if sensor.get_sensor_id() not in self.sensor_map: data = {} data['event'] = 'add_sensor_proxy' data['sensorId'] = sensor.get_sensor_id() data['name'] = sensor.get_sensor_name() data['data_type'] = sensor.get_data_type() data['binary_type'] = sensor.get_binary_type() data['override'] = override TopicClient.get_instance().publish('sensor-manager', data, False) self.sensor_map[sensor.get_sensor_id()] = sensor self.overrides_map[sensor.get_sensor_id()] = override print "adding sensor id: " + sensor.get_sensor_id()
def subscribe_to_type(self, thing, thing_event, path, callback): ''' Subscribe to any objects of a given type that get put on the blackboard ''' if path not in self.subscription_map: TopicClient.get_instance().subscribe(path + 'blackboard', self.on_event) self.subscription_map[path] = collections.defaultdict(set) if thing not in self.subscription_map[path]: data = {} data['event'] = 'subscribe_to_type' data['type'] = thing data['event_mask'] = thing_event TopicClient.get_instance().publish(path + 'blackboard', data, False) self.subscription_map[path][thing] = [] self.subscription_map[path][thing].append(Subscriber(callback, thing_event, path))
def on_event(self, data): ''' Callback that can add or remove proxy agents to and from the society ''' error = False payload = json.loads(data) if payload['agentId'] not in self.agent_map: print("Could not find agent id: " + payload['agentId']) error = True elif payload['event'] == 'start_agent': self.agent_map[payload['agentId']].on_start() elif payload['event'] == 'stop_agent': self.agent_map[payload['agentId']].on_stop() if error: data = {} data['failed_event'] = payload['event'] data['event'] = 'error' TopicClient.get_instance().publish('agent-society', data, False)
def unsubscribe_to_type(self, thing, callback, path): ''' Unsubscribe from a given type of objects that get put on the blackboard ''' if path in self.subscription_map: if thing in self.subscription_map[path]: for subscription in self.subscription_map[path][thing]: if subscription.callback == callback: self.subscription_map[path][thing].remove(subscription) break if len(self.subscription_map[path][thing]) == 0: self.subscription_map[path].remove(thing) if thing not in self.subscription_map[path]: data = {} data['event'] = 'unsubscribe_from_type' data['type'] = thing TopicClient.get_instance().publish(path + 'blackboard', data, False)
def on_event(self, data): ''' Callback for different kinds of gesture-related events, such as adding and removing gesture proxies ''' error = False payload = json.loads(data) if payload['gestureId'] not in self.gestures_map: print "Failed to find gesture: " + payload['gestureId'] error = True elif payload['event'] == 'execute_gesture': params = payload['params'] self.gestures_map[payload['gestureId']].execute(params) elif payload['event'] == 'abort_gesture': self.gestures_map[payload['gestureId']].abort() if error: data = {} data['failed_event'] = payload['event'] data['event'] = 'error' TopicClient.get_instance().publish('gesture-manager', data, False)
def on_event(self, data): ''' Respond to different events such as requests to start, pause, or resume sensors ''' error = False payload = json.loads(data) if payload['sensorId'] not in self.sensor_map: print "Failed to find sensor: " + payload['sensorId'] error = True elif payload['event'] == 'start_sensor': self.sensor_map[payload['sensorId']].on_start() elif payload['event'] == 'pause_sensor': self.sensor_map[payload['sensorId']].on_pause() elif payload['event'] == "resume_sensor": self.sensor_map[payload['sensorId']].on_resume() if error: data = {} data['failed_event'] = payload['event'] data['event'] = 'error' TopicClient.get_instance().publish('sensor-manager', data, False)
def run_thread(headers): print "Thread is running!!" sensor = CameraSensor(str(uuid.uuid4()), "Camera", "VideoData", "image/jpeg") mic = MicrophoneSensor(str(uuid.uuid4()), "Microphone", "AudioData", "audio/L16;rate=16000;channels=1") gesture = SpeechGesture("tts", str(uuid.uuid4())) agent = ExampleAgent('ExampleAgent', str(uuid.uuid4())) while TopicClient.get_instance().isConnected() == False: print "Topic client not connected yet!" time.sleep(1) SensorManager.get_instance().subscribe() GestureManager.get_instance().subscribe() AgentSociety.get_instance().subscribe() SensorManager.get_instance().add_sensor(sensor, True) SensorManager.get_instance().add_sensor(mic, True) GestureManager.get_instance().add_gesture(gesture, True) AgentSociety.get_instance().add_agent(agent, False)
def subscribe(self): ''' Subscribe to the agent-society topic ''' TopicClient.get_instance().subscribe('agent-society', self.on_event)
def shutdown(self): ''' Unsubscribe from the agent-society topic ''' TopicClient.get_instance().unsubscribe('agent-society')
def subscribe(self): ''' Subscribe to the gesture-manager topic ''' TopicClient.get_instance().subscribe('gesture-manager', self.on_event)
def subscribe(self): ''' Subscribe to the sensor-manager topic ''' TopicClient.get_instance().subscribe("sensor-manager", self.on_event)
def shutdown(self): TopicClient.get_instance().unsubscribe('sensor-manager')
def remove_thing(self, thing, path): ''' Remove a specific thing from the blackboard ''' data = {} data['event'] = 'remove_object' data['thing_guid'] = thing.get_guid() TopicClient.get_instance().publish(path + 'blackboard', data, False)