public HttpResponseMessage Delete(HttpRequestMessage request) { var content = request.Content; var data = JsonConvert.DeserializeObject <Dictionary <string, string> >(content.ReadAsStringAsync().Result); XDocument msgToSend = XDocument.Parse(@"<?xml version=""1.0"" encoding=""UTF8""?> <SMARTPLUG id=""edimax""> <CMD id=""get""> <SCHEDULE/> </CMD> </SMARTPLUG>"); try { msgToSend = cm.SendMessage(msgToSend, Config.IPadress, Config.Username, Config.Password); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } msgToSend.Descendants("CMD").First().Attribute("id").Value = "setup"; var scheduleList = JsonConvert.DeserializeObject <List <Schedule> >(data["schedule"]); var currentValue = msgToSend.Descendants("Device.System.Power.Schedule." + scheduleList[0].DayOfWeek + ".List").First().Value; string decodedVal; try { decodedVal = DecodeHelper.EncodeSchedule(scheduleList); } catch { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } if (currentValue.Contains("-" + decodedVal)) { decodedVal = "-" + decodedVal; } else if (!currentValue.Contains(decodedVal)) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } msgToSend.Descendants("Device.System.Power.Schedule." + scheduleList[0].DayOfWeek + ".List").First().Value = currentValue.Replace(decodedVal, ""); UpdateSchedule(ref msgToSend); try { cm.SendMessage(msgToSend, Config.IPadress, Config.Username, Config.Password); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.GatewayTimeout, ex)); } return(Get()); }
public HttpResponseMessage Post(HttpRequestMessage request) { var content = request.Content; var data = JsonConvert.DeserializeObject <Dictionary <string, string> >(content.ReadAsStringAsync().Result); XDocument msgToSend = XDocument.Parse(@"<?xml version=""1.0"" encoding=""UTF8""?> <SMARTPLUG id=""edimax""> <CMD id=""get""> <SCHEDULE/> </CMD> </SMARTPLUG>"); try { msgToSend = cm.SendMessage(msgToSend, Config.IPadress, Config.Username, Config.Password); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.GatewayTimeout, ex)); } msgToSend.Descendants("CMD").First().Attribute("id").Value = "setup"; var overridden = false; var scheduleList = JsonConvert.DeserializeObject <List <Schedule> >(data["schedule"]); for (int i = 0; i <= 6; i++) { var list = scheduleList.Where(x => x.DayOfWeek == i).ToList(); var decodedListStr = DecodeHelper.EncodeSchedule(list); if (overridden) { msgToSend.Descendants("Device.System.Power.Schedule." + i + ".List").First().Value = decodedListStr; } else { var connector = (msgToSend.Descendants("Device.System.Power.Schedule." + i + ".List").First().Value.ToString().Length > 0) ? "-" : ""; msgToSend.Descendants("Device.System.Power.Schedule." + i + ".List").First().Value += connector + decodedListStr; } } UpdateSchedule(ref msgToSend); XDocument msgReceived; try { msgReceived = cm.SendMessage(msgToSend, Config.IPadress, Config.Username, Config.Password); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.GatewayTimeout, ex)); } return(Get()); }