/// <summary> /// Parses a command message and invokes a callback function delegate for status reporting /// </summary> /// <param name="msg">list of messages to parse</param> /// <param name="SendResponse">Callback taking a string as parameter to report return values back to sender</param> /// <param name="service">Service class for invoking application exit</param> public static void Parse(List <string> msg, Action <string> SendResponse, Service service) { AdmConfigBuilder builder = AdmConfigBuilder.Instance(); RuntimeConfig rtc = RuntimeConfig.Instance(); msg.ForEach(message => { switch (message) { case Command.Switch: Logger.Info("signal received: time based theme switch"); ThemeManager.TimedSwitch(builder); SendResponse(Response.Ok); break; case Command.Swap: Logger.Info("signal received: swap themes"); if (RegistryHandler.AppsUseLightTheme()) { ThemeManager.SwitchTheme(builder.Config, Theme.Dark); } else { ThemeManager.SwitchTheme(builder.Config, Theme.Light); } SendResponse(Response.Ok); break; case Command.AddAutostart: Logger.Info("signal received: adding service to autostart"); bool regOk; bool taskOk; if (builder.Config.Tunable.UseLogonTask) { regOk = RegistryHandler.RemoveAutoStart(); taskOk = TaskSchdHandler.CreateLogonTask(); } else { taskOk = TaskSchdHandler.RemoveLogonTask(); regOk = RegistryHandler.AddAutoStart(); } if (regOk && taskOk) { SendResponse(Response.Ok); } else { SendResponse(Response.Err); } break; case Command.RemoveAutostart: Logger.Info("signal received: removing service from autostart"); bool ok; if (builder.Config.Tunable.UseLogonTask) { ok = TaskSchdHandler.RemoveLogonTask(); } else { ok = RegistryHandler.RemoveAutoStart(); } if (ok) { SendResponse(Response.Ok); } else { SendResponse(Response.Err); } break; case Command.Location: Logger.Info("signal received: request location update"); Task <bool> geoTask = Task.Run(() => LocationHandler.UpdateGeoposition(AdmConfigBuilder.Instance())); geoTask.Wait(); var result = geoTask.Result; if (result) { SendResponse(Response.Ok); } else { SendResponse(Response.NoLocAccess); } break; case Command.UpdateConfig: Logger.Info("signal received: updating configuration files"); try { AdmConfigBuilder.Instance().Load(); AdmConfigBuilder.Instance().LoadLocationData(); SendResponse(Response.Ok); } catch (Exception e) { Logger.Error(e, "could not read config file"); SendResponse(Response.Err); } break; case Command.Update: Logger.Info("signal received: checking for update"); SendResponse(UpdateHandler.CheckNewVersion()); break; case Command.Shutdown: Logger.Info("signal received, exiting"); SendResponse(Response.Ok); service.Exit(null, null); break; case Command.TestError: Logger.Info("signal received: test error"); SendResponse(Response.Err); break; case Command.Alive: Logger.Info("signal received: request for running status"); SendResponse(Response.Ok); break; case Command.Light: Logger.Info("signal received: force light theme"); rtc.ForcedTheme = Theme.Light; ThemeManager.SwitchTheme(builder.Config, Theme.Light); SendResponse(Response.Ok); break; case Command.Dark: Logger.Info("signal received: force dark theme"); rtc.ForcedTheme = Theme.Dark; ThemeManager.SwitchTheme(builder.Config, Theme.Dark); SendResponse(Response.Ok); break; case Command.NoForce: Logger.Info("signal received: resetting forced modes"); rtc.ForcedTheme = Theme.Undefined; ThemeManager.TimedSwitch(builder); SendResponse(Response.Ok); break; default: Logger.Debug("unknown message received"); SendResponse(Response.Err); break; } }); }
//private static readonly ComponentManager cm = ComponentManager.Instance(); /// <summary> /// Parses a command message and invokes a callback function delegate for status reporting /// </summary> /// <param name="msg">list of messages to parse</param> /// <param name="SendResponse">Callback taking a string as parameter to report return values back to sender</param> /// <param name="service">Service class for invoking application exit</param> public static void Parse(List <string> msg, Action <string> SendResponse, Service service) { WaitForConfigUpdateCompletion(); msg.ForEach(message => { switch (message) { #region Switch case Command.Switch: Logger.Info("signal received: invoke theme switch"); //cm.ForceAll(); ThemeManager.TimedSwitch(builder, false); SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); break; #endregion #region Swap case Command.Swap: Logger.Info("signal received: swap themes"); if (RegistryHandler.AppsUseLightTheme()) { ThemeManager.SwitchTheme(builder.Config, Theme.Dark); } else { ThemeManager.SwitchTheme(builder.Config, Theme.Light); } SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); break; #endregion #region AddAutoStart case Command.AddAutostart: Logger.Info("signal received: add service to autostart"); bool regOk; bool taskOk; if (builder.Config.Tunable.UseLogonTask) { Logger.Debug("logon task mode selected"); regOk = RegistryHandler.RemoveAutoStart(); taskOk = TaskSchdHandler.CreateLogonTask(); } else { Logger.Debug("autostart mode selected"); taskOk = TaskSchdHandler.RemoveLogonTask(); regOk = RegistryHandler.AddAutoStart(); } if (regOk && taskOk) { SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); } else { SendResponse(new ApiResponse() { StatusCode = StatusCode.Err, Message = $"RegOk: {regOk}, TaskOk: {taskOk}" }.ToString()); } break; #endregion #region RemoveAutostart case Command.RemoveAutostart: Logger.Info("signal received: remove service from autostart"); bool ok; if (builder.Config.Tunable.UseLogonTask) { Logger.Debug("logon task mode selected"); ok = TaskSchdHandler.RemoveLogonTask(); } else { Logger.Debug("autostart mode selected"); ok = RegistryHandler.RemoveAutoStart(); } if (ok) { SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); } else { SendResponse(new ApiResponse() { StatusCode = StatusCode.Err }.ToString()); } break; #endregion #region LocationAccess case Command.LocationAccess: Logger.Info("signal received: checking location access permissions"); Task <bool> geoTask = Task.Run(async() => await LocationHandler.HasPermission()); geoTask.Wait(); var result = geoTask.Result; if (result) { SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); } else { SendResponse(new ApiResponse() { StatusCode = StatusCode.NoLocAccess, Message = "location service needs to be enabled" }.ToString()); } break; #endregion #region GeoloatorIsUpdating case Command.GeolocatorIsUpdating: Logger.Info("signal received: check if geolocator is busy"); if (state.GeolocatorIsUpdating) { SendResponse(new ApiResponse() { StatusCode = StatusCode.InProgress }.ToString()); } else { SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); } break; #endregion #region CheckForUpdates case Command.CheckForUpdate: Logger.Info("signal received: checking for update"); SendResponse(UpdateHandler.CheckNewVersion().ToString()); break; #endregion #region CheckForUpdateNotify case Command.CheckForUpdateNotify: Logger.Info("signal received: checking for update and requesting notification"); ApiResponse updateCheckData = UpdateHandler.CheckNewVersion(); updateCheckData = UpdateHandler.CanUseUpdater(); if (updateCheckData.StatusCode == StatusCode.New) { ToastHandler.InvokeUpdateToast(); } else if (updateCheckData.StatusCode == StatusCode.UnsupportedOperation || updateCheckData.StatusCode == StatusCode.Disabled) { ToastHandler.InvokeUpdateToast(canUseUpdater: false); } SendResponse(updateCheckData.ToString()); break; #endregion #region Update case Command.Update: Logger.Info("signal received: update adm"); if (!UpdateHandler.Updating) { ApiResponse response = UpdateHandler.CanUseUpdater(); if (response.StatusCode == StatusCode.New) { SendResponse(response.ToString()); // this is run sync, as such it will block the ZMQ thread! _ = Task.Run(() => UpdateHandler.Update()); } else { SendResponse(response.ToString()); } } else { SendResponse(new ApiResponse() { StatusCode = StatusCode.InProgress, Message = "Update already in progress", Details = $"Download Progress: {UpdateHandler.Progress}" }.ToString()); } //_ = UpdateHandler.CheckNewVersion(); break; #endregion #region Shutdown case Command.Shutdown: Logger.Info("signal received, exiting"); SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); service.Exit(null, null); break; #endregion #region TestError case Command.TestError: Logger.Info("signal received: test error"); SendResponse(new ApiResponse() { StatusCode = StatusCode.Err }.ToString()); break; #endregion #region Alive case Command.Alive: Logger.Info("signal received: request for running status"); SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); break; #endregion #region Light case Command.Light: Logger.Info("signal received: force light theme"); state.ForcedTheme = Theme.Light; ThemeManager.SwitchTheme(builder.Config, Theme.Light); SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); break; #endregion #region Dark case Command.Dark: Logger.Info("signal received: force dark theme"); state.ForcedTheme = Theme.Dark; ThemeManager.SwitchTheme(builder.Config, Theme.Dark); SendResponse(StatusCode.Ok); break; #endregion #region NoForce case Command.NoForce: Logger.Info("signal received: resetting forced modes"); state.ForcedTheme = Theme.Unknown; ThemeManager.TimedSwitch(builder); SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); break; #endregion #region DetectMonitors case Command.DetectMonitors: Logger.Info("signal received: detecting new monitors"); WallpaperHandler.DetectMonitors(); SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); break; #endregion #region CleanMonitors case Command.CleanMonitors: Logger.Info("signal received: removing disconnected monitors"); WallpaperHandler.CleanUpMonitors(); SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); break; #endregion #region UpdateFailed case Command.UpdateFailed: Logger.Info("signal received: notify about failed update"); ToastHandler.InvokeFailedUpdateToast(); SendResponse(StatusCode.Ok); break; #endregion #region TestNotifications case Command.TestNotifications: Logger.Info("signal received: test notifications"); ToastHandler.InvokeUpdateInProgressToast(); SendResponse(new ApiResponse() { StatusCode = StatusCode.Ok }.ToString()); break; #endregion default: Logger.Debug("unknown message received"); SendResponse(new ApiResponse() { StatusCode = StatusCode.Err, Message = "requested command does not exist" }.ToString()); break; } }); }
/// <summary> /// Parses a command message and invokes a callback function delegate for status reporting /// </summary> /// <param name="msg">list of messages to parse</param> /// <param name="SendResponse">Callback taking a string as parameter to report return values back to sender</param> /// <param name="service">Service class for invoking application exit</param> public static void Parse(List <string> msg, Action <string> SendResponse, Service service) { AdmConfigBuilder Properties = AdmConfigBuilder.Instance(); RuntimeConfig rtc = RuntimeConfig.Instance(); msg.ForEach(message => { switch (message) { case Command.Switch: Logger.Info("signal received: time based theme switch"); ThemeManager.TimedSwitch(Properties); SendResponse(Command.Ok); break; case Command.Swap: Logger.Info("signal received: swap themes"); if (RegistryHandler.AppsUseLightTheme()) { ThemeManager.SwitchTheme(Properties.Config, Theme.Dark); } else { ThemeManager.SwitchTheme(Properties.Config, Theme.Light); } SendResponse(Command.Ok); break; case Command.AddAutostart: Logger.Info("signal received: adding service to autostart"); RegistryHandler.AddAutoStart(); SendResponse(Command.Ok); break; case Command.RemoveAutostart: Logger.Info("signal received: removing service from autostart"); RegistryHandler.RemoveAutoStart(); SendResponse(Command.Ok); break; case Command.CreateTask: Logger.Info("signal received: creating win scheduler based time switch task"); try { DateTime sunrise = Convert.ToDateTime(Properties.Config.Sunrise); DateTime sunset = Convert.ToDateTime(Properties.Config.Sunset); if (Properties.Config.Location.Enabled) { LocationHandler.GetSunTimesWithOffset(Properties, out sunrise, out sunset); } TaskSchdHandler.CreateSwitchTask(sunrise.Hour, sunrise.Minute, sunset.Hour, sunset.Minute); SendResponse(Command.Ok); } catch (FormatException e) { Logger.Error(e, "could not create win scheduler tasks"); SendResponse(Command.Err); Console.WriteLine(e); } break; case Command.RemoveTask: Logger.Info("signal received: removing win tasks"); TaskSchdHandler.RemoveTasks(); SendResponse(Command.Ok); break; case Command.Location: Logger.Info("signal received: request location update"); Task <bool> geoTask = Task.Run(() => LocationHandler.UpdateGeoposition(AdmConfigBuilder.Instance())); geoTask.Wait(); var result = geoTask.Result; if (result) { SendResponse(Command.Ok); } else { SendResponse(Command.NoLocAccess); } break; case Command.UpdateConfig: Logger.Info("signal received: updating configuration files"); try { AdmConfigBuilder.Instance().Load(); AdmConfigBuilder.Instance().LoadLocationData(); SendResponse(Command.Ok); } catch (Exception e) { Logger.Error(e, "could not read config file"); SendResponse(Command.Err); } break; case Command.Update: Logger.Info("signal received: checking for update"); SendResponse(UpdateHandler.CheckNewVersion()); break; case Command.Shutdown: Logger.Info("signal received, exiting"); SendResponse(Command.Ok); service.Exit(null, null); break; case Command.TestError: Logger.Info("signal received: test error"); SendResponse(Command.Err); break; case Command.Alive: Logger.Info("signal received: request for running status"); SendResponse(Command.Ok); break; case Command.Light: Logger.Info("signal received: force light theme"); rtc.ForcedTheme = Theme.Light; ThemeManager.SwitchTheme(Properties.Config, Theme.Light); SendResponse(Command.Ok); break; case Command.Dark: Logger.Info("signal received: force dark theme"); rtc.ForcedTheme = Theme.Dark; ThemeManager.SwitchTheme(Properties.Config, Theme.Dark); SendResponse(Command.Ok); break; case Command.NoForce: Logger.Info("signal received: resetting forced modes"); rtc.ForcedTheme = Theme.Undefined; ThemeManager.TimedSwitch(Properties); SendResponse(Command.Ok); break; default: Logger.Debug("unknown message received"); SendResponse(Command.Err); break; } }); }