Ejemplo n.º 1
0
        private void OnDownloadClick(ICDEThing pThing, object pPara)
        {
            TheProcessMessage pMSG = pPara as TheProcessMessage;

            if (pMSG == null || pMSG.Message == null)
            {
                return;
            }

            string[] cmd = pMSG.Message.PLS.Split(':');
            if (cmd.Length > 2)
            {
                TheThing tThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(cmd[2]), true);
                if (tThing == null)
                {
                    return;
                }

                TSM tFilePush = new TSM(eEngineName.ContentService, string.Format("CDE_FILE:{0}.JSON:application/zip", tThing.FriendlyName))
                {
                    SID = pMSG.Message.SID,
                    PLS = "bin",
                    PLB = TheCommonUtils.CUTF8String2Array(TheCommonUtils.SerializeObjectToJSONString(tThing))
                };
                TheCommCore.PublishToOriginator(pMSG.Message, tFilePush);
            }
        }
Ejemplo n.º 2
0
        void SendRuleTSM(bool serviceOnly)
        {
            string engine  = TheCommonUtils.CStr(GetProperty("TSMEngine", false));
            string text    = TheCommonUtils.CStr(GetProperty("TSMText", false));
            string payload = TheCommonUtils.CStr(GetProperty("TSMPayload", false));

            //payload = payload.Replace("%DTO%", TheCommonUtils.CStr(DateTimeOffset.Now));
            //text = text.Replace("%DTO%", TheCommonUtils.CStr(DateTimeOffset.Now));
            ICDEThing triggerThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(TriggerObject)) as ICDEThing;
            string    escPayload   = TheCommonUtils.GenerateFinalStr(payload, triggerThing);

            escPayload = TheCommonUtils.GenerateFinalStr(escPayload, MyBaseThing);
            string escText = TheCommonUtils.GenerateFinalStr(text, triggerThing);

            escText = TheCommonUtils.GenerateFinalStr(escText, MyBaseThing);

            if (!string.IsNullOrEmpty(engine) && !string.IsNullOrEmpty(text))
            {
                TSM customTSM = new TSM(engine, escText, escPayload);
                if (serviceOnly)
                {
                    customTSM.SetToServiceOnly(true);
                }
                TheCommCore.PublishCentral(customTSM, true);
            }
            if (IsRuleLogged)
            {
                LogEvent(escPayload);
            }
            if (TheThing.GetSafePropertyBool(MyBaseThing, "IsEVTLogged"))
            {
                TheLoggerFactory.LogEvent(eLoggerCategory.RuleEvent, TheCommonUtils.GenerateFinalStr(MyBaseThing.FriendlyName, MyBaseThing), eMsgLevel.l4_Message, TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false), escText, escPayload);
            }
        }
Ejemplo n.º 3
0
        IEnumerable <JSonOpcArrayElement> GetJSONArray(TheThingStore thingEvent)
        {
            // TODO Replace with linkid some identifier that represents the activated instance/customer, and make this more efficient (avoid or cache thing registry lookup)
            string linkid;
            var    tThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(thingEvent.cdeMID), true);

            if (tThing != null && !String.IsNullOrEmpty(tThing.FriendlyName))
            {
                linkid = tThing.FriendlyName;
            }
            else
            {
                if (thingEvent.PB.ContainsKey("Address"))
                {
                    linkid = TheCommonUtils.CStr(thingEvent.PB["Address"]);
                }
                else
                {
                    if (tThing != null)
                    {
                        linkid = tThing.Address;
                    }
                    else
                    {
                        linkid = TheCommonUtils.cdeGuidToString(thingEvent.cdeMID);
                    }
                }
            }

            var jsonArray = new List <JSonOpcArrayElement>();

            jsonArray.AddRange(thingEvent.PB.Select(propKV => GetJSONArrayElement(linkid, propKV.Key, thingEvent.cdeCTIM, propKV.Value)));
            return(jsonArray.OrderBy(ev => ev.sourcetimestamp));
        }
Ejemplo n.º 4
0
 TheThing GetOpcThing()
 {
     if (_opcThing == null)
     {
         _opcThing = TheThingRegistry.GetThingByMID(OpcThingAddress.EngineName, OpcThingAddress.ThingMID);
     }
     return(_opcThing);
 }
Ejemplo n.º 5
0
 public TheThing GetThing()
 {
     if (_pThing == null)
     {
         _pThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(this.ThingMID), true);
     }
     return(_pThing);
 }
 public TheThing GetBaseThing()
 {
     if (_baseThing == null && ThingMID != Guid.Empty)
     {
         _baseThing = TheThingRegistry.GetThingByMID(ThingMID, true);
     }
     return(_baseThing);
 }
Ejemplo n.º 7
0
 internal TheThing GetThing()
 {
     if (_thing == null)
     {
         _thing = TheThingRegistry.GetThingByMID(ThingMid, true);
     }
     return(_thing);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// If this is a service the SimplexProc event will be called when the C-DEngine receives a new event sent by a subscriber to this service
        /// </summary>
        /// <param name="pMsg">The Message to be Processed</param>
        private void ProcessServiceMessage(TheProcessMessage pMsg)
        {
            string[] cmd = pMsg.Message.TXT.Split(':');
            switch (cmd[0]) //string 2 cases
            {
            case "WEBRELAY_RESPONSE":
                TheRequestData tState = TheCommonUtils.DeserializeJSONStringToObject <TheRequestData>(pMsg.Message.PLS);
                if (pMsg.Message.PLB != null && pMsg.Message.PLB.Length > 0)
                {
                    tState.ResponseBuffer = pMsg.Message.PLB;
                }
                tState.RequestUri = new Uri(tState.RequestUriString);
                //tState.SessionState = new TheSessionState() { SScopeID = pMsg.SID };
                sinkResults(tState);
                break;

            case "WEBRELAY_REQUEST":
                TheRequestData tData = TheCommonUtils.DeserializeJSONStringToObject <TheRequestData>(pMsg.Message.PLS);
                if (tData != null)
                {
                    tData.RequestUri = new Uri(tData.RequestUriString);
                    //tData.SessionState = new TheSessionState() { SScopeID = pMsg.SID };
                    if (pMsg.Message.PLB != null && pMsg.Message.PLB.Length > 0)
                    {
                        tData.PostData = pMsg.Message.PLB;
                    }
                    tData.CookieObject = pMsg.Message;
                    ReadHttpPage(tData, tData.SessionState.ARApp, null, sinkProcessResponse);
                    //InterceptHttpRequest(tData,tData.SessionState.ARApp);
                }
                break;

            case "WEBRELAY_REQUESTWRA":
                TSM             tTSM    = new TSM();
                List <TheThing> webApps = TheThingRegistry.GetThingsOfEngine(MyBaseEngine.GetEngineName());
                foreach (TheThing tApp in webApps)
                {
                    if (tApp.Address.Equals(pMsg.Message.PLS))
                    {
                        tTSM.PLS = $"/CDEWRA{tApp.cdeMID}" + TheThing.GetSafePropertyString(tApp, "HomePage");
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(tTSM.PLS))
                {
                    string[]    org          = pMsg.Message.ORG.Split(':');
                    TheThing    senderThing  = TheThingRegistry.GetThingByMID(TheCommonUtils.CGuid(org[1]), true);
                    IBaseEngine senderEngine = TheThingRegistry.GetBaseEngine(senderThing, true);
                    tTSM.ENG = senderEngine.GetEngineName();
                    tTSM.TXT = "RESPONSEWRA";
                    TheCommCore.PublishToOriginator(pMsg.Message, tTSM, true);
                }
                break;
            }
        }
Ejemplo n.º 9
0
        internal static void DisableNMISubscriptions(Guid NodeID)
        {
            List <TheNMISubscription> tToUnregister = new List <TheNMISubscription>();

            lock (MyNMIElements.MyLock) //This can cause jams of threads here! Highly optimized to get out of lock as fast as possible - could use a reader lock as we dont delete/add to the array- just change a prop
            {
                foreach (var k in MyNMIElements.Keys)
                {
                    TheNMISubscription tSub = null;
                    bool IsOneStillAlive    = false;
                    foreach (var tK in MyNMIElements[k].Keys)
                    {
                        if (MyNMIElements[k][tK].cdeN == NodeID)
                        {
                            MyNMIElements[k][tK].HasLiveSub = false;
                            tSub = MyNMIElements[k][tK];
                            continue;
                        }
                        if (MyNMIElements[k][tK].HasLiveSub == true)
                        {
                            IsOneStillAlive = true;
                            break;
                        }
                    }
                    if (!IsOneStillAlive && tSub != null)
                    {
                        tToUnregister.Add(tSub);
                    }
                }
            }
            if (tToUnregister.Count > 0)
            {
                foreach (var tG in tToUnregister)
                {
                    var tThing = TheThingRegistry.GetThingByMID(tG.cdeO);
                    if (tThing == null)
                    {
                        continue;
                    }
                    var OnUpdateName = tG.DataItem;
                    if (OnUpdateName.StartsWith("MyPropertyBag."))  //TODO: Test this with Prop of Prop
                    {
                        OnUpdateName = OnUpdateName.Split('.')[1];
                    }
                    tThing.GetProperty(OnUpdateName, true).SetPublication(false, Guid.Empty); //Guid.Empty uses PublishCentral - a specific node would use SYSTEMWIDE
                    MyNMIElements.RemoveNoCare(tG.cdeMID);
                    MyAliveThings.RemoveNoCare(tThing.cdeMID);
                }
            }
        }
Ejemplo n.º 10
0
        private void StartVerifier()
        {
            var thing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(this.ThingToVerify));

            if (thing != null)
            {
                thing.RegisterEvent(eThingEvents.PropertySet, OnThingUpdate);
                m_thingToVerify = thing;
                g_sw            = new System.Diagnostics.Stopwatch();
                g_sw.Start();
                MyBaseThing.StatusLevel = 1;
                IsStarted = true;
                MyBaseThing.LastMessage = "Verifier started";
            }
        }
Ejemplo n.º 11
0
        private void RemoveTrigger(TheRule tRule, bool DoForce)
        {
            if (TheCDEngines.MyThingEngine == null || !TheBaseAssets.MasterSwitch)
            {
                return;
            }
            if (tRule.IsRuleActive || DoForce)
            {
                if (TheThingRegistry.HasThingsWithFunc(MyBaseEngine.GetEngineName(), s => s.cdeMID != tRule.GetBaseThing().cdeMID&&
                                                       TheThing.GetSafePropertyString(s, "TriggerObject") == tRule.TriggerObject && TheThing.GetSafePropertyBool(s, "IsRuleActive")))
                {
                    return;
                }
                switch (tRule.TriggerObjectType)
                {
                default:
                    TheThing tTriggerThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(tRule.TriggerObject));
                    if (tTriggerThing != null)
                    {
                        cdeP tProp = tTriggerThing.GetProperty(tRule.TriggerProperty);
                        if (tProp != null)
                        {
                            tProp.UnregisterEvent(eThingEvents.PropertyChanged, sinkRuleAction);
                        }
                    }
                    break;

                case "CDE_ENGINE":
                    TheThing tBase = TheThingRegistry.GetBaseEngineAsThing(tRule.TriggerObject);
                    if (tBase != null)
                    {
                        tBase.UnregisterEvent(eEngineEvents.IncomingMessage, sinkRuleIncoming);
                    }
                    break;

                case "CDE_EVENTFIRED":
                    TheThing tBaseE = TheThingRegistry.GetThingByID("*", tRule.TriggerObject);
                    if (tBaseE != null)
                    {
                        tBaseE.UnregisterEvent(tRule.TriggerProperty, sinkRuleThingEvent);
                    }
                    break;
                }
                tRule.IsRuleActive  = false;
                tRule.IsRuleRunning = false;
                tRule.IsRuleWaiting = true;
            }
        }
