Example #1
0
 public Event(int eventId, string name, int actionDeviceId, ActionDeviceStates state, bool isEnabled)
 {
     EventId        = eventId;
     Name           = name;
     ActionDeviceId = actionDeviceId;
     State          = state;
     IsEnabled      = isEnabled;
 }
        public async Task <IActionResult> PutState(int actionDeviceId, [FromBody] ActionDeviceStates state)
        {
            ActionDevice actionDevice = await _repository.FindAsync(actionDeviceId);

            if (actionDevice == null)
            {
                return(NotFound(actionDeviceId));
            }
            dynamic parameters = JObject.Parse(actionDevice.Parameters);

            switch (actionDevice.Type)
            {
            case ActionDeviceTypes.iHome:

                string id = parameters.id;
                using (HttpClient client = new HttpClient())
                {
                    using HttpRequestMessage request = new HttpRequestMessage();
                    string json = "[{\"value\":\"" + state + "\"}]";
                    request.RequestUri = new Uri($"https://api.evrythng.com/thngs/{id}/properties/targetpowerstate1", UriKind.RelativeOrAbsolute);
                    request.Method     = HttpMethod.Put;
                    request.Content    = new StringContent(json);
                    request.Content.Headers.ContentType   = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
                    request.Content.Headers.ContentLength = json.Length;
                    request.Content.Headers.Add("Authorization", "z8RdfbcZfXDiIxxilVmiqg4fMr154fVoxKTor2V7CDWkOP7iiSjf7NPb9Qq8siLaXmxtJOmbeVbQw1GZ");
                    request.Content.Headers.Add("Accept", "application/json");
                    await client.SendAsync(request);
                }
                break;

            case ActionDeviceTypes.WiFi:
                string url = parameters.url + state;
                using (HttpClient client = new HttpClient())
                {
                    using HttpRequestMessage request = new HttpRequestMessage();
                    request.RequestUri = new Uri(url, UriKind.RelativeOrAbsolute);
                    request.Method     = HttpMethod.Get;
                    await client.SendAsync(request);
                }
                break;
            }

            return(NoContent());
        }