Example #1
0
        /// <summary>
        /// Message Handler of TheThingEngine
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pIncoming"></param>
        public override void HandleMessage(ICDEThing sender, object pIncoming)
        {
            if (!(pIncoming is TheProcessMessage pMsg) || pMsg.Message == null)
            {
                return;
            }
            string[] tCmd = pMsg.Message.TXT.Split(':');
            switch (tCmd[0])   //string 2 cases
            {
            case "CDE_INITIALIZED":
                MyBaseEngine.SetInitialized(pMsg.Message);
                break;

            case "CDE_INITIALIZE":
                if (!MyBaseEngine.GetEngineState().IsEngineReady)
                {
                    MyBaseEngine.SetInitialized(pMsg.Message);
                }
                MyBaseEngine.ReplyInitialized(pMsg.Message);
                break;

            case "CDE_REGISTERPROPERTY":
                if (tCmd.Length > 1)
                {
                    TheThing tThing2 = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(tCmd[1]));
                    if (tThing2 != null)
                    {
                        cdeP tProp = tThing2.GetProperty(pMsg.Message.PLS);
                        if (tProp == null)
                        {
                            return;
                        }
                        tProp.SetPublication(true, pMsg.Message.GetOriginator());       //OK - Very late "Binding"
                    }
                }
                break;

            case "CDE_REGISTERTHING":
                if (MyThingRegistry != null)
                {
                    //if (TheScopeManager.IsNodeTrusted(pMsg.Message.GetOriginator())) // CODE-REVIEW: This security enhancement will not all Global Things to work anymore. WE need to bring this back when we have the meshmanager working
                    //{
                    //    TheBaseAssets.MySYSLOG.WriteToLog(7678, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(eEngineName.ContentService, String.Format("Register Thing from untrusted node received {0} - disallowed", pMsg.Message.GetOriginator()), eMsgLevel.l3_ImportantMessage));
                    //    return;
                    //}
                    TheThing tThing = TheCommonUtils.DeserializeJSONStringToObject <TheThing>(pMsg.Message.PLS);
                    if (tThing != null)
                    {
                        TheThingRegistry.RegisterThing(tThing);
                    }
                }
                break;

            case "CDE_UNREGISTERTHING":
                if (MyThingRegistry != null)
                {
                    //if (TheScopeManager.IsNodeTrusted(pMsg.Message.GetOriginator()))
                    //{
                    //    TheBaseAssets.MySYSLOG.WriteToLog(7678, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(eEngineName.ContentService, String.Format("Unregister Thing from untrusted node received {0} - disallowed", pMsg.Message.GetOriginator()), eMsgLevel.l3_ImportantMessage));
                    //    return;
                    //}
                    TheThingRegistry.DeleteThingByID(TheCommonUtils.CGuid(pMsg.Message.PLS));
                }
                break;

            case "CDE_SETP":
                if (!string.IsNullOrEmpty(pMsg.Message.PLS) && tCmd.Length > 1)
                {
                    TheThing tG = TheThingRegistry.GetThingByMID(TheCommonUtils.CGuid(tCmd[1]), true);
                    if (tG != null)
                    {
                        cdeP p = TheCommonUtils.DeserializeJSONStringToObject <cdeP>(pMsg.Message.PLS);
                        if (p != null)
                        {
                            tG.UpdatePropertyInBag(p, true, true);
                        }
                    }
                }
                break;

            case "CDE_SYNC_THINGS":
                if (pMsg.Message.PLS.Length > 2)
                {
                    List <TheThing> tList = TheCommonUtils.DeserializeJSONStringToObject <List <TheThing> >(pMsg.Message.PLS);
                    TheBaseAssets.MySYSLOG.WriteToLog(7678, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM(eEngineName.ContentService, String.Format("CDE_SYNC_THINGS: Received for node {0}", pMsg.Message.GetOriginator()), eMsgLevel.l3_ImportantMessage));     //, pMsg.Message.PLS));
                    TheThingRegistry.SyncGlobalThings(pMsg.Message.GetOriginator(), tList);
                }
                break;

            case "CDE_SEND_GLOBAL_THINGS":
                SendGlobalThings(pMsg.Message.GetOriginator());
                break;

            case "CDE_REGISTERRULE":
                var tEngs = TheThingRegistry.GetBaseEnginesByCap(eThingCaps.RulesEngine);
                foreach (var t in tEngs)
                {
                    t.GetBaseThing()?.HandleMessage(sender, pMsg);
                }
                break;
            }
        }