/// <summary>
        /// Heartbeat ping. Failure will result in the heartbeat being stopped, which will
        /// make any future calls throw an exception as the heartbeat indicator will be disabled.
        /// </summary>
        /// <returns>>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task RefreshAsync()
        {
            // Get current revision status
            var summaryRequest = new ThermostatSummaryRequest {
                Selection = new Selection {
                    SelectionType = "registered"
                }
            };
            var status = await _client.GetAsync <ThermostatSummaryRequest, ThermostatSummaryResponse>(summaryRequest);

            foreach (var thermostatRevision in status.RevisionList)
            {
                _log.LogInformation($"Got revision: {thermostatRevision}");
                var revisionStatus = new RevisionStatus(thermostatRevision);

                if (_revisionStatusCache[revisionStatus.ThermostatIdentifier].ThermostatRevision != revisionStatus.ThermostatRevision ||
                    _revisionStatusCache[revisionStatus.ThermostatIdentifier].RuntimeRevision != revisionStatus.RuntimeRevision)
                {
                    await RefreshThermostatAsync(revisionStatus);
                }

                // Cache last run values
                _revisionStatusCache[revisionStatus.ThermostatIdentifier] = revisionStatus;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get, cache, and publish initial states.
        /// </summary>
        /// <returns>An awaitable <see cref="Task"/>.</returns>
        private async Task GetInitialStatusAsync()
        {
            var summaryRequest = new ThermostatSummaryRequest {
                Selection = new Selection {
                    SelectionType = "registered"
                }
            };
            var summary = await _client.GetAsync <ThermostatSummaryRequest, ThermostatSummaryResponse>(summaryRequest)
                          .ConfigureAwait(false);

            // Set initial revision cache
            _revisionStatusCache.Clear();
            _thermostatStatus.Clear();
            foreach (var revision in summary.RevisionList)
            {
                _log.LogInformation($"Got revision: {revision}");
                var revisionStatus = new RevisionStatus(revision);

                RefreshThermostatAsync(revisionStatus);
                _revisionStatusCache.Add(revisionStatus.ThermostatIdentifier, revisionStatus);
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var appKey          = "";
            var accessToken     = "";
            var refreshToken    = "";
            var tokenExpiration = DateTime.Now;

            if (File.Exists(@"token.txt"))
            {
                Console.WriteLine("Reading cached tokens");
                var tokenText = File.ReadAllLines(@"token.txt");

                tokenExpiration = DateTime.Parse(tokenText[0]);
                accessToken     = tokenText[1];
                refreshToken    = tokenText[2];
            }
            else
            {
                Console.WriteLine("Getting new tokens");
                var pin = Client.GetPin(appKey).Result;

                Console.WriteLine("Pin: " + pin.EcobeePin);
                Console.WriteLine("You have " + pin.ExpiresIn + " minutes to enter this on the Ecobee site.");

                Console.ReadLine();

                var authToken = Client.GetAccessToken(appKey, pin.Code).Result;
                WriteTokenFile(authToken);

                accessToken     = authToken.AccessToken;
                refreshToken    = authToken.RefreshToken;
                tokenExpiration = DateTime.Now.AddSeconds(authToken.ExpiresIn);
            }

            Console.WriteLine("Access Token: " + accessToken);
            Console.WriteLine("Refresh Token: " + refreshToken);
            Console.WriteLine("Hold onto these");

            // Setup client
            var client = new Client(appKey, accessToken, refreshToken, tokenExpiration);

            client.AuthTokenUpdated += (o, e) =>
            {
                WriteTokenFile(e);
            };

            Console.WriteLine("Getting thermostat information");

            var request = new ThermostatSummaryRequest
            {
                Selection = new Protocol.Objects.Selection
                {
                    SelectionType = "registered"
                }
            };

            var response = client.Get <ThermostatSummaryRequest, ThermostatSummaryResponse>(request).Result;

            Console.WriteLine();
            Console.WriteLine(JsonSerializer <ThermostatSummaryResponse> .Serialize(response));

            Console.ReadLine();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var             appKey = "";
            StoredAuthToken storedAuthToken;

            if (File.Exists(@"token.txt"))
            {
                Console.WriteLine("Reading cached tokens");
                var tokenText = File.ReadAllLines(@"token.txt");

                storedAuthToken = new StoredAuthToken
                {
                    TokenExpiration = DateTime.Parse(tokenText[0]),
                    AccessToken     = tokenText[1],
                    RefreshToken    = tokenText[2]
                };
            }
            else
            {
                Console.WriteLine("Getting new tokens");
                var pin = Client.GetPinAsync(appKey).Result;

                Console.WriteLine("Pin: " + pin.EcobeePin);
                Console.WriteLine("You have " + pin.ExpiresIn + " minutes to enter this on the Ecobee site.");

                Console.ReadLine();

                storedAuthToken = Client.GetAccessTokenAsync(appKey, pin.Code).Result;
                WriteTokenFile(storedAuthToken);
            }

            Console.WriteLine("Access Token: " + storedAuthToken.AccessToken);
            Console.WriteLine("Refresh Token: " + storedAuthToken.RefreshToken);
            Console.WriteLine("Hold onto these");

            // Setup client
            var client = new Client(appKey, storedAuthToken, async(authToken, cancellationToken) => { WriteTokenFile(authToken); });

            // Get thermostat summary
            Console.WriteLine("Getting thermostat summary");

            var request = new ThermostatSummaryRequest
            {
                Selection = new Protocol.Objects.Selection
                {
                    SelectionType = "registered"
                }
            };

            var response = client.GetAsync <ThermostatSummaryRequest, ThermostatSummaryResponse>(request).Result;

            Console.WriteLine();
            Console.WriteLine(JsonSerializer <ThermostatSummaryResponse> .Serialize(response));

            // Set the HvacMode
            //Console.WriteLine("Setting thermostat HVAC mode");

            //var updateRequest = new ThermostatUpdateRequest
            //{
            //    Selection = new Protocol.Objects.Selection
            //    {
            //        SelectionType = "registered"
            //    },
            //    Thermostat = new { Settings = new { HvacMode = "auto" } }
            //};

            //var updateResponse = client.PostAsync<ThermostatUpdateRequest, Response>(updateRequest).Result;
            //Console.WriteLine();
            //Console.WriteLine(JsonSerializer<Response>.Serialize(updateResponse));

            // Get thermostat settings
            //Console.WriteLine("Getting thermostat information");

            //var theroRequest = new ThermostatRequest
            //{
            //    Selection = new Protocol.Objects.Selection
            //    {
            //        SelectionType = "registered",
            //        IncludeSettings = true
            //    }
            //};

            //var thermoResponse = client.GetAsync<ThermostatRequest, ThermostatResponse>(theroRequest).Result;
            //Console.WriteLine();
            //Console.WriteLine(JsonSerializer<ThermostatResponse>.Serialize(thermoResponse));

            // Set thermostat fan
            //Console.WriteLine("Setting thermostat fan hold");

            //var themroFanRequest = new ThermostatUpdateRequest
            //{
            //    Selection = new Protocol.Objects.Selection
            //    {
            //        SelectionType = "registered"
            //    },
            //    Functions = new List<Protocol.Objects.Function>
            //    {
            //        new Protocol.Functions.SetHoldFunction
            //        {
            //            Params = new Protocol.Functions.SetHoldParams
            //            {
            //                HoldType = "nextTransition",
            //                Fan = "on"
            //            }
            //        }
            //    }
            //};

            //var themroFanResponse = client.PostAsync<ThermostatUpdateRequest, Response>(themroFanRequest).Result;
            //Console.WriteLine();
            //Console.WriteLine(JsonSerializer<Response>.Serialize(themroFanResponse));

            Console.ReadLine();
        }
Ejemplo n.º 5
0
        private static async Task Main()
        {
            // Setup client
            var appKey = "";
            var client = new Client(appKey, ReadTokenFileAsync, WriteTokenFileAsync);

            if (!File.Exists(@"token.txt"))
            {
                Console.WriteLine("Getting new tokens");
                var pin = await client.GetPinAsync();

                Console.WriteLine("Pin: " + pin.EcobeePin);
                Console.WriteLine("You have " + pin.ExpiresIn + " minutes to enter this on the Ecobee site and hit enter.");

                Console.ReadLine();

                await client.GetAccessTokenAsync(pin.Code);
            }
            else
            {
                Console.WriteLine("Loading existing tokens");
                var storedAuthToken = await ReadTokenFileAsync();
            }

            Console.WriteLine("Access Token: " + _currentAuthToken.AccessToken);
            Console.WriteLine("Refresh Token: " + _currentAuthToken.RefreshToken);
            Console.WriteLine("Hold onto these");

            // Get thermostat summary
            Console.WriteLine("Getting thermostat summary");

            var request = new ThermostatSummaryRequest
            {
                Selection = new Protocol.Objects.Selection
                {
                    SelectionType = "registered"
                }
            };

            var response = await client.GetAsync <ThermostatSummaryRequest, ThermostatSummaryResponse>(request);

            Console.WriteLine();
            Console.WriteLine(JsonSerializer <ThermostatSummaryResponse> .Serialize(response));

            // Set the HvacMode
            //Console.WriteLine("Setting thermostat HVAC mode");

            //var updateRequest = new ThermostatUpdateRequest
            //{
            //    Selection = new Protocol.Objects.Selection
            //    {
            //        SelectionType = "registered"
            //    },
            //    Thermostat = new { Settings = new { HvacMode = "auto" } }
            //};

            //var updateResponse = await client.PostAsync<ThermostatUpdateRequest, Response>(updateRequest);
            //Console.WriteLine();
            //Console.WriteLine(JsonSerializer<Response>.Serialize(updateResponse));

            // Get thermostat settings
            //Console.WriteLine("Getting thermostat information");

            //var theroRequest = new ThermostatRequest
            //{
            //    Selection = new Protocol.Objects.Selection
            //    {
            //        SelectionType = "registered",
            //        IncludeSettings = true
            //    }
            //};

            //var thermoResponse = await client.GetAsync<ThermostatRequest, ThermostatResponse>(theroRequest);
            //Console.WriteLine();
            //Console.WriteLine(JsonSerializer<ThermostatResponse>.Serialize(thermoResponse));

            // Set thermostat fan
            //Console.WriteLine("Setting thermostat fan hold");

            //var themroFanRequest = new ThermostatUpdateRequest
            //{
            //    Selection = new Protocol.Objects.Selection
            //    {
            //        SelectionType = "registered"
            //    },
            //    Functions = new List<Protocol.Objects.Function>
            //    {
            //        new Protocol.Functions.SetHoldFunction
            //        {
            //            Params = new Protocol.Functions.SetHoldParams
            //            {
            //                HoldType = "nextTransition",
            //                Fan = "on"
            //            }
            //        }
            //    }
            //};

            //var themroFanResponse = await client.PostAsync<ThermostatUpdateRequest, Response>(themroFanRequest);
            //Console.WriteLine();
            //Console.WriteLine(JsonSerializer<Response>.Serialize(themroFanResponse));

            Console.ReadLine();
        }