protected void Application_Error(object sender, EventArgs e)
        {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            Exception ex = context.Server.GetLastError();
            string url = context.Request.Url.PathAndQuery;
            string additionalInfo = context.Request.ServerVariables.ToString();

            ThermostatMonitorLib.Error error = new ThermostatMonitorLib.Error();
            error.ErrorMessage = ex.ToString() + "\n\n" + additionalInfo;
            error.Url = url;
            error.LogDate = DateTime.Now;
            //error.UserId = 0;
            ThermostatMonitorLib.Error.SaveError(error);
        }
        protected void Application_Error(object sender, EventArgs e)
        {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            Exception ex             = context.Server.GetLastError();
            string    url            = context.Request.Url.PathAndQuery;
            string    additionalInfo = context.Request.ServerVariables.ToString();

            ThermostatMonitorLib.Error error = new ThermostatMonitorLib.Error();
            error.ErrorMessage = ex.ToString() + "\n\n" + additionalInfo;
            error.Url          = url;
            error.LogDate      = DateTime.Now;
            //error.UserId = 0;
            ThermostatMonitorLib.Error.SaveError(error);
        }
Example #3
0
        private void CheckIn(ThermostatMonitorLib.Location location)
        {
            StreamReader reader    = new StreamReader(Request.InputStream, Request.ContentEncoding);
            string       jsonInput = reader.ReadToEnd();

            reader.Close();
            reader.Dispose();
            CheckinRequest  request  = CheckinRequest.Load(location, jsonInput);
            CheckinResponse response = new CheckinResponse();

            //Update inside temperature and thermostat state
            foreach (Thermostat thermostat in request.Thermostats)
            {
                string     key = location.Id.ToString() + "_" + thermostat.IpAddress;
                Thermostat previousThermostat = new Thermostat();
                if (Thermostats.Cache.ContainsKey(key))
                {
                    previousThermostat = (Thermostat)Thermostats.Cache[key];
                }
                if (thermostat.Temperature != previousThermostat.Temperature)
                {
                    LogTemperatureChange(location.Id, thermostat, previousThermostat);
                }
                if (thermostat.State != previousThermostat.State)
                {
                    LogStateChange(location.Id, thermostat, previousThermostat);
                }
                Thermostats.Cache[key] = thermostat;
            }

            //Update outside temperature
            response.ZipCode = request.ZipCode;


            string cacheKey = "OutsideTemperature" + location.Id.ToString();

            if (Cache.Get(cacheKey) == null)
            {
                try
                {
                    response.OutsideTemperature = ThermostatMonitorLib.Weather.GetTemperature(location.OpenWeatherCityId);
                    Cache.Add(cacheKey, response.OutsideTemperature, null, DateTime.Now.AddMinutes(4).AddSeconds(30), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
                } catch {}
            }
            else
            {
                response.OutsideTemperature = Convert.ToInt32(Cache.Get(cacheKey));
            }


            int previousTemperature = -999;

            if (CheckinResponse.ConditionHash.ContainsKey(location.Id))
            {
                previousTemperature = Convert.ToInt32(CheckinResponse.ConditionHash[location.Id]);
            }

            if (response.OutsideTemperature != previousTemperature && response.OutsideTemperature != 0)
            {
                ThermostatMonitorLib.OutsideCondition.Log(location.Id, response.OutsideTemperature);
            }
            CheckinResponse.ConditionHash[location.Id] = response.OutsideTemperature;

            //Add any pending commands to the response
            response.ReplyCommands = Commands.Get(location.Id);
            Commands.Remove(location.Id);

            try
            {
                Thermostats.CleanCache();
            }
            catch (Exception ex)
            {
                ThermostatMonitorLib.Error error = new ThermostatMonitorLib.Error();
                error.ErrorMessage = ex.ToString();
                error.LogDate      = DateTime.Now;
                error.UserId       = 0;
                error.Url          = "/json/default.aspx";
                ThermostatMonitorLib.Error.SaveError(error);
            }

            Response.Write(response.GetJson());
            Response.End();
        }
        private void CheckIn(ThermostatMonitorLib.Location location)
        {
            StreamReader reader = new StreamReader(Request.InputStream, Request.ContentEncoding);
            string jsonInput = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            CheckinRequest request = CheckinRequest.Load(location, jsonInput);
            CheckinResponse response = new CheckinResponse();

            //Update inside temperature and thermostat state
            foreach (Thermostat thermostat in request.Thermostats)
            {
                string key=location.Id.ToString() + "_" + thermostat.IpAddress;
                Thermostat previousThermostat = new Thermostat();
                if (Thermostats.Cache.ContainsKey(key)) previousThermostat = (Thermostat)Thermostats.Cache[key];
                if (thermostat.Temperature != previousThermostat.Temperature) LogTemperatureChange(location.Id, thermostat, previousThermostat);
                if (thermostat.State != previousThermostat.State) LogStateChange(location.Id, thermostat, previousThermostat);
                Thermostats.Cache[key] = thermostat;
            }

            //Update outside temperature
            response.ZipCode = request.ZipCode;

            string cacheKey = "OutsideTemperature" + location.Id.ToString();
            if (Cache.Get(cacheKey)==null)
            {
                try
                {
                    response.OutsideTemperature = ThermostatMonitorLib.Weather.GetTemperature(location.OpenWeatherCityId);
                    Cache.Add(cacheKey, response.OutsideTemperature, null, DateTime.Now.AddMinutes(4).AddSeconds(30), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
                } catch{}
            } else {
                response.OutsideTemperature = Convert.ToInt32(Cache.Get(cacheKey));
            }

            int previousTemperature = -999;
            if (CheckinResponse.ConditionHash.ContainsKey(location.Id)) previousTemperature = Convert.ToInt32(CheckinResponse.ConditionHash[location.Id]);

            if (response.OutsideTemperature != previousTemperature  && response.OutsideTemperature!=0) ThermostatMonitorLib.OutsideCondition.Log(location.Id, response.OutsideTemperature);
            CheckinResponse.ConditionHash[location.Id] = response.OutsideTemperature;

            //Add any pending commands to the response
            response.ReplyCommands = Commands.Get(location.Id);
            Commands.Remove(location.Id);

            try
            {
                Thermostats.CleanCache();
            }
            catch (Exception ex)
            {
                ThermostatMonitorLib.Error error = new ThermostatMonitorLib.Error();
                error.ErrorMessage = ex.ToString();
                error.LogDate = DateTime.Now;
                error.UserId = 0;
                error.Url = "/json/default.aspx";
                ThermostatMonitorLib.Error.SaveError(error);
            }

            Response.Write(response.GetJson());
            Response.End();
        }