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);
        }
        void sinkThingWasUpdated(ICDEThing sender, object pPara)
        {
            if (TheBaseAssets.MyServiceHostInfo.IsCloudService)
            {
                return;                                                 //TODO: Allow Cloud Rules
            }
            TheThing pThing = sender as TheThing;

            if (pThing != null && TheThing.GetSafePropertyString(pThing, "DeviceType") == eKnownDeviceTypes.TheThingRule)
            {
                TheRule tRule = pThing.GetObject() as TheRule;
                if (tRule == null)
                {
                    tRule = new TheRule(pThing, this);
                    tRule.RegisterEvent(eEngineEvents.ThingUpdated, sinkUpdated);
                    RegisterRule(tRule);
                }

                {
                    tRule.IsRuleWaiting = true;
                    tRule.IsRuleRunning = false;
                    TheSystemMessageLog.WriteLog(4445, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(eKnownDeviceTypes.TheThingRule, $"Rule {tRule.FriendlyName} stopped on Rule Update"), false);

                    TheThingRegistry.UpdateThing(tRule, false);
                }
                ActivateRules();
            }
        }
Exemple #3
0
        private static void defaultOutput(LogData data, string path)
        {
            var log = data.ToString();

            if (TheBaseAssets.MySYSLOG != null)
            {
                var msgLevel   = eMsgLevel.l4_Message;
                var debugLevel = eDEBUG_LEVELS.FULLVERBOSE;
                switch (data.Level)
                {
                case LogLevel.Error:
                case LogLevel.Fatal:
                    msgLevel   = eMsgLevel.l1_Error;
                    debugLevel = eDEBUG_LEVELS.OFF;
                    break;

                case LogLevel.Warn:
                    msgLevel   = eMsgLevel.l2_Warning;
                    debugLevel = eDEBUG_LEVELS.OFF;
                    break;

                case LogLevel.Info:
                    msgLevel   = eMsgLevel.l4_Message;
                    debugLevel = eDEBUG_LEVELS.VERBOSE;
                    break;

                case LogLevel.Debug:
                    msgLevel   = eMsgLevel.l6_Debug;
                    debugLevel = eDEBUG_LEVELS.VERBOSE;
                    break;

                case LogLevel.Trace:
                    msgLevel   = eMsgLevel.l6_Debug;
                    debugLevel = eDEBUG_LEVELS.FULLVERBOSE;
                    break;
                }
                TheBaseAssets.MySYSLOG?.WriteToLog(4365, TSM.L(debugLevel) ? null : new TSM("WebSocketSharp", "WebSocketSharp", msgLevel, log));
            }
            else
            {
                TheSystemMessageLog.ToCo(log);
            }
            if (path != null && path.Length > 0)
            {
                writeToFile(log, path);
            }
        }
Exemple #4
0
        private void output(string message, LogLevel level)
        {
            lock (_sync) {
                if (_level > level)
                {
                    return;
                }

                LogData data = null;
                try {
                    data = new LogData(level, new StackFrame(2, true), message);
                    _output(data, _file);
                }
                catch (Exception ex) {
                    data = new LogData(LogLevel.Fatal, new StackFrame(0, true), ex.Message);
                    TheSystemMessageLog.ToCo(data.ToString());
                }
            }
        }
