예제 #1
0
 public static ProgramOption Parse(string argument)
 {
     var programOption = new ProgramOption();
     var colonIndex = argument.IndexOf('=');
     if (colonIndex > 0)
     {
         var parts = new[] {
                 argument.Substring(1,colonIndex-1),
                 argument.Substring(colonIndex+1)
             };
         programOption.Command = parts[0].ToLowerInvariant();
         programOption.Value = parts[1].Replace("\"", string.Empty).Replace("'", string.Empty);
     }
     return programOption;
 }
예제 #2
0
 private async void DecenLogOrderRadioBtn_Checked(object sender, RoutedEventArgs e)
 {
     AcenLogOrderRadioBtn.IsChecked = false;
     if (_isLoading)
     {
         return;
     }
     using (var context = new ZvsContext(_app.EntityContextConnection))
     {
         await ProgramOption.TryAddOrEditAsync(context, new ProgramOption()
         {
             UniqueIdentifier = "LOGDIRECTION",
             Value            = "Descending"
         }, _app.Cts.Token);
     }
 }
        private async void BrowseBtn_Click(object sender, RoutedEventArgs e)
        {
            var dlg    = new FolderBrowserDialog();
            var result = dlg.ShowDialog();

            if (result != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            BackupDirectory = dlg.SelectedPath;

            //Save selected DIR to database
            using (var context = new ZvsContext(EntityContextConnection))
            {
                await ProgramOption.TryAddOrEditAsync(context, new ProgramOption()
                {
                    UniqueIdentifier = "BackupLocation",
                    Value            = dlg.SelectedPath
                }, CancellationTokenSource.Token);
            }
        }
예제 #4
0
        /// <summary>
        /// Parse command line options. Throws ArgumentException for invalid options or
        /// missing required arguments.
        /// </summary>
        /// <param name="args">The command line options.</param>
        /// <exception cref="ArgumentException">ArgumentException</exception>
        public void Parse(string[] args)
        {
            if (args.Length == 0)
            {
                Usage();
                Environment.Exit(1);
            }
            for (int i = 0; i < args.Length; ++i)
            {
                ProgramOption option = ProgramOption.Create(args[i]);
                switch (option.Key)
                {
                case "-v":
                case "--verbose":
                    verbose = true;
                    break;

                case "-h":
                case "--help":
                    Usage();
                    Environment.Exit(0);
                    break;

                case "-V":
                case "--version":
                    Version();
                    Environment.Exit(0);
                    break;

                //
                // Application reason:
                //
                case "-i":
                case "--logon":
                    reason = Reason.Login;
                    break;

                case "-o":
                case "--logout":
                    reason = Reason.Logout;
                    break;

                case "-l":
                case "--list":
                    reason = Reason.List;
                    break;

                case "-F":
                case "--close":
                    reason = Reason.Close;
                    break;

                //
                // Filter options:
                //
                case "--id":
                    if (option.HasValue)
                    {
                        filter.EventID = int.Parse(option.Value);
                    }
                    else
                    {
                        filter.EventID = int.Parse(args[++i]);
                    }
                    break;

                case "--start":
                    if (option.HasValue)
                    {
                        filter.StartTime = DateTime.Parse(option.Value);
                    }
                    else
                    {
                        filter.StartTime = DateTime.Parse(args[++i]);
                    }
                    break;

                case "--end":
                    if (option.HasValue)
                    {
                        filter.EndTime = DateTime.Parse(option.Value);
                    }
                    else
                    {
                        filter.EndTime = DateTime.Parse(args[++i]);
                    }
                    break;

                case "--comp":
                    if (option.HasValue)
                    {
                        filter.Workstation = option.Value;
                    }
                    else
                    {
                        filter.Workstation = args[++i];
                    }
                    break;

                case "--host":
                    if (option.HasValue)
                    {
                        filter.Hostname = option.Value;
                    }
                    else
                    {
                        filter.Hostname = args[++i];
                    }
                    break;

                case "--ip":
                case "--ipaddr":
                    if (option.HasValue)
                    {
                        filter.IpAddress = option.Value;
                    }
                    else
                    {
                        filter.IpAddress = args[++i];
                    }
                    break;

                case "--hw":
                case "--hwaddr":
                case "--mac":
                    if (option.HasValue)
                    {
                        filter.HwAddress = option.Value;
                    }
                    else
                    {
                        filter.HwAddress = args[++i];
                    }
                    break;

                case "--user":
                case "--username":
                    if (option.HasValue)
                    {
                        filter.Username = option.Value;
                    }
                    else
                    {
                        filter.Username = args[++i];
                    }
                    break;

                case "--domain":
                    if (option.HasValue)
                    {
                        filter.Domain = option.Value;
                    }
                    else
                    {
                        filter.Domain = args[++i];
                    }
                    break;

                case "--first":
                    if (option.HasValue)
                    {
                        filter.FirstID = int.Parse(option.Value);
                    }
                    else
                    {
                        filter.FirstID = int.Parse(args[++i]);
                    }
                    break;

                case "--last":
                    if (option.HasValue)
                    {
                        filter.LastID = int.Parse(option.Value);
                    }
                    else
                    {
                        filter.LastID = int.Parse(args[++i]);
                    }
                    break;

                case "-L":
                case "--limit":
                    if (option.HasValue)
                    {
                        filter.Limit = int.Parse(option.Value);
                    }
                    else
                    {
                        filter.Limit = int.Parse(args[++i]);
                    }
                    break;

                //
                // Match options:
                //
                case "-a":
                case "--active":
                    match = LogonEventMatch.Active;
                    break;

                case "-c":
                case "--closed":
                    match = LogonEventMatch.Closed;
                    break;

                case "--between":
                    match = LogonEventMatch.Between;
                    break;

                case "--before":
                    match = LogonEventMatch.Before;
                    break;

                case "--after":
                    match = LogonEventMatch.After;
                    break;

                case "-e":
                case "--exact":
                    match = LogonEventMatch.Exact;
                    break;

                case "-t":
                case "--this":
                {
                    Network network = new Network();
                    filter.HwAddress   = network.HwAddress;
                    filter.Workstation = network.Computer;
                }
                break;

                //
                // Formatting:
                //
                case "-T":
                case "--tabbed":
                    format = Format.Tabbed;
                    break;

                case "-C":
                case "--compact":
                    format = Format.Compact;
                    break;

                case "-H":
                case "--human":
                    format = Format.Human;
                    break;

                case "-X":
                case "--XML":
                case "--xml":
                    format = Format.XML;
                    break;

                //
                // Miscellanous:
                //
                case "-r":
                case "--register":
                    reason = Reason.Register;
                    break;

                case "-u":
                case "--uninstall":
                    reason = Reason.Uninstall;
                    break;

                //
                // Authentication:
                //
                case "-U":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    if (option.HasValue)
                    {
                        credentials.UserName.UserName = option.Value;
                    }
                    else
                    {
                        credentials.UserName.UserName = args[++i];
                    }
                    break;

                case "-P":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    if (option.HasValue)
                    {
                        credentials.UserName.Password = option.Value;
                    }
                    else
                    {
                        credentials.UserName.Password = args[++i];
                    }
                    break;

                case "-W":
                case "--Windows":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    break;

                default:
                    throw new ArgumentException("Unknown option '" + args[i] + "'");
                }
            }

            if (reason == Reason.Unknown)
            {
                throw new ArgumentException("Missing -l, -i or -o option, see --help");
            }
            if (filter.LastID != 0)
            {
                if (match != LogonEventMatch.Between)
                {
                    match = LogonEventMatch.Between;
                }
                if (filter.EventID != 0)
                {
                    filter.FirstID = filter.EventID;    // Make ID an alias for FirstID
                }
            }
            if (filter.FirstID != 0 && filter.EventID == 0)
            {
                filter.EventID = filter.FirstID;        // Make FirstID an alias for ID
            }
        }
예제 #5
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);
        }
