public async Task<Device[]> GetDevicesAsync(bool includeDeviceState = false, Enums.EndpointGetModes loadMode = EndpointGetModes.None, bool allowCache = true) { if (loadMode == EndpointGetModes.IncludeEndpointInfoOnly) throw new ArgumentException("IncludeEndpointInfoOnly is not supported because it doesn't contain information to correlate devices to endpoints. Please specify IncludeFullAttributes."); if (allowCache && _cachedDevicesList != null && _cachedLoadMode == loadMode) return _cachedDevicesList; var request = new RestRequest("devices", HttpMethod.Get); var devices = await _httpClient.ExecuteWithPolicyAsync<Device[]>(this, request); if (includeDeviceState) { // Do a seperate call to get all device statuses and correlate device state with devices // based on uuid var stateRequest = new RestRequest("devices/statuses", HttpMethod.Get); var deviceStatuses = await _httpClient.ExecuteWithPolicyAsync<Device[]>(this, stateRequest); foreach (var device in devices) { var deviceState = deviceStatuses.FirstOrDefault(d => d.Uuid.Equals(device.Uuid)); if (deviceState != null) device.State = deviceState.State; } } if (loadMode == EndpointGetModes.IncludeFullAttributes || loadMode == EndpointGetModes.IncludeFullAttributesWithValues) { // Do seperate calls to get endpoint, endpoint attributes and maybe even attribute values // and correlate them to devices var endpoints = await GetEndpointsAsync(loadMode, allowCache); foreach (var device in devices) { var deviceWithEndpoints = endpoints.Where( e => e.Attributes != null && e.Attributes.Any(a => a.Device != null && a.Device.Uuid.Equals(device.Uuid))).ToArray(); if (deviceWithEndpoints.Length > 0) { if (device.Endpoints == null) device.Endpoints = new List<Endpoint>(); device.Endpoints.AddRange(deviceWithEndpoints); } } } if (allowCache) { _cachedDevicesList = devices; _cachedLoadMode = loadMode; } return devices; }
/// <summary> /// Finds an endpoint by name. Will call GetEndpointsAsync if no endpoints are loaded yet. /// Will use cached data whenever possible. /// </summary> /// <param name="endpointName">Endpoint name</param> /// <returns>An Endpoint instance</returns> public async Task <Endpoint> GetEndpointAsync(string endpointName, Enums.EndpointGetModes loadMode = Enums.EndpointGetModes.IncludeEndpointInfoOnly) { var endpoints = await GetEndpointsAsync(loadMode); var endpoint = endpoints.First(e => e.Name == endpointName); return(endpoint); }
public async Task <Device[]> GetDevicesAsync(bool includeDeviceState = false, Enums.EndpointGetModes loadMode = EndpointGetModes.None, bool allowCache = true) { if (loadMode == EndpointGetModes.IncludeEndpointInfoOnly) { throw new ArgumentException("IncludeEndpointInfoOnly is not supported because it doesn't contain information to correlate devices to endpoints. Please specify IncludeFullAttributes."); } if (allowCache && _cachedDevicesList != null && _cachedLoadMode == loadMode) { return(_cachedDevicesList); } var request = new RestRequest("devices", HttpMethod.Get); var devices = await _httpClient.ExecuteWithPolicyAsync <Device[]>(this, request); if (includeDeviceState) { // Do a seperate call to get all device statuses and correlate device state with devices // based on uuid var stateRequest = new RestRequest("devices/statuses", HttpMethod.Get); var deviceStatuses = await _httpClient.ExecuteWithPolicyAsync <Device[]>(this, stateRequest); foreach (var device in devices) { var deviceState = deviceStatuses.FirstOrDefault(d => d.Uuid.Equals(device.Uuid)); if (deviceState != null) { device.State = deviceState.State; } } } if (loadMode == EndpointGetModes.IncludeFullAttributes || loadMode == EndpointGetModes.IncludeFullAttributesWithValues) { // Do seperate calls to get endpoint, endpoint attributes and maybe even attribute values // and correlate them to devices var endpoints = await GetEndpointsAsync(loadMode, allowCache); foreach (var device in devices) { var deviceWithEndpoints = endpoints.Where( e => e.Attributes != null && e.Attributes.Any(a => a.Device != null && a.Device.Uuid.Equals(device.Uuid))).ToArray(); if (deviceWithEndpoints.Length > 0) { if (device.Endpoints == null) { device.Endpoints = new List <Endpoint>(); } device.Endpoints.AddRange(deviceWithEndpoints); } } } if (allowCache) { _cachedDevicesList = devices; _cachedLoadMode = loadMode; } return(devices); }