Ejemplo n.º 12
0
        public static bool CopyStateSensorInfo(TheThing pBaseThing)
        {
            if (pBaseThing == null)
            {
                return(false);
            }
            var t = TheThingRegistry.GetThingByMID("*", TheThing.GetSafePropertyGuid(pBaseThing, "RealSensorThing"));

            if (t != null && TheThing.GetSafePropertyBool(t, "IsStateSensor"))
            {
                if (string.IsNullOrEmpty(TheThing.GetSafePropertyString(pBaseThing, "StateSensorType")))
                {
                    TheThing.SetSafePropertyString(pBaseThing, "StateSensorType", TheThing.GetSafePropertyString(t, "StateSensorType"));
                }
                if (string.IsNullOrEmpty(TheThing.GetSafePropertyString(pBaseThing, "StateSensorUnit")))
                {
                    TheThing.SetSafePropertyString(pBaseThing, "StateSensorUnit", TheThing.GetSafePropertyString(t, "StateSensorUnit"));
                }
                if (string.IsNullOrEmpty(TheThing.GetSafePropertyString(pBaseThing, "StateSensorValueName")))
                {
                    TheThing.SetSafePropertyString(pBaseThing, "StateSensorValueName", TheThing.GetSafePropertyString(t, "StateSensorValueName"));
                }
                if (pBaseThing.GetProperty("StateSensorAverage") == null)
                {
                    TheThing.SetSafePropertyNumber(pBaseThing, "StateSensorAverage", TheThing.GetSafePropertyNumber(t, "StateSensorAverage"));
                }
                if (pBaseThing.GetProperty("StateSensorMinValue") == null)
                {
                    TheThing.SetSafePropertyNumber(pBaseThing, "StateSensorMinValue", TheThing.GetSafePropertyNumber(t, "StateSensorMinValue"));
                }
                if (pBaseThing.GetProperty("StateSensorMaxValue") == null)
                {
                    TheThing.SetSafePropertyNumber(pBaseThing, "StateSensorMaxValue", TheThing.GetSafePropertyNumber(t, "StateSensorMaxValue"));
                }
                if (string.IsNullOrEmpty(TheThing.GetSafePropertyString(pBaseThing, "StateSensorIcon")))
                {
                    TheThing.SetSafePropertyString(pBaseThing, "StateSensorIcon", TheThing.GetSafePropertyString(t, "StateSensorIcon"));
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 13
0
 internal static TheThing RegisterNMISubscription(TheClientInfo pClientInfo, string pDataItem, TheMetaDataBase tFld)
 {
     if (!string.IsNullOrEmpty(pDataItem))
     {
         TheThing tThing = null;
         lock (MyNMIElements.MyLock) //This can cause jams of threads here! Highly optimized to get out of lock as fast as possible
         {
             if (!MyNMIElements.ContainsKey(tFld.cdeMID))
             {
                 MyNMIElements.TryAdd(tFld.cdeMID, new cdeConcurrentDictionary <Guid, TheNMISubscription>());
             }
             if (!MyNMIElements[tFld.cdeMID].ContainsKey(pClientInfo.NodeID))
             {
                 tThing = TheThingRegistry.GetThingByMID(tFld.cdeO); //Most expensive call in here
                 if (tThing != null)
                 {
                     var tNewEle = new TheNMISubscription()
                     {
                         cdeN = pClientInfo.NodeID, cdeO = tThing.cdeMID, HasLiveSub = true, DataItem = pDataItem, cdeMID = tFld.cdeMID
                     };
                     MyNMIElements[tFld.cdeMID].TryAdd(pClientInfo.NodeID, tNewEle);
                 }
             }
         }
         if (tThing != null)
         {
             if (tFld is TheFieldInfo && (tFld as TheFieldInfo).Type == eFieldType.Table)    //fix for Bug#1195
             {
                 return(tThing);
             }
             RegisterNewNMINode(pClientInfo.NodeID, tFld.cdeMID, tThing.cdeMID, pDataItem, true);
             var OnUpdateName = pDataItem;
             if (OnUpdateName.StartsWith("MyPropertyBag."))  //TODO: Test this with Prop of Prop
             {
                 OnUpdateName = OnUpdateName.Split('.')[1];
             }
             tThing.GetProperty(OnUpdateName, true).SetPublication(true, Guid.Empty); //Guid.Empty uses PublishCentral - a specific node would use SYSTEMWIDE
             return(tThing);
         }
     }
     return(null);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Creates a new rule or updates an existing one
 /// </summary>
 /// <param name="pRule">Rule to be created or updated</param>
 /// <returns></returns>
 public static bool CreateOrUpdateRule(TheThingRule pRule)
 {
     InitRR();
     if (MyRulesRegistry != null && pRule != null && pRule.GetBaseThing() != null)
     {
         var t = TheThingRegistry.GetThingByMID(pRule.GetBaseThing().cdeMID);
         if (t != null)
         {
             pRule.SetBaseThing(t);
         }
         pRule.Parent = MyRulesRegistry.GetBaseThing().ID;
         MyRulesRegistry.RegisterRule(pRule);
         if (pRule.IsRuleActive)
         {
             MyRulesRegistry.ActivateRules();
         }
         return(true);
     }
     return(false);
 }
Ejemplo n.º 15
0
        public bool LogEvent(string ActionText)
        {
            TheEventLogData tSec = new TheEventLogData
            {
                EventTime   = DateTimeOffset.Now,
                EventLevel  = eMsgLevel.l4_Message,
                StationName = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false),
                EventName   = TheCommonUtils.GenerateFinalStr(MyBaseThing.FriendlyName, MyBaseThing)
            };

            if (!string.IsNullOrEmpty(tSec.EventName))
            {
                tSec.EventName = tSec.EventName.Replace("%OldValue%", TriggerOldValue);
            }
            tSec.EventTrigger = TheThingRegistry.GetThingByMID(TheCommonUtils.CGuid(TriggerObject))?.FriendlyName;
            tSec.ActionObject = ActionText;
            var tEng = MyBaseEngine.GetBaseThing().GetObject() as TheRulesEngine;

            tEng?.LogEvent(tSec);
            return(true);
        }
Ejemplo n.º 16
0
        private void CreateConfigurationSection(int writeEnableFlag, TheFormInfo tMyForm, int pStartFld, int pParentFld)
        {
            UseTree = TheCommonUtils.CBool(TheBaseAssets.MySettings.GetSetting("UseTreeView"));

            TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.CollapsibleGroup, pStartFld, 2, 0xc0, "Tag Management...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup()
            {
                DoClose = !UseTree, IsSmall = true, ParentFld = pParentFld, TileWidth = UseTree ? 18 : 6
            }));

            if (!UseTree)
            {
                TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.SingleEnded, pStartFld + 1, writeEnableFlag, 0xC0, "Browse Branch", "BrowseBranch", new nmiCtrlSingleEnded()
                {
                    ParentFld = pStartFld
                });
            }
#if USE_WEBIX
            else
            {
                var stringCols = TheCommonUtils.SerializeObjectToJSONString(new List <TheWXColumn> {
                    { new TheWXColumn()
                      {
                          TileWidth = 6, Header = "Node Name", ID = "DisplayName", FilterType = "textFilter", Template = "{common.treetable()}{common.treecheckbox()}&nbsp;<strong>#DisplayName#</strong>"
                      } },
                    //{ new TheWXColumn() { TileWidth = 2, Header = "Host Property", ID = "HostPropertyNameOverride", SortType = "string", Flags=2 } },
                    { new TheWXColumn()
                      {
                          TileWidth = 1, Header = "Sample-Rate", ID = "SampleRate", SortType = "int", Flags = 2
                      } },
                    { new TheWXColumn()
                      {
                          TileWidth = 1, Header = "Deadband-Filter", ID = "DeadbandFilterValue", SortType = "int", Flags = 2
                      } },
                    { new TheWXColumn()
                      {
                          TileWidth = 1, Header = "Trigger", ID = "ChangeTrigger", SortType = "int", Flags = 2
                      } },
                    { new TheWXColumn()
                      {
                          TileWidth = 6, Header = "Node ID", ID = "NodeIdName", FilterType = "textFilter", SortType = "int"
                      } }
                });

                OpcTagTree = TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.UserControl, pStartFld + 6, 2, 0, "Sample Tree", "SelectedIs", new nmiCtrlWXTreeTable()
                {
                    ParentFld       = pStartFld,
                    TileWidth       = 12,
                    TileHeight      = 12,
                    NoTE            = true,
                    RootNode        = "Objects",
                    TreeNode        = "Parent",
                    NameNode        = "DisplayName",
                    SelectNode      = "HasActiveHostThing",
                    LeftSplit       = 1,
                    OpenAllBranches = false,
                    SplitCharacter  = ".",
                    Columns         = stringCols
                });
                OpcTagTree.RegisterEvent2("NMI_FIELD_EVENT", (pMSG, para) => {
                    var tUpdate = TheCommonUtils.DeserializeJSONStringToObject <TheWXFieldEvent>(pMSG.Message.PLS);
                    if (tUpdate != null)
                    {
                        var MyTag = MyTags.MyMirrorCache.GetEntryByID(tUpdate.cdeMID);
                        if (MyTag != null)
                        {
                            switch (tUpdate.Name)
                            {
                            case "SampleRate":
                                MyTag.SampleRate = TheCommonUtils.CInt(tUpdate.Value);
                                break;

                            case "DeadbandFilterValue":
                                MyTag.DeadbandFilterValue = TheCommonUtils.CDbl(tUpdate.Value);
                                break;

                            case "ChangeTrigger":
                                MyTag.ChangeTrigger = TheCommonUtils.CInt(tUpdate.Value);
                                break;
                            }
                        }
                    }
                });
            }