Exemple #5
0
        public bool Init()
        {
            if (mIsInitCalled)
            {
                return(false);
            }

            mIsInitCalled           = true;
            MyBaseThing.StatusLevel = 1;

            MyBaseThing.Version      = TheBaseAssets.MyAppInfo.CurrentVersion.ToString(CultureInfo.InvariantCulture);
            MyBaseThing.Capabilities = TheBaseAssets.MyAppInfo.Capabilities;
            MyBaseThing.Address      = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false);
            TheThing.SetSafePropertyString(MyBaseThing, "Description", TheBaseAssets.MyAppInfo.LongDescription);
            mIsInitialized = true;

            TheThing.SetSafePropertyBool(MyBaseThing, "EnableKPIs", TheCommonUtils.CBool(TheBaseAssets.MySettings.GetSetting("EnableKPIs")));

            if (TheBaseAssets.MyServiceHostInfo.EnableTaskKPIs)
            {
                var taskKpiThread = new System.Threading.Thread(() =>
                {
                    TheSystemMessageLog.ToCo($"Tasks {DateTime.Now}: NodeHost starting Task KPI thread");
                    do
                    {
                        Thread.Sleep(1000); // Keeping it simple here, to minimize interference on task scheduler/thread scheduler etc. (Assumption: not used on production systems) // TheCommonUtils.SleepOneEye(1000, 1000);
                        var kpis = TheCommonUtils.GetTaskKpis(null);
                        TheSystemMessageLog.ToCo($"Tasks {DateTime.Now}: {TheCommonUtils.SerializeObjectToJSONString(kpis)}");
                    } while (TheBaseAssets.MasterSwitch && TheBaseAssets.MyServiceHostInfo.EnableTaskKPIs);
                    TheSystemMessageLog.ToCo($"Tasks {DateTime.Now}: NodeHost ending Task KPI thread");
                });
                taskKpiThread.Start();
            }
            KPIHarvestInterval = TheCommonUtils.CInt(TheBaseAssets.MySettings.GetAppSetting("KPIHarvestIntervalInSeconds", "5", false, true));
            if (KPIHarvestInterval > 0)
            {
                TheQueuedSenderRegistry.RegisterHealthTimer(sinkCyclic);
            }
            return(true);
        }
        static readonly TimeSpan defaultRequestTimeout = new TimeSpan(0, 1, 0); // TODO make this configurable?

        /// <summary>
        /// Sends a TSM message to the targetThing and return the matching response message
        /// </summary>
        /// <param name="originator">Thing or engine to use for response messages. If NULL defaults to ContentService.</param>
        /// <param name="target">Thing or engine to which the message is to be sent.</param>
        /// <param name="messageName">Name of the message, as defined by the target thing. The response message will be messageName_RESPONSE.</param>
        /// <param name="txtParameters">Array of simple string parameters, to be attached to the message's TXT field, as defined by the target thing. txtParameters must not contain ":" characters.</param>
        /// <param name="PLS">String payload to be set as the message's PLS field, as defined by the target thing.</param>
        /// <param name="PLB">Binary pauload to be set as the message's PLB field, as defined by the target thing.</param>
        /// <param name="timeout">Time to wait for the response message. Can not exceed TheBaseAsset.MaxMessageResponseTimeout (default: 1 hour).</param>
        /// <returns>The response message as defined by the target thing. The response parameters and correlation token can be parsed using ParseRequestOrResponseMessage, if the target thing used the PublishResponseMessage or an equivalent message format.
        /// If the message times out or an error occured while sending the message, the return value will be null.
        /// </returns>
        public static Task <TSM> PublishRequestAsync(TheMessageAddress originator, TheMessageAddress target, string messageName, TimeSpan timeout, string[] txtParameters = null, string PLS = null, byte[] PLB = null)
        {
            Guid correlationToken = Guid.NewGuid();

            if (originator == null)
            {
                originator = TheThingRegistry.GetBaseEngineAsThing(eEngineName.ContentService);
            }
            var msg = PrepareRequestMessage(originator, target, messageName, correlationToken, txtParameters, PLS, PLB);

            if (msg == null)
            {
                return(null);
            }

            if (timeout > MaxTimeOut)
            {
                timeout = MaxTimeOut;
            }
            if (timeout == TimeSpan.Zero)
            {
                timeout = defaultRequestTimeout;
            }

            var tcsResponse = new TaskCompletionSource <TSM>();

            var callback = new Action <ICDEThing, object>((tSenderThing, responseMsgObj) =>
            {
                var responseMsg = CheckResponseMessage(messageName, correlationToken, responseMsgObj);
                if (responseMsg != null)
                {
                    var bReceived = tcsResponse.TrySetResult(responseMsg);
                    if (bReceived)
                    {
                        TheSystemMessageLog.WriteLog(1000, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("Thing Registry", $"Processed response message for {messageName}", eMsgLevel.l2_Warning, $"{correlationToken}: {responseMsg?.TXT}"), false);
                    }
                    else
                    {
                        TheSystemMessageLog.WriteLog(1000, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("Thing Registry", $"Failed to process response message for {messageName}: timed out or double response", eMsgLevel.l2_Warning, $"{correlationToken}: {responseMsg?.TXT}"), false);
                    }
                }
                else
                {
                    responseMsg = (responseMsgObj as TheProcessMessage)?.Message;
                    TheSystemMessageLog.WriteLog(1000, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("Thing Registry", $"Ignoring response message for {messageName}", eMsgLevel.l2_Warning, $"{correlationToken}: {responseMsg?.TXT}"), false);
                }
            });

            var originatorThingOrEngine = RegisterRequestCallback(originator, callback);

            if (originatorThingOrEngine == null)
            {
                return(null);
            }

            if (target.SendToProvisioningService)
            {
                TheISMManager.SendToProvisioningService(msg);
            }
            else if (target.Node != Guid.Empty)
            {
                TheCommCore.PublishToNode(target.Node, msg);
            }
            else
            {
                TheCommCore.PublishCentral(msg, true);
            }

            TheCommonUtils.TaskWaitTimeout(tcsResponse.Task, timeout).ContinueWith((t) =>
            {
                UnregisterRequestCallback(originatorThingOrEngine, callback);
                var timedOut = tcsResponse.TrySetResult(null); // Will preserve value if actual result has already been set
                if (timedOut)
                {
                    TheSystemMessageLog.WriteLog(1000, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("Thing Registry", $"Timeout waiting for response message for {messageName}", eMsgLevel.l2_Warning, $"{correlationToken}"), false);
                }
            });
            return(tcsResponse.Task);
        }
