Exemple #1
0
            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());
                }
            }
Exemple #2
0
        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);
                        }
                    }
                }
            }
        }