Exemplo n.º 1
0
        }               //	end constructor

        public override int HandleReadRequests()
        {
            int count = Requests.Count;

            for (int i = 0; i < count; i++)
            {
                DaRequest request = (DaRequest)Requests[i];
                if (request.PropertyId == 0)
                {
                    // get address space element value
                    // take the Toolkit cache value
                    ValueQT cacheValue = null;
                    request.AddressSpaceElement.GetCacheValue(ref cacheValue);
                    request.Value  = cacheValue;
                    request.Result = EnumResultCode.S_OK;
                }
                else
                {
                    // get property value
                    // get the value from the address space element
                    MyDaAddressSpaceElement element = request.AddressSpaceElement as MyDaAddressSpaceElement;
                    if (element != null)
                    {
                        element.GetPropertyValue(request);
                    }
                    else
                    {
                        request.Result = EnumResultCode.E_FAIL;
                    }   //	end if ... else
                }       //	end if ... else
            }           //	end for
            return(CompleteRequests());
        }               //	end HandleReadRequests
        // Return the incremented value for a ValueQT
        private int IncrementItemValue(MyDaAddressSpaceElement item)
        {
            // get actual value
            ValueQT itemValue = null;

            item.GetCacheValue(ref itemValue);
            Type   dataType = itemValue.Data.GetType();
            object newValue = null;

            if (dataType == typeof(bool))
            {
                newValue = !(bool)itemValue.Data;
            }
            if (dataType == typeof(Byte))
            {
                newValue = (Byte)((Byte)itemValue.Data + m_increment);
            }
            if (dataType == typeof(SByte))
            {
                newValue = (SByte)((SByte)itemValue.Data + m_increment);
            }
            if (dataType == typeof(UInt16))
            {
                newValue = (UInt16)((UInt16)itemValue.Data + m_increment);
            }
            if (dataType == typeof(Int16))
            {
                newValue = (Int16)((Int16)itemValue.Data + m_increment);
            }
            if (dataType == typeof(UInt32))
            {
                newValue = (UInt32)((UInt32)itemValue.Data + m_increment);
            }
            if (dataType == typeof(Int32))
            {
                newValue = (Int32)((Int32)itemValue.Data + m_increment);
            }
            if (dataType == typeof(UInt64))
            {
                newValue = (UInt64)((UInt64)itemValue.Data + m_increment);
            }
            if (dataType == typeof(Int64))
            {
                newValue = (Int64)((Int64)itemValue.Data + m_increment);
            }
            if (dataType == typeof(Single))
            {
                newValue = (Single)((Single)itemValue.Data + m_increment);
            }
            if (dataType == typeof(Double))
            {
                newValue = (Double)((Double)itemValue.Data + m_increment);
            }

            // apply new value
            return(item.ValueChanged(new ValueQT(newValue, EnumQuality.GOOD, DateTime.Now)));
        }
        /// <summary>
        /// Simulate a value change for the items
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SimulateValues(object sender, ElapsedEventArgs e)
        {
            try
            {
                if (m_repeatCount > 0 || m_continousSimulation)
                {
                    // Retreive parent folder
                    AddressSpaceElement parentFolder = GetParent();

                    // Get children
                    ArrayList simulationItems = parentFolder.GetChildren();

                    // increment values
                    foreach (DaAddressSpaceElement item in simulationItems)
                    {
                        // filter only the MyDaAddressSpaceElement items
                        MyDaAddressSpaceElement daItem = item as MyDaAddressSpaceElement;

                        if (daItem == null)
                        {
                            continue;
                        }

                        IncrementItemValue(daItem);
                    }

                    if (!m_continousSimulation)
                    {
                        m_repeatCount--;
                    }
                }
                else
                {
                    StopSimulation();
                }
            }
            catch
            {
                // stop simulation
                m_repeatCount = 0;
            }
        }
        /// <summary>
        /// Create a list o items in the AddressSpace
        /// </summary>
        /// <param name="parentFolder"></param>
        /// <param name="itemCount"></param>
        /// <param name="itemType"></param>
        /// <param name="itemSetName"></param>
        private EnumResultCode CreateItems(
            AddressSpaceElement parentFolder,
            DaAddressSpaceElement resultItem,
            uint itemCount,
            Type itemType,
            bool generateRandomValues,
            string guid,
            bool isAnalogItem)
        {
            // Create item folder
            MyDaAddressSpaceElement itemFolder = new MyDaAddressSpaceElement();

            itemFolder.Name         = String.Format("TestItems_{0}", m_createdFolders);
            itemFolder.HasChildren  = true;
            itemFolder.AccessRights = EnumAccessRights.READABLE;
            if (!parentFolder.AddChild(itemFolder))
            {
                return(EnumResultCode.E_FAIL);
            }

            m_createdFolders++;

            //add guid to ItemSetId node
            ValueQT cacheValue = null;

            resultItem.GetCacheValue(ref cacheValue);
            string[] currentValue = cacheValue.Data as string[];
            string[] newValue     = new string[currentValue.Length + 1];
            currentValue.CopyTo(newValue, 0);
            newValue[currentValue.Length] = itemFolder.ItemId + "#" + guid;
            resultItem.ValueChanged(new ValueQT(newValue, EnumQuality.GOOD, DateTime.Now));


            // Add Start/Stop simulation command
            DaStartStopSimulationCommand simulationCmd = new DaStartStopSimulationCommand();

            simulationCmd.Name        = "StartStopSimulationCommand";
            simulationCmd.HasChildren = true;
            if (!itemFolder.AddChild(simulationCmd))
            {
                return(EnumResultCode.E_FAIL);
            }
            simulationCmd.ValueChanged(new ValueQT(false, EnumQuality.GOOD, DateTime.Now));

            MyDaAddressSpaceElement param;

            // Add command parameters

            // item count parameter
            param = new MyDaAddressSpaceElement(DaStartStopSimulationCommand.ChangeIntervalParam, DaStartStopSimulationCommand.ChangeIntervalParamType);
            if (!simulationCmd.AddChild(param))
            {
                return(EnumResultCode.E_FAIL);
            }
            param.ValueChanged(new ValueQT((uint)0, EnumQuality.GOOD, DateTime.Now));

            // repeat count parameter
            param = new MyDaAddressSpaceElement(DaStartStopSimulationCommand.RepeatCountParam, DaStartStopSimulationCommand.RepeatCountParamType);
            if (!simulationCmd.AddChild(param))
            {
                return(EnumResultCode.E_FAIL);
            }
            param.ValueChanged(new ValueQT((uint)0, EnumQuality.GOOD, DateTime.Now));

            // incrementt parameter
            param = new MyDaAddressSpaceElement(DaStartStopSimulationCommand.IncrementParam, DaStartStopSimulationCommand.IncrementParamType);
            if (!simulationCmd.AddChild(param))
            {
                return(EnumResultCode.E_FAIL);
            }
            param.ValueChanged(new ValueQT((uint)0, EnumQuality.GOOD, DateTime.Now));

            // Add Delete Items command
            DaDeleteItemsCommand deleteItemsCmd = new DaDeleteItemsCommand();

            deleteItemsCmd.Name        = "DeleteItemsCommand";
            deleteItemsCmd.HasChildren = true;
            if (!itemFolder.AddChild(deleteItemsCmd))
            {
                return(EnumResultCode.E_FAIL);
            }
            deleteItemsCmd.ValueChanged(new ValueQT(false, EnumQuality.GOOD, DateTime.Now));

            // Add items to list
            for (uint i = 0; i < itemCount; i++)
            {
                MyDaAddressSpaceElement item = new MyDaAddressSpaceElement(String.Format("TV_{0}", i), itemType);
                if (!itemFolder.AddChild(item))
                {
                    return(EnumResultCode.E_FAIL);
                }

                if (isAnalogItem)
                {
                    DaConstantProperty euType = new DaConstantProperty(
                        (int)EnumPropertyId.ITEM_EU_TYPE,
                        "EUType",
                        "7",
                        new ValueQT(EnumEUType.ANALOG, EnumQuality.GOOD, DateTime.Now));
                    euType.AccessRights = EnumAccessRights.READABLE;
                    item.AddProperty(euType);

                    DaProperty euHigh = new DaProperty();
                    euHigh.Id           = (int)EnumPropertyId.HIGH_EU;
                    euHigh.Name         = "High EU";
                    euHigh.Description  = "High EU";
                    euHigh.ItemId       = euHigh.Id.ToString();
                    euHigh.Datatype     = typeof(System.Double);
                    euHigh.AccessRights = EnumAccessRights.READABLE;
                    item.AddProperty(euHigh);

                    DaProperty euLow = new DaProperty();
                    euLow.Id           = (int)EnumPropertyId.LOW_EU;
                    euLow.Name         = "Low EU";
                    euLow.Description  = "Low EU";
                    euLow.ItemId       = euLow.Id.ToString();
                    euLow.Datatype     = typeof(System.Double);
                    euLow.AccessRights = EnumAccessRights.READABLE;
                    item.AddProperty(euLow);
                }

                if (generateRandomValues)
                {
                    // item.ValueChanged(m_generator.GetRandomValueQT(item.Datatype));
                }
                else
                {
                    item.ValueChanged(new ValueQT(0, EnumQuality.GOOD, DateTime.Now));
                }
            }



            return(EnumResultCode.S_OK);
        }
