public List <Ups> GetUpsFromServer(Server server, string password, string upsName) { // 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); ServerConnection serverConnection = new ServerConnection(server); serverConnection.ConnectAsync(CancellationToken.None).Wait(); try { Dictionary <string, string> listResponse = serverConnection .ListUpsAsync(CancellationToken.None).Result; List <Ups> upsList = new List <Ups>(); foreach (string thisUpsName in listResponse.Keys) { if (!string.IsNullOrWhiteSpace(upsName) && !string.Equals(thisUpsName, upsName)) { continue; } Dictionary <string, string> upsVars = serverConnection .ListVarsAsync(thisUpsName, CancellationToken.None).Result; var ups = Ups.Create(thisUpsName, server, upsVars); var upsContext = ServiceRuntime.Instance.UpsContexts.FirstOrDefault( ctx => ctx.Name == thisUpsName && ctx.ServerState.Name == server.Name); ups.IsManaged = upsContext != null; upsList.Add(ups); } return(upsList); } finally { serverConnection.Disconnect(); } }
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; } }