#endif

            // BROWSE Button
            TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.TileButton, pStartFld + 2, writeEnableFlag, 0xC0, "Browse", null, new nmiCtrlTileButton()
            {
                ParentFld = pStartFld, ClassName = "cdeGoodActionButton", TileWidth = 3, NoTE = true
            })
            .RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "BROWSE", (pThing, pObj) =>
            {
                TheProcessMessage pMsg = pObj as TheProcessMessage;
                if (pMsg == null || pMsg.Message == null)
                {
                    return;
                }
                if (ConnectionState != ConnectionStateEnum.Connected)
                {
                    TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Server not connected - please connect first"));
                }
                else
                {
                    TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Browsing..."));
                    LastMessage   = "Browsing Started at " + DateTimeOffset.Now.ToString();
                    BrowsedTagCnt = 0;
                    Browser(currentRoot, currentRoot.ToString(), true, false, null);


                    var dataModelJson = TheCommonUtils.SerializeObjectToJSONString(
                        MyTags.MyMirrorCache.TheValues
                        .Where(t => t.HostPropertyNameOverride?.Contains("].[") != true)     // Filter out any DataValue sub-properties (i.e. engineering units), as the tree view doesn't seem to handle nodes under leaf-nodes (or inner nodes with data) yet
                        .ToList());
                    OpcTagTree?.SetUXProperty(pMsg.Message.GetOriginator(), $"DataModel={dataModelJson}", true);
                    LastMessage += " - Browsing done at " + DateTimeOffset.Now.ToString();
                    TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", LastMessage));
                }
            });
            TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.Number, pStartFld + 3, 0, 0x0, "Browsed Tags:", "BrowsedTagCnt", new nmiCtrlNumber()
            {
                ParentFld = pStartFld, TileWidth = 3
            });


            ///Browsed TAGS Form
            {
                var tDataSource = "TheOPCTags";
                if (MyTags != null)
                {
                    tDataSource = MyTags.StoreMID.ToString();
                }
                var tOPCTagForm = new TheFormInfo(TheThing.GetSafeThingGuid(MyBaseThing, "TAGLIST_ID"), eEngineName.NMIService, "OPC-UA Server Tags", $"{tDataSource};:;1000")
                {
                    IsNotAutoLoading = true, AddButtonText = "Add new Tag", PropertyBag = new nmiCtrlTableView {
                        ShowFilterField = true
                    }
                };
                TheNMIEngine.AddFormToThingUX(MyBaseThing, tOPCTagForm, "CMyTable", "Tag List", 1, 3, 0xF0, null, null, new ThePropertyBag()
                {
                    "Visibility=false"
                });
                TheNMIEngine.AddFields(tOPCTagForm, new List <TheFieldInfo> {
                    // 1: Thing subscription
                    { new TheFieldInfo()
                      {
                          FldOrder = 3, DataItem = "IsSubscribedAsThing", Flags = 0, Type = eFieldType.SingleCheck, Header = "Has Tag Thing", FldWidth = 1
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 11, DataItem = "Parent", Flags = 0, Type = eFieldType.SingleEnded, Header = "Parent", FldWidth = 4
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 12, DataItem = "DisplayName", Flags = 0, Type = eFieldType.SingleEnded, Header = "Name", FldWidth = 3
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 13, DataItem = nameof(TheOPCTag.HostPropertyNameOverride), Flags = writeEnableFlag, Type = eFieldType.SingleEnded, Header = "Host Property Name", FldWidth = 3
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 14, DataItem = "HostThingMID", Flags = writeEnableFlag, Type = eFieldType.ThingPicker, Header = "Host Thing", FldWidth = 3
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 15, DataItem = nameof(TheOPCTag.ChangeTrigger), Flags = writeEnableFlag, Type = eFieldType.ComboBox, Header = "Change Trigger", FldWidth = 1, PropertyBag = new nmiCtrlComboBox {
                              Options = "Status:0;Value:1;Value & Timestamp:2"
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 16, DataItem = nameof(TheOPCTag.SampleRate), Flags = writeEnableFlag, Type = eFieldType.Number, Header = "Sample Rate (ms)", FldWidth = 1
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 17, DataItem = nameof(TheOPCTag.DeadbandFilterValue), Flags = writeEnableFlag, Type = eFieldType.Number, Header = "Deadband", FldWidth = 1
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 18, DataItem = "NodeIdName", Flags = 2, Type = eFieldType.SingleEnded, Header = "NodeId", FldWidth = 6
                      } },
                    // 30: Property subscription
                    { new TheFieldInfo()
                      {
                          FldOrder = 19, DataItem = nameof(TheOPCTag.HistoryStartTime), Flags = writeEnableFlag, Type = eFieldType.SingleEnded, Header = "History Start", FldWidth = 3
                      } },
                    //{  new TheFieldInfo() { FldOrder=13,DataItem="PropAttr.7.Value.Value",Flags=0,Type=eFieldType.SingleEnded,Header="Value",FldWidth=5 }},
                    //{  new TheFieldInfo() { FldOrder=14,DataItem="PropAttr.7.DataType",Flags=0,Type=eFieldType.SingleEnded,Header="Type",FldWidth=5 }},
                });
                TheNMIEngine.AddTableButtons(tOPCTagForm, false, 100, 0);

                // Button Subscribe as Thing
                TheNMIEngine.AddSmartControl(MyBaseThing, tOPCTagForm, eFieldType.TileButton, 1, writeEnableFlag, 0xC0, "Create Tag Thing", null, new nmiCtrlTileButton()
                {
                    ClassName = "cdeGoodActionButton", TileHeight = 1
                })
                .RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "SUBSCRIBE", (tThing, pMsg) => SubscribeAsThing(tThing, pMsg, MyTags));

                // Button: Subscribe property into host thing
                TheNMIEngine.AddSmartControl(MyBaseThing, tOPCTagForm, eFieldType.TileButton, 5, writeEnableFlag, 0xC0, "Monitor as Property", null, new nmiCtrlTileButton()
                {
                    ClassName = "cdeGoodActionButton", TileHeight = 1
                })
                .RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "SUBSCRIBEPROP", (tThing, pMsg) => SubscribeAsProperty(tThing, pMsg, MyTags));

                TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.TileButton, pStartFld + 4, writeEnableFlag, 0xF0, "Show Tag List", null, new nmiCtrlTileButton()
                {
                    OnClick = $"TTS:{tOPCTagForm.cdeMID}", ParentFld = pStartFld, ClassName = "cdeTransitButton", TileWidth = 3, NoTE = true
                });
            }

            TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.Number, pStartFld + 5, writeEnableFlag, 0xC0, "Default Sample Rate", nameof(DefSampleRate), new nmiCtrlNumber()
            {
                ParentFld = pStartFld, TileWidth = 3
            });


            TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.ThingPicker, pStartFld + 6, writeEnableFlag, 0xC0, "Thing for Property Subs", "TagHostThingForSubscribeAll", new nmiCtrlThingPicker()
            {
                NoTE = true, ParentFld = pStartFld, TileWidth = 4
            });

            // SUBSCRIBE all tags as Properties
            TheNMIEngine.AddSmartControl(MyBaseThing, tMyForm, eFieldType.TileButton, pStartFld + 7, writeEnableFlag, 0xC0, $"Subscribe {(UseTree ? "selected" : "all")} in properties", null, new nmiCtrlTileButton()
            {
                ParentFld = pStartFld, ClassName = "cdeGoodActionButton", TileWidth = 2, NoTE = true
            })
            .RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "ALLTAGS", (pThing, pObj) =>
            {
                TheProcessMessage pMsg = pObj as TheProcessMessage;
                if (pMsg == null || pMsg.Message == null)
                {
                    return;
                }
                if (ConnectionState != ConnectionStateEnum.Connected)
                {
                    TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Server not connected - please connect first"));
                }
                else
                {
                    var tHostThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(this.TagHostThingForSubscribeAll));
                    if (tHostThing == null)
                    {
                        TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Host Thing not specified or invalid"));
                        return;
                    }
                    TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Subscribing..."));

                    if (MyTags.MyMirrorCache.Count == 0 && !UseTree)
                    {
                        // Clear all previous subscription in either MyTags (tag as thing) oder HostThing (tag as property)
                        MyTags.MyMirrorCache.Reset();

                        // TODO Figure out how to clean up subscribed things in properties.
                        // - Can't just delete as a customer may have hand picked certain properties via browse paths etc.
                        // - Allow customer to use multiple host things?
                        //var tThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(TagHostThing));
                        //if (tThing != null)
                        //{
                        //    foreach (var prop in tThing.GetPropertiesMetaStartingWith("OPCUA:"))
                        //    {
                        //        tThing.RemoveProperty(prop.Name);
                        //    }
                        //}

                        LastMessage = "Subscribing started at " + DateTimeOffset.Now.ToString();

                        BrowsedTagCnt = 0;

                        Browser(currentRoot, currentRoot.ToString(), true, true, tHostThing, CreateIDFilter());

                        LastMessage += " - Subscribing done at " + DateTimeOffset.Now.ToString();
                        TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", LastMessage));
                    }
                    else
                    {
                        // Previously browsed: use those tags to do the subscriptions
                        LastMessage = "Subscribing started at " + DateTimeOffset.Now.ToString();

                        var subscription = GetOrCreateSubscription(0);
                        var pFilterIDs   = CreateIDFilter();
                        int count        = 0;
                        //var results = new List<Opc.Ua.Client.MonitoredItem>();//CM: removed as Nothing is done with "results"

                        foreach (var tag in MyTags.TheValues)
                        {
                            string tagNameForFilter;
                            if (tag.DisplayName == "EngineeringUnits" || tag.DisplayName == "EURange")
                            {
                                tagNameForFilter = tag.Parent;
                            }
                            else
                            {
                                tagNameForFilter = $"{tag.Parent}.{tag.DisplayName}";
                            }

                            if (pFilterIDs != null && pFilterIDs.Contains(tagNameForFilter))
                            {
                                tag.HostThingMID = TheCommonUtils.cdeGuidToString(tHostThing.cdeMID);
                                var childTags    = tag.GetChildTags();
                                if (childTags?.Any() == true)
                                {
                                    foreach (var childTag in childTags)
                                    {
                                        childTag.HostThingMID = tag.HostThingMID;
                                    }
                                }
                                if (!RegisterAndMonitorTagInHostThing(subscription, tag, out string error, false, true, false) || !string.IsNullOrEmpty(error))
                                {
                                    // error
                                }
Ejemplo n.º 17
0
        internal static TheScreenInfo GenerateLiveScreen(Guid pScreenId, TheClientInfo tClientInfo) // Guid pUserGuid, int lcid, int pFlag)
        {
            if (TheCDEngines.MyNMIService == null)
            {
                return(null);
            }

            TheScreenInfo tInfo = new TheScreenInfo
            {
                cdeMID          = pScreenId,
                MyDashboard     = null,
                MyStorageInfo   = new List <TheFormInfo>(),
                MyStorageMeta   = new cdeConcurrentDictionary <string, TheFormInfo>(),
                MyStorageMirror = new List <object>(),
                MyDashPanels    = new List <TheDashPanelInfo>()
            };

            TheThing tLiveForm = TheThingRegistry.GetThingByMID("*", pScreenId);

            if (tLiveForm == null || !TheUserManager.HasUserAccess(tClientInfo.UserID, tLiveForm.cdeA))
            {
                return(null); //V3.1: BUG 126 - could lead to racing condition. TODO: Revisit later
                //TheFormInfo tI = new TheFormInfo(tLiveForm) { FormTitle = (tLiveForm == null ? "Form not Found!" : "Access Denied!") };
                //tI.TargetElement = pScreenId.ToString();
                //tI.AssociatedClassName = pScreenId.ToString();
                //tInfo.MyStorageInfo.Add(tI);
                //tI.FormFields = new List<TheFieldInfo>();
                //TheFieldInfo tFldInfo = new TheFieldInfo(null, null, 10, 0, 0);
                //tFldInfo.Type = eFieldType.SmartLabel;
                //tFldInfo.Header = (tLiveForm == null ? "This Form was defined but has not Meta-Data associated with it." : "You do not have the required access permissions!");
                //tI.FormFields.Add(tFldInfo);
                //return tInfo;
            }
            string          tFormName = TheThing.GetSafePropertyString(tLiveForm, "FriendlyName");
            List <TheThing> tFields   = TheThingRegistry.GetThingsByFunc("*", s => s.cdeO == TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID && TheThing.GetSafePropertyString(s, "FormName") == tFormName && TheThing.GetSafePropertyBool(s, "IsLiveTag") && (s.UID == Guid.Empty || s.UID == tClientInfo.UserID));

            if (tFields != null && tFields.Any())
            {
                string tFormTitle = TheThing.GetSafePropertyString(tLiveForm, "FormTitle");
                if (string.IsNullOrEmpty(tFormTitle))
                {
                    tFormTitle = tFormName;
                }
                TheFormInfo tI = new TheFormInfo(tLiveForm)
                {
                    FormTitle           = tFormTitle,
                    TargetElement       = pScreenId.ToString(),
                    DefaultView         = eDefaultView.Form,
                    TileWidth           = TheCommonUtils.CInt(TheThing.GetSafePropertyNumber(tLiveForm, "TileWidth")),
                    TileHeight          = TheCommonUtils.CInt(TheThing.GetSafePropertyNumber(tLiveForm, "TileHeight")),
                    IsUsingAbsolute     = TheThing.GetSafePropertyBool(tLiveForm, "IsAbsolute"),
                    AssociatedClassName = pScreenId.ToString()
                };
                tInfo.MyStorageInfo.Add(tI);
                tI.FormFields = new List <TheFieldInfo>();
                int fldNo = 10;
                foreach (TheThing tTh in tFields)
                {
                    int tfldNo = TheCommonUtils.CInt(TheThing.GetSafePropertyNumber(tTh, "FldOrder"));
                    if (tfldNo == 0)
                    {
                        tfldNo = fldNo;
                    }
                    int          tFlags   = TheCommonUtils.CInt(TheThing.GetSafePropertyNumber(tTh, "Flags"));
                    cdeP         ValProp  = tTh.GetProperty("Value");
                    bool         IsNewFld = true;
                    TheFieldInfo tFldInfo = TheNMIEngine.GetFieldById(TheThing.GetSafePropertyGuid(tTh, "FldID"));
                    if (tFldInfo == null)
                    {
                        tFldInfo = new TheFieldInfo(tTh, "Value", tfldNo, tFlags & 0xFFBF, tTh.GetBaseThing().cdeA);
                    }
                    else
                    {
                        tFldInfo.FldOrder = tfldNo;
                        tFldInfo.Flags    = tFlags;
                        IsNewFld          = false;
                    }
                    if (tFldInfo.PropertyBag == null)
                    {
                        tFldInfo.PropertyBag = new ThePropertyBag();
                    }
                    ThePropertyBag.PropBagUpdateValue(tFldInfo.PropertyBag, "IsOnTheFly", "=", "True");
                    ThePropertyBag.PropBagUpdateValue(tFldInfo.PropertyBag, "UXID", "=", $"{tTh.cdeMID}");
                    tFldInfo.Header = tTh.FriendlyName;
                    RegisterNMISubscription(tClientInfo, "Value", tFldInfo);

                    string tControlType = TheThing.GetSafePropertyString(tTh, "ControlType");
                    if (TheCommonUtils.CInt(tControlType) == 0 && !TheCommonUtils.IsNullOrWhiteSpace(tControlType))
                    {
                        tFldInfo.Type = eFieldType.UserControl;
                        RegisterFieldEvents(tTh, ValProp, IsNewFld, tFldInfo, tControlType);
                    }
                    else
                    {
                        tFldInfo.Type = (eFieldType)TheCommonUtils.CInt(tControlType);
                    }
                    tFldInfo.DefaultValue = ValProp?.ToString();
                    tFldInfo.TileWidth    = TheCommonUtils.CInt(TheThing.GetSafePropertyNumber(tTh, "TileWidth"));
                    tFldInfo.TileHeight   = TheCommonUtils.CInt(TheThing.GetSafePropertyNumber(tTh, "TileHeight"));

                    foreach (cdeP prop in tTh.GetNMIProperties())
                    {
                        ThePropertyBag.PropBagUpdateValue(tFldInfo.PropertyBag, prop.Name, "=", prop.ToString());
                    }
                    if (tFldInfo.Type == eFieldType.TileButton)
                    {
                        tTh.DeclareNMIProperty("IsDown", ePropertyTypes.TBoolean);
                        ThePropertyBag.PropBagUpdateValue(tFldInfo.PropertyBag, "EnableTap", "=", "True");
                        tFldInfo.RegisterUXEvent(tTh, eUXEvents.OnPropertyChanged, "IsDown", (pThing, pObj) =>
                        {
                            if (!(pObj is TheProcessMessage pMsg) || pMsg.Message == null)
                            {
                                return;
                            }
                            TheThing.SetSafePropertyBool(pThing, "IsDown", TheCommonUtils.CBool(pMsg.Message.PLS));
                        });
Ejemplo n.º 18
0
        private IEnumerable <TheThing> GetMatchingThings(Subscription subscription)
        {
            var things = new List <TheThing>();

            if (_thingReference.ThingMID.HasValue && _thingReference.ThingMID != Guid.Empty)
            {
                var thing = TheThingRegistry.GetThingByMID(_thingReference.ThingMID.Value, true);
                if (thing != null)
                {
                    things.Add(thing);
                }
                return(things);
            }

            IBaseEngine     baseEngine     = null;
            List <TheThing> matchingThings = null;

            if (!string.IsNullOrEmpty(_thingReference.EngineName))
            {
                baseEngine = TheThingRegistry.GetBaseEngine(_thingReference.EngineName);
                if (baseEngine == null)
                {
                    TheCDEngines.RegisterNewMiniRelay(_thingReference.EngineName);
                    baseEngine = TheThingRegistry.GetBaseEngine(_thingReference.EngineName);
                }
                if (baseEngine != null)
                {
                    subscription?.AddSubscription(baseEngine, this);
                    matchingThings = TheThingRegistry.GetThingsOfEngine(_thingReference.EngineName, true, true);
                }
            }
            else
            {
                object deviceTypeObj = null;
                if (_thingReference.PropertiesToMatch?.TryGetValue("DeviceType", out deviceTypeObj) == true)
                {
                    var deviceType = TheCommonUtils.CStr(deviceTypeObj);
                    if (!string.IsNullOrEmpty(deviceType))
                    {
                        matchingThings = TheThingRegistry.GetThingsByFunc("*", t => t.DeviceType.Equals(deviceType, StringComparison.Ordinal), true);
                    }
                }
            }

            if (matchingThings != null)
            {
                foreach (var thing in matchingThings)
                {
                    if (Matches(thing))
                    {
                        if (baseEngine == null)
                        {
                            subscription?.AddSubscription(thing.GetBaseEngine(), this);
                        }
                        things.Add(thing);
                    }
                }
            }

            return(things);
        }
Ejemplo n.º 19
0
        public override bool CreateUX()
        {
            if (TheBaseAssets.MyServiceHostInfo.IsCloudService)
            {
                mIsUXInitialized = true;
                return(true);
            }
            if (!TheNMIEngine.IsInitialized())
            {
                return(false);
            }
            if (mIsUXInitCalled)
            {
                return(false);
            }
            mIsUXInitCalled = true;

            MyDash = TheNMIEngine.AddDashboard(MyBaseThing, new TheDashboardInfo(MyBaseEngine, "Rules Engine")
            {
                FldOrder = 5000, cdeA = 0xC0, PropertyBag = new ThePropertyBag {
                    "Caption=Rules Engine", "Thumbnail=FA5:f546", "Category=Services"
                }
            });

            TheFormInfo tFormGuid = new TheFormInfo(new Guid("{6C34871C-D49B-4D7A-84E0-35E25B1F18B0}") /*TheThing.GetSafeThingGuid(MyBaseThing, "RULESREG")*/, eEngineName.NMIService, "The Rules Registry", $"TheThing;:;0;:;True;:;DeviceType={eKnownDeviceTypes.TheThingRule};EngineName={MyBaseEngine.GetEngineName()}")
            {
                GetFromFirstNodeOnly = true, IsNotAutoLoading = true, AddButtonText = "Add Rule", AddTemplateType = "44444444-6AD1-45AE-BE61-96AF02329613"
            };

            //TheNMIEngine.AddForm(tFormGuid);
            TheNMIEngine.AddFormToThingUX(MyDash, MyBaseThing, tFormGuid, "CMyTable", "Rules Registry", 5, 9, 128, TheNMIEngine.GetNodeForCategory(), null, new ThePropertyBag {
                "Thumbnail=FA5:f07b"
            });
            TheNMIEngine.AddFields(tFormGuid, new List <TheFieldInfo>
            {
                { new TheFieldInfo()
                  {
                      FldOrder = 10, DataItem = "MyPropertyBag.IsRuleActive.Value", Flags = 2, Type = eFieldType.SingleCheck, Header = "Active", FldWidth = 1, TileLeft = 2, TileTop = 6, TileWidth = 4, TileHeight = 1
                  } },
                { new TheFieldInfo()
                  {
                      FldOrder = 20, DataItem = "MyPropertyBag.IsRuleLogged.Value", Flags = 2, Type = eFieldType.SingleCheck, Header = "Logged", FldWidth = 1, TileLeft = 6, TileTop = 6, TileWidth = 4, TileHeight = 1
                  } },

                { new TheFieldInfo()
                  {
                      FldOrder = 30, DataItem = "MyPropertyBag.FriendlyName.Value", Flags = 2, Type = eFieldType.SingleEnded, Header = "Rule Name", FldWidth = 3, TileLeft = 0, TileTop = 1, TileWidth = 11, TileHeight = 1, PropertyBag = new nmiCtrlSingleEnded()
                      {
                          HelpText = "Give this rule a friendly name"
                      }
                  } },
                { new TheFieldInfo()
                  {
                      FldOrder = 137, DataItem = "cdeO", Flags = 16, Type = eFieldType.SingleEnded, Header = "Status", FldWidth = 3, Format = "<span style='font-size:xx-small'>Running:%MyPropertyBag.IsRuleRunning.Value%<br>TrigerAlive:%MyPropertyBag.IsTriggerObjectAlive.Value%</span>"
                  } },
            });
            TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.StatusLight, 35, 0x40, 0x0, "Status", "StatusLevel", new TheNMIBaseControl()
            {
                FldWidth = 1
            });


            TheNMIEngine.AddTableButtons(tFormGuid, true, 1000, 0xA2);

            TheFieldInfo tTriggerBut = TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.TileButton, 5, 2, 0xC0, "Trigger Now", null, new nmiCtrlTileButton {
                ClassName = "cdeGoodActionButton", TileLeft = 1, TileTop = 6, TileWidth = 1, TileHeight = 1, NoTE = true
            });

            tTriggerBut.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "TriggerNow", (psender, pPara) =>
            {
                TheProcessMessage pMSG = pPara as TheProcessMessage;
                if (pMSG == null || pMSG.Message == null)
                {
                    return;
                }

                string[] cmd = pMSG.Message.PLS.Split(':');
                if (cmd.Length > 2)
                {
                    TheThing tThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(cmd[2]));
                    if (tThing == null)
                    {
                        return;
                    }
                    TheRule tRule = tThing.GetObject() as TheRule;
                    if (tRule != null)
                    {
                        tRule.FireAction(true);
                        TheCommCore.PublishToOriginator(pMSG.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", string.Format("Rule {0} triggered", tRule.FriendlyName)));
                    }
                }
            });

            TheNMIEngine.AddAboutButton4(MyBaseThing, MyDash, null, true, false, "REFRESH_DASH");

            AddRulesWizard();

            mIsUXInitialized = true;
            return(true);
        }
Ejemplo n.º 20
0
        void sinkLMChange(cdeP prop)
        {
            var t = TheThingRegistry.GetThingByMID(prop.cdeO);

            TheBaseAssets.MySYSLOG.WriteToLog(23000, new TSM(MyBaseEngine.GetEngineName(), $"Plugin {t?.EngineName} Message: {prop}", eMsgLevel.l4_Message));
        }
Ejemplo n.º 21
0
        void sinkSTChange(cdeP prop)
        {
            var t = TheThingRegistry.GetThingByMID(prop.cdeO);

            TheBaseAssets.MySYSLOG.WriteToLog(23010 + TheCommonUtils.CInt(prop), new TSM(MyBaseEngine.GetEngineName(), $"Plugin {t?.EngineName} Status Level Changed to {prop}", eMsgLevel.l4_Message));
        }
Ejemplo n.º 22
0
        public override void HandleMessage(ICDEThing sender, object pIncoming)
        {
            TheProcessMessage pMsg = pIncoming as TheProcessMessage;

            if (pMsg == null || 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_REGISTERRULE":
                TheRule tRule = TheCommonUtils.DeserializeJSONStringToObject <TheRule>(pMsg.Message.PLS);
                if (tRule != null)
                {
                    RegisterRule(tRule);
                }
                break;

            case "CLAIM_RULES":
                List <TheThing> tList = TheThingRegistry.GetThingsByFunc(eEngineName.ThingService, s => TheThing.GetSafePropertyString(s, "DeviceType").Equals(eKnownDeviceTypes.TheThingRule));
                if (tList != null && tList.Count > 0)
                {
                    foreach (TheThing tThing in tList)
                    {
                        tThing.Parent     = MyBaseThing.ID;
                        tThing.EngineName = MyBaseEngine.GetEngineName();
                    }
                }
                ActivateRules();
                MyDash.Reload(null, false);
                TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", $"{tList.Count} New Rules claimed"));
                break;

            case "REFRESH_DASH":
                ActivateRules();
                MyDash.Reload(null, false);
                break;

            case "FIRE_RULE":
                var t = TheThingRegistry.GetThingByMID(MyBaseEngine.GetEngineName(), TheCommonUtils.CGuid(pMsg?.Message?.PLS));
                if (t != null)
                {
                    TheRule tR = t.GetObject() as TheRule;
                    if (tR != null)
                    {
                        tR.RuleTrigger(tR.TriggerOldValue, true);
                    }
                }
                break;
            }
        }
Ejemplo n.º 23
0
        public bool ActivateRules()
        {
            if (TheCDEngines.MyThingEngine == null || TheBaseAssets.MyServiceHostInfo.IsCloudService || TheCommonUtils.cdeIsLocked(LockRules) || !mIsInitialized)
            {
                return(false);
            }

            lock (LockRules)
            {
                if (mIsInitialized)
                {
                    InitRules();
                }
                //List<TheRule> tList = MyRulesStore.MyMirrorCache.GetEntriesByFunc(s => s.IsRuleActive && !s.IsRuleRunning && s.IsRuleWaiting);
                List <TheThing> tList = TheThingRegistry.GetThingsByFunc("*", s =>
                                                                         TheThing.GetSafePropertyString(s, "Parent").Equals(MyBaseThing.ID) && //TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID) &&
                                                                         TheThing.GetSafePropertyBool(s, "IsRuleActive") &&
                                                                         !TheThing.GetSafePropertyBool(s, "IsRuleRunning") &&
                                                                         TheThing.GetSafePropertyBool(s, "IsRuleWaiting"));
                if (tList != null && tList.Count > 0)
                {
                    foreach (TheThing tThing in tList)
                    {
                        TheRule tRule = tThing.GetObject() as TheRule;
                        if (tRule == null)
                        {
                            continue;
                        }
                        if (string.IsNullOrEmpty(tRule.TriggerProperty))
                        {
                            tRule.IsIllegal     = true;
                            tRule.IsRuleWaiting = false;
                            continue;
                        }
                        if (tRule.TriggerStartTime > DateTimeOffset.Now)
                        {
                            continue;
                        }
                        if (tRule.TriggerEndTime < DateTimeOffset.Now)
                        {
                            RemoveTrigger(tRule, false);
                            continue;
                        }
                        switch (tRule.TriggerObjectType)
                        {
                        case "CDE_ENGINE":
                            TheThing tBase = TheThingRegistry.GetBaseEngineAsThing(tRule.TriggerObject);
                            if (tBase != null)
                            {
                                tBase.RegisterEvent(eEngineEvents.IncomingMessage, sinkRuleIncoming);
                                tRule.IsRuleWaiting        = false;
                                tRule.IsRuleRunning        = true;
                                tRule.IsTriggerObjectAlive = true;
                                TheSystemMessageLog.WriteLog(4445, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(eKnownDeviceTypes.TheThingRule, $"Rule {tRule.FriendlyName} started with TriggerType: {tRule.TriggerObjectType}"), false);
                            }
                            break;

                        case "CDE_EVENTFIRED":
                            TheThing tBaseE = TheThingRegistry.GetBaseEngineAsThing(tRule.TriggerObject);
                            if (tBaseE != null)
                            {
                                tBaseE.RegisterEvent(tRule.TriggerProperty, sinkRuleThingEvent);
                                tRule.IsRuleWaiting        = false;
                                tRule.IsRuleRunning        = true;
                                tRule.IsTriggerObjectAlive = true;
                                TheSystemMessageLog.WriteLog(4445, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(eKnownDeviceTypes.TheThingRule, $"Rule {tRule.FriendlyName} started with TriggerType: {tRule.TriggerObjectType}"), false);
                            }
                            break;

                        default:
                            TheThing tTriggerThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(tRule.TriggerObject));
                            if (tTriggerThing != null)
                            {
                                //if (tTriggerThing.GetObject() == null) continue;  //TODO: Verify if this can stay removed
                                //if (tTriggerThing.GetProperty("FriendlyName").Value.ToString().Contains("Motion")) continue;
                                cdeP tProp = tTriggerThing.GetProperty(tRule.TriggerProperty, true);
                                if (tProp != null)
                                {
                                    tProp.UnregisterEvent(eThingEvents.PropertyChanged, sinkRuleAction);
                                    tProp.RegisterEvent(eThingEvents.PropertyChanged, sinkRuleAction);
                                }
                                tRule.IsRuleWaiting        = false;
                                tRule.IsRuleRunning        = true;
                                tRule.IsTriggerObjectAlive = true;
                                tRule.RuleTrigger(tProp.ToString(), true);
                                TheSystemMessageLog.WriteLog(4445, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(eKnownDeviceTypes.TheThingRule, $"Rule {tRule.FriendlyName} started with TriggerType: {tRule.TriggerObjectType}"), false);
                            }
                            break;
                        }
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 24
0
        public void CheckForDeviceMatch(TheUPnPDeviceInfo tHistory, Uri pLocationUrl)
        {
            if (mUPnpUIDs.Count <= 0)
            {
                return;
            }
            if (pLocationUrl == null || TheCommonUtils.IsGuid(pLocationUrl.Host))
            {
                pLocationUrl = null;
            }
            bool sendOnce   = false;
            bool hasAllFlag = false;

            foreach (string tUID in mUPnpUIDs.Keys)
            {
                if (tUID.Equals("*") && mUPnpUIDs[tUID] == null)
                {
                    if (pLocationUrl == null && mUPnpUIDs[tUID] != null)
                    {
                        mUPnpUIDs[tUID](tHistory);
                    }
                    hasAllFlag = true;
                }
                else
                {
                    string[] stParts = TheCommonUtils.cdeSplit(tUID, ";:;", true, true);
                    if (stParts.Length == 1)
                    {
                        if (!string.IsNullOrEmpty(tHistory?.ST) && (tUID == "*" || tHistory?.ST?.ToUpper().Contains(tUID.ToUpper()) == true))
                        {
                            if ((pLocationUrl == null || !string.IsNullOrEmpty(tHistory.USN)) && mUPnpUIDs[tUID] != null)
                            {
                                mUPnpUIDs[tUID](tHistory);
                            }
                            else
                            {
                                var t = TheThingRegistry.GetThingByMID(tHistory.ScannerID);
                                (t.GetObject() as ICDEDiscoService)?.GetDeviceDetails(tHistory, mUPnpUIDs[tUID]);
                                //if (pLocationUrl!=null)
                                //    TheREST.GetRESTAsync(pLocationUrl, 0, sinkUPnPCustomDeviceFound, mUPnpUIDs[tUID]);
                            }
                            sendOnce = true;
                        }
                    }
                    else
                    {
                        if (CompareUPnPField(tHistory, stParts[0], stParts[1]))
                        {
                            if ((pLocationUrl == null || !string.IsNullOrEmpty(tHistory.USN)) && mUPnpUIDs[tUID] != null)
                            {
                                mUPnpUIDs[tUID](tHistory);
                            }
                            else
                            {
                                var t = TheThingRegistry.GetThingByMID(tHistory.ScannerID);
                                (t.GetObject() as ICDEDiscoService)?.GetDeviceDetails(tHistory, mUPnpUIDs[tUID]);
                                //if (pLocationUrl != null)
                                //    TheREST.GetRESTAsync(pLocationUrl, 0, sinkUPnPCustomDeviceFound, mUPnpUIDs[tUID]);
                            }
                            sendOnce = true;
                        }
                    }
                }
            }
            if (!sendOnce && hasAllFlag && pLocationUrl != null)
            {
                //TheSystemMessageLog.ToCo(string.Format("UPnP Get Meta From: {0}", pLocationUrl));
                var t = TheThingRegistry.GetThingByMID(tHistory.ScannerID);
                (t?.GetObject() as ICDEDiscoService)?.GetDeviceDetails(tHistory, null);
                //TheREST.GetRESTAsync(pLocationUrl, 0, sinkUPnPCustomDeviceFound, null);
            }
        }
Ejemplo n.º 25
0
        internal void FireAction(bool FireNow)
        {
            if (TheCDEngines.MyThingEngine == null || !TheBaseAssets.MasterSwitch)
            {
                return;
            }
            if (!FireNow)
            {
                int tDelay = ActionDelay;
                if (tDelay > 0)
                {
                    Timer tTimer = GetDelayTimer();
                    if (tTimer != null)
                    {
                        tTimer.Dispose();
                        tTimer = null;
                    }
                    tTimer = new Timer(sinkFireOnTimer, this, tDelay * 1000, Timeout.Infinite);
                    SetDelayTimer(tTimer);
                    return;
                }
            }
            switch (ActionObjectType)
            {
            case "CDE_PUBLISHCENTRAL":
                SendRuleTSM(false);
                break;

            case "CDE_PUBLISH2SERVICE":
                SendRuleTSM(true);
                break;

            default:     //case "CDE_THING":
                TheThing tActionThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(ActionObject));
                if (tActionThing != null)
                {
                    string tActionValue = ActionValue;
                    if (!string.IsNullOrEmpty(tActionValue))
                    {
                        ICDEThing triggerThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(TriggerObject)) as ICDEThing;
                        tActionValue = TheCommonUtils.GenerateFinalStr(tActionValue, triggerThing);
                        tActionValue = tActionValue.Replace("%OldValue%", TriggerOldValue);
                        tActionValue = TheCommonUtils.GenerateFinalStr(tActionValue, MyBaseThing);
                    }

                    ICDEThing tObject = tActionThing.GetObject() as ICDEThing;
                    if (tObject != null)
                    {
                        tObject.SetProperty(ActionProperty, tActionValue);
                    }
                    else
                    {
                        tActionThing.SetProperty(ActionProperty, tActionValue);
                    }
                    if (IsRuleLogged)
                    {
                        LogEvent(tActionValue);
                    }
                    if (TheThing.GetSafePropertyBool(MyBaseThing, "IsEVTLogged"))
                    {
                        TheLoggerFactory.LogEvent(eLoggerCategory.RuleEvent, TheCommonUtils.GenerateFinalStr(MyBaseThing.FriendlyName, MyBaseThing), eMsgLevel.l4_Message, TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false), TriggerObject, tActionValue);
                    }
                }
                break;
            }
            TheThing.SetSafePropertyDate(MyBaseThing, "LastAction", DateTimeOffset.Now);
            FireEvent("RuleFired", this, this, true);
        }
Ejemplo n.º 26
0
        public virtual bool CreateUX()
        {
            if (mIsUXInitCalled)
            {
                return(false);
            }
            mIsUXInitCalled = true;

            var tFlds = TheNMIEngine.AddStandardForm(MyBaseThing, null, 12, null, "Value");

            MyStatusForm            = tFlds["Form"] as TheFormInfo;
            SummaryForm             = tFlds["DashIcon"] as TheDashPanelInfo;
            SummaryForm.PropertyBag = new ThePropertyBag()
            {
                string.Format("Format={0}<br>{{0}} Properties", MyBaseThing.FriendlyName)
            };
            SummaryForm.RegisterPropertyChanged(sinkStatChanged);

            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 21, 0, 0x0, "###CDMyVThings.TheVThings#CountOfProperties598472#Count of Properties###", "Value", new nmiCtrlNumber()
            {
                ParentFld = 1
            });
            TheFieldInfo mSendbutton = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.TileButton, 22, 2, 0, "###CDMyVThings.TheVThings#Reload598472#Reload###", false, "", null, new nmiCtrlTileButton()
            {
                ParentFld = 1, NoTE = true, ClassName = "cdeGoodActionButton"
            });

            mSendbutton.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "", (pThing, pPara) =>
            {
                TheProcessMessage pMsg = pPara as TheProcessMessage;
                if (pMsg?.Message == null)
                {
                    return;
                }
                sinkStatChanged(null, null);
                MyStatusForm.Reload(pMsg, true);
            });
            PropTable = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Table, 40, 0xA0, 0x80, "All Properties", "mypropertybag;0", new TheNMIBaseControl()
            {
                NoTE = true, ParentFld = 1, TileWidth = 12, TileHeight = 16
            });
            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleEnded, 30, 0x0A, 0, "New Property Name", "ScratchName", new nmiCtrlSingleEnded()
            {
                ParentFld = 1
            });
            TheFieldInfo tBut = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.TileButton, 35, 0x0A, 0, "Add Property", false, null, null, new nmiCtrlTileButton()
            {
                ParentFld = 1, NoTE = true
            });

            tBut.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "AddProp", (pThing, pObj) =>
            {
                TheProcessMessage pMsg = pObj as TheProcessMessage;
                if (pMsg?.Message == null)
                {
                    return;
                }
                string[] parts = pMsg.Message.PLS.Split(':');
                TheThing tOrg  = TheThingRegistry.GetThingByMID(MyBaseEngine.GetEngineName(), TheCommonUtils.CGuid(parts[2]));
                if (tOrg == null)
                {
                    return;
                }

                string tNewPropName = TheThing.GetSafePropertyString(tOrg, "ScratchName");
                if (string.IsNullOrEmpty(tNewPropName))
                {
                    TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Please specify a new property name"));
                }
                else
                {
                    if (tOrg.GetProperty(tNewPropName) != null)
                    {
                        TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Property already exists"));
                    }
                    else
                    {
                        tOrg.DeclareNMIProperty(tNewPropName, ePropertyTypes.TString);
                        TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Property Added"));
                        MyStatusForm.Reload(pMsg, false);
                    }
                }
                tOrg.RemoveProperty("ScratchName");
            });
            mIsUXInitialized = true;
            return(true);
        }
