Exemple #1
0
        void sinkByeBye(string pUSN)
        {
            TheUPnPDeviceInfo tInfo = MyUPnPDiscoveryPast?.MyMirrorCache?.GetEntryByFunc(s => s.USN == pUSN);

            if (tInfo != null)
            {
                sinkDeviceExpired(tInfo);
                MyUPnPDiscoveryPast.RemoveAnItem(tInfo, null);
            }
        }
 internal void RemoveSession(TheSessionState SessionState)
 {
     if (SessionState == null)
     {
         return;
     }
     SessionState.HasExpired = true;
     SessionState.EndTime    = DateTimeOffset.Now;
     SessionState.ARApp      = Guid.Empty;
     TheBaseAssets.MySYSLOG.WriteToLog(1234, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM("SSM", $"Session killed at {TheCommonUtils.GetDateTimeString(DateTimeOffset.Now)} for RemoteAdr: {TheCommonUtils.GetDeviceIDML(SessionState.MyDevice)}", eMsgLevel.l3_ImportantMessage));
     TheCommonUtils.cdeRunAsync("RemoveSession", true, (o) => MySessionStates.RemoveAnItem(SessionState, null)); //Do not block the main thread...take your time HasExpired is set :)
 }
Exemple #3
0
#pragma warning restore CS0649

        private async void RunScriptsAsync(object _)
        {
            try
            {
                MyBaseThing.SetStatus(4, "Running scripts.");
                var variables = new TheThing(); // Using this as a property bag to hold variables that can be shared across the scripts -> Maybe use the script thing itself for this? Keeps an audit trail of the state of the script...

                var scriptDir      = TheCommonUtils.cdeFixupFileName(@"ClientBin\scripts");
                var scriptFiles    = Directory.EnumerateFiles(scriptDir, "*.cdescript").ToList();
                var scriptsRun     = new List <string>();
                var pendingScripts = new List <string>();
                var scriptsToRun   = new List <TheScript>();
                int pendingScriptCount;
                pendingScriptCount = pendingScripts.Count;
                foreach (var scriptFile in scriptFiles)
                {
                    try
                    {
                        TheScript script = LoadScript(scriptFile);
                        scriptsToRun.Add(script);
                    }
                    catch (Exception e)
                    {
                        string scriptName = scriptFile;
                        try
                        {
                            scriptName = Path.GetFileNameWithoutExtension(scriptFile);
                        }
                        catch { }
                        UpdateStorageList(scriptFile, $"Failed to read: {e.Message}", -1, null, null, false);
                        TheBaseAssets.MySYSLOG.WriteToLog(175000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Error reading cde script file", eMsgLevel.l1_Error, e.ToString()));
                    }
                }
                foreach (var dependentScript in scriptsToRun.Where(script => script.DependsOn != null && script.DependsOn.Length > 0).ToList())
                {
                    foreach (var dependencyName in dependentScript.DependsOn)
                    {
                        var dependencyScriptIndex = scriptsToRun.FindIndex(script => script.Name == dependencyName);
                        if (dependencyScriptIndex < 0)
                        {
                            TheBaseAssets.MySYSLOG.WriteToLog(175007, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Unable to run script with dependencies", eMsgLevel.l1_Error, $"Dependent: '{dependentScript.Name}'. Dependency not found: '{dependencyName}'"));
                        }
                        else
                        {
                            var dependentScriptIndex = scriptsToRun.IndexOf(dependentScript);
                            if (dependentScriptIndex >= 0 && dependencyScriptIndex > dependentScriptIndex)
                            {
                                scriptsToRun.Insert(dependencyScriptIndex + 1, dependentScript);
                                scriptsToRun.RemoveAt(dependentScriptIndex);
                            }
                        }
                    }
                }

                // This assumes RunScriptsAsync is only called from Init() (= on gate restart)
                foreach (var oldScript in MyScriptTableStorage.TheValues)
                {
                    if (oldScript.ScriptStatus == "Not found")
                    {
                        MyScriptTableStorage.RemoveAnItem(oldScript, null);
                    }
                    else
                    {
                        oldScript.ScriptStatus = "Not found";
                    }
                }

                foreach (var script in scriptsToRun)
                {
                    int stepNumber = 1;
                    foreach (var step in script.Steps ?? new TheScriptStep[0])
                    {
                        UpdateStorageList(script.Name, "Pending", stepNumber, script, null, false);
                        stepNumber++;
                    }
                }

                int index       = 0;
                var scriptTasks = new List <Task>();
                foreach (var script in scriptsToRun)
                {
                    Task task = null;
                    if (script.DependsOn?.Length > 0)
                    {
                        if (index > 1)
                        {
                            task = scriptTasks[index - 1].ContinueWith(t => RunScriptAsync(script, variables));
                        }
                    }
                    if (task == null)
                    {
                        task = RunScriptAsync(script, variables);
                    }
                    scriptTasks.Add(task);
                    scriptsRun.Add(script.Name);
                }
                await TheCommonUtils.TaskWhenAll(scriptTasks);

                if (MyBaseThing.StatusLevel == 4)
                {
                    if (scriptTasks?.Count > 0)
                    {
                        MyBaseThing.SetStatus(1, $"All {scriptTasks?.Count} scripts applied.");
                    }
                    else
                    {
                        MyBaseThing.SetStatus(1, "No scripts found.");
                    }
                    TheBaseAssets.MySYSLOG.WriteToLog(175001, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM(MyBaseThing.EngineName, "Scripts applied", eMsgLevel.l3_ImportantMessage, $"Number of scripts: {scriptTasks?.Count}"));
                }
            }
            catch (DirectoryNotFoundException)
            {
                TheBaseAssets.MySYSLOG.WriteToLog(175001, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM(MyBaseThing.EngineName, "Error finding or running script files", eMsgLevel.l3_ImportantMessage, "Script directory not found"));
                MyBaseThing.SetStatus(1, "No script directory found.");
            }
            catch (Exception e)
            {
                TheBaseAssets.MySYSLOG.WriteToLog(175001, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Error finding or running script files", eMsgLevel.l1_Error, e.ToString()));
                MyBaseThing.SetStatus(3, $"Error while finding or running cdescript files: {e.Message}");
            }
        }
        public override void HandleMessage(ICDEThing sender, object pIncoming)
        {
            if (TheCDEngines.MyIStorageService == null)
            {
                return;                                         //No processing if Storage is not active
            }
            TheProcessMessage pMsg = pIncoming as TheProcessMessage;

            if (pMsg == null)
            {
                return;
            }
            if (!mIsInitialized)
            {
                //InitializeStores(); //V4.106: we dont allow this anymore during runtime - the store is either initialized during startup or never available
                return;
            }
            if (MyBaseEngine.GetEngineState().IsService)
            {
                var tCmd = pMsg.Message.TXT.Split(':');
                switch (tCmd[0])
                {
                case "NEW_SENSOR_VALUE":
                    TheSensorValue tData = TheCommonUtils.DeserializeJSONStringToObject <TheSensorValue>(pMsg.Message.PLS);
                    if (tData != null && MySensorStore != null)
                    {
                        //TODO: tCmd[1] has the "ChartToken" that allows to put the value into different tables/charts. For now just one table for all charts
                        MySensorStore.AddAnItem(tData);
                    }
                    break;

                default:
                    ProcessStorageServiceCommands(pMsg.Message, pMsg.LocalCallback, false);
                    break;
                }
                return;
            }

            //CM: Next section is about Remote Storage Services. This will come in a later step
            string[]         reqGuid = pMsg.Message.TXT.Split(':');
            TheTimedCallback tCall   = null;

            if (reqGuid.Length > 1 && (tCall = MyTimedCallbacks.MyMirrorCache.GetEntryByID(TheCommonUtils.CGuid(reqGuid[1]))) != null)
            {
                tCall.MyCallback?.Invoke(pMsg.Message);
                MyTimedCallbacks.RemoveAnItem(tCall, null);
            }
            else
            {
                object tStorageMirror = TheCDEngines.GetStorageMirror(pMsg.Topic); //, out tStorageMirror);
                if (tStorageMirror != null)
                {
                    Type magicType = tStorageMirror.GetType();
#if CDE_STANDARD
                    MethodInfo magicMethod = magicType.GetTypeInfo().GetDeclaredMethod("sinkProcessServiceMessages");
#else
                    MethodInfo magicMethod = magicType.GetMethod("sinkProcessServiceMessages");
#endif
                    if (magicMethod != null)
                    {
                        magicMethod.Invoke(tStorageMirror, new object[] { pMsg.Message }); //object magicValue =
                    }
                }
            }
        }