public async Task UpdateSchedule() { Schedule schedule = new Schedule(); schedule.Name = "t1"; schedule.Description = "test"; schedule.Time = new HueDateTime { DateTime = DateTime.UtcNow.AddDays(1) }; schedule.Command = new ScheduleCommand(); schedule.Command.Body = new LightCommand(); schedule.Command.Body.Alert = Alert.Once; schedule.Command.Address = "/api/huelandspoor/lights/5/state"; schedule.Command.Method = "PUT"; var scheduleId = await _client.CreateScheduleAsync(schedule); //Update name schedule.Name = "t2"; await _client.UpdateScheduleAsync(scheduleId, schedule); //Get saved schedule var savedSchedule = await _client.GetScheduleAsync(scheduleId); //Check Assert.AreEqual(schedule.Name, savedSchedule.Name); }
/// <summary> /// Get all schedules /// </summary> /// <returns></returns> public async Task<List<Schedule>> GetSchedulesAsync() { CheckInitialized(); HttpClient client = new HttpClient(); string stringResult = await client.GetStringAsync(new Uri(String.Format("{0}schedules", ApiBase))).ConfigureAwait(false); List<Schedule> results = new List<Schedule>(); JToken token = JToken.Parse(stringResult); if (token.Type == JTokenType.Object) { //Each property is a light var jsonResult = (JObject)token; foreach (var prop in jsonResult.Properties()) { Schedule newSchedule = new Schedule(); newSchedule.Id = prop.Name; newSchedule.Name = prop.First["name"].ToString(); results.Add(newSchedule); } } return results; }
/// <summary> /// Create a schedule /// </summary> /// <param name="schedule"></param> /// <returns></returns> public async Task<string> CreateScheduleAsync(Schedule schedule) { if (schedule == null) throw new ArgumentNullException(nameof(schedule)); CheckInitialized(); //Set these fields to null var scheduleJson = JObject.FromObject(schedule, new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore }); scheduleJson.Remove("Id"); scheduleJson.Remove("created"); string command = JsonConvert.SerializeObject(scheduleJson, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); HttpClient client = await GetHttpClient().ConfigureAwait(false); //Create schedule var result = await client.PostAsync(new Uri(ApiBase + "schedules"), new JsonContent(command)).ConfigureAwait(false); var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false); DefaultHueResult[] scheduleResult = JsonConvert.DeserializeObject<DefaultHueResult[]>(jsonResult); if (scheduleResult.Length > 0 && scheduleResult[0].Success != null && !string.IsNullOrEmpty(scheduleResult[0].Success.Id)) { return scheduleResult[0].Success.Id; } return null; }
public async Task CreateScheduleSingle() { Schedule schedule = new Schedule(); schedule.Name = "t1"; schedule.Description = "test"; schedule.Time = DateTime.Now.AddDays(1); schedule.Command = new ScheduleCommand(); schedule.Command.Body = new LightCommand(); schedule.Command.Body.Alert = Alert.Once; schedule.Command.Address = "/api/huelandspoor/lights/5/state"; schedule.Command.Method = "PUT"; var result = await _client.CreateScheduleAsync(schedule); Assert.IsNotNull(result); }
public async Task CreateScheduleSingle() { Schedule schedule = new Schedule(); schedule.Name = "t1"; schedule.Description = "test"; schedule.LocalTime = new HueDateTime() { DateTime = DateTime.Now.AddDays(1) }; schedule.Command = new InternalBridgeCommand(); var commandBody = new LightCommand(); commandBody.Alert = Alert.Once; schedule.Command.Body = commandBody; schedule.Command.Address = "/api/huelandspoor/lights/5/state"; schedule.Command.Method = HttpMethod.Put; var result = await _client.CreateScheduleAsync(schedule); Assert.IsNotNull(result); }
/// <summary> /// Create a schedule /// </summary> /// <param name="schedule"></param> /// <returns></returns> public async Task<string> CreateScheduleAsync(Schedule schedule) { CheckInitialized(); string command = JsonConvert.SerializeObject(schedule, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); HttpClient client = HueClient.GetHttpClient(); //Create schedule var result = await client.PostAsync(new Uri(ApiBase + "schedules"), new StringContent(command)).ConfigureAwait(false); var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false); DefaultHueResult[] scheduleResult = JsonConvert.DeserializeObject<DefaultHueResult[]>(jsonResult); if (scheduleResult.Length > 0 && scheduleResult[0].Success != null && !string.IsNullOrEmpty(scheduleResult[0].Success.Id)) { return scheduleResult[0].Success.Id; } return null; }
public async Task CreateGenericScheduleSingle() { Schedule schedule = new Schedule(); schedule.Name = "t1"; schedule.Description = "test"; schedule.LocalTime = new HueDateTime() { DateTime = DateTime.Now.AddDays(1) }; schedule.Command = new InternalBridgeCommand(); dynamic dynamicCOmmand = new ExpandoObject(); dynamicCOmmand.status = 1; var jsonString = JsonConvert.SerializeObject(dynamicCOmmand); var commandBody = new GenericScheduleCommand(jsonString); schedule.Command.Body = commandBody; schedule.Command.Address = "/api/huelandspoor/lights/5/state"; schedule.Command.Method = HttpMethod.Put; var result = await _client.CreateScheduleAsync(schedule); Assert.IsNotNull(result); }
public async Task DeleteSchedule() { Schedule schedule = new Schedule(); schedule.Name = "t1"; schedule.Description = "test"; schedule.Time = new HueDateTime() { DateTime = DateTime.UtcNow.AddDays(1) }; schedule.Command = new ScheduleCommand(); schedule.Command.Body = new LightCommand(); schedule.Command.Body.Alert = Alert.Once; schedule.Command.Address = "/api/huelandspoor/lights/5/state"; schedule.Command.Method = "PUT"; var scheduleId = await _client.CreateScheduleAsync(schedule); //Delete await _client.DeleteScheduleAsync(scheduleId); }
/// <summary> /// Update a schedule /// </summary> /// <param name="id"></param> /// <param name="schedule"></param> /// <returns></returns> public async Task<HueResults> UpdateScheduleAsync(string id, Schedule schedule) { CheckInitialized(); string command = JsonConvert.SerializeObject(schedule, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); HttpClient client = HueClient.GetHttpClient(); //Create schedule var result = await client.PutAsync(new Uri(string.Format("{0}schedules/{1}", ApiBase, id)), new StringContent(command)).ConfigureAwait(false); var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false); return DeserializeDefaultHueResult(jsonResult); }
public async Task DontClearIdWhenUpdatingSchedule() { Schedule schedule = new Schedule(); schedule.Id = "1"; schedule.Name = "test"; await _client.UpdateScheduleAsync(schedule.Id, schedule); Assert.IsNotNull(schedule.Id); }
/// <summary> /// Update a schedule /// </summary> /// <param name="id"></param> /// <param name="schedule"></param> /// <returns></returns> public async Task<HueResults> UpdateScheduleAsync(string id, Schedule schedule) { if (id == null) throw new ArgumentNullException(nameof(id)); if (id.Trim() == String.Empty) throw new ArgumentException("id must not be empty", nameof(id)); if (schedule == null) throw new ArgumentNullException(nameof(schedule)); CheckInitialized(); //Set these fields to null var scheduleJson = JObject.FromObject(schedule, new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore }); scheduleJson.Remove("Id"); scheduleJson.Remove("created"); string command = JsonConvert.SerializeObject(scheduleJson, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); HttpClient client = await GetHttpClient().ConfigureAwait(false); //Update schedule var result = await client.PutAsync(new Uri(string.Format("{0}schedules/{1}", ApiBase, id)), new JsonContent(command)).ConfigureAwait(false); var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false); return DeserializeDefaultHueResult(jsonResult); }