Ejemplo n.º 27
0
        public override bool CreateUX()
        {
            if (!mIsUXInitStarted)
            {
                mIsUXInitStarted = true;

                // Creates a "portal" for each rule. This is how we create
                // a tile for each rule on the rules engine's main page.
                mThisFormFields = TheNMIEngine.AddStandardForm(MyBaseThing, MyBaseThing.FriendlyName, 18);

                // Update our status.
                TheFormInfo tFormGuid = mThisFormFields["Form"] as TheFormInfo;
                tFormGuid.RegisterEvent2(eUXEvents.OnShow, (pMSG, para) => {
                    UpdateStatus();
                });

                // Create "Rule Status" settings group
                // Field Order = 10
                // Parent = 1 (main form)
                // Get standard Status Block.
                var tstFlds = TheNMIEngine.AddStatusBlock(MyBaseThing, tFormGuid, idGroupStatus);
                tstFlds["Group"].SetParent(idForm);
                tstFlds["Group"].Header = "Rule Status";
                // tstFlds["Group"].PropertyBag = new ThePropertyBag { "DoClose=true" };
                tstFlds["FriendlyName"].Header = "Rule Name";

                // When the Friendly Name changes, propogate to the other UI elements that use it.
                tstFlds["FriendlyName"].RegisterUXEvent(MyBaseThing, eUXEvents.OniValueChanged, null, (psender, pPara) =>
                {
                    (mThisFormFields["Header"] as TheFieldInfo).SetUXProperty(Guid.Empty, $"Title={MyBaseThing.FriendlyName}");
                    (mThisFormFields["DashIcon"] as TheDashPanelInfo).SetUXProperty(Guid.Empty, $"Caption={MyBaseThing.FriendlyName}");
                });

                // Add fields to Status Block that are specific to this plugin.
                TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.SingleCheck, 60, 2, 0, "Activate Rule", "IsRuleActive", new nmiCtrlSingleCheck {
                    ParentFld = idGroupStatus, TileWidth = 3
                });
                TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.SingleCheck, 70, 2, 0, "Log Action", "IsRuleLogged", new nmiCtrlSingleCheck {
                    ParentFld = idGroupStatus, TileWidth = 3
                });
                TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.SingleCheck, 75, 2, 0, "Log Action to Eventlog", "IsEVTLogged", new nmiCtrlSingleCheck {
                    ParentFld = idGroupStatus, TileWidth = 3
                });
                TheFieldInfo tTriggerBut = TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.TileButton, 65, 2, 0xC0, "Trigger Now", null, new nmiCtrlTileButton {
                    ParentFld = idGroupStatus, ClassName = "cdeGoodActionButton", NoTE = true, TileWidth = 3
                });
                tTriggerBut.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "TriggerNow", (psender, pPara) =>
                {
                    TheProcessMessage pMSG = pPara as TheProcessMessage;
                    if (pMSG == null || pMSG.Message == null)
                    {
                        return;
                    }

                    string[] cmd = pMSG.Message.PLS.Split(':');
                    if (cmd.Length > 2)
                    {
                        TheThing tThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(cmd[2]));
                        if (tThing == null)
                        {
                            return;
                        }
                        TheRule tRule = tThing.GetObject() as TheRule;
                        if (tRule != null)
                        {
                            //FireAction(tRule, true);
                            tRule.FireAction(true);
                            MyBaseEngine.ProcessMessage(new TheProcessMessage(new TSM(MyBaseEngine.GetEngineName(), "FIRE_RULE", MyBaseThing.cdeMID.ToString())));
                            TheCommCore.PublishToOriginator(pMSG.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", string.Format("Rule {0} triggered", tRule.GetBaseThing().FriendlyName)));
                        }
                    }
                });

                // Create "Trigger Object" settings group (Field Order = idGroupTriggerObject (100), Parent = 1)
                TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.CollapsibleGroup, idGroupTriggerObject, 2, 0, "Trigger Object", null, new nmiCtrlCollapsibleGroup {
                    ParentFld = idForm, TileWidth = 6, IsSmall = true
                });

                // Create "Action Settings" settings group (Field Order = idGroupActionSettings (500), Parent = 1)
                TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.CollapsibleGroup, idGroupActionSettings, 2, 0, "Action Settings", null, new nmiCtrlCollapsibleGroup {
                    ParentFld = idForm, TileWidth = 6, IsSmall = true
                });

                // Create "Thing Property Action" settings group (Field Order = idGroupThingPropAction (550), Parent = 500)
                TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.CollapsibleGroup, idGroupThingPropAction, 2, 0, "Thing/Property Action", null, new nmiCtrlCollapsibleGroup {
                    ParentFld = idGroupActionSettings, TileWidth = 6, IsSmall = true
                });

                // Create "TSM Action" settings group (Field Order = idGroupTSMAction (600), Parent = 500)
                TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.CollapsibleGroup, idGroupTSMAction, 2, 0, "TSM Action", null, new nmiCtrlCollapsibleGroup {
                    ParentFld = idGroupActionSettings, TileWidth = 6, IsSmall = true, DoClose = true
                });

                // Create all other (non-group header) fields.
                TheNMIEngine.AddFields(tFormGuid, new List <TheFieldInfo>
                {
                    /* Trigger Object Group */
                    { new TheFieldInfo()
                      {
                          FldOrder = 140, DataItem = "MyPropertyBag.TriggerObject.Value", Flags = 2, Type = eFieldType.ThingPicker, Header = "Trigger Object", PropertyBag = new nmiCtrlThingPicker()
                          {
                              ParentFld = idGroupTriggerObject, HelpText = "If this objects...", IncludeEngines = true
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 150, DataItem = "MyPropertyBag.TriggerProperty.Value", Flags = 2, Type = eFieldType.PropertyPicker, Header = "Trigger Property", DefaultValue = "Value", PropertyBag = new nmiCtrlPropertyPicker()
                          {
                              ParentFld = idGroupTriggerObject, HelpText = "...property is...", ThingFld = 140
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 160, DataItem = "MyPropertyBag.TriggerCondition.Value", Flags = 2, Type = eFieldType.ComboBox, Header = "Trigger Condition", DefaultValue = "2", PropertyBag = new nmiCtrlComboBox()
                          {
                              ParentFld = idGroupTriggerObject, HelpText = "... then this value, this rule will fire...", DefaultValue = "2", Options = "Fire:0;State:1;Equals:2;Larger:3;Smaller:4;Not:5;Contains:6;Set:7;StartsWith:8;EndsWith:9;Flank:10"
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 170, DataItem = "MyPropertyBag.TriggerValue.Value", Flags = 2, Type = eFieldType.SingleEnded, Header = "Trigger Value", PropertyBag = new ThePropertyBag()
                          {
                              "ParentFld=100", "HelpText=...this objects..."
                          }
                      } },

                    /* Action Settings Group */
                    { new TheFieldInfo()
                      {
                          FldOrder = 505, DataItem = "MyPropertyBag.ActionObjectType.Value", Flags = 2, Type = eFieldType.ComboBox, Header = "Action Object Type", PropertyBag = new nmiCtrlComboBox()
                          {
                              ParentFld = idGroupActionSettings, Options = "Set Property on a Thing:CDE_THING;Publish Central:CDE_PUBLISHCENTRAL;Publish to Service:CDE_PUBLISH2SERVICE", DefaultValue = "CDE_THING"
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 506, DataItem = "MyPropertyBag.ActionDelay.Value", Flags = 2, Type = eFieldType.Number, Header = "Delay", DefaultValue = "0", PropertyBag = new ThePropertyBag()
                          {
                              "ParentFld=500", "HelpText=...after a delay of these seconds..."
                          }
                      } },

                    /* Thing / Property Action Sub-Group */
                    { new TheFieldInfo()
                      {
                          FldOrder = 560, DataItem = "MyPropertyBag.ActionObject.Value", Flags = 2, Type = eFieldType.ThingPicker, Header = "Action Object", PropertyBag = new nmiCtrlThingPicker()
                          {
                              ParentFld = 550, HelpText = "...this objects...", IncludeEngines = true
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 562, DataItem = "MyPropertyBag.ActionProperty.Value", Flags = 2, Type = eFieldType.PropertyPicker, Header = "Action Property", DefaultValue = "Value", PropertyBag = new nmiCtrlPropertyPicker()
                          {
                              ParentFld = idGroupThingPropAction, HelpText = "...property will change to...", ThingFld = 560
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 563, DataItem = "MyPropertyBag.ActionValue.Value", Flags = 2, Type = eFieldType.SingleEnded, Header = "Action Value", PropertyBag = new nmiCtrlSingleEnded {
                              ParentFld = idGroupThingPropAction, HelpText = "...this value", Style = "text-overflow:ellipsis;overflow:hidden; max-width:400px"
                          }
                      } },

                    /* TSM Action Sub-Group */
                    { new TheFieldInfo()
                      {
                          FldOrder = 630, DataItem = "MyPropertyBag.TSMEngine.Value", Flags = 2, Type = eFieldType.ThingPicker, Header = "TSM Engine", PropertyBag = new nmiCtrlThingPicker()
                          {
                              ParentFld = 600, ValueProperty = "EngineName", IncludeEngines = true, Filter = "DeviceType=IBaseEngine"
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 631, DataItem = "MyPropertyBag.TSMText.Value", Flags = 2, Type = eFieldType.SingleEnded, Header = "TSM Text", PropertyBag = new ThePropertyBag()
                          {
                              "ParentFld=600", "HelpText=Command of the TSM"
                          }
                      } },
                    { new TheFieldInfo()
                      {
                          FldOrder = 632, DataItem = "MyPropertyBag.TSMPayload.Value", Flags = 2, Type = eFieldType.TextArea, Header = "TSM Payload", PropertyBag = new nmiCtrlTextArea()
                          {
                              ParentFld = idGroupTSMAction, TileHeight = 2, HelpText = "Body of the TSM"
                          }
                      } },
                });

                mIsUXInitCompleted = true;
            }
            return(true);
        }
Ejemplo n.º 28
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;
            }
        }
Ejemplo n.º 29
0
        public override bool DoCreateUX()
        {
            MyBaseThing.RegisterProperty("ClickState");
            var tFlds = TheNMIEngine.AddStandardForm(MyBaseThing, null, 24, null, "Value");

            MyStatusForm = tFlds["Form"] as TheFormInfo;
            //MyStatusForm.PropertyBag = new ThePropertyBag { "TileWidth=6" };
            MyStatusForm.RegisterEvent2(eUXEvents.OnShow, (pmse, sen) => { UpdateUx(); });
            SummaryForm             = tFlds["DashIcon"] as TheDashPanelInfo;
            SummaryForm.PropertyBag = new nmiDashboardTile()
            {
                Format = $"{MyBaseThing.FriendlyName}<br>{{0}}", Caption = MyBaseThing.FriendlyName
            };

            CountBar             = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleEnded, 5, 2, MyBaseThing.cdeA, "CurrentValue", "Value", null);
            ChangeTimestampField = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.DateTime, 6, 0, MyBaseThing.cdeA, "Last Change", nameof(ValueChangeTimestamp), new nmiCtrlDateTime {
                ParentFld = 1, Visibility = ShowChangeTimestamp
            });
            TimestampField = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.DateTime, 7, 0, MyBaseThing.cdeA, "Timestamp", nameof(ValueTimestamp), new nmiCtrlDateTime {
                ParentFld = 1, Visibility = ShowChangeTimestamp
            });

            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 800, 2, 0x80, "Configuration...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup()
            {
                ParentFld = 1, DoClose = true, IsSmall = true
            }));

            var ts = TheNMIEngine.AddStatusBlock(MyBaseThing, MyStatusForm, 810);

            ts["Group"].SetParent(800);
            ts["Group"].PropertyBag = new nmiCtrlCollapsibleGroup {
                DoClose = true
            };



            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 1000, 2, 0x80, "NMI Settings...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup()
            {
                ParentFld = 800, DoClose = true, IsSmall = true, TileWidth = 6
            }));
            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 3000, 2, 0x80, "Advanced NMI Settings...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup()
            {
                ParentFld = 1000, DoClose = true, IsSmall = true, TileWidth = 6
            }));
            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.ComboOption, 3010, 2, 0x80, "NMI Screen", "FormName", new ThePropertyBag()
            {
                "Options=%GetLiveScreens%", "TileWidth=6", "TileHeight=1", "ParentFld=3000"
            });
            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CheckField, 3020, 2, 0x80, "Flags", "Flags", new ThePropertyBag()
            {
                "Bits=6", "TileHeight=6", "TileFactorY=2",
                "ImageList=<span class='fa-stack'><i class='fa fa-stack-1x'>&#xf21b;</i><i class='fa fa-stack-2x'>&#x2003;</i></span>," +
                "<span class='fa-stack'><i class='fa fa-stack-1x'>&#xf044;</i><i class='fa fa-stack-2x'>&#x2003;</i></span>," +
                "<span class='fa-stack'><i class='fa fa-stack-1x'>&#xf10b;</i><i class='fa fa-stack-2x text-danger' style='opacity:0.5'>&#xf05e;</i></span>," +
                "<span class='fa-stack'><i class='fa fa-stack-1x'>&#xf0ce;</i><i class='fa fa-stack-2x text-danger' style='opacity:0.5'>&#xf05e;</i></span>," +
                "<span class='fa-stack'><i class='fa fa-stack-1x'>&#xf0f6;</i><i class='fa fa-stack-2x text-danger' style='opacity:0.5'>&#xf05e;</i></span>," +
                "<span class='fa-stack'><i class='fa fa-stack-1x'>&#xf15c;</i><i class='fa fa-stack-2x text-danger' style='opacity:0.5'>&#xf05e;</i></span>", "ParentFld=3000"
            }).FldWidth = 1;

            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.ComboBox, 1500, 2, 0x80, "Control Type", "ControlType", new ThePropertyBag()
            {
                "Options=%RegisteredControlTypes%", "ParentFld=1000"
            });

            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 2000, 2, 0x80, "All Properties...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup()
            {
                NoTE = true, ParentFld = 1000, DoClose = true, IsSmall = true, TileWidth = 6
            }));

            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Table, 2010, 0xA2, 0x80, "All Properties", "mypropertybag;1", new ThePropertyBag()
            {
                "NoTE=true", "TileHeight=4", "TileLeft=9", "TileTop=3", "TileWidth=6", "FldWidth=6", "ParentFld=2000"
            });
            TheNMIEngine.AddFields(MyStatusForm, new List <TheFieldInfo> {
                { new TheFieldInfo()
                  {
                      FldOrder = 2020, DataItem = "MyPropertyBag.ScratchName.Value", Flags = 0x0A, Type = eFieldType.SingleEnded, Header = "New Property Name", TileWidth = 6, TileHeight = 1, PropertyBag = new ThePropertyBag()
                      {
                          "ParentFld=2000"
                      }
                  } },
            });

            TheFieldInfo tBut = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.TileButton, 2040, 0x0A, 0, "Add Property", false, null, null, new nmiCtrlTileButton()
            {
                ParentFld = 2000, NoTE = true, ClassName = "cdeGoodActionButton"
            });

            tBut.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "AddProp", (pThing, pObj) =>
            {
                TheProcessMessage pMsg = pObj as TheProcessMessage;
                if (pMsg?.Message == null)
                {
                    return;
                }
                string[] parts = pMsg.Message.PLS.Split(':');
                TheThing tOrg  = TheThingRegistry.GetThingByMID(MyBaseEngine.GetEngineName(), TheCommonUtils.CGuid(parts[2]));
                if (tOrg == null)
                {
                    return;
                }

                string tNewPropName = TheThing.GetSafePropertyString(tOrg, "ScratchName");
                if (string.IsNullOrEmpty(tNewPropName))
                {
                    TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Please specify a new property name"));
                }
                else
                {
                    if (tOrg.GetProperty(tNewPropName) != null)
                    {
                        TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Property already exists"));
                        tOrg.SetProperty("ScratchName", "");
                    }
                    else
                    {
                        tOrg.DeclareNMIProperty(tNewPropName, ePropertyTypes.TString);
                        TheCommCore.PublishToOriginator(pMsg.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", "Property Added"));
                        tOrg.SetProperty("ScratchName", "");
                        MyStatusForm.Reload(pMsg, false);
                    }
                }
            });
            TheFieldInfo mSendbutton = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.TileButton, 1550, 2, 0x80, "Reload", false, "", null, new nmiCtrlTileButton()
            {
                TileWidth = 6, NoTE = true, ClassName = "cdeGoodActionButton", ParentFld = 1000
            });

            mSendbutton.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "", (pThing, pPara) =>
            {
                TheProcessMessage pMsg = pPara as TheProcessMessage;
                if (pMsg?.Message == null)
                {
                    return;
                }
                UpdateUx();
                MyStatusForm.Reload(pMsg, true);
                //TheNMIEngine.GetEngineDashBoardByThing(MyBaseEngine.GetBaseThing()).Reload(pMsg, true);
            });


            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 5000, 2, 0x80, "Source...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup()
            {
                DoClose = true, IsSmall = true, ParentFld = 800, TileWidth = 6
            }));
            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.ThingPicker, 5010, 2, 0x80, "Source Thing", "Address", new nmiCtrlThingPicker()
            {
                ParentFld = 5000, IncludeEngines = true, IncludeRemotes = true
            });
            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.PropertyPicker, 5020, 2, 0x80, "Source Property", "SourceProp", new nmiCtrlPropertyPicker()
            {
                ParentFld = 5000, ThingFld = 5010
            });


            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleCheck, 5030, 2, 0x80, "Show Timestamp", nameof(ShowTimestamp), new nmiCtrlSingleCheck {
                ParentFld = 5000, TileWidth = 3
            });
            TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleCheck, 5040, 2, 0x80, "Show Change Time", nameof(ShowChangeTimestamp), new nmiCtrlSingleCheck {
                ParentFld = 5000, TileWidth = 3
            });
            TheFieldInfo mSendbutton2 = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.TileButton, 5050, 2, 0x80, "Engage", false, "", null, new nmiCtrlTileButton()
            {
                NoTE = true, TileWidth = 6, ClassName = "cdeGoodActionButton", ParentFld = 5000
            });

            mSendbutton2.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "", (pThing, pPara) =>
            {
                TheProcessMessage pMsg = pPara as TheProcessMessage;
                if (pMsg?.Message == null)
                {
                    return;
                }
                UpdateUx();
            });

            UpdateUx();
            return(true);
        }
