public async Task <IActionResult> OnPostAsync(SmartMeDeviceConfiguration config)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Redirect("/"));
            }

            var accessToken = await HttpContext.GetTokenAsync("access_token");

            return(await DevicesApi.SetSmartMeDeviceConfigurationAsync(accessToken, config, new ResultHandler <SmartMeDeviceConfiguration>
            {
                OnSuccess = (empty) =>
                {
                    // Trigger another GET to show the updated SmartMeDeviceConfiguration
                    return Redirect("/Devices/SetSmartMeDeviceConfiguration");
                },

                OnError = DefaultErrorHandler.Handle
            }));
        }
Ejemplo n.º 2
0
        public static async Task DeviceConfigurationAsync(UserPassword credentials)
        {
            // We will use this device to fetch its details later
            Device sampleDevice;

            // Get all devices
            {
                Helpers.WriteConsoleTitle("Get all devices");

                List <Device> devices = await DevicesApi.GetDevicesAsync(credentials);

                foreach (var device in devices)
                {
                    Console.WriteLine($"Id: {device.Id}, Name: {device.Name}");
                }

                // Store the first device to show fow to fetch its details in various ways. Make sure you have at least
                // one device in your smart-me account.
                sampleDevice = devices.First();
            }

            // Get smart-me Device Configuration
            {
                Helpers.WriteConsoleTitle("Get smart-me device configuration");

                var configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, sampleDevice.Id);

                Console.WriteLine($"Id: {configuration.Id}, UploadInterval: {configuration.UploadInterval}");
            }

            // Modify smart-me device configuration
            {
                Helpers.WriteConsoleTitle("Set smart-me device configuration");

                var configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, sampleDevice.Id);

                configuration.DevicePinCode = "1234";

                try
                {
                    await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error: {e.Message}");
                }

                Thread.Sleep(5000);

                configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, sampleDevice.Id);

                configuration.DevicePinCode = "7890";

                try
                {
                    await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error: {e.Message}");
                }

                Thread.Sleep(5000);
            }
        }