Exemple #7
0
 public void Print(bool dumped)
 {
     TheSystemMessageLog.ToCo(dumped ? dump(this) : print(this));
 }
        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);
        }
        internal static bool GetLocalIPs(bool AddLoopBack)
        {
            bool NewAddition = false;
            bool HasLoop     = false;

            if ((Type.GetType("Mono.Runtime") != null))
            {
                try
                {
                    if (string.IsNullOrEmpty(MyHostName))
                    {
                        MyHostName = cdeGetHostName();
                    }

                    IPHostEntry hostByName = cdeGetHostEntry(MyHostName);
                    if (hostByName == null)
                    {
                        return(false);
                    }
                    TheSystemMessageLog.ToCo(string.Format("NetworkInfo - HostName : {0}", hostByName.HostName));
                    ArrayList tAddressTable = new ArrayList(hostByName.AddressList);
                    foreach (IPAddress address in tAddressTable)
                    {
                        if (address.AddressFamily != AddressFamily.InterNetworkV6)
                        {
                            NewAddition = true;
                            byte[]   ipAdressBytes = address.GetAddressBytes();
                            TheIPDef tI            = new TheIPDef()
                            {
                                ipAdressBytes = ipAdressBytes, SubnetBytes = new byte[] { 255, 0, 0, 0 }, IPAddr = address, IsDnsEnabled = true, IsPrivateIP = IsPrivateIPBytes(ipAdressBytes)
                            };
                            if (address.ToString().Equals(IPAddress.Loopback.ToString()))
                            {
                                HasLoop       = true;
                                tI.IsLoopBack = true;
                            }
                            AddressTable.TryAdd(address, tI);
                        }
                    }
                    //TheSystemMessageLog.ToCo($"NetworkInfo - AddressTable : {AddressTable.Aggregate("", (s, a) => $"{s} {a}")}");
                }
                catch (Exception e)
                {
                    TheSystemMessageLog.ToCo($"NetworkInfo - Exception: {e.ToString()}");
                    // throw;
                }
            }
            else
            {
                NetworkInterface[] networkInterfaces;
                try
                {
                    networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                }
                catch
                {
                    networkInterfaces = null;
                }
                if (networkInterfaces != null)
                {
                    foreach (var f in networkInterfaces)
                    {
                        var ipInterface = f.GetIPProperties();

                        foreach (UnicastIPAddressInformation unicastAddress in ipInterface.UnicastAddresses)
                        {
                            try
                            {
                                if (unicastAddress.IPv4Mask != null)
                                {
                                    IPv4InterfaceProperties ipv4_properties = null;
                                    try
                                    {
                                        ipv4_properties = ipInterface.GetIPv4Properties();
                                    }
                                    catch
                                    {
                                        //ignored
                                    }
                                    if (ipv4_properties == null)
                                    {
                                        continue;                           //TODO: IPv6 Support later
                                    }
                                    byte[] tSubnetBytes = unicastAddress.IPv4Mask.GetAddressBytes();
                                    if (tSubnetBytes[0] == 0)
                                    {
                                        continue;
                                    }
                                    byte[]    ipAdressBytes = unicastAddress.Address.GetAddressBytes();
                                    IPAddress tIP           = new IPAddress(ipAdressBytes);
                                    if (!AddressTable.ContainsKey(tIP))
                                    {
                                        TheIPDef tI = new TheIPDef()
                                        {
                                            ipAdressBytes = ipAdressBytes,
                                            SubnetBytes   = tSubnetBytes,
                                            IPAddr        = tIP,
                                            //IsDnsEnabled = unicastAddress.IsDnsEligible,
                                            IsPrivateIP           = IsPrivateIPBytes(ipAdressBytes),
                                            NetworkInterfaceIndex = ipv4_properties.Index,
                                            MACAddress            = f.GetPhysicalAddress().ToString(),
                                        };
                                        if (!_platformDoesNotSupportUnicastIPAddressInformationIsDNSEnabled)
                                        {
                                            try
                                            {
                                                tI.IsDnsEnabled = unicastAddress.IsDnsEligible;
                                            }
                                            catch (PlatformNotSupportedException)
                                            {
                                                _platformDoesNotSupportUnicastIPAddressInformationIsDNSEnabled = true;
                                            }
                                        }
                                        if (_platformDoesNotSupportUnicastIPAddressInformationIsDNSEnabled)
                                        {
                                            if (string.IsNullOrEmpty(MyHostName))
                                            {
                                                MyHostName = cdeGetHostName();
                                            }
                                            var ipAddresses = cdeGetHostEntry(MyHostName)?.AddressList;
                                            if (ipAddresses?.FirstOrDefault(ip => ip.Equals(unicastAddress.Address)) != null)
                                            {
                                                tI.IsDnsEnabled = true;
                                            }
                                        }
                                        if (tIP.ToString().Equals(IPAddress.Loopback.ToString()))
                                        {
                                            HasLoop       = true;
                                            tI.IsLoopBack = true;
                                        }
                                        AddressTable.TryAdd(tIP, tI);
                                        NewAddition = true;
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }

            if (AddLoopBack && !HasLoop)
            {
                AddressTable.TryAdd(IPAddress.Loopback, new TheIPDef()
                {
                    IPAddr = IPAddress.Loopback, ipAdressBytes = IPAddress.Loopback.GetAddressBytes(), SubnetBytes = new byte[] { 255, 0, 0, 0 }, IsPrivateIP = true, IsDnsEnabled = false, IsLoopBack = true
                });
                NewAddition = true;
            }
            return(NewAddition);
        }