public bool SaveUserConfiguration(string clientId, UserConfiguration userConfiguration)
 {
     /*
      * <userID>1</userID>
      * <userLogPath>C:\Logs</userLogPath>
      * <userCheckInterval>5</userCheckInterval>
      * <userOpenOnStartUp>1</userOpenOnStartUp>
      */
     if (CheckCredential(clientId))
     {
         var confReader   = new ConfigurationReader();
         var serverConfig = confReader.GetServiceConfig();
         var confSaver    = new ConfigurationSaver(serverConfig.UserLogPath);
         if (!confSaver.AddAttribute("userID", userConfiguration.ID.ToString()) ||
             !confSaver.AddAttribute("userLogPath", userConfiguration.LogPath) ||
             !confSaver.AddAttribute("userCheckInterval", userConfiguration.CheckInterval) ||
             !confSaver.AddAttribute("userOpenOnStartup", userConfiguration.OpenOnStartup))
         {
             return(false);
         }
         else
         {
             string userConfigFolder   = (serverConfig.UserConfigPath.Equals("ApplicationFolder")) ? AppDomain.CurrentDomain.BaseDirectory : serverConfig.UserConfigPath;
             string userConfigFullPath = Path.Combine(userConfigFolder, serverConfig.UserConfigFileName);
             return(confSaver.SaveXML(userConfigFullPath));
         }
     }
     return(false);
 }