예제 #6
0
        /// <summary>
        /// Parse command line options. Throws ArgumentException for invalid options or
        /// missing required arguments.
        /// </summary>
        /// <param name="args">The command line options.</param>
        /// <exception cref="ArgumentException">ArgumentException</exception>
        public void Parse(string[] args)
        {
            for (int i = 0; i < args.Length; ++i)
            {
                ProgramOption option = ProgramOption.Create(args[i]);
                switch (option.Key)
                {
                case "-h":
                case "--help":
                    Usage();
                    Environment.Exit(0);
                    break;

                case "-V":
                case "--version":
                    Version();
                    Environment.Exit(0);
                    break;

                //
                // Miscellanous:
                //
                case "-i":
                case "--install":
                    reason = Reason.Install;
                    break;

                case "-r":
                case "--remove":
                    reason = Reason.Remove;
                    break;

                //
                // Authentication:
                //
                case "-U":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    if (option.HasValue)
                    {
                        credentials.UserName.UserName = option.Value;
                    }
                    else
                    {
                        credentials.UserName.UserName = args[++i];
                    }
                    break;

                case "-P":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    if (option.HasValue)
                    {
                        credentials.UserName.Password = option.Value;
                    }
                    else
                    {
                        credentials.UserName.Password = args[++i];
                    }
                    break;

                case "-W":
                case "--Windows":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    break;

                default:
                    throw new ArgumentException("Unknown option '" + args[i] + "'");
                }
            }
        }