Example #1
0
        /// <summary>
        ///     Trys to start the given modules.
        ///     Print's hint if a module was created the wrong way
        /// </summary>
        private void StartModules()
        {
            bool possibleAfterEffects = false;

            // Process each module
            foreach (Assembly moduleAssembly in _moduleAssemblies)
            {
                try
                {
                    // Search for "ModulePropert" interface class in assembly
                    // and, if given, start the module4
                    foreach (Type moduleClass in moduleAssembly.GetTypes())
                    {
                        foreach (Type moduleInterface in moduleClass.GetInterfaces())
                        {
                            if (Attribute.IsDefined(moduleInterface, typeof(ModuleProperties)))
                            {
                                // Load module properties from interface
                                ModuleProperties moduleProperties = (ModuleProperties)
                                                                    Attribute.GetCustomAttribute(moduleInterface, typeof(ModuleProperties));

                                // Moduletype is not given as startup parameter -> next module;
                                if (!ModuleTypeHandler.IsModuleTypeValid
                                        (moduleProperties.ModuleType))
                                {
                                    continue;
                                }
                                try
                                {
                                    ConsoleOutput.AppendPrefix("\t");
                                    // Start module
                                    _kernel.Get(moduleClass);
                                }
                                finally
                                {
                                    ConsoleOutput.ResetPrefix();
                                }
                            }
                        }
                    }
                }
                catch (ActivationException e)
                {
                    ConsoleOutput.WriteLine(ConsoleType.Error,
                                            $"The module ~o~{moduleAssembly.FullName}~;~ could not start, because one or more dependecies are missing!.");

                    if (possibleAfterEffects)
                    {
                        ConsoleOutput.WriteLine(ConsoleType.Note,
                                                "This could be a follow-up error, " +
                                                "because other modules have not been started.");
                    }

                    ConsoleOutput.WriteLine(ConsoleType.Error, $"{e.Message}");
                    possibleAfterEffects = true;
//#if DEBUG
//                    throw;
//#endif
                }
            }
        public static void ShowModuleProperties(Object obj)
        {
            Window mainWindow     = Application.Current.MainWindow;
            var    ModulePropsDlg = new ModuleProperties((MainWindowVM)mainWindow?.DataContext)
            {
                Owner = mainWindow
            };

            ModulePropsDlg.ShowDialog();
        }
Example #3
0
        public static void ShowModuleProperties(Object obj)
        {
            ModuleProperties ModulePropsDlg = new ModuleProperties((MainWindowVM)Application.Current.MainWindow.DataContext);

            ModulePropsDlg.ShowDialog();
        }
Example #4
0
        /// <summary>
        ///     Trys to bind the given modules.
        ///     Print's hint if a module was created the wrong way
        /// </summary>
        /// <param name="modulePaths">Path to the modules, wich should binded</param>
        /// <returns>IKernel</returns>
        private void BindModules(IEnumerable <string> modulePaths)
        {
            ConsoleOutput.AppendPrefix("\t");
            // Progressing each module
            foreach (string modulePath in modulePaths)
            {
                bool hasNeededInterface = false;

                // load assembly
                Assembly moduleAssembly = Assembly.LoadFrom(modulePath);

                //Search for interface that's using the ModuleProperties attribute
                foreach (Type moduleClass in moduleAssembly.GetTypes())
                {
                    foreach (Type moduleInterface in moduleClass.GetInterfaces())
                    {
                        if (Attribute.IsDefined(moduleInterface, typeof(ModuleProperties)))
                        {
                            hasNeededInterface = true;
                            //  Main module class didn't based on BaseModule -> Exception
                            if (!moduleClass.GetAllBaseTypes().Contains(typeof(BaseModule)))
                            {
                                throw new NotValidModuleException(
                                          $"The module {modulePath} didn't base on the \"BaseModule\" abstract class." +
                                          Environment.NewLine +
                                          "Inherit from the BaseModule class.");
                            }

                            // Load module interface Attribute, to get module informations
                            ModuleProperties moduleProperties = (ModuleProperties)
                                                                Attribute.GetCustomAttribute(moduleInterface, typeof(ModuleProperties));

                            // Moduletype is not given as startup parameter -> Message & next module;
                            if (!ModuleTypeHandler.IsModuleTypeValid(moduleProperties.ModuleType))
                            {
                                ConsoleOutput.WriteLine(ConsoleType.Core,
                                                        $"~#51ff76~{moduleInterface.Name}~;~ skipped. Wrong gamemode. ~c~{moduleProperties.ModuleType}");
                                continue;
                            }

                            // Console output
                            ConsoleOutput.WriteLine(ConsoleType.Core,
                                                    $"~#51ff76~{moduleInterface.Name}~;~ -> ~#83ff9d~{moduleClass.FullName}~;~.");

                            // Add to list & bind module
                            _moduleAssemblies.Add(moduleAssembly);
                            _kernel.Bind(moduleInterface, moduleClass).To(moduleClass).InSingletonScope()
                            .WithConstructorArgument("api", context => Api).OnActivation(SharedEvents.OnOnModuleLoaded);
                        }
                    }
                }

                // No implemention of "ModuleProperties" -> exception
                if (!hasNeededInterface)
                {
                    throw new NotValidModuleException(
                              $"The module {modulePath} didn't implement the \"ModuleAttribute\" " +
                              "in the main Interface. " + Environment.NewLine +
                              "Please add the needed interface");
                }
            }

            // Sort modules by priority
            _moduleAssemblies.Sort((assembly1, assembly2) =>
            {
                int GetAssemblyPrio(Assembly ass)
                {
                    foreach (Type moduleClass in ass.GetTypes())
                    {
                        foreach (Type moduleInterface in moduleClass.GetInterfaces())
                        {
                            if (Attribute.IsDefined(moduleInterface, typeof(ModuleProperties)))
                            {
                                // Load module properties from interface
                                ModuleProperties moduleProperties = (ModuleProperties)
                                                                    Attribute.GetCustomAttribute(moduleInterface, typeof(ModuleProperties));

                                // Moduletype is not given as startup parameter -> next module;
                                if (!ModuleTypeHandler.IsModuleTypeValid
                                        (moduleProperties.ModuleType))
                                {
                                    continue;
                                }

                                return(moduleProperties.Priority);
                            }
                        }
                    }

                    return(int.MaxValue);
                }

                return(GetAssemblyPrio(assembly1).CompareTo(GetAssemblyPrio(assembly2)));
            });
            ConsoleOutput.WriteLine(ConsoleType.Core, "Sort modules done.");


            ConsoleOutput.ResetPrefix();
            // return created kernel
        }
Example #5
0
        private static Device CreateDevice(Module module, string hgEndpoint)
        {
            var device = new Device
            {
                name       = module.Name,
                offUrl     = $"http://{hgEndpoint}/api/{module.Domain}/{module.Address}/Control.Off",
                onUrl      = $"http://{hgEndpoint}/api/{module.Domain}/{module.Address}/Control.On",
                deviceType = "switch"
            };

            var guidProperty = module.Properties.SingleOrDefault(x => x.Name == "Module.GUID");

            if (guidProperty == null)
            {
                Log.Warn(
                    "Existing GUID Property not Found, Creating new guid and adding property to the module {0}",
                    module.Name);

                var newGuid = Guid.NewGuid().ToString();

                //new property
                guidProperty = new ModuleProperties
                {
                    Name        = "Module.GUID",
                    Value       = newGuid,
                    NeedsUpdate = true,
                    UpdateTime  = DateTime.UtcNow.ToString()
                };

                // Convert existing properties to a list so we can append it with the new GUID
                var newProperties = module.Properties.ToList();
                newProperties.Add(guidProperty);

                // Copy module and add the new properties
                var newModule = module;
                newModule.Properties = newProperties.ToArray();
                UpdateModule(newModule, hgEndpoint);
            }
            else
            {
                if (IsGuidValid(guidProperty.Value))
                {
                    Log.Info("Valid Guid found for module {0}", module.Name);
                }
                else // invalid guid
                {
                    // Copy module and properties and reset the GUID to something valid
                    var newGuid = Guid.NewGuid().ToString();
                    Log.Warn(
                        "GUID Property found with invalid data, Creating new guid {0} and updating property on module {1}",
                        newGuid, module.Name);
                    guidProperty.Value       = newGuid;
                    guidProperty.NeedsUpdate = true;
                    guidProperty.UpdateTime  = DateTime.UtcNow.ToString();
                    var newModule = module;
                    UpdateModule(newModule, hgEndpoint);
                }
            }

            device.id = guidProperty.Value;
            return(device);
        }