private Task StartComponent(ComponentInfo componentInfo, Type type)
        {
            _taskExecutorService.Enqueue(async() =>
            {
                var sw = new Stopwatch();
                sw.Start();

                var runningComponent = new RunningComponentInfo
                {
                    Id          = componentInfo.Id,
                    Name        = componentInfo.Name,
                    Version     = componentInfo.Version,
                    Description = componentInfo.Description,
                    Status      = ComponentStatusEnum.STOPPED
                };
                try
                {
                    _logger.LogInformation($"Initialize component {componentInfo.Name} v{componentInfo.Version}");
                    var obj  = _servicesManager.Resolve(type) as IComponent;
                    var attr = type.GetCustomAttribute <ComponentAttribute>();


                    RunningComponents.Add(runningComponent);
                    runningComponent.Component = obj;
                    var componentConfig        = (IComponentConfig)LoadComponentConfig(attr.ComponentConfigType);


                    if (componentConfig != null)
                    {
                        await obj.InitConfiguration(componentConfig);
                    }
                    else
                    {
                        _logger.LogWarning($"Component {obj.GetType().Name} don't have configuration");
                        componentConfig = (IComponentConfig)obj.GetDefaultConfig();
                        SaveComponentConfig(componentConfig, attr.ComponentConfigType);
                        await obj.InitConfiguration(componentConfig);
                    }

                    if (componentConfig != null && componentConfig.Enabled)
                    {
                        await obj.Start();
                        runningComponent.Status = ComponentStatusEnum.STARTED;
                    }

                    sw.Stop();

                    _logger.LogInformation($"Component {componentInfo.Name} loaded id {sw.Elapsed}");
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Error during start component {componentInfo.Name} => {ex.Message}");
                    runningComponent.Error  = ex;
                    runningComponent.Status = ComponentStatusEnum.ERROR;
                }
            });


            return(Task.CompletedTask);
        }
示例#2
0
        private Registry BuildRegistry()
        {
            var registry = new Registry();

            AssemblyUtils.ScanAllAssembliesFromAttribute(typeof(SchedulerJobTaskAttribute)).ForEach(t =>
            {
                try
                {
                    var attr = t.GetCustomAttribute <SchedulerJobTaskAttribute>();
                    var job  = _servicesManager.Resolve(t) as IJobSchedulerTask;
                    _logger.LogInformation(
                        $"Adding Job {t.Name} StartNow {attr.StartNow} every {attr.Seconds} seconds");
                    var schedule = registry.Schedule(() => job.Execute()).WithName(t.Name);

                    if (attr.StartNow)
                    {
                        schedule.ToRunNow().AndEvery(attr.Seconds).Seconds();
                    }
                    else
                    {
                        schedule.ToRunEvery(attr.Seconds).Seconds();
                    }

                    _jobs.Add(job);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Error during add job {t.Name} => {ex}");
                }
            });

            return(registry);
        }
        private void LoadConnector()
        {
            var connectorType = NoSqlUtils.GetNoSqlConnector(_config.EventsDatabase.ConnectorName);

            if (connectorType == null)
            {
                throw new Exception($"NoSQL connector named {_config.EventsDatabase.ConnectorName} not found! ");
            }

            _noSqlConnector = (INoSqlConnector)_servicesManager.Resolve(connectorType);

            _noSqlConnector.Init(_config.EventsDatabase.ConnectionString);
        }
示例#4
0
        private void OnMessageReceived(MqttApplicationMessage message)
        {
            _logger.LogDebug($"Received message from topic {message.Topic} {Encoding.UTF8.GetString(message.Payload)}");
            ((ReplaySubject <MqttMessage>)OnMqttMessage).OnNext(new MqttMessage
            {
                Message = Encoding.UTF8.GetString(message.Payload), Topic = message.Topic
            });

            _notificationService = _servicesManager.Resolve <INotificationService>();
            _notificationService.BroadcastMessage(new MqttMessageEvent
            {
                Topic   = message.Topic,
                Message = Encoding.UTF8.GetString(message.Payload)
            });
        }
示例#5
0
        public async Task <bool> Start()
        {
            if (_scriptsEngines.ContainsKey(_neonConfig.Scripts.EngineName))
            {
                _scriptEngine          = (IScriptEngine)_servicesManager.Resolve(_scriptsEngines[_neonConfig.Scripts.EngineName]);
                _scriptEngineAttribute = _scriptEngine.GetType().GetCustomAttribute <ScriptEngineAttribute>();

                _logger.LogInformation($"Initializing script engine {_scriptEngineAttribute.Name} {_scriptEngineAttribute.Version}");

                _bootstrapFile = _fileSystemManager.BuildFilePath(_neonConfig.Scripts.Directory + Path.DirectorySeparatorChar + $"bootstrap{_scriptEngineAttribute.FileExtension}");
                _fileSystemManager.CreateDirectory(_neonConfig.Scripts.Directory);
                CheckBootstrapFile();
                //StartMonitorDirectory();

                _logger.LogInformation("Initializing Script manager");
                ScanForScriptClasses();

                _logger.LogInformation($"Loading bootstrap file");
                _scriptEngine.LoadFile(_bootstrapFile, true);

                _logger.LogInformation($"Scanning files in directory {_neonConfig.Scripts.Directory}");
                LoadScriptsFiles();

                await _scriptEngine.Build();

                _logger.LogInformation("Script manager initialized");
            }
            else
            {
                throw new Exception("No script engine found");
            }



            return(true);
        }
