void OnAudioEvent(TopicClient.Payload a_Payload) { Log.Status("SelfRemoteSpeechGesture", "OnAudioEvent() Called!"); float[] f = ConvertByteToFloat(a_Payload.Data); AudioClip clip = AudioClip.Create("test", f.Length, 1, 22050, false); clip.SetData(f, 0); PlayClip(clip); }
private void OnMicrophoneData(TopicClient.Payload a_Payload) { Log.Debug("TopicClient", "OnMicrophoneData() received. Payload: {0}", a_Payload); Test(TopicClient.Instance.Unsubscribe("sensor-Microphone", OnMicrophoneData)); m_bSubscribeBinaryTested = true; TopicClient.Instance.Subscribe("blackboard", OnBlackboard); TopicClient.Instance.Subscribe("invalid-topic", OnInvalidTopic); TopicClient.Instance.Publish("conversation", "tell me a joke"); }
//! Callback for sensor-manager topic. void OnGestureManagerEvent(TopicClient.Payload a_Payload) { IDictionary json = Json.Deserialize(Encoding.UTF8.GetString(a_Payload.Data)) as IDictionary; bool bFailed = false; string gestureId = json["gestureId"] as string; string instanceId = json["instanceId"] as string; string gestureKey = gestureId + "/" + instanceId; string event_name = json["event"] as string; IGesture gesture = null; if (m_Gestures.ContainsKey(gestureKey)) { if (m_Gestures.TryGetValue(gestureKey, out gesture)) { if (event_name.CompareTo("execute_gesture") == 0) { if (!gesture.Execute(OnGestureDone, json["params"] as IDictionary)) { Log.Error("GestureManager", "Failed to execute gesture {0}", gestureId); bFailed = true; } } else if (event_name.CompareTo("abort_gesture") == 0) { if (!gesture.Abort()) { Log.Error("GestureManager", "Failed to abort gesture {0}", gestureId); bFailed = true; } } } else { Log.Error("GestureManager", "Failed to find gesture {0}", gestureKey); bFailed = true; } // if we failed, send the message back with a different event if (bFailed) { json["failed_event"] = event_name; json["event"] = "error"; TopicClient.Publish("gesture-manager", Json.Serialize(json)); } } else { //do nothing } }
//! Callback for sensor-manager topic. void OnSensorManagerEvent(TopicClient.Payload a_Payload) { IDictionary json = Json.Deserialize(Encoding.UTF8.GetString(a_Payload.Data)) as IDictionary; bool bFailed = false; string sensorId = json["sensorId"] as string; ISensor sensor = null; if (m_Sensors.TryGetValue(sensorId, out sensor)) { string event_name = json["event"] as string; if (event_name.CompareTo("start_sensor") == 0) { if (!sensor.OnStart()) { Log.Error("SensorManager", "Failed to start sensor {0}", sensorId); bFailed = true; } } else if (event_name.CompareTo("stop_sensor") == 0) { if (!sensor.OnStop()) { Log.Error("SensorManager", "OnStop() returned failure for sensor {0}", sensorId); bFailed = true; } } else if (event_name.CompareTo("pause_sensor") == 0) { sensor.OnPause(); } else if (event_name.CompareTo("resume_sensor") == 0) { sensor.OnResume(); } } else { Log.Error("SensorManager", "Failed to find sensor {0}", sensorId); bFailed = true; } // if we failed, send the message back with a different event if (bFailed) { json["failed_event"] = json["event"]; json["event"] = "error"; TopicClient.Publish("sensor-manager", Json.Serialize(json)); } }
private void OnInvalidTopic(TopicClient.Payload a_Payload) { Log.Debug("TopicClient", "OnInvalidTopic(). Payload: {0}", a_Payload); Test(a_Payload == null); m_bSubFailedTested = true; }
private void OnBlackboard(TopicClient.Payload a_Payload) { Log.Debug("TopicClient", "OnBlackboard(). Payload: {0} \n Data: {1}", a_Payload, Encoding.UTF8.GetString(a_Payload.Data)); Test(TopicClient.Instance.Unsubscribe("blackboard", OnBlackboard)); m_bSubscribeTextTested = true; }
private void OnTopicManagerEvent(TopicClient.Payload a_Event) { if (a_Event != null) { IDictionary json = a_Event.ParseJson(); string event_name = json["event"] as string; if (event_name == "connected") { string selfId = json["selfId"] as string; if ((bool)json["parent"]) { if (Parent != null) { throw new WatsonException("Parent is already set!"); } Parent = new Node(m_Explorer); Parent.Children.Add(this); if (m_Explorer.OnNodeAdded != null) { m_Explorer.OnNodeAdded(Parent); } Parent.Refresh(m_Path + "../", SelfId); } else { Node child = new Node(m_Explorer); m_Children.Add(child); if (m_Explorer.OnNodeAdded != null) { m_Explorer.OnNodeAdded(child); } child.Refresh(m_Path + selfId + "/", SelfId); } } else if (event_name == "disconnected") { string selfId = json["selfId"] as string; if (Parent != null && Parent.SelfId == selfId) { if (m_Explorer.OnNodeRemoved != null) { m_Explorer.OnNodeRemoved(Parent); } Parent = null; } else { foreach (Node child in m_Children) { if (child.SelfId == selfId) { if (m_Explorer.OnNodeRemoved != null) { m_Explorer.OnNodeRemoved(child); } m_Children.Remove(child); break; } } } } } else { Log.Error("SelfExplorer", "Failed to subscribe to topic-manager, node: {0}", ToString()); } }
void OnBlackBoardEvent(TopicClient.Payload a_Payload) { IDictionary json = a_Payload.ParseJson(); bool bFailed = false; string event_name = json["event"] as string; string type = json["type"] as string; ThingEvent te = new ThingEvent(); te.m_EventType = ThingEventType.TE_NONE; te.m_Event = json; if (event_name == "add_object") { te.m_EventType = ThingEventType.TE_ADDED; // TODO: Create correct type based on type name, fall back to just making an IThing object te.m_Thing = new IThing(); try { te.m_Thing.Deserialize(json["thing"] as IDictionary); te.m_Thing.Origin = a_Payload.Origin; #if ENABLE_DEBUGGING Log.Debug("BlackBoard", "Adding object {0} from {1}", te.m_Thing.GUID, te.m_Thing.Origin); #endif if (json.Contains("parent")) { te.m_Thing.ParentGUID = json["parent"] as string; } m_ThingMap[te.m_Thing.GUID] = te.m_Thing; } catch (Exception e) { Log.Error("BlackBoard", "Failed to deserialize object: {0}, stack: {1}", e.Message, e.StackTrace); bFailed = true; } } else if (event_name == "remove_object") { te.m_EventType = ThingEventType.TE_REMOVED; string guid = json["thing_guid"] as string; if (m_ThingMap.TryGetValue(guid, out te.m_Thing)) { #if ENABLE_DEBUGGING Log.Debug("BlackBoard", "Removing object {0}", guid); #endif m_ThingMap.Remove(guid); } #if ENABLE_DEBUGGING else { Log.Debug("BlackBoard", "Failed to find object by guid {0}.", guid); } #endif } else if (event_name == "set_object_state") { string guid = json["thing_guid"] as string; if (m_ThingMap.TryGetValue(guid, out te.m_Thing)) { string state = json["state"] as string; #if ENABLE_DEBUGGING Log.Debug("BlackBoard", "Updating object {0} state to {1}", guid, state); #endif te.m_Thing.State = json["state"] as string; } #if ENABLE_DEBUGGING else { Log.Debug("BlackBoard", "Failed to find object by guid {0}.", guid); } #endif } else if (event_name == "set_object_importance") { string guid = json["thing_guid"] as string; if (m_ThingMap.TryGetValue(guid, out te.m_Thing)) { float fImportance = (float)json["importance"]; #if ENABLE_DEBUGGING Log.Debug("BlackBoard", "Updating object {0} importance to {1}", guid, fImportance); #endif te.m_Thing.Importance = fImportance; } #if ENABLE_DEBUGGING else { Log.Debug("BlackBoard", "Failed to find object by guid {0}.", guid); } #endif } // if we failed, send the message back with a different event if (bFailed) { json["failed_event"] = event_name; json["event"] = "error"; TopicClient.Instance.Publish(a_Payload.Origin, Json.Serialize(json)); } else if (te.m_EventType != ThingEventType.TE_NONE) { foreach (var path in m_SubscriberMap) { List <Subscriber> subs = null; if (path.Value.TryGetValue(type, out subs)) { for (int i = 0; i < subs.Count; ++i) { Subscriber sub = subs[i]; if (sub.m_Callback == null) { continue; } if ((sub.m_EventMask & te.m_EventType) == 0) { continue; } sub.m_Callback(te); } } } } }