public ZvsEngine(IFeedback<LogEntry> feedback, IAdapterManager adapterManager, IPluginManager pluginManager,
            IEntityContextConnection entityContextConnection, TriggerRunner triggerRunner, ScheduledTaskRunner scheduledTaskRunner)
        {
            if (entityContextConnection == null)
                throw new ArgumentNullException("entityContextConnection");

            if (feedback == null)
                throw new ArgumentNullException("feedback");

            if (adapterManager == null)
                throw new ArgumentNullException("adapterManager");

            if (pluginManager == null)
                throw new ArgumentNullException("pluginManager");

            if (triggerRunner == null)
                throw new ArgumentNullException("triggerRunner");

            if (scheduledTaskRunner == null)
                throw new ArgumentNullException("scheduledTaskRunner");

            EntityContextConnection = entityContextConnection;
            Log = feedback;
            AdapterManager = adapterManager;
            PluginManager = pluginManager;
            TriggerRunner = triggerRunner;
            ScheduledTaskRunner = scheduledTaskRunner;
            Log.Source = "Zvs Engine";


            AppDomain.CurrentDomain.SetData("DataDirectory", Utils.AppDataPath);
        }
Exemple #2
0
        public ZvsEngine(IFeedback <LogEntry> feedback, IAdapterManager adapterManager, IPluginManager pluginManager,
                         IEntityContextConnection entityContextConnection, TriggerRunner triggerRunner, ScheduledTaskRunner scheduledTaskRunner)
        {
            if (entityContextConnection == null)
            {
                throw new ArgumentNullException(nameof(entityContextConnection));
            }

            if (feedback == null)
            {
                throw new ArgumentNullException(nameof(feedback));
            }

            if (adapterManager == null)
            {
                throw new ArgumentNullException(nameof(adapterManager));
            }

            if (pluginManager == null)
            {
                throw new ArgumentNullException(nameof(pluginManager));
            }

            if (triggerRunner == null)
            {
                throw new ArgumentNullException(nameof(triggerRunner));
            }

            if (scheduledTaskRunner == null)
            {
                throw new ArgumentNullException(nameof(scheduledTaskRunner));
            }

            EntityContextConnection = entityContextConnection;
            Log                 = feedback;
            AdapterManager      = adapterManager;
            PluginManager       = pluginManager;
            TriggerRunner       = triggerRunner;
            ScheduledTaskRunner = scheduledTaskRunner;
            Log.Source          = "Zvs Engine";


            AppDomain.CurrentDomain.SetData("DataDirectory", Utils.AppDataPath);
        }