Exemplo n.º 5
0
        }               //	end HandleReadRequests

        public override int HandleWriteRequests()
        {
            int count = Requests.Count;

            lock (OpcServer.LockTransactionForWrite)
            {
                for (int i = 0; i < count; i++)
                {
                    DaRequest request = Requests[i] as DaRequest;
                    if (request != null)
                    {
                        MyDaAddressSpaceElement element = request.AddressSpaceElement as MyDaAddressSpaceElement;
                        if (element != null)
                        {
                            ValueQT valueQT = request.Value;
                            request.Result = (EnumResultCode)element.ValueChanged(valueQT);
                        }                               //	end if

                        DaCreateItemsCommand createCommand = request.AddressSpaceElement as DaCreateItemsCommand;
                        if (createCommand != null)
                        {
                            ValueQT valueQT = request.Value;
                            // Execute create items command
                            EnumResultCode result = createCommand.CreateItems(valueQT);
                            request.Result = (EnumResultCode)createCommand.ValueChanged(valueQT);

                            if (result != EnumResultCode.S_OK)
                            {
                                request.Result = result;
                            }
                        }

                        DaDeleteItemsCommand deleteCommand = request.AddressSpaceElement as DaDeleteItemsCommand;
                        if (deleteCommand != null)
                        {
                            // Execute delete items command
                            request.Result = deleteCommand.DeleteItems();
                        }

                        DaStartStopSimulationCommand simulationCommand = request.AddressSpaceElement as DaStartStopSimulationCommand;
                        if (simulationCommand != null)
                        {
                            ValueQT valueQT = request.Value;
                            request.Result = (EnumResultCode)simulationCommand.ValueChanged(valueQT);

                            EnumResultCode result;
                            bool           startCommand = (bool)valueQT.Data;

                            if (startCommand)
                            {
                                // Execute Start simulation command
                                result = simulationCommand.StartSimulation();
                            }
                            else
                            {
                                // Execute Stop simulation command
                                result = simulationCommand.StopSimulation();
                            }

                            if (result != EnumResultCode.S_OK)
                            {
                                request.Result = result;
                            }
                        }
                    }                   //	end if
                }                       //	end for
            }
            return(CompleteRequests());
        }               //	end HandleWriteRequests