Ejemplo n.º 1
0
        public async Task <bool> Find(string deviceName)
        {
            // Find all the installed device types
            var response = await myConnection.GetAsync(myConnection.DeviceTypeList);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(false);
            }

            var htmlResult = await response.Content.ReadAsStringAsync();

            // From String
            var htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(htmlResult);

            var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//a");

            foreach (var node in htmlNodes)
            {
                if (node.Attributes["href"]?.Value.StartsWith("/ide/device/editor") == true)
                {
                    if (node.InnerText.Trim().Contains(deviceName))
                    {
                        Id = node.Attributes["href"].Value.Replace("/ide/device/editor/", "");
                        break;
                    }
                }
            }

            if (Id == null)
            {
                return(false);
            }

            // Get the resource list for the smart app
            response = await myConnection.GetAsync($"{myConnection.DeviceTypeResourceListUri.ToString()}?id={Id}");

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(false);
            }

            var jsonResult = response.Content.ReadAsStringAsync();
            var jobject    = JObject.Parse("{ root: " + jsonResult.Result + " }");

            ResourceId   = null;
            ResourceType = null;
            foreach (var jo in jobject.SelectToken("root").Children())
            {
                if (jo["type"].ToString() == "file")
                {
                    ResourceId   = (string)jo["id"];
                    ResourceType = (string)jo["li_attr"]["resource-type"];
                    break;
                }
            }
            if (ResourceId == null)
            {
                return(false);
            }

            // Get the code for this app
            List <KeyValuePair <string, string> > parms = new List <KeyValuePair <string, string> >();

            parms.Add(new KeyValuePair <string, string>("id", Id));
            parms.Add(new KeyValuePair <string, string>("resourceId", ResourceId));
            parms.Add(new KeyValuePair <string, string>("resourceType", ResourceType));
            var content = new System.Net.Http.FormUrlEncodedContent(parms);

            response = await myConnection.PostAsync(myConnection.DeviceTypeCodeForResourceUri, content);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(false);
            }

            // Here is the code!
            Code = await response.Content.ReadAsStringAsync();

            // Success
            return(true);
        }
Ejemplo n.º 2
0
        public async Task <bool> Find(string smartAppName)
        {
            // Find all the installed smart apps
            var response = await myConnection.GetAsync(myConnection.SmartAppInstallationsUri);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(false);
            }

            var htmlResult = await response.Content.ReadAsStringAsync();

            foreach (Match m in Regex.Matches(htmlResult, "\\<a href=\"/ide/app/editor/([^\"]+)\".*?\\>\\<img .+?\\>\\s*(.+?)\\s*:\\s*(.+?)\\</a\\>", RegexOptions.Multiline | RegexOptions.IgnoreCase))
            {
                if (m.Groups[3].Value == smartAppName)
                {
                    Id = m.Groups[1].Value;
                    break;
                }
            }
            if (Id == null)
            {
                return(false);
            }

            // Get the resource list for the smart app
            response = await myConnection.GetAsync($"{myConnection.SmartAppResourceListUri.ToString()}?id={Id}");

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(false);
            }

            var jsonResult = response.Content.ReadAsStringAsync();
            var jobject    = JObject.Parse("{ root: " + jsonResult.Result + " }");

            ResourceId   = null;
            ResourceType = null;
            foreach (var jo in jobject.SelectToken("root").Children())
            {
                if (jo["type"].ToString() == "file")
                {
                    ResourceId   = (string)jo["id"];
                    ResourceType = (string)jo["li_attr"]["resource-type"];
                    break;
                }
            }
            if (ResourceId == null)
            {
                return(false);
            }

            // Get the code for this app
            List <KeyValuePair <string, string> > parms = new List <KeyValuePair <string, string> >();

            parms.Add(new KeyValuePair <string, string>("id", Id));
            parms.Add(new KeyValuePair <string, string>("resourceId", ResourceId));
            parms.Add(new KeyValuePair <string, string>("resourceType", ResourceType));
            var content = new System.Net.Http.FormUrlEncodedContent(parms);

            response = await myConnection.PostAsync(myConnection.SmartAppCodeForResourceUri, content);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(false);
            }

            // Here is the code!
            Code = await response.Content.ReadAsStringAsync();

            // Success
            return(true);
        }