Exemple #3
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await Log.ReportInfoFormatAsync(cancellationToken, "Starting zvsEngine {0}", Utils.ApplicationName);

            #region Install Base Commands and Properties
            var builtinCommandBuilder = new BuiltinCommandBuilder(EntityContextConnection);

            var repollMeResult = await builtinCommandBuilder.RegisterAsync(new BuiltinCommand
            {
                UniqueIdentifier = "REPOLL_ME",
                Name             = "Re-poll Device",
                ArgumentType     = DataType.INTEGER,
                Description      = "This will force a re-poll on an object."
            }, cancellationToken);

            if (repollMeResult.HasError)
            {
                await Log.ReportResultAsync(repollMeResult, cancellationToken);
            }

            var repollAllResult = await builtinCommandBuilder.RegisterAsync(new BuiltinCommand
            {
                UniqueIdentifier = "REPOLL_ALL",
                Name             = "Re-poll all Devices",
                ArgumentType     = DataType.NONE,
                Description      = "This will force a re-poll on all objects."
            }, cancellationToken);

            if (repollAllResult.HasError)
            {
                await Log.ReportResultAsync(repollAllResult, cancellationToken);
            }

            var groupOnResult = await builtinCommandBuilder.RegisterAsync(new BuiltinCommand
            {
                UniqueIdentifier = "GROUP_ON",
                Name             = "Turn Group On",
                ArgumentType     = DataType.STRING,
                Description      = "Activates a group."
            }, cancellationToken);

            if (groupOnResult.HasError)
            {
                await Log.ReportResultAsync(groupOnResult, cancellationToken);
            }

            var groupOffResult = await builtinCommandBuilder.RegisterAsync(new BuiltinCommand
            {
                UniqueIdentifier = "GROUP_OFF",
                Name             = "Turn Group Off",
                ArgumentType     = DataType.STRING,
                Description      = "Deactivates a group."
            }, cancellationToken);

            if (groupOffResult.HasError)
            {
                await Log.ReportResultAsync(groupOffResult, cancellationToken);
            }

            var timeDelayResult = await builtinCommandBuilder.RegisterAsync(new BuiltinCommand
            {
                UniqueIdentifier = "TIMEDELAY",
                Name             = "Time Delay (sec)",
                ArgumentType     = DataType.INTEGER,
                Description      = "Pauses a execution for x seconds."
            }, cancellationToken);

            if (timeDelayResult.HasError)
            {
                await Log.ReportResultAsync(timeDelayResult, cancellationToken);
            }

            var runSceneResult = await builtinCommandBuilder.RegisterAsync(new BuiltinCommand
            {
                UniqueIdentifier = "RUN_SCENE",
                Name             = "Run Scene",
                ArgumentType     = DataType.INTEGER,
                Description      = "Argument = SceneId"
            }, cancellationToken);

            if (runSceneResult.HasError)
            {
                await Log.ReportResultAsync(runSceneResult, cancellationToken);
            }
            #endregion

            await AdapterManager.StartAsync(cancellationToken);

            await PluginManager.StartAsync(cancellationToken);

            await ScheduledTaskRunner.StartAsync(cancellationToken);

            await TriggerRunner.StartAsync(cancellationToken);
        }
        protected async override void OnStartup(StartupEventArgs e)
        {
            var adapterLoader = new AdapterLoader();
            var result = await adapterLoader.FindAdaptersAsync("Adapters", Cts.Token);
            if (result.HasError)
                await Log.ReportErrorAsync(result.Message, Cts.Token);

            var adapterManager = new Processor.AdapterManager(result.Adapters, EntityContextConnection, new DatabaseFeedback(EntityContextConnection));

            var pluginLoader = new PluginLoader();
            var pluginFindResult = await pluginLoader.FindPluginsAsync("plugins", Cts.Token);
            if (pluginFindResult.HasError)
                await Log.ReportErrorAsync(pluginFindResult.Message, Cts.Token);

            var pluginManager = new Processor.PluginManager(pluginFindResult.Plugins, EntityContextConnection, new DatabaseFeedback(EntityContextConnection), adapterManager);


            var triggerRunner = new TriggerRunner(new DatabaseFeedback(EntityContextConnection), new CommandProcessor(adapterManager, EntityContextConnection, new DatabaseFeedback(EntityContextConnection)), EntityContextConnection);
            var scheduledTaskRunner = new ScheduledTaskRunner(new DatabaseFeedback(EntityContextConnection), new CommandProcessor(adapterManager, EntityContextConnection, new DatabaseFeedback(EntityContextConnection)), EntityContextConnection, new CurrentTimeProvider());
            ZvsEngine = new ZvsEngine(new DatabaseFeedback(EntityContextConnection), adapterManager, pluginManager, EntityContextConnection, triggerRunner, scheduledTaskRunner);

            var splashscreen = new SplashScreen();
            splashscreen.SetLoadingTextFormat("Starting {0}", Utils.ApplicationNameAndVersion);
            splashscreen.Show();
            await Task.Delay(10);

#if DEBUG
            var sw = new Stopwatch();
            sw.Start();
#endif

#if (RELEASE)
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#endif
            using (var context = new ZvsContext(new ZvsEntityContextConnection()))
            {
                //Install Program Options
                var option =
                    await
                        context.ProgramOptions.FirstOrDefaultAsync(o => o.UniqueIdentifier == "LOGDIRECTION",
                            Cts.Token);
                if (option == null)
                {
                    var registerLogDirectionResult = await ProgramOption.TryAddOrEditAsync(context, new ProgramOption
                    {
                        UniqueIdentifier = "LOGDIRECTION",
                        Value = "Descending"
                    }, Cts.Token);

                    if (registerLogDirectionResult.HasError)
                        await Log.ReportErrorAsync(registerLogDirectionResult.Message, Cts.Token);
                }
            }
            //using (var context = new ZvsContext(new ZvsEntityContextConnection()))
            //{
            //    var adapter = CreateFakeAdapter();
            //    context.Devices.Add(new Device
            //    {
            //        Name = "Light Switch",
            //        Location = "Living Room",
            //        Type = new DeviceType
            //        {
            //            Name = "Switch Device Type",
            //            UniqueIdentifier = "Switch",
            //            Adapter = adapter
            //        }
            //    });

            //    context.Devices.Add(new Device
            //    {
            //        Name = "Main Controller",
            //        Location = "Utility",
            //        Type = new DeviceType
            //        {
            //            Name = "Controller Type",
            //            UniqueIdentifier = "Controller",
            //            Adapter = adapter
            //        }
            //    });

            //    context.Devices.Add(new Device
            //    {
            //        Name = "Can Lights",
            //        Location = "Kitchen",
            //        Type = new DeviceType
            //        {
            //            Name = "Dimmer Type",
            //            UniqueIdentifier = "Dimmer",
            //            Adapter = adapter
            //        }
            //    });

            //    context.Devices.Add(new Device
            //    {
            //        Name = "Thermostat",
            //        Location = "Kitchen",
            //        Type = new DeviceType
            //        {
            //            Name = "Thermostat Type",
            //            UniqueIdentifier = "Thermostat",
            //            Adapter = adapter
            //        }
            //    });

            //    context.Devices.Add(new Device
            //    {
            //        Name = "Front Door",
            //        Location = "Entry",
            //        Type = new DeviceType
            //        {
            //            Name = "Doorlock Type",
            //            UniqueIdentifier = "Doorlock",
            //            Adapter = adapter
            //        }
            //    });
            //    var sensorDt = new DeviceType
            //    {
            //        Name = "Sensor Type",
            //        UniqueIdentifier = "Sensor",
            //        Adapter = adapter
            //    };
            //    var device = new Device
            //    {
            //        Name = "Motion Sensor",
            //        Location = "Entry",
            //        Type = sensorDt,
            //    };
            //    var value = new DeviceValue
            //    {
            //        Name = "Level",
            //        ValueType = DataType.BYTE,
            //        Value = "255"
            //    };
            //    device.Values.Add(value);
            //    context.Devices.Add(device);

            //     //context.SaveChanges();
            //}

            #region Create Logger

            await Log.ReportInfoFormatAsync(Cts.Token, "Init Complete ({0})", (Utils.DebugMode ? "Debug Mode" : "Release Mode"));
#if DEBUG
            await Log.ReportInfoAsync("--------------DUMPING ENVIRONMENT--------------", Cts.Token);
            await Log.ReportInfoFormatAsync(Cts.Token, "AppDataPath:{0}", Utils.AppDataPath);
            await Log.ReportInfoFormatAsync(Cts.Token, "AppPath:{0}", Utils.AppPath);
            await Log.ReportInfoFormatAsync(Cts.Token, "ApplicationNameAndVersion:{0}", Utils.ApplicationNameAndVersion);
            await Log.ReportInfoFormatAsync(Cts.Token, "ApplicationVersionLong:{0}", Utils.ApplicationVersionLong);
            await Log.ReportInfoFormatAsync(Cts.Token, "HasDotNet45:{0}", Utils.HasDotNet45());
            await Log.ReportInfoFormatAsync(Cts.Token, "HasSQLCE4:{0}", Utils.HasSQLCE4());
            await Log.ReportInfoFormatAsync(Cts.Token, "CommandLine:{0}", Environment.CommandLine);
            await Log.ReportInfoFormatAsync(Cts.Token, "CurrentDirectory:{0}", Environment.CurrentDirectory);
            await Log.ReportInfoFormatAsync(Cts.Token, "Is64BitOperatingSystem:{0}", Environment.Is64BitOperatingSystem);
            await Log.ReportInfoFormatAsync(Cts.Token, "Is64BitProcess:{0}", Environment.Is64BitProcess);
            await Log.ReportInfoFormatAsync(Cts.Token, "MachineName:{0}", Environment.MachineName);
            await Log.ReportInfoFormatAsync(Cts.Token, "OSVersion:{0}", Environment.OSVersion);
            await Log.ReportInfoFormatAsync(Cts.Token, "ProcessorCount:{0}", Environment.ProcessorCount);
            await Log.ReportInfoFormatAsync(Cts.Token, "UserDomainName:{0}", Environment.UserDomainName);
            await Log.ReportInfoFormatAsync(Cts.Token, "UserInteractive:{0}", Environment.UserInteractive);
            await Log.ReportInfoFormatAsync(Cts.Token, "UserName:{0}", Environment.UserName);
            await Log.ReportInfoFormatAsync(Cts.Token, "Version:{0}", Environment.Version);
            await Log.ReportInfoFormatAsync(Cts.Token, "WorkingSet:{0}", Environment.WorkingSet);
            await Log.ReportInfoAsync("--------------/DUMPING ENVIRONMENT--------------", Cts.Token);
#endif
            AppDomain.CurrentDomain.SetData("DataDirectory", Utils.AppDataPath);
            #endregion

            #region Checking for other running instances
            await Task.Delay(10);
            splashscreen.SetLoadingTextFormat("Checking for other running instances");
            await Task.Delay(10);

            try
            {
                _zvsMutex = Mutex.OpenExisting("zVirtualScenesGUIMutex");
                ProgramHasToClosePrompt(Utils.ApplicationName + " can't start because it is already running");
            }
            catch
            {
                //the specified mutex doesn't exist, we should create it
                _zvsMutex = new Mutex(true, "zVirtualScenesGUIMutex"); //these names need to match.
            }
            #endregion

            #region Check for .Net Framework 4.5
            await Task.Delay(10);
            splashscreen.SetLoadingTextFormat("Checking for .Net framework 4.5");
            await Task.Delay(10);

            if (!Utils.HasDotNet45())
            {
                ProgramHasToClosePrompt(
                    $"Microsoft .NET Framework 4.5 Full/Extended is required to run {Utils.ApplicationName}. \r\n\r\nPlease install Microsoft .NET Framework 4.5 and re-launch the application.");
            }
            #endregion

            #region Checking for Microsoft® SQL Server® Compact 4.0 SP1
            await Task.Delay(10);
            splashscreen.SetLoadingTextFormat("Checking for Microsoft® SQL Server® Compact 4.0 SP1");
            await Task.Delay(10);

            if (!Utils.HasSQLCE4())
            {
                ProgramHasToClosePrompt(
                    $"Microsoft® SQL Server® Compact 4.0 SP1 is required to run {Utils.ApplicationName}. \r\n\r\nPlease install Microsoft® SQL Server® Compact 4.0 SP1 and re-launch the application.");
            }
            #endregion

            #region Initializing and upgrading local database
            await Task.Delay(10);
            splashscreen.SetLoadingTextFormat("Initializing and migrating database");
            await Task.Delay(10);

            await Task.Run(() =>
            {
                using (var context = new ZvsContext())
                {
                    var configuration = new Configuration();
                    var migrator = new DbMigrator(configuration);

                    migrator.Update();
                    context.Database.Initialize(true);
                }
            });
            #endregion

            //TODO: Check for VCRedist

            #region Start zvsEngine Services
            await Task.Delay(10);
            splashscreen.SetLoadingTextFormat("Starting zvsEngine services");
            await Task.Delay(10);

            //Initialize the zvsEngine

            try
            {
                await Task.Run(() => ZvsEngine.StartAsync(Cts.Token));
            }
            catch (Exception ex)
            {
                ProgramHasToClosePrompt(ex.Message);
            }

            #endregion

            //Create taskbar Icon 
            TaskbarIcon = new ZVSTaskbarIcon();
            TaskbarIcon.ShowBalloonTip(Utils.ApplicationName, Utils.ApplicationNameAndVersion + " started", 3000, ToolTipIcon.Info);

            //close Splash Screen
            splashscreen.Close();

#if DEBUG
            sw.Stop();
            Debug.WriteLine("App Startup initialized in {0}", sw.Elapsed.ToString() as object);
#endif

            base.OnStartup(e);
        }