Пример #1
0
        public static WingnutConfiguration CreateBasicConfiguration(int port)
        {
            var config = new WingnutConfiguration
            {
                ServiceConfiguration  = WingnutServiceConfiguration.CreateDefault(),
                ShutdownConfiguration = ShutdownConfiguration.CreateDefault()
            };

            config.UpsConfigurations.Add(
                new UpsConfiguration
            {
                ServerConfiguration = new ServerConfiguration
                {
                    Address  = IPAddress.Loopback.ToString(),
                    Port     = port,
                    Username = "******",
                    Password = SecureStringExtensions.FromString("testpass"),
                    UseSSL   = SSLUsage.Optional,
                    PreferredAddressFamily = AddressFamily.InterNetwork,
                },
                NumPowerSupplies = 1,
                DeviceName       = "testups"
            });

            return(config);
        }
Пример #2
0
        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();
            }
        }
Пример #3
0
        private async Task AuthorizeAccount()
        {
            HttpRequestMessage request = CreateRequestMessage(
                HttpMethod.Get,
                Constants.DefaultApiUrl + Constants.ApiAuthorizeAccountUrl);

            request.Headers.Authorization = new AuthenticationHeaderValue(
                "Basic",
                Convert.ToBase64String(
                    Encoding.UTF8.GetBytes(
                        this.accountId + ":" + this.applicationKey.GetDecrytped())));

            LogApiCallCounter(request);

            using (HttpResponseMessage response = await this.httpClient.SendAsync(request).ConfigureAwait(false))
            {
                await ThrowIfFatalResponse(response).ConfigureAwait(false);

                JObject responseObject =
                    await response.Content.ReadAsJObjectAsync().ConfigureAwait(false);

                this.connectionInfo?.Dispose();

                this.connectionInfo = new BackblazeConnectionInfo
                {
                    AuthorizationToken = SecureStringExtensions.FromString(
                        responseObject.Value <string>("authorizationToken")),
                    ApiUrl                  = responseObject.Value <string>("apiUrl"),
                    DownloadUrl             = responseObject.Value <string>("downloadUrl"),
                    RecommendedPartSize     = responseObject.Value <int>("recommendedPartSize"),
                    AbsoluteMinimumPartSize = responseObject.Value <int>("absoluteMinimumPartSize"),
                    WhenAcquired            = DateTime.UtcNow,
                };

                this.ConnectionInfoChanged?.Invoke(
                    this,
                    new ConnectionInfoChangedEventArgs
                {
                    AccountId      = this.accountId,
                    ConnectionInfo = this.connectionInfo
                });
            }
        }
Пример #4
0
        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;
            }
        }