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

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

            return(await DevicesApi.AddDeviceAsync(accessToken, device, new ResultHandler <Device>
            {
                OnSuccess = (newDevice) =>
                {
                    return Redirect("/Devices/AddDeviceResult?id=" + newDevice.Id);
                },

                OnError = DefaultErrorHandler.Handle
            }));
        }
        public async Task <IActionResult> OnPostAsync(Device device)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Redirect("/"));
            }

            var tokenData = await SmartMeOAuthConfiguration.GetAccessToken(HttpContext);

            return(await DevicesApi.AddDeviceAsync(tokenData.AccessToken, device, new ResultHandler <Device>
            {
                OnSuccess = (newDevice) =>
                {
                    return Redirect("/Devices/AddDeviceResult?id=" + newDevice.Id);
                },

                OnError = DefaultErrorHandler.Handle
            }));
        }
Ejemplo n.º 3
0
        public static async Task DevicesAsync(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 how to fetch its details in various ways. Make sure you have at least
                // one device in your smart-me account.
                sampleDevice = devices.First();
            }

            // Get all devices with a certain energy type
            {
                Helpers.WriteConsoleTitle("Get all devices with energy type 'MeterTypeElectricity'");

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

                foreach (var device in devices)
                {
                    Console.WriteLine($"Id: {device.Id}, Name: {device.Name}, EnergyType: {Enum.GetName(typeof(MeterEnergyType), device.DeviceEnergyType)}");
                }
            }

            // Get all devices with a certain meter sub type
            {
                Helpers.WriteConsoleTitle("Get all devices with meter sub type 'MeterSubTypeHeat'");

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

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

                    if (device.MeterSubType != null)
                    {
                        Console.Write($"SubType: {Enum.GetName(typeof(MeterSubType), device.MeterSubType)}");
                    }

                    Console.WriteLine();
                }
            }

            // Get device by Id
            {
                Helpers.WriteConsoleTitle("Get device by Id");

                Device device = await DevicesApi.GetDeviceAsync(credentials, sampleDevice.Id);

                Console.WriteLine($"Name: {device.Name}, Id: {device.Id}");
            }

            // Get device by Serial
            {
                Helpers.WriteConsoleTitle("Get device by serial number");

                Device device = await DevicesApi.GetDeviceAsync(credentials, sampleDevice.Serial);

                Console.WriteLine($"Name: {device.Name}, Serial: {device.Serial}");
            }

            // Get additional device information
            {
                Helpers.WriteConsoleTitle("Get additional device information");

                var info = await DevicesApi.GetAdditionalDeviceInformationAsync(credentials, sampleDevice.Id);

                Console.WriteLine($"ID: {info.ID}, HardwareVersion: {info.HardwareVersion}, FirmwareVersion: {info.FirmwareVersion}, AdditionalMeterSerialNumber: {info.AdditionalMeterSerialNumber}");
            }

            // Add and update device
            {
                Helpers.WriteConsoleTitle("Add a new device");

                Device newDevice = await DevicesApi.AddDeviceAsync(credentials, new Device
                {
                    Name             = "NewDevice",
                    DeviceEnergyType = MeterEnergyType.MeterTypeElectricity,
                    ActivePower      = 0,
                    CounterReading   = 0,
                    Voltage          = 230,
                    VoltageL1        = 230,
                    Current          = 1.0,
                    PowerFactor      = 0,
                    PowerFactorL1    = 0,
                    Temperature      = 26
                });

                Helpers.WriteConsoleTitle("Update the name of a device");

                newDevice.Name = "UpdatedDevice";

                await DevicesApi.UpdateDeviceAsync(credentials, newDevice);

                Helpers.WriteConsoleTitle("Update CounterReadingImport and ActivePower of a device");

                for (int i = 0; i < 10; i++)
                {
                    newDevice.CounterReading = i + 1;
                    newDevice.ActivePower    = i * 1.1;

                    await DevicesApi.UpdateDeviceAsync(credentials, newDevice);

                    Thread.Sleep(1000);
                }
            }
        }