public async Task <GetStatusResult> GetStructureAndDeviceStatusAsync(Structure structure) { var structureResult = await GetStructureStatusAsync(structure); if (structureResult.Exception != null) { return(structureResult); } foreach (var thermostat in structure.Thermostats) { GetThermostatStatusResult sharedResult = await GetSharedThermostatPropertiesAsync(thermostat); if (sharedResult.Exception != null) { return(new GetStatusResult(sharedResult.Error, sharedResult.Exception)); } GetThermostatStatusResult thermostatResult = await GetDeviceThermostatPropertiesAsync(thermostat, sharedResult); if (thermostatResult.Exception != null) { return(new GetStatusResult(thermostatResult.Error, thermostatResult.Exception)); } structureResult.Structures.ElementAt(0).Thermostats.Add(sharedResult.Thermostat); } return(structureResult); }
public void ShouldSubscribeToThermostatStatusUpdatesWhenCreated() { var expectedResult = new GetThermostatStatusResult(12.3d, 32.1d, true, true); _statusProvider.FireThermostatStatusUpdated(expectedResult); Assert.AreEqual(expectedResult.TargetTemperature, _viewModel.TargetTemperature); Assert.AreEqual(expectedResult.CurrentTemperature, _viewModel.CurrentTemperature); Assert.AreEqual(expectedResult.IsHeating, _viewModel.IsHeating); Assert.AreEqual(expectedResult.IsCooling, _viewModel.IsCooling); }
public async Task <GetThermostatStatusResult> GetThermostatStatusAsync(Thermostat thermostat) { if (_sessionProvider.IsSessionExpired) { return(new GetThermostatStatusResult(WebServiceError.SessionTokenExpired, new SessionExpiredException())); } GetThermostatStatusResult result = await GetSharedThermostatPropertiesAsync(thermostat); if (result.Exception == null) { result = await GetDeviceThermostatPropertiesAsync(thermostat, result); } return(result); }
public void FireThermostatStatusUpdated(GetThermostatStatusResult result) { if(ThermostatStatusUpdated != null) ThermostatStatusUpdated(this, new ThermostatStatusEventArgs(result)); }
public void CacheThermostatStatus(GetThermostatStatusResult thermostatStatus) { }
private async Task <GetThermostatStatusResult> GetDeviceThermostatPropertiesAsync(Thermostat thermostat, GetThermostatStatusResult result) { string url = string.Format("{0}/v2/subscribe", _sessionProvider.TransportUrl); IWebRequest request = GetPostJsonRequest(url); SetAuthorizationHeaderOnRequest(request, _sessionProvider.AccessToken); SetNestHeadersOnRequest(request, _sessionProvider.UserId); string requestString = string.Format("{{\"keys\":[{{\"key\":\"device.{0}\"}}]}}", thermostat.ID); await request.SetRequestStringAsync(requestString); Exception exception; try { IWebResponse response = await request.GetResponseAsync(); string strContent = await response.GetResponseStringAsync(); result.Thermostat.FanMode = _deserializer.ParseFanModeFromDeviceSubscribeResult(strContent); result.Thermostat.IsLeafOn = _deserializer.ParseLeafFromDeviceSubscribeResult(strContent); return(result); } catch (Exception ex) { exception = ex; } var error = await _deserializer.ParseWebServiceErrorAsync(exception); return(new GetThermostatStatusResult(error, exception)); }
private async Task<GetThermostatStatusResult> GetDeviceThermostatPropertiesAsync(Thermostat thermostat, GetThermostatStatusResult result) { string url = string.Format("{0}/v2/subscribe", _sessionProvider.TransportUrl); IWebRequest request = GetPostJsonRequest(url); SetAuthorizationHeaderOnRequest(request, _sessionProvider.AccessToken); SetNestHeadersOnRequest(request, _sessionProvider.UserId); string requestString = string.Format("{{\"keys\":[{{\"key\":\"device.{0}\"}}]}}", thermostat.ID); await request.SetRequestStringAsync(requestString); Exception exception; try { IWebResponse response = await request.GetResponseAsync(); string strContent = await response.GetResponseStringAsync(); result.Thermostat.FanMode = _deserializer.ParseFanModeFromDeviceSubscribeResult(strContent); result.Thermostat.IsLeafOn = _deserializer.ParseLeafFromDeviceSubscribeResult(strContent); return result; } catch (Exception ex) { exception = ex; } var error = await _deserializer.ParseWebServiceErrorAsync(exception); return new GetThermostatStatusResult(error, exception); }