/// <summary>
        ///
        /// </summary>
        /// <param name="newDevice"></param>
        /// <param name="isEdit"></param>
        /// <returns></returns>
        public String CreateDeviceOnClick(MWDevice newDevice, bool isEdit)
        {
            string result = "";

            // Check if device Name is set
            if (newDevice.deviceName.Equals(""))
            {
                return("Error no device name set!");
            }

            // Check if vendor name is set
            if (newDevice.vendorName.Equals(""))
            {
                return("Error no vendor name set!");
            }
            if (newDevice.deviceName != null && newDevice.vendorName != null)
            {
                // create the device
                result = mWData.CreateDevice(newDevice, isEdit);
            }

            // update the device list
            if (isEdit)
            {
            }
            else
            {
                devices.Add(newDevice);
            }


            return(result);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="newDevice"></param>
        /// <param name="isEdit"></param>
        /// <returns></returns>
        public String CreateDeviceOnClick(MWDevice newDevice, bool isEdit)
        {
            // Check if device Name is set
            if (newDevice.deviceName.Equals(""))
            {
                return("Error no device name set!");
            }

            // Check if vendor name is set
            if (newDevice.vendorName.Equals(""))
            {
                return("Error no vendor name set!");
            }

            // create the device
            string result = mWData.CreateDevice(newDevice, isEdit);


            // update the device list
            if (isEdit)
            {
                ReloadObjects();
            }
            else
            {
                devices.Add(newDevice);
                GetStartGUI().updateDeviceDropdown(devices);
            }

            ChangeGui(MWController.MWGUIType.Start);
            return(result);
        }
        /// <summary>
        /// Read the all attributes in <paramref name="attributes"/> and write the values into <paramref name="mWInterface"/>
        /// </summary>
        /// <param name="mWInterface">the object to write the data into</param>
        /// <param name="attributes">the list of attributes</param>


        /// <summary>
        /// Read the all attributes in <paramref name="attributes"/> and write the values into <paramref name="device"/>
        /// </summary>
        /// <param name="device">the object to write the data into</param>
        /// <param name="attributes">the list of attributes</param>
        private void fillDeviceWithData(MWDevice device, AttributeSequence attributes)
        {
            // iterate over all atttributes
            foreach (AttributeType attribute in attributes)
            {
                // apply the value of the attribute to the correct interface parameter
                switch (attribute.Name)
                {
                }
            }
        }
Example #4
0
        /// <summary>
        /// Fill the input fields with data from the device
        /// </summary>
        /// <param name="device">The device with the data</param>
        internal void prefill(MWDevice device)
        {
            deviceTypeListBox.SelectedItem = device.deviceType;
            txtVendorId.Text       = device.vendorID.ToString();
            txtVendorName.Text     = device.vendorName;
            txtVendorHomepage.Text = device.vendorHomepage;
            txtDeviceId.Text       = device.deviceID.ToString();
            txtDeviceName.Text     = device.deviceName;

            txtDeviceFamily.Text = device.deviceFamily;
            txtProductName.Text  = device.productName;
            txtOrderNumber.Text  = device.orderNumber;
            productTxtBox.Text   = device.productText;
            txtIpProduction.Text = device.ipProtection;
            hwRelTxt.Text        = device.harwareRelease;
            swRelTxt.Text        = device.softwareRelease;

            if (!Double.IsNaN(device.minTemperature))
            {
                txtMinTemp.Text = device.minTemperature.ToString();
            }
            else
            {
                txtMinTemp.Text = "";
            }

            if (!Double.IsNaN(device.maxTemperature))
            {
                txtMaxTemp.Text = device.maxTemperature.ToString();
            }
            else
            {
                txtMaxTemp.Text = "";
            }

            createDeviceBtn.Text  = "Update Device";
            txtDeviceName.Enabled = false;
            txtVendorName.Enabled = false;

            vendorPicturePath = device.vendorLogo;
            deviceIconPath    = device.deviceIcon;
            devicePicturePath = device.devicePicture;

            isEditing = true;
        }
Example #5
0
 /// <summary>
 /// assign the values of the <paramref name="device"/> to the corresponding attributes
 /// </summary>
 /// <param name="ie">the DeviceIdentification InternalElement</param>
 /// <param name="device">the device for this aml</param>
 private void setCAEXattribute(InternalElementType ie, MWDevice device)
 {
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("CommunicationTechnology"), device.deviceType);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("VendorId"), device.vendorID);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("VendorName"), device.vendorName);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("DeviceId"), device.deviceID);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("DeviceName"), device.deviceName);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("DeviceFamiliy"), device.deviceFamily);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("OrderNumber"), device.orderNumber);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("ProductName"), device.productName);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("ProductText"), device.productText);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("IPProtection"), device.ipProtection);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("OperatingTemperatureMin"), device.minTemperature);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("OperatingTemperatureMax"), device.maxTemperature);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("VendorHompage"), device.vendorHomepage);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("HardwareRelease"), device.harwareRelease);
     writeIfNotNull(ie.Attribute.GetCAEXAttribute("SoftwareRelease"), device.softwareRelease);
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="newDevice"></param>
        /// <param name="isEdit"></param>
        /// <returns></returns>
        public String CreateDeviceOnClick(MWDevice newDevice, bool isEdit)
        {
            string result = "";

            if (newDevice.deviceName != null && newDevice.vendorName != null)
            {
                // create the device
                result = mWData.CreateDevice(newDevice, isEdit);
            }

            // update the device list
            if (isEdit)
            {
            }
            else
            {
                devices.Add(newDevice);
            }


            return(result);
        }
        /// <summary>
        /// Create the AMLX File with the correct AML File and optional pictures
        /// </summary>
        /// <param name="device">The device which will be created</param>
        /// <param name="isEdit">true if an amlx file get update, false if a new file will be created</param>
        /// <returns></returns>
        public string CreateDevice(MWDevice device, bool isEdit)
        {
            CAEXDocument          document = null;
            AutomationMLContainer amlx     = null;

            // Init final .amlx Filepath
            //first of all create a folder on "Vendor Name"
            string vendorCompanyName         = device.vendorName;
            string vendorCompanyNameFilePath = "";



            string fileName = device.fileName;

            string amlFilePath = System.IO.Path.Combine(device.filepath, fileName + ".amlx");


            FileInfo file = new FileInfo(amlFilePath);



            // Create directory if it's not existing
            file.Directory.Create();


            // Init CAEX Document
            if (isEdit)
            {
                // Load the amlx file
                amlx = new AutomationMLContainer(amlFilePath, FileMode.Open);

                IEnumerable <PackagePart> rootParts = amlx.GetPartsByRelationShipType(AutomationMLContainer.RelationshipType.Root);

                // We expect the aml to only have one root part
                if (rootParts.First() != null)
                {
                    PackagePart part = rootParts.First();

                    // load the aml file as an CAEX document
                    document = CAEXDocument.LoadFromStream(part.GetStream());
                }
                else
                {
                    // the amlx contains no aml file
                    document = CAEXDocument.New_CAEXDocument();
                }
            }
            else
            {
                // create a new CAEX document
                document = CAEXDocument.New_CAEXDocument();
                try
                { amlx = new AutomationMLContainer(amlFilePath, FileMode.Create); } catch (Exception) {}
            }



            // Init the default Libs
            AutomationMLBaseRoleClassLibType.RoleClassLib(document);
            AutomationMLInterfaceClassLibType.InterfaceClassLib(document);

            var structureRoleFamilyType = AutomationMLBaseRoleClassLibType.RoleClassLib(document).Structure;


            SystemUnitFamilyType systemUnitClass = null;

            // Create the SystemUnitClass for our device
            if (!isEdit)
            {
                systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(device.deviceName);


                device.listWithURIConvertedToString = new List <AttachablesDataGridViewParameters>();
                foreach (AttachablesDataGridViewParameters eachparameter in device.dataGridAttachablesParametrsList)
                {
                    if (eachparameter.FilePath.Contains("https://") || eachparameter.FilePath.Contains("http://") || eachparameter.FilePath.Contains("www") || eachparameter.FilePath.Contains("WWW"))
                    {
                        interneturl(eachparameter.FilePath, eachparameter.ElementName.ToString(), "ExternalDataConnector", systemUnitClass);
                    }
                    else
                    {
                        Boolean myBool;
                        Boolean.TryParse(eachparameter.AddToFile, out myBool);

                        if (myBool == true)
                        {
                        }

                        Uri eachUri = null;
                        AttachablesDataGridViewParameters par = new AttachablesDataGridViewParameters();
                        eachUri         = createPictureRef(eachparameter.FilePath, eachparameter.ElementName.ToString(), "ExternalDataConnector", systemUnitClass);
                        par.ElementName = eachUri.ToString();
                        par.FilePath    = eachparameter.FilePath;

                        device.listWithURIConvertedToString.Add(par);
                    }
                }
                foreach (var pair in device.DictionaryForRoleClassofComponent)
                {
                    SupportedRoleClassType supportedRoleClass = null;


                    Match  numberfromElectricalConnectorType = Regex.Match(pair.Key.ToString(), @"\((\d+)\)");
                    string initialnumberbetweenparanthesisofElectricalConnectorType = numberfromElectricalConnectorType.Groups[1].Value;
                    // string stringinparanthesis = Regex.Match(pair.Key.ToString(), @"\{(\d+)\}").Groups[1].Value;

                    string supportedRoleClassFromDictionary = Regex.Replace(pair.Key.ToString(), @"\(.+?\)", "");
                    supportedRoleClassFromDictionary = Regex.Replace(supportedRoleClassFromDictionary, @"\{.+?\}", "");



                    var SRC = systemUnitClass.SupportedRoleClass.Append();



                    var attributesOfSystemUnitClass = systemUnitClass.Attribute;

                    foreach (var valueList in pair.Value)
                    {
                        foreach (var item in valueList)
                        {
                            if (item.AttributePath.Contains("/") || item.AttributePath.Contains("."))
                            {
                                int          count               = 2;
                                int          counter             = 0;
                                Stack <char> stack               = new Stack <char>();
                                string       searchAttributeName = item.AttributePath.Substring(0, item.AttributePath.Length - item.Name.Length);

                                foreach (var character in searchAttributeName.Reverse())
                                {
                                    if (!char.IsLetterOrDigit(character))
                                    {
                                        counter++;
                                        if (counter == count)
                                        {
                                            break;
                                        }
                                    }
                                    if (char.IsLetterOrDigit(character))
                                    {
                                        stack.Push(character);
                                    }
                                }

                                string finalAttributeName = new string(stack.ToArray());

                                foreach (var attribute in systemUnitClass.Attribute)
                                {
                                    if (attribute.Name == finalAttributeName)
                                    {
                                        var eachattribute = attribute.Attribute.Append(item.Name.ToString());
                                        eachattribute.Value             = item.Value;
                                        eachattribute.DefaultValue      = item.Default;
                                        eachattribute.Unit              = item.Unit;
                                        eachattribute.AttributeDataType = item.DataType;
                                        eachattribute.Description       = item.Description;
                                        eachattribute.Copyright         = item.CopyRight;

                                        eachattribute.ID = item.ID;

                                        foreach (var val in item.RefSemanticList.Elements)
                                        {
                                            var refsem = eachattribute.RefSemantic.Append();
                                            refsem.CorrespondingAttributePath = val.FirstAttribute.Value;
                                        }



                                        SRC.RefRoleClassPath = item.SupportesRoleClassType;
                                    }
                                    if (attribute.Attribute.Exists)
                                    {
                                        SearchForAttributesInsideAttributesofAutomationComponent(finalAttributeName, attribute, item, SRC);
                                    }
                                }
                            }
                            else
                            {
                                var eachattribute = attributesOfSystemUnitClass.Append(item.Name.ToString());
                                eachattribute.Value             = item.Value;
                                eachattribute.DefaultValue      = item.Default;
                                eachattribute.Unit              = item.Unit;
                                eachattribute.AttributeDataType = item.DataType;
                                eachattribute.Description       = item.Description;
                                eachattribute.Copyright         = item.CopyRight;

                                eachattribute.ID = item.ID;


                                foreach (var val in item.RefSemanticList.Elements)
                                {
                                    var refsem = eachattribute.RefSemantic.Append();
                                    refsem.CorrespondingAttributePath = val.FirstAttribute.Value;
                                }


                                SRC.RefRoleClassPath = item.SupportesRoleClassType;
                            }
                        }
                    }


                    foreach (var pairofList in device.DictionaryForExternalInterfacesUnderRoleClassofComponent)
                    {
                        Match  numberfromElectricalConnectorPins = Regex.Match(pairofList.Key.ToString(), @"\((\d+)\)");
                        string initialnumberbetweenparanthesisElectricalConnectorPins = numberfromElectricalConnectorPins.Groups[1].Value;

                        string electricalConnectorPinName = Regex.Replace(pairofList.Key.ToString(), @"\(.*?\)", "");
                        electricalConnectorPinName = Regex.Replace(electricalConnectorPinName, @"\{.*?\}", "");
                        electricalConnectorPinName = electricalConnectorPinName.Replace(supportedRoleClassFromDictionary, "");



                        /*if (initialnumberbetweenparanthesisofElectricalConnectorType == initialnumberbetweenparanthesisElectricalConnectorPins)
                         * {
                         *  supportedRoleClass.RoleReference = pairofList.Key.ToString();
                         *
                         *  systemUnitClass.SupportedRoleClass.Append(supportedRoleClass);
                         *  systemUnitClass.BaseClass.Name = supportedRoleClassFromDictionary;
                         *
                         *  var attributesOfSystemUnitClassattributes = systemUnitClass.Attribute;
                         *
                         *  foreach (var valueList in pairofList.Value)
                         *  {
                         *      foreach (var item in valueList)
                         *      {
                         *          var eachattribute = attributesOfSystemUnitClassattributes.Append(item.Name.ToString());
                         *          eachattribute.Value = item.Value;
                         *          eachattribute.DefaultValue = item.Default;
                         *          eachattribute.Unit = item.Unit;
                         *          //eachattribute.AttributeDataType =
                         *          eachattribute.Description = item.Description;
                         *          eachattribute.Copyright = item.CopyRight;
                         *
                         *          eachattribute.ID = item.ID;
                         *
                         *
                         *
                         *         // systemUnitClass.BaseClass.Name   = item.RefBaseClassPath;
                         *      }
                         *  }
                         * }*/
                    }
                }
            }
            else
            {
                // check if our format is given in the amlx file if not: create it
                bool foundSysClassLib = false;
                foreach (var sysclasslib in document.CAEXFile.SystemUnitClassLib)
                {
                    if (sysclasslib.Name.Equals("ComponentSystemUnitClassLib"))
                    {
                        bool foundSysClass = false;
                        foreach (var sysclass in sysclasslib.SystemUnitClass)
                        {
                            if (sysclass.Name.Equals(device.deviceName))
                            {
                                foundSysClass   = true;
                                systemUnitClass = sysclass;
                                break;
                            }
                        }
                        if (!foundSysClass)
                        {
                            systemUnitClass = sysclasslib.SystemUnitClass.Append(device.deviceName);
                        }
                        foundSysClassLib = true;
                    }
                }
                if (!foundSysClassLib)
                {
                    systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(device.deviceName);
                }
            }



            // Create the internalElement Electrical Interfaces

            if (device.vendorName != null)
            {
                InternalElementType  electricalInterface = null;
                RoleRequirementsType roleRequirements    = null;
                foreach (var internalElement in systemUnitClass.InternalElement)
                {
                    if (internalElement.Name.Equals("ElectricalInterfaces"))
                    {
                        electricalInterface = internalElement;
                        roleRequirements    = electricalInterface.RoleRequirements.Append();
                        roleRequirements.RefBaseRoleClassPath = structureRoleFamilyType.CAEXPath();
                        break;
                    }
                }
                if (electricalInterface == null)
                {
                    electricalInterface = systemUnitClass.InternalElement.Append("ElectricalInterfaces");
                }
                roleRequirements = electricalInterface.RoleRequirements.Append();

                roleRequirements.RefBaseRoleClassPath = structureRoleFamilyType.CAEXPath();

                foreach (var pair in device.DictionaryForInterfaceClassesInElectricalInterfaces)
                {
                    InternalElementType   internalElementofElectricalConnectorType = null;
                    ExternalInterfaceType electricalConnectorType = null;

                    ExternalInterfaceType electricalConnectorPins = null;

                    Match  numberfromElectricalConnectorType = Regex.Match(pair.Key.ToString(), @"\((\d+)\)");
                    string initialnumberbetweenparanthesisofElectricalConnectorType = numberfromElectricalConnectorType.Groups[1].Value;


                    string electricalConnectorTypeName = Regex.Replace(pair.Key.ToString(), @"\(.+?\)", "");
                    electricalConnectorTypeName = Regex.Replace(electricalConnectorTypeName, @"\{.+?\}", "");

                    internalElementofElectricalConnectorType = electricalInterface.InternalElement.Append(electricalConnectorTypeName);

                    electricalConnectorType = internalElementofElectricalConnectorType.ExternalInterface.Append(electricalConnectorTypeName);

                    var attributesOfConnectorType = electricalConnectorType.Attribute;

                    foreach (var valueList in pair.Value)
                    {
                        foreach (var item in valueList)
                        {
                            if (item.AttributePath.Contains("/") || item.AttributePath.Contains("."))
                            {
                                int          count               = 2;
                                int          counter             = 0;
                                Stack <char> stack               = new Stack <char>();
                                string       searchAttributeName = item.AttributePath.Substring(0, item.AttributePath.Length - item.Name.Length);

                                foreach (var character in searchAttributeName.Reverse())
                                {
                                    if (!char.IsLetterOrDigit(character))
                                    {
                                        counter++;
                                        if (counter == count)
                                        {
                                            break;
                                        }
                                    }
                                    if (char.IsLetterOrDigit(character))
                                    {
                                        stack.Push(character);
                                    }
                                }

                                string finalAttributeName = new string(stack.ToArray());

                                foreach (var attribute in electricalConnectorType.Attribute)
                                {
                                    if (attribute.Name == finalAttributeName)
                                    {
                                        var eachattribute = attribute.Attribute.Append(item.Name.ToString());
                                        eachattribute.Value             = item.Value;
                                        eachattribute.DefaultValue      = item.Default;
                                        eachattribute.Unit              = item.Unit;
                                        eachattribute.AttributeDataType = item.DataType;
                                        eachattribute.Description       = item.Description;
                                        eachattribute.Copyright         = item.CopyRight;

                                        eachattribute.ID = item.ID;

                                        foreach (var val in item.RefSemanticList.Elements)
                                        {
                                            var refsem = eachattribute.RefSemantic.Append();
                                            refsem.CorrespondingAttributePath = val.FirstAttribute.Value;
                                        }

                                        electricalConnectorType.RefBaseClassPath = item.RefBaseClassPath;
                                    }
                                    if (attribute.Attribute.Exists)
                                    {
                                        SearchAttributesInsideAttributesOFElectricConnectorType(finalAttributeName, attribute, item, electricalConnectorType);
                                    }
                                }
                            }
                            else
                            {
                                var eachattribute = attributesOfConnectorType.Append(item.Name.ToString());
                                eachattribute.Value             = item.Value;
                                eachattribute.DefaultValue      = item.Default;
                                eachattribute.Unit              = item.Unit;
                                eachattribute.AttributeDataType = item.DataType;
                                eachattribute.Description       = item.Description;
                                eachattribute.Copyright         = item.CopyRight;

                                eachattribute.ID = item.ID;

                                foreach (var val in item.RefSemanticList.Elements)
                                {
                                    var refsem = eachattribute.RefSemantic.Append();
                                    refsem.CorrespondingAttributePath = val.FirstAttribute.Value;
                                }

                                electricalConnectorType.RefBaseClassPath = item.RefBaseClassPath;
                            }
                        }
                    }


                    foreach (var pairofList in device.DictionaryForExternalInterfacesUnderInterfaceClassInElectricalInterfaces)
                    {
                        Match  numberfromElectricalConnectorPins = Regex.Match(pairofList.Key.ToString(), @"\((\d+)\)");
                        string initialnumberbetweenparanthesisElectricalConnectorPins = numberfromElectricalConnectorPins.Groups[1].Value;

                        string electricalConnectorPinName = Regex.Replace(pairofList.Key.ToString(), @"\(.*?\)", "");
                        electricalConnectorPinName = Regex.Replace(electricalConnectorPinName, @"\{.*?\}", "");
                        electricalConnectorPinName = electricalConnectorPinName.Replace(electricalConnectorTypeName, "");



                        if (initialnumberbetweenparanthesisofElectricalConnectorType == initialnumberbetweenparanthesisElectricalConnectorPins)
                        {
                            electricalConnectorPins = electricalConnectorType.ExternalInterface.Append(electricalConnectorPinName);

                            var attributesOfConnectorPins = electricalConnectorPins.Attribute;

                            foreach (var valueList in pairofList.Value)
                            {
                                foreach (var item in valueList)
                                {
                                    if (item.AttributePath.Contains("/") || item.AttributePath.Contains("."))
                                    {
                                        int          count               = 2;
                                        int          counter             = 0;
                                        Stack <char> stack               = new Stack <char>();
                                        string       searchAttributeName = item.AttributePath.Substring(0, item.AttributePath.Length - item.Name.Length);

                                        foreach (var character in searchAttributeName.Reverse())
                                        {
                                            if (!char.IsLetterOrDigit(character))
                                            {
                                                counter++;
                                                if (counter == count)
                                                {
                                                    break;
                                                }
                                            }
                                            if (char.IsLetterOrDigit(character))
                                            {
                                                stack.Push(character);
                                            }
                                        }

                                        string finalAttributeName = new string(stack.ToArray());

                                        foreach (var attribute in electricalConnectorPins.Attribute)
                                        {
                                            if (attribute.Name == finalAttributeName)
                                            {
                                                var eachattribute = attribute.Attribute.Append(item.Name.ToString());
                                                eachattribute.Value             = item.Value;
                                                eachattribute.DefaultValue      = item.Default;
                                                eachattribute.Unit              = item.Unit;
                                                eachattribute.AttributeDataType = item.DataType;
                                                eachattribute.Description       = item.Description;
                                                eachattribute.Copyright         = item.CopyRight;

                                                eachattribute.ID = item.ID;

                                                foreach (var val in item.RefSemanticList.Elements)
                                                {
                                                    var refsem = eachattribute.RefSemantic.Append();
                                                    refsem.CorrespondingAttributePath = val.FirstAttribute.Value;
                                                }

                                                electricalConnectorPins.RefBaseClassPath = item.RefBaseClassPath;
                                            }
                                            if (attribute.Attribute.Exists)
                                            {
                                                SearchAttributesInsideAttributesOFElectricConnectorType(finalAttributeName, attribute, item, electricalConnectorPins);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        var eachattribute = attributesOfConnectorPins.Append(item.Name.ToString());
                                        eachattribute.Value             = item.Value;
                                        eachattribute.DefaultValue      = item.Default;
                                        eachattribute.Unit              = item.Unit;
                                        eachattribute.AttributeDataType = item.DataType;
                                        eachattribute.Description       = item.Description;
                                        eachattribute.Copyright         = item.CopyRight;

                                        eachattribute.ID = item.ID;

                                        foreach (var val in item.RefSemanticList.Elements)
                                        {
                                            var refsem = eachattribute.RefSemantic.Append();
                                            refsem.CorrespondingAttributePath = val.FirstAttribute.Value;
                                        }

                                        electricalConnectorPins.RefBaseClassPath = item.RefBaseClassPath;
                                    }
                                }
                            }
                        }
                    }
                }
            }



            // create the PackageUri for the root aml file
            Uri partUri = PackUriHelper.CreatePartUri(new Uri("/" + fileName + "-root.aml", UriKind.Relative));


            // tcreate the aml file as a temporary file
            string path = Path.GetTempFileName();

            document.SaveToFile(path, true);

            if (isEdit)
            {
                // delete the old aml file
                amlx.Package.DeletePart(partUri);

                // delete all files in the amlx package.
                // Directory.Delete(Path.GetFullPath(amlx.ContainerFilename), true);
            }

            // write the new aml file into the package
            PackagePart root = amlx.AddRoot(path, partUri);


            if (!isEdit)
            {
                foreach (AttachablesDataGridViewParameters listWithUri in device.listWithURIConvertedToString)
                {
                    if (listWithUri.ElementName != null)
                    {
                        Uri newuri = null;
                        newuri = new Uri(listWithUri.ElementName, UriKind.Relative);
                        amlx.AddAnyContent(root, listWithUri.FilePath.ToString(), newuri);
                    }
                }
            }
            DirectoryInfo directory = new DirectoryInfo(Path.GetDirectoryName(amlFilePath));

            foreach (FileInfo fileInfos in directory.GetFiles())
            {
                if (fileInfos.Extension != ".amlx")
                {
                    fileInfos.Delete();
                }
            }


            amlx.Save();
            amlx.Close();
            if (isEdit)
            {
                return("Sucessfully updated device!\nFilepath " + amlFilePath);
            }
            else
            {
                return("Device description file created!\nFilepath " + amlFilePath);
            }
        }
Example #8
0
        /// <summary>
        /// Load the amlx container and try to load it as an <see cref="MWObject"/>
        /// </summary>
        /// <param name="file">The full path to the amlx file</param>
        /// <returns></returns>
        public MWObject loadObject(string file)
        {
            try
            {
                FileInfo fileInfo   = new FileInfo(file);
                string   objectName = fileInfo.Name;

                // Load the amlx container from the given filepath
                AutomationMLContainer amlx = new AutomationMLContainer(file);

                // Get the root path -> main .aml file
                IEnumerable <PackagePart> rootParts = amlx.GetPartsByRelationShipType(AutomationMLContainer.RelationshipType.Root);

                // We expect the aml to only have one root part
                if (rootParts.First() != null)
                {
                    PackagePart part = rootParts.First();

                    // load the aml file as an CAEX document
                    CAEXDocument document = CAEXDocument.LoadFromStream(part.GetStream());


                    // Iterate over all SystemUnitClassLibs and SystemUnitClasses and scan if it matches our format
                    // since we expect only one device per aml(x) file, return after on is found
                    foreach (SystemUnitClassLibType classLibType in document.CAEXFile.SystemUnitClassLib)
                    {
                        foreach (SystemUnitFamilyType classLib in classLibType.SystemUnitClass)
                        {
                            // check if it matches our format
                            foreach (InternalElementType internalElement in classLib.InternalElement)
                            {
                                // is the DeviceIdentification there?
                                if (internalElement.Name.Equals("DeviceIdentification"))
                                {
                                    // is it an interface or a device?
                                    if (internalElement.Attribute.GetCAEXAttribute("InterfaceNumber") != null)
                                    {
                                        MWInterface mWInterface = new MWInterface();
                                        mWInterface.numberOfInterface = Convert.ToInt32(internalElement.Attribute.GetCAEXAttribute("InterfaceNumber").Value);

                                        // read the attributes and write them directly into the interface
                                        fillInterfaceWithData(mWInterface, internalElement.Attribute);

                                        amlx.Close();
                                        return(mWInterface);
                                    }
                                    else if (internalElement.Attribute.GetCAEXAttribute("DeviceName") != null)
                                    {
                                        MWDevice mWDevice = new MWDevice();

                                        // read the attributes and write them directly into the device
                                        fillDeviceWithData(mWDevice, internalElement.Attribute);

                                        // check if there are pictures provided
                                        foreach (InternalElementType ie in classLib.InternalElement)
                                        {
                                            switch (ie.Name)
                                            {
                                            case "ManufacturerIcon":
                                                try
                                                {
                                                    mWDevice.vendorLogo = ie.ExternalInterface.First().Attribute.GetCAEXAttribute("refURI").Value;
                                                }
                                                catch (Exception)
                                                {
                                                    // No vendorLogo
                                                }
                                                break;

                                            case "ComponentPicture":
                                                try
                                                {
                                                    mWDevice.devicePicture = ie.ExternalInterface.First().Attribute.GetCAEXAttribute("refURI").Value;
                                                }
                                                catch (Exception)
                                                {
                                                    // No vendorLogo
                                                }
                                                break;

                                            case "ComponentIcon":
                                                try
                                                {
                                                    mWDevice.deviceIcon = ie.ExternalInterface.First().Attribute.GetCAEXAttribute("refURI").Value;
                                                }
                                                catch (Exception)
                                                {
                                                    // No vendorLogo
                                                }
                                                break;
                                            }
                                        }
                                        amlx.Close();
                                        return(mWDevice);
                                    }
                                }
                            }
                        }
                    }
                }
                amlx.Close();
                return(null);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error while loading the AMLX-File", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return(null);
            }
        }
Example #9
0
        /// <summary>
        /// Create the AMLX File with the correct AML File and optional pictures
        /// </summary>
        /// <param name="device">The device which will be created</param>
        /// <param name="isEdit">true if an amlx file get update, false if a new file will be created</param>
        /// <returns></returns>
        public string CreateDevice(MWDevice device, bool isEdit)
        {
            Uri                   manuPart       = null;
            Uri                   deviceIconPart = null;
            Uri                   devicePicPart  = null;
            CAEXDocument          document       = null;
            AutomationMLContainer amlx           = null;

            // Init final .amlx Filepath
            string   fileName    = device.vendorName + "-" + device.deviceName + "-V.1.0-" + DateTime.Now.Date.ToShortDateString();
            string   amlFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\modellingwizard\\" + fileName + ".amlx";
            FileInfo file        = new FileInfo(amlFilePath);

            // Create directory if it's not existing
            file.Directory.Create();

            // Init CAEX Document
            if (isEdit)
            {
                // Load the amlx file
                amlx = new AutomationMLContainer(amlFilePath, FileMode.Open);

                IEnumerable <PackagePart> rootParts = amlx.GetPartsByRelationShipType(AutomationMLContainer.RelationshipType.Root);

                // We expect the aml to only have one root part
                if (rootParts.First() != null)
                {
                    PackagePart part = rootParts.First();

                    // load the aml file as an CAEX document
                    document = CAEXDocument.LoadFromStream(part.GetStream());
                }
                else
                {
                    // the amlx contains no aml file
                    document = CAEXDocument.New_CAEXDocument();
                }
            }
            else
            {
                // create a new CAEX document
                document = CAEXDocument.New_CAEXDocument();
                amlx     = new AutomationMLContainer(amlFilePath, FileMode.Create);
            }


            // Init the default Libs
            AutomationMLBaseRoleClassLibType.RoleClassLib(document);
            AutomationMLInterfaceClassLibType.InterfaceClassLib(document);

            SystemUnitFamilyType systemUnitClass = null;

            // Create the SystemUnitClass for our device
            if (!isEdit)
            {
                systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(device.deviceName);
            }
            else
            {
                // check if our format is given in the amlx file if not: create it
                bool foundSysClassLib = false;
                foreach (var sysclasslib in document.CAEXFile.SystemUnitClassLib)
                {
                    if (sysclasslib.Name.Equals("ComponentSystemUnitClassLib"))
                    {
                        bool foundSysClass = false;
                        foreach (var sysclass in sysclasslib.SystemUnitClass)
                        {
                            if (sysclass.Name.Equals(device.deviceName))
                            {
                                foundSysClass   = true;
                                systemUnitClass = sysclass;
                                break;
                            }
                        }
                        if (!foundSysClass)
                        {
                            systemUnitClass = sysclasslib.SystemUnitClass.Append(device.deviceName);
                        }
                        foundSysClassLib = true;
                    }
                }
                if (!foundSysClassLib)
                {
                    systemUnitClass = document.CAEXFile.SystemUnitClassLib.Append("ComponentSystemUnitClassLib").SystemUnitClass.Append(device.deviceName);
                }
            }

            // Convert picture paths to relativ package paths (if they are given)
            // Convert vendorLogo path
            if (device.vendorLogo != null && !device.vendorLogo.Equals(""))
            {
                try
                {
                    // Create File Paths
                    manuPart = createPictureRef(device.vendorLogo, "ManufacturerIcon", "ExternalDataConnector", systemUnitClass);
                }
                catch (Exception)
                {
                    // No vendor Logo
                }
            }

            // Convert deviceIcon
            if (device.deviceIcon != null && !device.deviceIcon.Equals(""))
            {
                try
                {
                    deviceIconPart = createPictureRef(device.deviceIcon, "ComponentIcon", "ExternalDataReference", systemUnitClass);
                }
                catch (Exception)
                {
                    // No Device Icon
                }
            }

            // Convert devicePicture
            if (device.devicePicture != null && !device.devicePicture.Equals(""))
            {
                try
                {
                    devicePicPart = createPictureRef(device.devicePicture, "ComponentPicture", "ExternalDataReference", systemUnitClass);
                }
                catch (Exception)
                {
                    // No device Picture
                }
            }



            // Create the internalElement DeviceIdentification
            InternalElementType ie = null;

            foreach (var internalelement in systemUnitClass.InternalElement)
            {
                if (internalelement.Name.Equals("DeviceIdentification"))
                {
                    ie = internalelement;
                    break;
                }
            }
            if (ie == null)
            {
                ie = systemUnitClass.InternalElement.Append("DeviceIdentification");
            }

            // Init the Attributes for our format and set the correct DataTypes
            initCAEXattributes(ie);

            // Set the correct values for the Attributes
            setCAEXattribute(ie, device);

            // create the PackageUri for the root aml file
            Uri partUri = PackUriHelper.CreatePartUri(new Uri("/" + fileName + "-root.aml", UriKind.Relative));


            // tcreate the aml file as a temporary file
            string path = Path.GetTempFileName();

            document.SaveToFile(path, true);

            if (isEdit)
            {
                // delete the old aml file
                amlx.Package.DeletePart(partUri);
            }

            // write the new aml file into the package
            PackagePart root = amlx.AddRoot(path, partUri);

            if (!isEdit)
            {
                // copy the images from disk into the package
                if (manuPart != null)
                {
                    amlx.AddAnyContent(root, device.vendorLogo, manuPart);
                }

                if (deviceIconPart != null)
                {
                    amlx.AddAnyContent(root, device.deviceIcon, deviceIconPart);
                }

                if (devicePicPart != null)
                {
                    amlx.AddAnyContent(root, device.devicePicture, devicePicPart);
                }
            }

            amlx.Save();
            amlx.Close();
            if (isEdit)
            {
                return("Sucessfully updated device!\nFilepath " + amlFilePath);
            }
            else
            {
                return("Sucessfully created device!\nFilepath " + amlFilePath);
            }
        }
Example #10
0
        /// <summary>
        /// Read the all attributes in <paramref name="attributes"/> and write the values into <paramref name="device"/>
        /// </summary>
        /// <param name="device">the object to write the data into</param>
        /// <param name="attributes">the list of attributes</param>
        private void fillDeviceWithData(MWDevice device, AttributeSequence attributes)
        {
            // iterate over all atttributes
            foreach (AttributeType attribute in attributes)
            {
                // apply the value of the attribute to the correct interface parameter
                switch (attribute.Name)
                {
                case "CommunicationTechonolgy":
                    device.deviceType = attribute.Value;
                    break;

                case "VendorId":
                    try
                    {
                        device.vendorID = Int32.Parse(attribute.Value);
                    }
                    catch (Exception)
                    {
                        // let the value be null
                    }
                    break;

                case "VendorName":
                    device.vendorName = attribute.Value;
                    break;

                case "DeviceId":
                    try
                    {
                        device.deviceID = Int32.Parse(attribute.Value);
                    }
                    catch (Exception)
                    {
                        // let the value be null
                    }
                    break;

                case "DeviceName":
                    device.deviceName = attribute.Value;
                    break;

                case "DeviceFamily":
                    device.deviceFamily = attribute.Value;
                    break;

                case "ProductName":
                    device.productName = attribute.Value;
                    break;

                case "OrderNumber":
                    device.orderNumber = attribute.Value;
                    break;

                case "ProductText":
                    device.productText = attribute.Value;
                    break;

                case "IPProtection":
                    device.ipProtection = attribute.Value;
                    break;

                case "OperatingTemperatureMin":
                    try
                    {
                        device.minTemperature = Double.Parse(attribute.Value);
                    }
                    catch (Exception)
                    {
                        device.minTemperature = Double.NaN;
                    }
                    break;

                case "OperatingTemperatureMax":
                    try
                    {
                        device.maxTemperature = Double.Parse(attribute.Value);
                    }
                    catch (Exception)
                    {
                        device.maxTemperature = Double.NaN;
                    }
                    break;

                case "VendorUrl":
                    device.vendorHomepage = attribute.Value;
                    break;

                case "HardwareRelease":
                    device.harwareRelease = attribute.Value;
                    break;

                case "SoftwareRelease":
                    device.softwareRelease = attribute.Value;
                    break;
                }
            }
        }
Example #11
0
        /// <summary>
        /// OnClick Methode of the Create Device Button.
        /// This creates a new <see cref="MWDevice"/>Device which will be passed to the <see cref="mWController"/>
        /// </summary>
        /// <param name="sender">Event Argument, not used</param>
        /// <param name="e">Event Argument, not used</param>
        private void createDeviceBtn_Click(object sender, System.EventArgs e)
        {
            // Create a new Device
            var device = new MWDevice();

            // Read all the input fields and write them to the device data
            if (deviceTypeListBox.SelectedItem != null)
            {
                device.deviceType = deviceTypeListBox.SelectedItem.ToString();
            }
            else
            {
                device.deviceType = "";
            }

            // Check if there was an input in this field, if so: try to convert it to integer
            if (!String.IsNullOrWhiteSpace(txtVendorId.Text))
            {
                try { device.vendorID = Convert.ToInt32(txtVendorId.Text); } catch (Exception) { MessageBox.Show("Vendor ID is in an invalid format (Expected only numbers)! Ignoring!"); }
            }
            // Check if there was an input in this field, if so: try to convert it to integer
            if (!String.IsNullOrWhiteSpace(txtDeviceId.Text))
            {
                try { device.deviceID = Convert.ToInt32(txtDeviceId.Text); } catch (Exception) { MessageBox.Show("Device ID is in an invalid format (Expected only numbers)! Ignoring!"); }
            }
            device.vendorName      = txtVendorName.Text;
            device.vendorHomepage  = txtVendorHomepage.Text;
            device.deviceName      = txtDeviceName.Text;
            device.deviceFamily    = txtDeviceFamily.Text;
            device.productName     = txtProductName.Text;
            device.orderNumber     = txtOrderNumber.Text;
            device.productText     = productTxtBox.Text;
            device.harwareRelease  = hwRelTxt.Text;
            device.softwareRelease = swRelTxt.Text;


            device.ipProtection = txtIpProduction.Text;
            if (!String.IsNullOrWhiteSpace(txtMaxTemp.Text))
            {
                try { device.minTemperature = Convert.ToDouble(txtMinTemp.Text); } catch (Exception) { device.minTemperature = Double.NaN; MessageBox.Show("Min Temperature is in an invalid format (Expected only numbers)! Ignoring!"); }
            }
            if (!String.IsNullOrWhiteSpace(txtMaxTemp.Text))
            {
                try { device.maxTemperature = Convert.ToDouble(txtMaxTemp.Text); } catch (Exception) { device.maxTemperature = Double.NaN; MessageBox.Show("Max Temperature is in an invalid format (Expected only numbers)! Ignoring!"); }
            }

            device.deviceIcon    = deviceIconPath;
            device.devicePicture = devicePicturePath;
            device.vendorLogo    = vendorPicturePath;

            // Pass the device to the controller
            string result = mWController.CreateDeviceOnClick(device, isEditing);

            clear();
            // Display the result
            if (result != null)
            {
                // Display error Dialog
                MessageBox.Show(result);
            }
        }