Ejemplo n.º 1
0
        public void ReadAddressSpaceElemProperties(DaSession session, string itemId)
        {
            panelBottom.Visible = false;

            DaGetPropertiesOptions propertyGetOptions = new DaGetPropertiesOptions();

            propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL;

            DaProperty[] properties = null;
            if (ResultCode.SUCCEEDED(session.GetDaProperties(itemId,
                                                             null,
                                                             propertyGetOptions,
                                                             out properties,
                                                             null)))
            {
                if (properties == null)
                {
                    return;
                }

                Helper.CustomProperties propertiesColl = new DemoClient.Helper.CustomProperties();

                for (int i = 0; i < properties.Length; i++)
                {
                    SetPropertyData(ref propertiesColl, properties[i], String.Format("{0} - {1}", properties[i].Id, properties[i].Description));
                }
                propertyGrid.PropertySort   = PropertySort.Categorized;
                propertyGrid.SelectedObject = propertiesColl;
            }
        }
		public override TestResult Execute()
		{
			if (string.IsNullOrEmpty(SessionProperty))
				return TestResult.ParameterMissing("Session");

			DaSession session = GetProperty(SessionProperty) as DaSession;
			if (session == null)
				return TestResult.ParameterInvalid("Session");

			try
			{
				Softing.OPCToolbox.Client.DaProperty[] elements;

				int result = int.MaxValue;
				if (ExecutionType == EnumExecutionType.SYNCHRONOUS)
				{
					result = session.GetDaProperties(RootID, RootPath, DaGetPropertiesOptions, out elements, new ExecutionOptions(EnumExecutionType.SYNCHRONOUS, m_asyncExecutionContext));
				}
				else
				{
					m_asyncExecutionContext = DaExecutionContext.Next;
					session.GetDaPropertiesCompleted += session_GetDaPropertiesCompleted;
					m_waitHandle = new System.Threading.AutoResetEvent(false);
					result = session.GetDaProperties(RootID, RootPath, DaGetPropertiesOptions, out elements, new ExecutionOptions(EnumExecutionType.ASYNCHRONOUS, m_asyncExecutionContext));

					if (m_waitHandle.WaitOne(3000))
					{
						result = m_browseAsyncResult;
						elements = m_browseAsyncResults;
					}
					session.GetDaPropertiesCompleted -= session_GetDaPropertiesCompleted;
				}

				if (ResultCode.SUCCEEDED(result))
				{
					if (!string.IsNullOrEmpty(Property))
						SetProperty(Property, elements);

					if (ExpectedElements.Count != 0 && ExpectedElements.Count != elements.Length)
						return TestResult.Failed(string.Format("Unexpected number of results, expected {0}, actual {1}", ExpectedElements.Count, elements.Length));

					for (int i = 0; i < ExpectedElements.Count; i++)
					{
						Softing.OPCToolbox.Client.DaProperty receivedElement = elements[i];
						TestClientGui.Support.DaProperty expectedElement = ExpectedElements[i];

						if ((expectedElement.ItemId != null && expectedElement.ItemId != receivedElement.ItemId)
							|| (expectedElement.ItemPath != null && expectedElement.ItemPath != receivedElement.ItemPath)
							|| (!string.IsNullOrEmpty(expectedElement.Name) && expectedElement.Name != receivedElement.Name)
							|| (expectedElement.DataType != null && expectedElement.DataType != receivedElement.DataType)
							|| (!string.IsNullOrEmpty(expectedElement.Description) && expectedElement.Description != receivedElement.Description)
							|| (expectedElement.ValueQT != null && !expectedElement.ValueQT.Data.Equals(receivedElement.ValueQT.Data))
							|| (expectedElement.ValueQT != null && expectedElement.ValueQT.Quality != receivedElement.ValueQT.Quality))
							return TestResult.Failed(string.Format("Unexpected result for item #{0}", i));

						if (DaGetPropertiesOptions.WhatPropertyData == EnumPropertyData.ITEMID || DaGetPropertiesOptions.WhatPropertyData == EnumPropertyData.NONE)
							if (receivedElement.ValueQT.Data != null)
								return TestResult.Failed(string.Format("Value was not NULL for element #{0}", i));
						if (DaGetPropertiesOptions.WhatPropertyData == EnumPropertyData.VALUE)
							if (!string.IsNullOrEmpty(receivedElement.ItemId))
								return TestResult.Failed(string.Format("ItemId was not NULL for element #{0}", i));
					}

					return TestResult.Success();
				}
				else
				{
					EnumResultCode resCode = (EnumResultCode)Enum.Parse(typeof(EnumResultCode), result.ToString());

					return TestResult.Failed(string.Format("Call returned: {0}\t({1})", resCode.ToString(), result));
				}
			}
			catch (Exception ex)
			{
				return new TestResult(ex);
			}
		}
        /// <summary>
        /// Write Address Space Element
        /// </summary>
        /// <param name="session"></param>
        /// <param name="itemId"></param>
        public void WriteAddresSpaceItem(DaSession session, string itemId, object selectedObject, bool isAddSpaceElem)
        {
            Helper.DataTypes dataTypeHelper = new DataTypes();
            this.panelBottom.Visible = true;

            isDaItem     = false;
            this.session = session;

            ValueQT[] itemValues;
            int[]     itemResults;
            int       result = session.Read(MAX_AGE_DEVICE, new string[] { itemId }, null, out itemValues, out itemResults, null);

            if (itemResults.Length == 0)
            {
                return;
            }
            if (itemValues.Length == 0)
            {
                return;
            }

            Type             itemDataType    = typeof(short);
            EnumAccessRights itemAccessRighs = EnumAccessRights.READWRITEABLE;
            bool             hasWriteAccess  = true;

            if (ResultCode.SUCCEEDED(result) && ResultCode.SUCCEEDED(itemResults[0]))
            {
                DaGetPropertiesOptions propertyGetOptions = new DaGetPropertiesOptions();
                propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL;

                DaProperty[] properties = null;
                if (isAddSpaceElem)
                {
                    if (ResultCode.SUCCEEDED(session.GetDaProperties(itemId,
                                                                     null,
                                                                     propertyGetOptions,
                                                                     out properties,
                                                                     null)))
                    {
                        if (properties == null)
                        {
                            this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, null);
                            return;
                        }
                        if (properties.Length < 5)
                        {
                            this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, null);
                            return;
                        }
                        itemDataType    = DataTypeConverter.GetSysType((short)properties[0].ValueQT.Data);
                        itemAccessRighs = ObjToAccesssRights(properties[4].ValueQT.Data);
                    }
                }
                else
                {
                    itemDataType = (selectedObject as DaProperty).DataType;
                }

                //the item value is null
                if (itemValues[0] == null)
                {
                    this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, itemDataType);
                    return;
                }

                if (itemAccessRighs == EnumAccessRights.READABLE)
                {
                    hasWriteAccess = false;
                }
            }
            this.propertyGrid.SelectedObject = FillWriteService(itemId, itemDataType, hasWriteAccess, itemValues[0]);
        }
        /// <summary>
        /// Write Da Value
        /// </summary>
        /// <param name="strValue"></param>
        private void WriteValue(string strValue)
        {
            TreeNode selectedNode = browseTreeView.SelectedNode;

            if (selectedNode == null)
            {
                return;
            }
            if (selectedNode.Tag == null)
            {
                return;
            }

            //get the session
            if (browseTreeView.Nodes.Count == 0)
            {
                return;
            }
            TreeNode rootNode = browseTreeView.Nodes[0];

            if (rootNode == null)
            {
                return;
            }
            if (rootNode.Tag == null)
            {
                return;
            }
            if (Type.ReferenceEquals(rootNode.Tag.GetType(), typeof(DaSession)))
            {
                DaSession session = rootNode.Tag as DaSession;
                if (session == null)
                {
                    return;
                }

                //get the itemId
                string itemId     = null;
                Type   dataType   = typeof(System.Byte);
                int    startIndex = 0;
                if (Type.ReferenceEquals(selectedNode.Tag.GetType(), typeof(DaAddressSpaceElement)))
                {
                    itemId = (selectedNode.Tag as DaAddressSpaceElement).ItemId;
                    DaAddressSpaceElement addSpaceElem = selectedNode.Tag as DaAddressSpaceElement;
                    //Get Data Type
                    DaProperty[]           properties         = null;
                    DaGetPropertiesOptions propertyGetOptions = new DaGetPropertiesOptions();
                    propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL;
                    if (ResultCode.SUCCEEDED(session.GetDaProperties(itemId,
                                                                     null,
                                                                     propertyGetOptions,
                                                                     out properties,
                                                                     null)))
                    {
                        if (properties == null)
                        {
                            dataType = typeof(System.Byte);
                        }
                        if (properties.Length < 5)
                        {
                            dataType = typeof(System.Byte);
                        }
                        dataType = DataTypeConverter.GetSysType((short)properties[0].ValueQT.Data);
                        if (dataType.IsArray)
                        {
                            startIndex = ((properties[1].ValueQT.Data) as Array).GetLowerBound(0);
                        }
                    }
                }
                else if (Type.ReferenceEquals(selectedNode.Tag.GetType(), typeof(DaProperty)))
                {
                    itemId   = (selectedNode.Tag as DaProperty).Id.ToString();
                    dataType = (selectedNode.Tag as DaProperty).DataType;
                    if (dataType.IsArray)
                    {
                        startIndex = (((selectedNode.Tag as DaProperty).ValueQT.Data) as Array).GetLowerBound(0);
                    }
                }
                if (String.IsNullOrEmpty(itemId))
                {
                    return;
                }
                if (dataType == null)
                {
                    return;
                }

                ValueQT itemValue = new ValueQT();

                if (!dataType.IsArray)
                {
                    //data type is not array
                    object objData = null;
                    ConvertToType(dataType, out objData, strValue);
                    itemValue.SetData(objData, EnumQuality.GOOD, DateTime.Now);
                }
                else
                {
                    //data type is array
                    string   typeStr   = dataType.FullName;
                    Type     arrayType = System.Type.GetType(typeStr.Substring(0, typeStr.IndexOf('[')));
                    string[] objDatas  = strValue.Trim().Split(';');
                    if (objDatas == null)
                    {
                        return;
                    }
                    Array array = null;
                    if (startIndex > 0)
                    {
                        //non-zero bound array
                        array = Array.CreateInstance(arrayType, new int[] { objDatas.Length }, new int[] { startIndex });
                    }
                    else
                    {
                        //zero boud array
                        array = Array.CreateInstance(arrayType, objDatas.Length);
                    }
                    int strIndex = 0;
                    for (int index = array.GetLowerBound(0);
                         index <= array.GetUpperBound(0);
                         index++)
                    {
                        object objData = null;
                        ConvertToType(arrayType, out objData, objDatas[strIndex]);
                        array.SetValue(objData, index);
                        strIndex++;
                    }
                    itemValue.SetData(array, EnumQuality.GOOD, DateTime.Now);
                }

                //Write DaItem Values
                ExecutionOptions executionOptions = new ExecutionOptions();
                executionOptions.ExecutionType    = EnumExecutionType.ASYNCHRONOUS;
                executionOptions.ExecutionContext = 0;


                ValueQT[] itemValues = new ValueQT[] { itemValue };
                int[]     results;
                session.Write(new string[] { itemId }, null, itemValues, out results, executionOptions);
            }
        }