示例#6
0
        private async Task ExecuteSeeds()
        {
            var seeds = AssemblyUtils.ScanAllAssembliesFromAttribute(typeof(DatabaseSeedAttribute));

            foreach (var t in seeds)
            {
                try
                {
                    var seedObj = _servicesManager.Resolve(t) as IDatabaseSeed;

                    _logger.LogInformation($"Executing seed {t.Name}");
                    await seedObj.Seed();
                }
                catch (Exception ex)
                {
                    _logger.LogInformation($"Error during execute seed {t.Name} => {ex}");
                }
            }
        }
示例#7
0
        private async void OnMessageReceived(MqttApplicationMessage message)
        {
            if (_neonConfig.Mqtt.MirrorConfig.SendToMirror)
            {
                if (_mirrorMqttClient != null)
                {
                    await _mirrorMqttClient?.PublishAsync(message.Topic, message.Payload);
                }
            }
            _logger.LogDebug($"Received message from topic {message.Topic} {Encoding.UTF8.GetString(message.Payload)}");
            ((ReplaySubject <MqttMessage>)OnMqttMessage).OnNext(new MqttMessage
            {
                Message = Encoding.UTF8.GetString(message.Payload), Topic = message.Topic
            });

            _notificationService = _servicesManager.Resolve <INotificationService>();
            _notificationService.BroadcastMessage(new MqttMessageEvent
            {
                Topic   = message.Topic,
                Message = Encoding.UTF8.GetString(message.Payload)
            });
        }
示例#8
0
        private void ScanForScriptClasses()
        {
            AssemblyUtils.ScanAllAssembliesFromAttribute(typeof(LuaScriptObjectAttribute)).ForEach(t =>
            {
                _logger.LogInformation($"Registering {t.Name} in LUA Objects");
                var obj = _servicesManager.Resolve(t);
                obj.GetType().GetMethods().ToList().ForEach(m =>
                {
                    try

                    {
                        var scriptFuncAttr = m.GetCustomAttribute <LuaScriptFunctionAttribute>();


                        if (scriptFuncAttr == null)
                        {
                            return;
                        }

                        _logger.LogInformation(
                            $"{obj.GetType().Name} - {scriptFuncAttr.FunctionName} [{scriptFuncAttr.Help}]");

                        _luaEngine.RegisterFunction(scriptFuncAttr.FunctionName, obj,
                                                    obj.GetType().GetMethod(m.Name));

                        GlobalFunctions.Add(new LuaScriptFunctionData
                        {
                            Category = scriptFuncAttr.FunctionCategory,
                            Help     = scriptFuncAttr.Help,
                            Name     = scriptFuncAttr.FunctionName,
                            Args     = m.GetParameters().GetMethodParamStrings()
                        });
                    }
                    catch (Exception ex)
                    {
                    }
                });
            });
        }
示例#9
0
        public ActionResult <bool> Authorize(string provider)
        {
            var type = AssemblyUtils.ScanAllAssembliesFromAttribute(typeof(OAuthProviderAttribute)).ToList()
                       .FirstOrDefault(t =>
            {
                var attr = t.GetCustomAttribute <OAuthProviderAttribute>();

                return(attr.Name.ToLower() == provider);
            });

            //	type = AssemblyUtils.GetInterfaceOfType(type);
            var oauthResult = new OAuthResult();

            if (!string.IsNullOrEmpty(HttpContext.Request.Query["code"]))
            {
                oauthResult.Code = HttpContext.Request.Query["code"];
            }

            if (!string.IsNullOrEmpty(HttpContext.Request.Query["token"]))
            {
                oauthResult.Code = HttpContext.Request.Query["token"];
            }

            if (!string.IsNullOrEmpty(HttpContext.Request.Query["status"]))
            {
                oauthResult.Code = HttpContext.Request.Query["status"];
            }

            if (type != null)
            {
                var callback = _servicesManager.Resolve(type) as IOAuthCallback;

                callback?.OnOAuthResult(oauthResult);
            }

            return(NotFound(true));
        }
 public object ResolveObject(Type type)
 {
     return(_servicesManager.Resolve(type));
 }
        public string Hook(string jsonData)
        {
            _servicesManager.Resolve <IPlexHookComponent>().Hook(jsonData);

            return("ok");
        }