public UpsContext( UpsConfiguration upsConfiguration, Server server) { this.UpsConfiguration = upsConfiguration; this.ServerState = server; this.Name = upsConfiguration.DeviceName; this.Connection = new ServerConnection(server); this.MetricDatabase = GetMetricDatabase(); }
public void UpdateUpsConfiguration(UpsConfiguration configuration) { var upsContext = ServiceRuntime.Instance.UpsContexts.FirstOrDefault( ctx => ctx.QualifiedName == configuration.GetQualifiedName()); if (upsContext == null) { throw new Exception("Failed to find a UPS with that name"); } // Only update properties that support changing like this upsContext.UpsConfiguration.EnableEmailNotification = configuration.EnableEmailNotification; upsContext.UpsConfiguration.EnablePowerShellNotification = configuration.EnablePowerShellNotification; ServiceRuntime.Instance.SaveConfiguration(); }
public Ups AddUps( Server server, string password, string upsName, int numPowerSupplies, bool monitorOnly, bool force) { // Update the password on the server object since it can't be passed as a SecureString // over the WCF channel server.Password = SecureStringExtensions.FromString(password); ServerConfiguration serverConfiguration = ServerConfiguration.CreateFromServer(server); serverConfiguration.ValidateProperties(); // Recreate the server object to ensure that it matches what would be created when it // is read from configuration at startup. server = Server.CreateFromConfiguration(serverConfiguration); UpsConfiguration upsConfiguration = new UpsConfiguration() { DeviceName = upsName, MonitorOnly = monitorOnly, NumPowerSupplies = numPowerSupplies, ServerConfiguration = serverConfiguration }; try { ServerConnection serverConnection = new ServerConnection(server); serverConnection.ConnectAsync(CancellationToken.None).Wait(); Dictionary <string, string> upsVars = serverConnection .ListVarsAsync(upsName, CancellationToken.None).Result; Ups ups = Ups.Create(upsName, server, upsVars); // Success. Add the configuration and save ServiceRuntime.Instance.Configuration.UpsConfigurations.Add( upsConfiguration); ServiceRuntime.Instance.SaveConfiguration(); // Add to the running instances UpsContext upsContext = new UpsContext(upsConfiguration, server) { State = ups }; ServiceRuntime.Instance.UpsContexts.Add(upsContext); #pragma warning disable 4014 Task.Run(() => { foreach (IManagementCallback callbackChannel in ServiceRuntime.Instance.ClientCallbackChannels) { try { callbackChannel.UpsDeviceAdded(ups); } catch (Exception e) { Logger.Error("Caught exception while updating device. " + e.Message); ServiceRuntime.Instance.ClientCallbackChannels.Remove(callbackChannel); break; } } }); #pragma warning restore 4014 return(ups); } catch (Exception exception) { Logger.Error("Exception while adding UPS device. {0}", exception.Message); if (force) { // Add the configuration and save ServiceRuntime.Instance.Configuration.UpsConfigurations.Add( upsConfiguration); ServiceRuntime.Instance.SaveConfiguration(); return(null); } throw; } }