Ejemplo n.º 30
0
        private static Image DrawStrip(Guid pThingGuid, DateTimeOffset now, TheFieldInfo pInfo)
        {
            int pixelWidth = 78 * TheCommonUtils.CInt(pInfo?.PropBagGetValue("TileWidth"));

            if (pixelWidth == 0)
            {
                pixelWidth = 78;
            }
            int Hours = TheCommonUtils.CInt(pInfo?.PropBagGetValue("Hours"));

            if (Hours == 0)
            {
                Hours = 1;
            }
            int    pixelHeight = 1;
            Bitmap bmp         = new Bitmap(pixelWidth, pixelHeight, PixelFormat.Format32bppArgb);

            int    st           = 0;
            string tColorString = pInfo?.PropBagGetValue("ChartColors");

            string[] htmlColors = null;
            if (string.IsNullOrEmpty(tColorString))
            {
                htmlColors = TheBaseAssets.MyServiceHostInfo.StatusColors.Split(';');
            }
            else
            {
                htmlColors = tColorString.Split(';');
            }
            Dictionary <int, SolidBrush> stateColorMapping = htmlColors.ToDictionary(x => st++, y => new SolidBrush(ColorTranslator.FromHtml(y)));

            TheThing tThing = TheThingRegistry.GetThingByMID("*", pThingGuid);

            if (tThing == null)
            {
                return(null);
            }
            cdeP pMSH = ((ICDEThing)tThing.GetObject())?.GetProperty("MachineStorageHistory", false);

            if (pMSH == null)
            {
                return(null);
            }
            List <TheMachineStateHistory> tList = TheCommonUtils.DeserializeJSONStringToObject <List <TheMachineStateHistory> >(pMSH.ToString());

            if (tList == null)
            {
                return(null);
            }
            tList = tList.Where(s => now.Subtract(s.StateChangeTime).TotalHours < Hours).OrderBy(s => s.StateChangeTime).ToList();

            cdeP LastDeletedEntry            = tThing.GetProperty("LastDeletedEntry", false);
            TheMachineStateHistory lastState = null;

            if (LastDeletedEntry != null)
            {
                TheMachineStateHistory tHis = TheCommonUtils.DeserializeJSONStringToObject <TheMachineStateHistory>(LastDeletedEntry.ToString());
                lastState = new TheMachineStateHistory()
                {
                    State = tHis.State, StateChangeTime = now.Subtract(new TimeSpan(Hours, 0, 0))
                };
            }

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.ScaleTransform((float)pixelWidth / (Hours * 60 * 60), 1.0f);
                List <KeyValuePair <int, RectangleF> > rectangles = new List <KeyValuePair <int, RectangleF> >();
                foreach (var t in tList)
                {
                    if (lastState != null && t.State != lastState.State)
                    {
                        float start = (float)now.Subtract(t.StateChangeTime).TotalSeconds;
                        float size  = (float)t.StateChangeTime.Subtract(lastState.StateChangeTime).TotalSeconds;
                        rectangles.Add(new KeyValuePair <int, RectangleF>(lastState.State, new RectangleF(new PointF(start, 0), new SizeF(size, pixelHeight))));
                        lastState = t;
                    }
                    if (lastState == null)
                    {
                        lastState = t;
                    }
                }
                if (lastState != null)
                {
                    float size = (float)now.Subtract(lastState.StateChangeTime).TotalSeconds;
                    rectangles.Add(new KeyValuePair <int, RectangleF>(lastState.State, new RectangleF(new PointF(0, 0), new SizeF(size, pixelHeight))));
                }
                if (rectangles.Count > 0)
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    // g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    for (int state = 0; state <= rectangles.Max(x => x.Key); state++)
                    {
                        if (stateColorMapping.ContainsKey(state))
                        {
                            IEnumerable <RectangleF> rects = rectangles.Where(x => x.Key == state).Select(x => x.Value);
                            var rectangleFs = rects as RectangleF[] ?? rects.ToArray();
                            if (rectangleFs.Any())
                            {
                                if (state > stateColorMapping.Count)
                                {
                                    g.FillRectangles(new SolidBrush(Color.Pink), rectangleFs.ToArray());
                                }
                                else
                                {
                                    g.FillRectangles(stateColorMapping[state], rectangleFs.ToArray());
                                }
                            }
                        }
                    }
                }
            }
            if (TheCommonUtils.CBool(pInfo?.PropBagGetValue("DrawRightToLeft")))
            {
                bmp.RotateFlip(RotateFlipType.RotateNoneFlipX); //draw right to left
            }
            return(bmp);
        }