Ejemplo n.º 1
0
        public bool RegisterRule(TheThingRule pRule)
        {
            if (pRule == null || TheBaseAssets.MyServiceHostInfo.IsCloudService)
            {
                return(false);
            }

            pRule.GetBaseThing().EngineName = MyBaseEngine.GetEngineName();
            if (pRule.TriggerCondition == eRuleTrigger.Set)
            {
                pRule.ActionValue = null;
            }
            pRule.IsRuleRunning        = false;
            pRule.IsRuleWaiting        = true;
            pRule.IsIllegal            = false;
            pRule.IsTriggerObjectAlive = false;
            pRule.Parent = MyBaseThing.ID;
            pRule.GetBaseThing().EngineName = MyBaseEngine.GetEngineName();
            TheSystemMessageLog.WriteLog(4445, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(eKnownDeviceTypes.TheThingRule, $"Rule {pRule.FriendlyName} stopped during Register Rule - waiting for startup"), false);

            TheThing.SetSafePropertyDate(pRule.GetBaseThing(), "LastTriggered", DateTimeOffset.MinValue);
            TheThing.SetSafePropertyDate(pRule.GetBaseThing(), "LastAction", DateTimeOffset.MinValue);
            TheThingRegistry.RegisterThing(pRule);
            return(true);
        }
Ejemplo n.º 2
0
        public void SetPropertyRoundtripTest()
        {
            var testThing = new TheThing();

            var date         = new DateTimeOffset(2017, 12, 21, 11, 12, 13, 456, new TimeSpan(1, 0, 0));
            var dateExpected = new DateTimeOffset(2017, 12, 21, 11, 12, 13, 456, new TimeSpan(1, 0, 0));

            TheThing.SetSafePropertyDate(testThing, "TestDatePropName", date);
            date = DateTimeOffset.MinValue;
            var dateReturned = TheThing.GetSafePropertyDate(testThing, "TestDatePropName");

            Assert.AreEqual(dateExpected, dateReturned);
        }
Ejemplo n.º 3
0
        void sinkRuleAction(cdeP pProp)
        {
            //List<TheRule> tList = MyRulesStore.MyMirrorCache.GetEntriesByFunc(s => TheCommonUtils.CGuid(s.TriggerObject) != Guid.Empty && TheCommonUtils.CGuid(s.TriggerObject) == TheCommonUtils.CGuid(pEvent.TXT));
            List <TheThing> tList = TheThingRegistry.GetThingsByFunc("*", s => pProp.cdeO != Guid.Empty && TheCommonUtils.CGuid(TheThing.GetSafePropertyString(s, "TriggerObject")) == pProp.cdeO);

            if (tList != null)
            {
                foreach (TheThing tThing in tList)
                {
                    TheRule tRule = tThing.GetObject() as TheRule;
                    if (tRule == null || !tRule.IsRuleActive)
                    {
                        continue;
                    }
                    if (tRule.TriggerStartTime > DateTimeOffset.Now)
                    {
                        continue;
                    }
                    if (tRule.TriggerEndTime < DateTimeOffset.Now)
                    {
                        RemoveTrigger(tRule, true);
                        continue;
                    }
                    if (string.IsNullOrEmpty(pProp.Name) || string.IsNullOrEmpty(pProp.ToString()))
                    {
                        continue;
                    }
                    if (!pProp.Name.Equals(tRule.TriggerProperty))
                    {
                        continue;
                    }
                    tRule.RuleTrigger(pProp.ToString());
                    TheThing.SetSafePropertyDate(tRule.GetBaseThing(), "LastTriggered", DateTimeOffset.Now);
                }
            }
        }
Ejemplo n.º 4
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);
        }