Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }