Example #1
0
        protected virtual void OnThermostatSystemModeChanged(ThermostatSystemModeChangedEventArgs e)
        {
            ThermostatSystemModeChangedHandler handler = ThermostatSystemModeChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Example #2
0
        private static void DomoShare_ThermostatSystemModeChanged(object sender, ThermostatSystemModeChangedEventArgs e)
        {
            var tenantId = Guid.Parse(Configuration.GetSection("AppSettings").GetSection("ControllerId").Value);

            if (EventStore == null)
            {
                Trace.TraceInformation($"{DateTime.Now}: EventStore is null. Creating a new one");
                Console.WriteLine($"{DateTime.Now}: EventStore is null. Creating a new one");
                EventStore = EventStoreFactory.CreateEventStore();
            }
            SystemModeChangeCommand cmd = new SystemModeChangeCommand(e.TenantId, e.ThermostatId,
                                                                      e.ThermostatGuid, e.NewSystemMode);
            ChangeSystemMode changeSystemModeCommand = new ChangeSystemMode(EventStore, e.ThermostatId, e.ThermostatGuid,
                                                                            e.TenantId, e.NewSystemMode);
            var handler = new ThermostatCommandHandlers();

            handler.Handle(changeSystemModeCommand);
        }
Example #3
0
        public void SubscribeToNestDeviceDataUpdates(Guid tenantId, Dictionary <string, Guid> thermostats)
        {
            var    accessToken = string.Empty;
            string file;

            file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "accesstoken.txt");
            if (File.Exists(file))
            {
                accessToken = File.ReadAllText(file);
            }

            var firebaseClient = new Firebase("https://developer-api.nest.com/", accessToken);

            try
            {
                var response = firebaseClient.GetStreaming("devices",
                                                           changed: (s, e) =>
                {
                    try
                    {
                        if (e.Path.Contains("ambient_temperature_f"))
                        {
                            Trace.TraceInformation("Current temperature of Nest Thermostat has been updated to: {0}.", e.Data);
                            double newValue;
                            double.TryParse(e.Data, out newValue);
                            var thermostatId = e.Path.Replace($"/devices/thermostats/",
                                                              string.Empty).Replace($"/ambient_temperature_f", string.Empty);
                            Guid thermostatGuid;
                            thermostats.TryGetValue(thermostatId, out thermostatGuid);
                            ThermostatAmbientTemperatureChangedEventArgs ambientTemperatureChangedArgs =
                                new ThermostatAmbientTemperatureChangedEventArgs(tenantId, thermostatId, thermostatGuid, newValue);
                            OnThermostatAmbientTemperatureChanged(ambientTemperatureChangedArgs);
                        }
                        else if (e.Path.Contains("humidity"))
                        {
                            Trace.TraceInformation("Current humidity of Nest Thermostat has been updated to: {0}%.", e.Data);
                            double newValue;
                            double.TryParse(e.Data, out newValue);
                            var thermostatId = e.Path.Replace($"/devices/thermostats/",
                                                              string.Empty).Replace($"/humidity", string.Empty);
                            Guid thermostatGuid;
                            thermostats.TryGetValue(thermostatId, out thermostatGuid);
                            ThermostatHumidityChangedEventArgs humidityChangedArgs =
                                new ThermostatHumidityChangedEventArgs(tenantId, thermostatId, thermostatGuid, newValue);
                            OnThermostatHumidityChanged(humidityChangedArgs);
                        }
                        else if (e.Path.Contains("hvac_state"))
                        {
                            Trace.TraceInformation("Current state of Nest Thermostat has been updated to: {0}.", e.Data);
                            var thermostatId = e.Path.Replace($"/devices/thermostats/",
                                                              string.Empty).Replace($"/hvac_state", string.Empty);
                            Guid thermostatGuid;
                            thermostats.TryGetValue(thermostatId, out thermostatGuid);
                            ThermostatSystemStatusChangedEventArgs systemStatusChangedArgs =
                                new ThermostatSystemStatusChangedEventArgs(tenantId, thermostatId, thermostatGuid, e.Data);
                            OnThermostatSystemStatusChanged(systemStatusChangedArgs);
                        }
                        else if (e.Path.Contains("hvac_mode"))
                        {
                            Trace.TraceInformation("Current mode of Nest Thermostat has been updated to: {0}.", e.Data);
                            var thermostatId = e.Path.Replace($"/devices/thermostats/",
                                                              string.Empty).Replace($"/hvac_mode", string.Empty);
                            Guid thermostatGuid;
                            thermostats.TryGetValue(thermostatId, out thermostatGuid);
                            ThermostatSystemModeChangedEventArgs systemModeChangedArgs =
                                new ThermostatSystemModeChangedEventArgs(tenantId, thermostatId, thermostatGuid, e.Data);
                            OnThermostatSystemModeChanged(systemModeChangedArgs);
                        }
                        else if (e.Path.Contains("target_temperature_low_f"))
                        {
                            Trace.TraceInformation("Heat Setpoint of Nest Thermostat has been updated to: {0}.", e.Data);
                            double newValue;
                            double.TryParse(e.Data, out newValue);
                            var thermostatId = e.Path.Replace($"/devices/thermostats/", string.Empty).Replace($"/target_temperature_low_f", string.Empty);
                            Guid thermostatGuid;
                            thermostats.TryGetValue(thermostatId, out thermostatGuid);
                            ThermostatHeatSetpointChangedEventArgs heatSetpointChangedArgs =
                                new ThermostatHeatSetpointChangedEventArgs(tenantId, thermostatId, thermostatGuid, newValue);
                            OnThermostatHeatSetpointChanged(heatSetpointChangedArgs);
                        }
                        else if (e.Path.Contains("target_temperature_high_f"))
                        {
                            Trace.TraceInformation("Cool Setpoint of Nest Thermostat has been updated to: {0}.", e.Data);
                            double newValue;
                            double.TryParse(e.Data, out newValue);
                            var thermostatId = e.Path.Replace($"/devices/thermostats/", string.Empty).Replace($"/target_temperature_high_f", string.Empty);
                            Guid thermostatGuid;
                            thermostats.TryGetValue(thermostatId, out thermostatGuid);
                            ThermostatCoolSetpointChangedEventArgs coolSetpointChangedArgs =
                                new ThermostatCoolSetpointChangedEventArgs(tenantId, thermostatId, thermostatGuid, newValue);
                            OnThermostatCoolSetpointChanged(coolSetpointChangedArgs);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Exception while processing data from Nest: {ex}");
                        Trace.TraceError($"Exception while processing data from Nest: {ex}");
                    }
                });

                //Console.WriteLine("Change the current temperature of the Nest Thermostat in the Nest Developer Chrome Extension to see the real-time updates.");
                Trace.TraceInformation("Change the current temperature of the Nest Thermostat in the Nest Developer Chrome Extension to see the real-time updates.");
            }
            catch (Exception ex)
            {
                Trace.TraceError($"Error connectiong to REST Streaming: {ex}");
            }
        }