コード例 #1
0
		public async Task<string> CreateSceneAsync(Scene scene)
		{
			CheckInitialized();

			if (scene == null)
				throw new ArgumentNullException(nameof(scene));
			if (scene.Lights == null)
				throw new ArgumentNullException(nameof(scene.Lights));
			if (scene.Name == null)
				throw new ArgumentNullException(nameof(scene.Name));

			//Filter non updatable properties
			scene.FilterNonUpdatableProperties();

			string jsonString = JsonConvert.SerializeObject(scene, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });

			HttpClient client = HueClient.GetHttpClient();
			var response = await client.PostAsync(new Uri(String.Format("{0}scenes", ApiBase)), new StringContent(jsonString)).ConfigureAwait(false);

			var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

			HueResults sceneResult = DeserializeDefaultHueResult(jsonResult);

			if (sceneResult.Count > 0 && sceneResult[0].Success != null && !string.IsNullOrEmpty(sceneResult[0].Success.Id))
			{
				return sceneResult[0].Success.Id;
			}

			if (sceneResult.HasErrors())
				throw new Exception(sceneResult.Errors.First().Error.Description);

			return null;

		}
コード例 #2
0
		/// <summary>
		/// UpdateSceneAsync
		/// </summary>
		/// <param name="id"></param>
		/// <param name="scene"></param>
		/// <returns></returns>
		public async Task<HueResults> UpdateSceneAsync(string id, Scene scene)
		{
			CheckInitialized();

			if (id == null)
				throw new ArgumentNullException("id");
			if (id.Trim() == String.Empty)
				throw new ArgumentException("id must not be empty", "id");
			if (scene == null)
				throw new ArgumentNullException(nameof(scene));

			//Set these fields to null
			scene.Id = null;
			scene.Recycle = null;
			scene.FilterNonUpdatableProperties();

			string jsonString = JsonConvert.SerializeObject(scene, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });

			HttpClient client = HueClient.GetHttpClient();
			var response = await client.PutAsync(new Uri(String.Format("{0}scenes/{1}", ApiBase, id)), new StringContent(jsonString)).ConfigureAwait(false);

			var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

			return DeserializeDefaultHueResult(jsonResult);

		}