Ejemplo n.º 1
0
        /// <summary>
        /// Makes a call to WebAPI to turn light on/off
        /// </summary>
        /// <param name="lightId">Id of the light</param>
        /// <param name="turnOn">on/off flag</param>
        /// <returns></returns>
        public async Task SwitchLight(string lightId, bool turnOn)
        {
            // validate parameters
            if (string.IsNullOrEmpty(lightId))
            {
                throw new ArgumentNullException(nameof(lightId));
            }

            // Create the model to be transmitted
            var lightswichModel = new LightSwitchModel()
            {
                Id = lightId, IsOn = turnOn
            };
            // convert it to JSON
            var modelJson = JsonConvert.SerializeObject(lightswichModel);

            // add model to the reuest as content with the right content type
            HttpContent content = new StringContent(modelJson);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            content.Headers.Add("SensorHatDataAddKey", _authKey);

            // make that call
            HttpResponseMessage response = await Client.PutAsync(_baseAddress + "/api/lights/switch/", content);

            // if the call is not successful, throw exception
            if (!response.IsSuccessStatusCode)
            {
                throw new Exception(response.ReasonPhrase);
            }
        }
Ejemplo n.º 2
0
        public void LogUpdateOnLightswitch(LightSwitchModel lightSwitch)
        {
            bool addNewRecord = true;

            // Check if at least a predefined ammount of time passed since the last record with the same state was inserted.
            // There can be the case when the same state is being triggered for a light switch multiple times in a short time span.
            // We don't want to log all of those events.
            var latestLightSwitchRecord = _dbContext.LightSwitches
                                          .OrderByDescending(l => l.ExecutionDateTime)
                                          .FirstOrDefault();

            if (latestLightSwitchRecord != null && latestLightSwitchRecord.State == lightSwitch.State)
            {
                TimeSpan timeElapsedSinceLastRecord = DateTime.Now.Subtract(latestLightSwitchRecord.ExecutionDateTime);
                TimeSpan buffer = new TimeSpan(0, 0, _lightSwitchOptions.NoLogUpdateInterval);

                if (timeElapsedSinceLastRecord < buffer)
                {
                    addNewRecord = false;
                }
            }

            if (addNewRecord)
            {
                _dbContext.LightSwitches.Add(new LightSwitch
                {
                    LightSwitchId     = lightSwitch.Id,
                    ExecutionDateTime = DateTime.Now,
                    State             = lightSwitch.State
                });
                _dbContext.SaveChanges();
            }
        }
Ejemplo n.º 3
0
 public IActionResult Update(int id, [FromBody] LightSwitchModel lightSwitch)
 {
     try
     {
         lightSwitch.Id = id;
         _lightSwitchesService.UpdateDevice(lightSwitch);
         return(NoContent());
     }
     catch (KeyNotFoundException e)
     {
         return(BadRequest());
     }
 }
Ejemplo n.º 4
0
        public void Put([FromBody] LightSwitchModel lightSwitch)
        {
            var homeContext = GlobalHost.ConnectionManager.GetHubContext <SwitchHub>();

            homeContext.Clients.All.FlipSwitch(lightSwitch.Id, lightSwitch.IsOn);
        }