Exemplo n.º 1
0
        public ActionResult Index()
        {
            UACLModel uaclModel = new UACLModel
            {
                StatusMessage = "",
                InstanceUrl   = "https://uacloudlibrary.opcfoundation.org"
            };

            if (BrowserController._nodeSetFilenames.Count > 0)
            {
                ViewBag.Nodesetfile = new SelectList(BrowserController._nodeSetFilenames, BrowserController._nodeSetFilenames[0]);
            }
            else
            {
                ViewBag.Nodesetfile = new SelectList(BrowserController._nodeSetFilenames);
            }

            return(View("Index", uaclModel));
        }
Exemplo n.º 2
0
        public ActionResult Upload(
            string instanceUrl,
            string clientId,
            string secret,
            string nodesettitle,
            string nodesetfile,
            string license,
            string copyright,
            string description,
            string addressspacename,
            string addressspacedescription,
            string addressspaceiconurl,
            string documentationurl,
            string iconurl,
            string licenseurl,
            string keywords,
            string purchasinginfo,
            string releasenotes,
            string testspecification,
            string locales,
            string orgname,
            string orgdescription,
            string orglogo,
            string orgcontact,
            string orgwebsite,
            bool overwrite)
        {
            UACLModel uaclModel = new UACLModel
            {
                InstanceUrl = instanceUrl,
                ClientId    = clientId,
                Secret      = secret,
            };

            try
            {
                if (BrowserController._nodeSetFilenames.Count < 1)
                {
                    throw new Exception("No nodeset file is currently loaded!");
                }

                // call the UA Cloud Library REST endpoint for info model upload
                if (string.IsNullOrWhiteSpace(instanceUrl) || !Uri.IsWellFormedUriString(instanceUrl.Trim(), UriKind.Absolute))
                {
                    throw new ArgumentException("Invalid UA Cloud Library instance Url entered!");
                }

                if (string.IsNullOrWhiteSpace(clientId))
                {
                    throw new ArgumentException("Invalid UA Cloud Library username entered!");
                }

                if (string.IsNullOrWhiteSpace(secret))
                {
                    throw new ArgumentException("Invalid UA Cloud Library password entered!");
                }

                AddressSpace uaAddressSpace = new AddressSpace();

                switch (license)
                {
                case "MIT": uaAddressSpace.License = License.MIT;
                    break;

                case "ApacheLicense20": uaAddressSpace.License = License.ApacheLicense20;
                    break;

                case "Custom": uaAddressSpace.License = License.Custom;
                    break;

                default: throw new ArgumentException("Invalid license entered!");
                }

                if (!string.IsNullOrWhiteSpace(nodesettitle))
                {
                    uaAddressSpace.Title = nodesettitle;
                }
                else
                {
                    throw new ArgumentException("Invalid nodeset title entered!");
                }

                if (!string.IsNullOrWhiteSpace(copyright))
                {
                    uaAddressSpace.CopyrightText = copyright;
                }
                else
                {
                    throw new ArgumentException("Invalid copyright text entered!");
                }

                if (!string.IsNullOrWhiteSpace(description))
                {
                    uaAddressSpace.Description = description;
                }
                else
                {
                    throw new ArgumentException("Invalid description entered!");
                }

                if (!string.IsNullOrWhiteSpace(addressspacename))
                {
                    uaAddressSpace.Category.Name = addressspacename;
                }
                else
                {
                    throw new ArgumentException("Invalid address space name entered!");
                }

                if (!string.IsNullOrWhiteSpace(addressspacedescription))
                {
                    uaAddressSpace.Category.Description = addressspacedescription;
                }

                if (!string.IsNullOrWhiteSpace(addressspaceiconurl))
                {
                    uaAddressSpace.Category.IconUrl = new Uri(addressspaceiconurl);
                }

                if (!string.IsNullOrWhiteSpace(documentationurl))
                {
                    uaAddressSpace.DocumentationUrl = new Uri(documentationurl);
                }

                if (!string.IsNullOrWhiteSpace(iconurl))
                {
                    uaAddressSpace.IconUrl = new Uri(iconurl);
                }

                if (!string.IsNullOrWhiteSpace(licenseurl))
                {
                    uaAddressSpace.LicenseUrl = new Uri(licenseurl);
                }

                if (!string.IsNullOrWhiteSpace(keywords))
                {
                    uaAddressSpace.Keywords = keywords.Split(',');
                }

                if (!string.IsNullOrWhiteSpace(purchasinginfo))
                {
                    uaAddressSpace.PurchasingInformationUrl = new Uri(purchasinginfo);
                }

                if (!string.IsNullOrWhiteSpace(releasenotes))
                {
                    uaAddressSpace.ReleaseNotesUrl = new Uri(releasenotes);
                }

                if (!string.IsNullOrWhiteSpace(testspecification))
                {
                    uaAddressSpace.TestSpecificationUrl = new Uri(testspecification);
                }

                if (!string.IsNullOrWhiteSpace(locales))
                {
                    uaAddressSpace.SupportedLocales = locales.Split(',');
                }

                if (!string.IsNullOrWhiteSpace(orgname))
                {
                    uaAddressSpace.Contributor.Name = orgname;
                }
                else
                {
                    throw new ArgumentException("Invalid organisation name entered!");
                }

                if (!string.IsNullOrWhiteSpace(orgdescription))
                {
                    uaAddressSpace.Contributor.Description = orgdescription;
                }

                if (!string.IsNullOrWhiteSpace(orglogo))
                {
                    uaAddressSpace.Contributor.LogoUrl = new Uri(orglogo);
                }

                if (!string.IsNullOrWhiteSpace(orgcontact))
                {
                    uaAddressSpace.Contributor.ContactEmail = new MailAddress(orgcontact).Address;
                }

                if (!string.IsNullOrWhiteSpace(orgwebsite))
                {
                    uaAddressSpace.Contributor.Website = new Uri(orgwebsite);
                }

                uaAddressSpace.Nodeset.NodesetXml = System.IO.File.ReadAllText(nodesetfile);

                instanceUrl = instanceUrl.Trim();
                if (!instanceUrl.EndsWith('/'))
                {
                    instanceUrl += '/';
                }

                HttpClient webClient = new HttpClient
                {
                    BaseAddress = new Uri(instanceUrl)
                };

                webClient.DefaultRequestHeaders.Add("Authorization", "basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(clientId + ":" + secret)));

                string address = webClient.BaseAddress + "InfoModel/upload";
                if (overwrite)
                {
                    address += "?overwrite=true";
                }

                string body = JsonConvert.SerializeObject(uaAddressSpace);
                HttpResponseMessage response = webClient.Send(new HttpRequestMessage(HttpMethod.Put, address)
                {
                    Content = new StringContent(body, Encoding.UTF8, "application/json")
                });
                webClient.Dispose();

                uaclModel.StatusMessage = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

                if (BrowserController._nodeSetFilenames.Count > 0)
                {
                    ViewBag.Nodesetfile = new SelectList(BrowserController._nodeSetFilenames, BrowserController._nodeSetFilenames[0]);
                }
                else
                {
                    ViewBag.Nodesetfile = new SelectList(BrowserController._nodeSetFilenames);
                }
                return(View("Index", uaclModel));
            }
            catch (Exception ex)
            {
                if ((ex is WebException) && (((WebException)ex).Response != null))
                {
                    uaclModel.StatusMessage = new StreamReader(((WebException)ex).Response.GetResponseStream()).ReadToEnd();
                }
                else
                {
                    uaclModel.StatusMessage = ex.Message;
                }

                if (BrowserController._nodeSetFilenames.Count > 0)
                {
                    ViewBag.Nodesetfile = new SelectList(BrowserController._nodeSetFilenames, BrowserController._nodeSetFilenames[0]);
                }
                else
                {
                    ViewBag.Nodesetfile = new SelectList(BrowserController._nodeSetFilenames);
                }
                return(View("Index", uaclModel));
            }
        }