Exemplo n.º 2
0
        private void SaveUserConfig()
        {
            ConfigurationSaver configurationSaver = new ConfigurationSaver(UserConfig.LogPath);

            if (!configurationSaver.SaveUserConfiguration(UserConfig))
            {
                ShowErrorDialog("Error", "Error when save user configuration!");
                EventLogger.WriteLog("CLIENT SAVE CLIENT CONFIG ERROR", "Error when save user configuration!", UserConfig.LogPath);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Show serialized item in browser
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnViewXmlClick(object sender, EventArgs e)
        {
            ConfigurationSaver.SaveAs(this, "temp.xml");

            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
            psi.FileName        = "temp.xml";
            psi.Verb            = "open";
            psi.UseShellExecute = true;

            psi.CreateNoWindow = true;
            System.Diagnostics.Process.Start(psi);
        }
Exemplo n.º 4
0
        internal void SaveConfiguration()
        {
            try
            {
                TableMapping[] tableMappings = mappingPageViewModel.TableMappingViewModels.Select(t => t.TableMapping).ToArray();

                ImportConfiguration config = new ImportConfiguration(tableMappings, connectionPageViewModel.DatabaseConnector.ConnectionSetup,
                                                                     connectionPageViewModel.SelectedDatabase.Name, importPageViewModel.ErrorHandling);

                ConfigurationSaver saver = new ConfigurationSaver(config, configurationPath);
                saver.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Save item from LM to a file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnSaveAsClick(object sender, EventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();

            fd.AddExtension = true;
            fd.DefaultExt   = "xml";

            if (fd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    ConfigurationSaver.SaveAs(this, fd.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Save as...");
                }
            }
        }
Exemplo n.º 6
0
        private void SavePCIDButton_Click(object sender, RoutedEventArgs e)
        {
            UserConfig.PCID = PCIDTextBox.Text;
            ConfigurationSaver configurationSaver = new ConfigurationSaver();

            if (!configurationSaver.SaveUserConfiguration(UserConfig))
            {
                ShowErrorDialog("Error", "Error when save user configuration!");
                EventLogger.WriteLog("CLIENT SAVE CLIENT CONFIG ERROR", "Error when save user configuration!");
            }
            else
            {
                if (WindowsServiceManager.RestartService(SERVICE_NAME))
                {
                    //Get new User Config
                    var userConfigReader = new ConfigurationReader(isService: false, userConfigPath: ServiceConfig.UserConfigPath, userConfigFile: ServiceConfig.UserConfigFileName);
                    UserConfig = userConfigReader.GetUserConfig();
                    string computerName = Environment.MachineName;
                    string currentUser  = Environment.UserName;
                    string macAddr      = MacAddress.GetFirstMacAddress();
                    var    PCID         = UserConfig.PCID.Equals("default") ? $"{computerName}-{currentUser}-{macAddr}" : UserConfig.PCID;

                    if (PCID.Equals(PCIDTextBox.Text))
                    {
                        SavePCIDButton.IsEnabled = false;
                        PCIDTextBox.Text         = UserConfig.PCID;
                    }
                    else
                    {
                        PCIDTextBox.Text = PCID;
                        ShowErrorDialog("Error", "Error when update new PC ID! Check Log for more detail");
                    }
                }
                else
                {
                    SavePCIDButton.IsEnabled = true;
                }
            }
        }
Exemplo n.º 7
0
        protected override void OnStart(string[] args)
        {
            EventLogger.WriteLog("SERVICE", "Initializing...");
            #region ---Read and check configuration---
            ConfigurationReader config = new ConfigurationReader();

            ServiceConfig = config.GetServiceConfig();

            if (!ClassUtils.CheckNull(ServiceConfig))
            {
                EventLogger.WriteLog("CONFIG ERROR", "Unable to retrieve service configuration file. Exiting...");
                return;
            }
            #endregion

            #region --Check User Config---

            //Get User config
            var userConfigReader = new ConfigurationReader(isService: false, userConfigPath: ServiceConfig.UserConfigPath, userConfigFile: ServiceConfig.UserConfigFileName);
            UserConfiguration CurrentUserConfig = userConfigReader.GetUserConfig();
            //user.config has changed
            UserConfig = CurrentUserConfig;
            ConfigurationSaver configurationSaver = new ConfigurationSaver();

            if (!ClassUtils.CheckNull(CurrentUserConfig))
            {
                //Fill unknown fields of user config
                UserConfig = new UserConfiguration
                {
                    ID                 = CurrentUserConfig == null || CurrentUserConfig.ID == 0 ? ServiceConfig.UserID : CurrentUserConfig.ID,
                    LogPath            = CurrentUserConfig == null || CurrentUserConfig.LogPath == null ? ServiceConfig.UserLogPath : CurrentUserConfig.LogPath,
                    OpenOnStartup      = CurrentUserConfig == null || CurrentUserConfig.OpenOnStartup == null ? ServiceConfig.UserOpenOnStartup : CurrentUserConfig.OpenOnStartup,
                    CheckInterval      = CurrentUserConfig == null || CurrentUserConfig.CheckInterval == null ? ServiceConfig.UserCheckInterval : CurrentUserConfig.CheckInterval,
                    MinimizedWhenClose = CurrentUserConfig == null || CurrentUserConfig.MinimizedWhenClose == null ? ServiceConfig.UserMinimizedWhenClose : CurrentUserConfig.MinimizedWhenClose,
                    PCID               = CurrentUserConfig == null || CurrentUserConfig.PCID == null ? ServiceConfig.UserPCID : CurrentUserConfig.PCID,
                    PreviousPCID       = CurrentUserConfig == null || CurrentUserConfig.PreviousPCID == null ? ServiceConfig.UserPCID : CurrentUserConfig.PreviousPCID
                };


                if (!configurationSaver.SaveUserConfiguration(UserConfig))
                {
                    EventLogger.WriteLog("SERVICE SAVE CLIENT CONFIG ERROR", "Error when save user configuration!");
                }
                EventLogger.WriteLog("SERVICE CREATED NEW CLIENT CONFIG", "");
            }
            #endregion


            #region --- Check NEW PCID ---
            if (!UserConfig.PCID.Equals(UserConfig.PreviousPCID))
            {
                var computerName = Environment.MachineName;
                var currentUser  = Environment.UserName;
                var macAddress   = MacAddress.GetFirstMacAddress();

                var PCID         = UserConfig.PCID.Equals("default") ? $"{computerName}-{currentUser}-{macAddress}" : UserConfig.PCID;
                var previousPCID = UserConfig.PreviousPCID.Equals("default") ? $"{computerName}-{currentUser}-{macAddress}" : UserConfig.PreviousPCID;

                string address     = ServiceConfig.ServerUpdateIDAddress.Replace("[oldname]", previousPCID).Replace("[newname]", PCID);
                var    httpRespond = HttpRequest.SendPutRequest(address, "", ServiceConfig.UserID.ToString());

                if (httpRespond.ToLower().Contains("ok"))
                {
                    var backupPCID = UserConfig.PreviousPCID;
                    UserConfig.PreviousPCID = UserConfig.PCID;
                    if (!configurationSaver.SaveUserConfiguration(UserConfig))
                    {
                        UserConfig.PreviousPCID = backupPCID;
                        EventLogger.WriteLog("SERVICE SAVE CLIENT NEW PC ID ERROR", "Error when save user configuration!");
                    }
                    else
                    {
                        EventLogger.WriteLog("SERVICE UPDATED NEW CLIENT PC ID", "");
                    }
                }
                else
                {
                    UserConfig.PCID = UserConfig.PreviousPCID;
                    if (!configurationSaver.SaveUserConfiguration(UserConfig))
                    {
                        EventLogger.WriteLog("SERVICE REVERTEDS CLIENT NEW PC ID ERROR", "Error when save user configuration!");
                    }
                    else
                    {
                        EventLogger.WriteLog("SERVICE REVERTED NEW CLIENT PC ID", "");
                    }
                }
            }
            #endregion

            #region --Detect GPU Cards--
            var    gpuList           = DeviceManager.GetAllGPUInformation();
            string informationString = $"Detected GPUs: {gpuList.Count}";
            foreach (var gpu in gpuList)
            {
                EventLogger.WriteLog("GPU LIST", "\n" + gpu.ToString());
            }


            #endregion

            #region ---Start Check Timer---
            EventLogger.WriteLog("TIMER", "Starting Timer");
            if (CheckTimer != null)
            {
                CheckTimer.Stop();
                CheckTimer.Dispose();
            }
            try
            {
                CheckTimer          = new Timer();
                CheckTimer.Elapsed += new ElapsedEventHandler(CheckGPUStatus);
                CheckTimer.Interval = ServiceConfig.CheckInterval * 1000;
                CheckTimer.Start();
            }
            catch (Exception ex)
            {
                EventLogger.WriteLog("TIMER ERROR", ex.ToString());
                EventLogger.WriteLog("", "Exitting....");
                return;
            }
            #endregion

            #region ---Start WCF Service---

            try
            {
                //Close Service host if it is already running (when restart Service)
                if (serviceHost != null)
                {
                    serviceHost.Close();
                }
                //Get address from config file
                var address = $"http://localhost:{ServiceConfig.ServicePort}/{ServiceConfig.ServiceName}";
                EventLogger.WriteLog("WCFSERVICE", $"Start service at:{address}");
                serviceHost = new ServiceHost(typeof(MinerTracker_WCFService), new Uri(address));
                //Add endpoint to host Service host
                serviceHost.AddServiceEndpoint(typeof(IMinerTracker_WCFService), new WSHttpBinding(), "");
                //Add Metadata Exchange
                ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior()
                {
                    HttpGetEnabled = true
                };
                serviceHost.Description.Behaviors.Add(serviceBehavior);
                //Start Service host
                serviceHost.Open();
            }
            catch (Exception ex)
            {
                serviceHost = null;
                EventLogger.WriteLog("ERROR", ex.ToString());
            }
            #endregion

            #region --Remove log if size is big--
            var timer = new System.Timers.Timer((DateTime.Today.AddDays(1) - DateTime.Now).TotalMilliseconds);
            timer.Elapsed += new ElapsedEventHandler(OnMidnight);
            timer.Start();
            #endregion
        }