public async Task <IActionResult> OnGetAsync() { if (User.Identity.IsAuthenticated) { var accessToken = await HttpContext.GetTokenAsync("access_token"); if (string.IsNullOrEmpty(accessToken)) { // We don't have an access token, sign out await HttpContext.SignOutAsync(); return(Redirect("/")); } // Test if access token is still valid else sign out and reload return(await DevicesApi.GetDevicesAsync(accessToken, new ResultHandler <List <Device> > { OnError = (errorType, errorMessage) => { HttpContext.SignOutAsync(); return Redirect("/"); } })); } return(Page()); }
public async Task <PagedDevices> GetAllDevicesAsync() { if (_allDevices == null) { _allDevices = await _devicesApi.GetDevicesAsync(); } return(_allDevices); }
public async Task <IActionResult> OnGetAsync() { if (!User.Identity.IsAuthenticated) { return(Redirect("/")); } var tokenData = await SmartMeOAuthConfiguration.GetAccessToken(HttpContext); return(await DevicesApi.GetDevicesAsync(tokenData.AccessToken, MeterSubType.MeterSubTypeHeat, new ResultHandler <List <Device> > { OnSuccess = (devices) => { Devices = devices; return Page(); }, OnError = DefaultErrorHandler.Handle })); }
public async Task <IActionResult> OnGetAsync() { if (!User.Identity.IsAuthenticated) { return(Redirect("/")); } var accessToken = await HttpContext.GetTokenAsync("access_token"); return(await DevicesApi.GetDevicesAsync(accessToken, new ResultHandler <List <Device> > { OnSuccess = (devices) => { Devices = devices; return Page(); }, OnError = DefaultErrorHandler.Handle })); }
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); } } }
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); } }
public static async Task ActionsAsync(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. Make sure you have at least one device in your smart-me account. sampleDevice = devices.First(); } // Get Actions { Helpers.WriteConsoleTitle("Get Actions"); List <SmartMeApiClient.Containers.Action> actions = await ActionsApi.GetActionsAsync(credentials, sampleDevice.Id); foreach (var action in actions) { Console.WriteLine($"Name: {action.Name}, ObisCode: {action.ObisCode}"); } } // Run Actions { Helpers.WriteConsoleTitle("Run Actions"); await ActionsApi.RunActionsAsync(credentials, new ActionToRun { DeviceID = sampleDevice.Id, Actions = new List <ActionToRunItem> { new ActionToRunItem(HexStringHelper.ByteArrayToString(ObisCodes.SmartMeSpecificPhaseSwitchL1), 1.0) } }); Console.WriteLine("Switch on"); Thread.Sleep(5000); await ActionsApi.RunActionsAsync(credentials, new ActionToRun { DeviceID = sampleDevice.Id, Actions = new List <ActionToRunItem> { new ActionToRunItem(HexStringHelper.ByteArrayToString(ObisCodes.SmartMeSpecificPhaseSwitchL1), 0.0) } }); Console.WriteLine("Switch off"); } }
public static async Task ValuesAsync(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. Make sure you have at least one device in your smart-me account. sampleDevice = devices.First(); } // Get Values { Helpers.WriteConsoleTitle("Get Values"); var deviceValues = await ValuesApi.GetDeviceValuesAsync(credentials, sampleDevice.Id); foreach (var deviceValue in deviceValues.Values) { Console.WriteLine($"Obis: {deviceValue.Obis}, Value: {deviceValue.Value}"); } } // Get Values In Past { Helpers.WriteConsoleTitle("Get Values In Past"); var deviceValues = await ValuesApi.GetDeviceValuesInPastAsync(credentials, sampleDevice.Id, new DateTime(2019, 8, 16, 12, 0, 0, DateTimeKind.Utc)); foreach (var deviceValue in deviceValues.Values) { Console.WriteLine($"Obis: {deviceValue.Obis}, Value: {deviceValue.Value}"); } } // Get Values In Past Multiple { Helpers.WriteConsoleTitle("Get Values In Past Multiple"); var multipleDeviceValues = await ValuesApi.GetDeviceValuesInPastMultipleAsync( credentials, sampleDevice.Id, new DateTime(2019, 8, 5, 10, 0, 0, DateTimeKind.Utc), new DateTime(2019, 8, 5, 12, 0, 0, DateTimeKind.Utc), 5); foreach (var deviceValues in multipleDeviceValues) { Console.WriteLine(deviceValues.Date); foreach (var deviceValue in deviceValues.Values) { Console.WriteLine($"Obis: {deviceValue.Obis}, Value: {deviceValue.Value}"); } } } // Get Meter Values { Helpers.WriteConsoleTitle("Get Meter Values"); var meterValues = await ValuesApi.GetMeterValuesAsync(credentials, sampleDevice.Id, new DateTime(2019, 8, 5, 12, 0, 0, DateTimeKind.Utc)); Console.WriteLine($"CounterReading: {meterValues.CounterReading}, CounterReadingImport: {meterValues.CounterReadingImport}"); } }