Exemplo n.º 1
0
        public async Task <List <NetatmoCurrentViewModel> > GetCurrentDataAsync()
        {
            try
            {
                var token = await m_Credentials.GetNetatmoOAuthAsync();

                if (string.IsNullOrEmpty(token.AccessToken) || string.IsNullOrEmpty(token.RefreshToken) || string.IsNullOrEmpty(Options.Value?.Netatmo?.DeviceId))
                {
                    Log.Error($"Failed to get current data from Netatmo API with accesstoken: '{token?.AccessToken}' and deviceid: '{Options.Value?.Netatmo?.DeviceId}'");
                    return(null);
                }

                var response = await m_HttpWrapper.GetAsync(
                    UrlBuilder.Netatmo.BuildStationUrl(token.AccessToken, Options.Value.Netatmo.DeviceId));

                var currentData = JsonConvert.DeserializeObject <NetatmoCurrentData>(response);

                return(NetatmoDtoMapper.MapToCurrent(currentData));
            }
            catch (Exception exception)
            {
                Log.Error(exception, $"Failed to get current data from Netatmo: '{exception.Message}'");
                throw;
            }
        }
Exemplo n.º 2
0
        public async Task <List <NetatmoSerieViewModel> > GetHistoricDataAsync(DateTime start, DateTime end)
        {
            try
            {
                var token = await m_Credentials.GetNetatmoOAuthAsync();

                if (string.IsNullOrEmpty(token.AccessToken) || string.IsNullOrEmpty(token.RefreshToken) || string.IsNullOrEmpty(Options.Value?.Netatmo?.DeviceId))
                {
                    Log.Error($"Failed to get historic data from Netatmo API with accesstoken: '{token?.AccessToken}' and deviceid: '{Options.Value?.Netatmo?.DeviceId}'");
                    return(null);
                }

                var unixStart = ((DateTimeOffset)start).ToUnixTimeSeconds();
                var unixEnd   = ((DateTimeOffset)end).ToUnixTimeSeconds();

                var responseIndoor = await m_HttpWrapper.GetAsync(
                    UrlBuilder.Netatmo.BuildMeasureUrl(token.AccessToken, Options.Value.Netatmo.DeviceId, null, unixStart, unixEnd));

                var historicDataIndoor = JsonConvert.DeserializeObject <NetatmoHistoricData>(responseIndoor);
                historicDataIndoor.Name = "Indoor";

                NetatmoHistoricData historicDataOutdoor = null;

                if (!string.IsNullOrEmpty(Options.Value?.Netatmo?.OutdoorModuleId))
                {
                    var responseOutdoor = await m_HttpWrapper.GetAsync(
                        UrlBuilder.Netatmo.BuildMeasureUrl(token.AccessToken, Options.Value.Netatmo.DeviceId, Options.Value.Netatmo.OutdoorModuleId, unixStart, unixEnd));

                    historicDataOutdoor      = JsonConvert.DeserializeObject <NetatmoHistoricData>(responseOutdoor);
                    historicDataOutdoor.Name = "Outdoor";
                }

                return(NetatmoDtoMapper.MapToSeries(new List <NetatmoHistoricData>()
                {
                    historicDataIndoor, historicDataOutdoor
                }));
            }
            catch (Exception exception)
            {
                Log.Error(exception, $"Failed to get historic data from Netatmo: '{exception.Message}'");
                throw;
            }
        }