Beispiel #1
0
        private static async Task RegisterDeviceAsync(Connection.Devices.Device device)
        {
            var devReg = await Client.RegisterDeviceAsync(device, false);

            if (devReg.Success)
            {
                Console.WriteLine("Device was successfully registered");
            }
            else
            {
                switch (devReg.Message)
                {
                case RegisterDeviceMessage.Unknown:
                    Console.WriteLine("Unknown error");
                    break;

                case RegisterDeviceMessage.Unauthorized:
                    Console.WriteLine("Failed to authorize");
                    break;

                case RegisterDeviceMessage.DeviceExists:
                    Console.WriteLine("This device was already registered");
                    break;
                }
            }
        }
Beispiel #2
0
        private static async Task <WinofyClient> PrepareClientAsync(HomeConfig config)
        {
            var client = new WinofyClient();

            if (!string.IsNullOrEmpty(config.AuthorizationToken))
            {
                var result = await client.ValidateTokenAsync(config.Username, config.AuthorizationToken);

                if (!result.Valid)
                {
                    config.AuthorizationToken = null;
                }
            }

            while (string.IsNullOrEmpty(config.AuthorizationToken))
            {
                Console.Write("Username : "******"Password : "******"Failed to login. Please check your username and password");
                }
            }

            client.SetAuthorizationToken(config.AuthorizationToken);

            if (string.IsNullOrEmpty(config.Device?.Id))
            {
                Console.WriteLine("This device may not be registered");
                Console.WriteLine("Tell us the device name and description");
                Console.Write("Name : ");
                var name = Console.ReadLine();
                Console.Write("Description : ");
                var desc = Console.ReadLine();

                var device = new Device()
                {
                    Name         = name,
                    Description  = desc,
                    Notification = true
                };

                var result = await client.RegisterDeviceAsync(device, true);

                if (result.Success)
                {
                    config.Device = device;
                }
                else
                {
                    Console.WriteLine("Failed to register this device. See messages from the server");
                    Console.WriteLine(result.Message.ToString());
                }
            }

            using (var sw = new StreamWriter(HomeConfig.ConfigPath))
            {
                sw.Write(JsonConvert.SerializeObject(config, Formatting.Indented));
            }

            return(client);
        }