Exemplo n.º 1
0
        public HttpResponseMessage Get()
        {
            XDocument msgToSend = XDocument.Parse(@"<?xml version=""1.0"" encoding=""UTF8""?>
                                                   <SMARTPLUG id=""edimax"">
                                                      <CMD id=""get"">
                                                         <SCHEDULE/>
                                                      </CMD>
                                                   </SMARTPLUG>");
            XDocument msgReceived;

            try
            {
                msgReceived = cm.SendMessage(msgToSend, Config.IPadress, Config.Username, Config.Password);
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.GatewayTimeout, ex));
            }

            List <string> decodedSchedule = new List <string>();

            decodedSchedule.Add((string)msgReceived.Descendants("Device.System.Power.Schedule.0.List").First());
            decodedSchedule.Add((string)msgReceived.Descendants("Device.System.Power.Schedule.1.List").First());
            decodedSchedule.Add((string)msgReceived.Descendants("Device.System.Power.Schedule.2.List").First());
            decodedSchedule.Add((string)msgReceived.Descendants("Device.System.Power.Schedule.3.List").First());
            decodedSchedule.Add((string)msgReceived.Descendants("Device.System.Power.Schedule.4.List").First());
            decodedSchedule.Add((string)msgReceived.Descendants("Device.System.Power.Schedule.5.List").First());
            decodedSchedule.Add((string)msgReceived.Descendants("Device.System.Power.Schedule.6.List").First());

            List <Schedule> scheduleList = new List <Schedule>();

            for (int i = 0; i < decodedSchedule.Count; i++)
            {
                string[] entries = decodedSchedule[i].Split('-');
                foreach (string entry in entries)
                {
                    if (!String.IsNullOrEmpty(entry))
                    {
                        scheduleList.Add(DecodeHelper.DecodeString(i, entry));
                    }
                }
            }

            var json     = JsonConvert.SerializeObject(scheduleList, Newtonsoft.Json.Formatting.None);
            var response = Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(json, Encoding.UTF8, "application/json");

            return(response);
        }
Exemplo n.º 2
0
        private void UpdateSchedule(ref XDocument msg)
        {
            for (int i = 0; i <= 6; i++) // for each day of week
            {
                var value = msg.Descendants("Device.System.Power.Schedule." + i + ".List").First().Value;
                if (value.Length > 0)
                {
                    string[] encodedHours = value.Split('-');
                    var      scheduleList = new List <Schedule>();
                    foreach (var h in encodedHours)
                    {
                        if (!String.IsNullOrEmpty(h))
                        {
                            scheduleList.Add(DecodeHelper.DecodeString(i, h));
                        }
                    }

                    msg.Descendants("Device.System.Power.Schedule." + i).First().Value = HexConverter.PreparePowerSchedule(scheduleList);
                }
            }
        }