Exemplo n.º 1
0
 public static DeviceHttpConnection GetInstance()
 {
     if (Instance == null)
     {
         Instance = new DeviceHttpConnection();
     }
     return(Instance);
 }
Exemplo n.º 2
0
        //member functions

        //send a call to this device's WLED HTTP API
        //API reference: http://apiref.wled.me
        public async Task <bool> SendAPICall(string call)
        {
            string url = "http://" + networkAddress;

            if (networkAddress.StartsWith("https://"))
            {
                url = networkAddress;
            }

            string response = await DeviceHttpConnection.GetInstance().Send_WLED_API_Call(url, call);

            if (response == null)
            {
                CurrentStatus = DeviceStatus.Unreachable;
                return(false);
            }

            if (response.Equals("err")) //404 or other non-success http status codes, indicates that target is not a WLED device
            {
                CurrentStatus = DeviceStatus.Error;
                return(false);
            }

            XmlApiResponse deviceResponse = XmlApiResponseParser.ParseApiResponse(response);

            if (deviceResponse == null) //could not parse XML API response
            {
                CurrentStatus = DeviceStatus.Error;
                return(false);
            }

            CurrentStatus = DeviceStatus.Default; //the received response was valid

            if (!NameIsCustom)
            {
                Name = deviceResponse.Name;
            }

            //only consider brightness if light is on and if it wasn't modified in the same call (prevents brightness slider "jumps")
            if (deviceResponse.Brightness > 0 && !call.Contains("A="))
            {
                brightnessReceived = deviceResponse.Brightness;
                BrightnessCurrent  = brightnessReceived;
                OnPropertyChanged("BrightnessCurrent"); //update slider binding
            }

            ColorCurrent = deviceResponse.LightColor;
            OnPropertyChanged("ColorCurrent");

            StateCurrent = deviceResponse.IsOn;
            return(true);
        }