Exemple #1
0
        public void                     setValue(object aValue)
        {
            if (ValuesCompare.isNotEqual(mValue, aValue))
            {
                bool lTypeChanged = true;
                if (mValue != null)
                {
                    lTypeChanged = (mValue.GetType() != aValue.GetType());
                }
                else
                {
                    lTypeChanged = true;
                }

                mValue = aValue;
                if (Access.HasFlag(EAccess.READ))
                {
                    raiseValueChanged();
                }

                if (lTypeChanged)
                {
                    raisePropertiesChanged();
                }
            }
        }
        public void                 setValue(object aNewValue)
        {
            bool lValueChanged = false;

            Monitor.Enter(mValueLock);
            //=========================================
            try
            {
                if (mNeedWrite)
                {
                    return;
                }

                if (ValuesCompare.isNotEqual(mValue, aNewValue))
                {
                    mValue        = aNewValue;
                    lValueChanged = true;
                }
            }
            finally
            {
                //=========================================
                Monitor.Exit(mValueLock);
            }

            if (lValueChanged)
            {
                raiseValueChanged();
            }
        }
        private void                    updateV()
        {
            if (mHolder.Count > 0)
            {
                var lNames = mHolder.Names;
                for (int i = 0; i < lNames.Length; i++)
                {
                    if (ValuesCompare.isEqual(mHolder.getValue(lNames[i]), mObjectValue.ValueObject))
                    {
                        mNoUpdate = true;
                        try
                        {
                            comboBox.SelectedIndex = i;
                        }
                        finally
                        {
                            mNoUpdate = false;
                        }
                        return;
                    }
                }
            }

            comboBox.SelectedIndex = -1;
        }
 private void            dataGridView_Thd_SelectionChanged(object aSender, EventArgs aEventArgs)
 {
     if (dataGridView_Thd.SelectedRows.Count != 0)
     {
         int RowIndex = dataGridView_Thd.SelectedRows[0].Index;
         comboBox_Operation.SelectedIndex = ValuesCompare.OperationNameToNumber(dataGridView_Thd.Rows[RowIndex].Cells[0].Value.ToString());
         textBox_ThdValue.Text            = StringUtils.ObjectToString(dataGridView_Thd.Rows[RowIndex].Cells[1].Value);
         itemEditBox_Thd.ItemName         = StringUtils.ObjectToString(dataGridView_Thd.Rows[RowIndex].Cells[2].Value);
         itemEditBox_Thd.ItemToolTip      = mBrowser.getItemToolTipByHandle(mBrowser.getItemHandleByName(itemEditBox_Thd.ItemName));
     }
 }
        public void                         onItemValueChange(int aItemHandle, object aItemValue)
        {
            if (aItemHandle == mOnItemHandle)
            {
                bool lValue;
                try
                {
                    lValue = Convert.ToBoolean(aItemValue);
                }
                catch (Exception lExc)
                {
                    throw new ArgumentException("Value conversion error for object to switch on. ", lExc);
                }

                if (mOn != lValue)
                {
                    mOn = lValue;
                    raiseValuesChanged();
                    push();
                    correctInterval();
                }

                return;
            }

            if (aItemHandle == mInValueItemHandle)
            {
                if (ValuesCompare.isNotEqual(aItemValue, mInValue))
                {
                    mInValue = aItemValue;
                    raiseValuesChanged();
                    push();
                    correctInterval();
                }
                return;
            }

            if (aItemHandle == mOutValueItemHandle)
            {
                if (ValuesCompare.isNotEqual(aItemValue, mOutValue))
                {
                    mValueChanged = true;
                }

                return;
            }
        }
Exemple #6
0
        private void                                    NotificationReceived(IList <MonitoredItemNotification> aDataChanges)
        {
            MonitoredItem lMItem;
            DataItem      lItem;
            int           lCount = aDataChanges.Count;

            for (int i = 0; i < lCount; i++)
            {
                mItemListLock.EnterReadLock();
                //========================================
                try
                {
                    lMItem = mSubscription.FindItemByClientHandle(aDataChanges[i].ClientHandle);
                    if (lMItem != null)
                    {
                        lItem = lMItem.Handle as DataItem;

                        if (aDataChanges[i].Value.StatusCode != lItem.StatusCode)
                        {
                            lItem.StatusCode = aDataChanges[i].Value.StatusCode;
                            lItem.raisePropertiesChanged();
                        }

                        if (aDataChanges[i].Value.Value != null)
                        {
                            if (ValuesCompare.isNotEqual(lItem.mValue, aDataChanges[i].Value.Value))
                            {
                                lItem.mValue = aDataChanges[i].Value.Value;
                                if (lItem.Access.HasFlag(EAccess.READ))
                                {
                                    lItem.raiseValueChanged();
                                }
                            }
                        }
                    }
                }
                catch (Exception lExc)
                {
                    raiseConnectionError("Error updating value. " + lExc.ToString());
                }
                finally
                {
                    //========================================
                    mItemListLock.ExitReadLock();
                }
            }
        }
Exemple #7
0
 private void                    updateV()
 {
     if (mHolder.Count > 0)
     {
         var lNames = mHolder.Names;
         mNoUpdate = true;
         try
         {
             for (int i = 0; i < lNames.Length; i++)
             {
                 mRB[i].Checked = ValuesCompare.isEqual(mHolder.getValue(lNames[i]), mObjectValue.ValueObject);
             }
         }
         finally
         {
             mNoUpdate = false;
         }
     }
 }
 private bool                toBool(object aValue)
 {
     if (ValuesCompare.isEqual(mTrueValue, MiscUtils.convertValue(mTrueValue.GetType(), aValue)))
     {
         return(true);
     }
     else if (ValuesCompare.isEqual(mFalseValue, MiscUtils.convertValue(mFalseValue.GetType(), aValue)))
     {
         return(false);
     }
     else
     {
         if (mTrueIfWrong)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
        public void                 checkValues(object aTrueValue, object aFalseValue)
        {
            Type lTrueType  = aTrueValue.GetType();
            Type lFalseType = aFalseValue.GetType();

            if (lTrueType.Equals(lFalseType) == false)
            {
                throw new ArgumentException("True and False Values should be the same type. ");
            }

            if (lTrueType.IsArray)
            {
                if (lTrueType.GetElementType().Equals(lFalseType.GetElementType()) == false)
                {
                    throw new ArgumentException("True and False Values should be the same type. ");
                }
            }

            if (ValuesCompare.isEqual(aTrueValue, aFalseValue))
            {
                throw new ArgumentException("True and False Values should not be equal. ");
            }
        }
Exemple #10
0
        public void                 setValue(SDataValue aValue)
        {
            if (mNeedWrite)
            {
                return;
            }

            mPDataType = aValue.Type;

            object lNewValue;

            switch (aValue.Type)
            {
            case EPrimitiveDataType.Bool:   lNewValue = aValue.Bool; break;

            case EPrimitiveDataType.Int8:   lNewValue = aValue.Int8; break;

            case EPrimitiveDataType.Int16:  lNewValue = aValue.Int16; break;

            case EPrimitiveDataType.Int32:  lNewValue = aValue.Int32; break;

            case EPrimitiveDataType.Int64:  lNewValue = aValue.Int64; break;

            case EPrimitiveDataType.UInt8:  lNewValue = aValue.UInt8; break;

            case EPrimitiveDataType.UInt16: lNewValue = aValue.UInt16; break;

            case EPrimitiveDataType.UInt32: lNewValue = aValue.UInt32; break;

            case EPrimitiveDataType.UInt64: lNewValue = aValue.UInt64; break;

            case EPrimitiveDataType.Float:  lNewValue = aValue.Float; break;

            case EPrimitiveDataType.Double: lNewValue = aValue.Double; break;

            case EPrimitiveDataType.Char:   lNewValue = aValue.Char; break;

            case EPrimitiveDataType.WChar:  lNewValue = aValue.WChar; break;

            default:                        throw new InvalidOperationException("Type is not supported. ");
            }

            bool lValueChanged = false;

            Monitor.Enter(mValueLock);
            //=========================================
            try
            {
                if (mNeedWrite)
                {
                    return;
                }

                if (ValuesCompare.isNotEqual(mValue, lNewValue))
                {
                    mValue        = lNewValue;
                    lValueChanged = true;
                }
            }
            finally
            {
                //=========================================
                Monitor.Exit(mValueLock);
            }

            if (lValueChanged)
            {
                raiseValueChanged();
            }
        }
        public void                         onItemValueChange(int aItemHandle, object aItemValue)
        {
            if (mArrayValue.Length == 0)
            {
                return;
            }

            if (mDataFlow.HasFlag(EDataFlow.FROM))
            {
                if (aItemHandle == mArrayValueItemHandle)
                {
                    Array lArray = aItemValue as Array;

                    if (lArray != null)
                    {
                        for (int i = 0; i < lArray.Length; i++)
                        {
                            if (i == mArrayValue.Length)
                            {
                                return;
                            }

                            if (ValuesCompare.isNotEqual(lArray.GetValue(i), mArrayValue[i]))
                            {
                                mArrayValue[i] = lArray.GetValue(i);
                                if (mElementItemHandles[i] != -1)
                                {
                                    mElementValueChanged[i] = true;
                                    mValueChanged           = true;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (ValuesCompare.isNotEqual(aItemValue, mArrayValue[0]))
                        {
                            mArrayValue[0] = aItemValue;
                            if (mElementItemHandles[0] != -1)
                            {
                                mElementValueChanged[0] = true;
                                mValueChanged           = true;
                            }
                        }
                    }

                    return;
                }

                if (mDataFlow.HasFlag(EDataFlow.TO) == false)
                {
                    for (int i = 0; i < mElementItemHandles.Length; i++)
                    {
                        if (aItemHandle == mElementItemHandles[i])
                        {
                            mElementValueChanged[i] = true;
                            mValueChanged           = true;
                            return;
                        }
                    }
                }
            }

            if (mDataFlow.HasFlag(EDataFlow.TO))
            {
                if (mDataFlow.HasFlag(EDataFlow.FROM) == false && aItemHandle == mArrayValueItemHandle)
                {
                    mArrayValueChanged = true;
                    mValueChanged      = true;
                    return;
                }

                for (int i = 0; i < mElementItemHandles.Length; i++)
                {
                    if (aItemHandle == mElementItemHandles[i])
                    {
                        if (ValuesCompare.isNotEqual(aItemValue, mArrayValue[i]))
                        {
                            mArrayValue[i]     = aItemValue;
                            mArrayValueChanged = true;
                            mValueChanged      = true;
                        }
                        return;
                    }
                }
            }
        }
Exemple #12
0
        public void                                         onItemValueChange(int aItemHandle, object aItemValue)
        {
            if (aItemHandle == mXValueItemHandle)
            {
                int lValue;
                try
                {
                    lValue = Convert.ToInt32(aItemValue);
                }
                catch (Exception lExc)
                {
                    throw new ArgumentException("X value conversion error. ", lExc);
                }

                if (mXValue != lValue)
                {
                    mXValue = lValue;
                    raiseValuesChanged();
                }

                return;
            }

            if (aItemHandle == mYValueItemHandle)
            {
                int lValue;
                try
                {
                    lValue = Convert.ToInt32(aItemValue);
                }
                catch (Exception lExc)
                {
                    throw new ArgumentException("Y value conversion error. ", lExc);
                }

                if (mYValue != lValue)
                {
                    mYValue = lValue;
                    raiseValuesChanged();
                }

                return;
            }

            if (aItemHandle == mVisibleItemHandle)
            {
                bool lValue;
                try
                {
                    lValue = Convert.ToBoolean(aItemValue);
                }
                catch (Exception lExc)
                {
                    throw new ArgumentException("Value conversion error for visibility state. ", lExc);
                }

                if (mVisible != lValue)
                {
                    mVisible = lValue;
                    raiseValuesChanged();
                }

                return;
            }

            if (aItemHandle == mWidthItemHandle)
            {
                int lValue;
                try
                {
                    lValue = Convert.ToInt32(aItemValue);
                }
                catch (Exception lExc)
                {
                    throw new ArgumentException("Width value conversion error. ", lExc);
                }

                if (lValue > MaxWidth)
                {
                    lValue = MaxWidth;
                }

                if (mWidth != lValue)
                {
                    mWidth = lValue;
                    updatePanelBitmap();
                    raisePropertiesChanged();
                }

                return;
            }

            if (aItemHandle == mHeightItemHandle)
            {
                int lValue;
                try
                {
                    lValue = Convert.ToInt32(aItemValue);
                }
                catch (Exception lExc)
                {
                    throw new ArgumentException("Height value conversion error. ", lExc);
                }

                if (lValue > MaxHeight)
                {
                    lValue = MaxHeight;
                }

                if (mHeight != lValue)
                {
                    mHeight = lValue;
                    updatePanelBitmap();
                    raisePropertiesChanged();
                }

                return;
            }

            if (aItemHandle == mLabelItemHandle)
            {
                string lValue = StringUtils.ObjectToString(aItemValue);

                if (lValue.Equals(mLabel, StringComparison.Ordinal) == false)
                {
                    mLabel = lValue;
                    updatePanelBitmap();
                    raisePropertiesChanged();
                }

                return;
            }

            if (aItemHandle == mRotateItemHandle)
            {
                float lValue;
                try
                {
                    lValue = Convert.ToSingle(aItemValue);
                }
                catch (Exception lExc)
                {
                    throw new ArgumentException("Ratation angle value conversion error. ", lExc);
                }

                if (ValuesCompare.isNotEqual(mRotate, lValue))
                {
                    mRotate = lValue;
                    updatePanelBitmap();
                    raisePropertiesChanged();
                }

                return;
            }
        }
        public void                         add(string aName, object aValue, bool mReplaceDefault)
        {
            #region Name

            if (String.IsNullOrWhiteSpace(aName))
            {
                throw new ArgumentException("Name is empty. ");
            }

            if (mReservedWords.Contains(aName))
            {
                throw new ArgumentException("Name is reserved. ");
            }

            mNameValidator?.Invoke(aName);

            if (mNameValue.ContainsKey(aName) == true)
            {
                if (mReplaceDefault && aName.Equals("Current value", StringComparison.Ordinal))
                {
                    mName.Remove("Current value");
                    mNameValue.Remove("Current value");
                }
                else
                {
                    throw new ArgumentException("Name '" + aName + "' already exists. ");
                }
            }

            #endregion

            #region Value

            if (aValue == null)
            {
                throw new ArgumentException("Null value is not allowed. ");
            }

            int lCount = mName.Count;
            for (int i = 0; i < lCount; i++)
            {
                if (ValuesCompare.isEqual(mNameValue[mName[i]], aValue))
                {
                    if (mReplaceDefault && mName[i].Equals("Current value", StringComparison.Ordinal))
                    {
                        mName.Remove("Current value");
                        mNameValue.Remove("Current value");
                        break;
                    }
                    else
                    {
                        if (mAllowSameValue == false)
                        {
                            throw new ArgumentException("Value '" + StringUtils.ObjectToString(aValue) + "' already exists. ");
                        }
                    }
                }
            }

            #endregion

            mName.Add(aName);
            mNameValue.Add(aName, aValue);

            raiseListChanged();
        }
        public void                         modify(string aOldName, string aNewName, object aValue)
        {
            #region Name

            if (mNameValue.ContainsKey(aOldName) == false)
            {
                throw new ArgumentException("Name '" + aOldName + "' does not exist. ");
            }

            if (String.IsNullOrWhiteSpace(aNewName))
            {
                throw new ArgumentException("New name is empty. ");
            }

            if (mReservedWords.Contains(aNewName))
            {
                throw new ArgumentException("Name is reserved. ");
            }

            mNameValidator?.Invoke(aNewName);

            if (aOldName.Equals(aNewName, StringComparison.Ordinal) == false)
            {
                if (mNameValue.ContainsKey(aNewName))
                {
                    throw new ArgumentException("Name '" + aNewName + "' already exists. ");
                }
            }

            #endregion

            #region Value

            if (aValue == null)
            {
                throw new ArgumentException("Null value is not allowed. ");
            }

            if (mAllowSameValue == false)
            {
                int lCount = mName.Count;
                for (int i = 0; i < lCount; i++)
                {
                    if (aOldName.Equals(mName[i], StringComparison.Ordinal) == false)
                    {
                        if (ValuesCompare.isEqual(mNameValue[mName[i]], aValue))
                        {
                            throw new ArgumentException("Value '" + StringUtils.ObjectToString(aValue) + "' already exists. ");
                        }
                    }
                }
            }

            #endregion

            mName[mName.IndexOf(aOldName)] = aNewName;
            mNameValue.Remove(aOldName);
            mNameValue.Add(aNewName, aValue);

            raiseListChanged();
        }
        private void                                    MSession_Notification(Session aSession, NotificationEventArgs aNotificationEventArgs)
        {
            if (mConnected == false || mItemList.Count == 0)
            {
                return;
            }

            var lMessage = aNotificationEventArgs.NotificationMessage;

            if (lMessage.NotificationData.Count == 0)
            {
                return;
            }

            var lDataChanges = lMessage.GetDataChanges(false);

            MonitoredItem lMItem;
            DataItem      lItem;
            int           lCount = lDataChanges.Count;

            for (int i = 0; i < lCount; i++)
            {
                mItemListLock.EnterReadLock();
                //========================================
                try
                {
                    lMItem = mSubscription.FindItemByClientHandle(lDataChanges[i].ClientHandle);
                    if (lMItem != null)
                    {
                        lItem = lMItem.Handle as DataItem;

                        if (lDataChanges[i].Value.StatusCode != lItem.StatusCode)
                        {
                            lItem.StatusCode = lDataChanges[i].Value.StatusCode;
                            lItem.raisePropertiesChanged();
                        }

                        if (lDataChanges[i].Value.Value != null)
                        {
                            if (ValuesCompare.isNotEqual(lItem.mValue, lDataChanges[i].Value.Value))
                            {
                                lItem.mValue = lDataChanges[i].Value.Value;
                                if (lItem.Access.HasFlag(EAccess.READ))
                                {
                                    lItem.raiseValueChanged();
                                }
                            }
                        }
                    }
                }
                catch (Exception lExc)
                {
                    raiseConnectionError("Error updating values. " + lExc.ToString());
                }
                finally
                {
                    //========================================
                    mItemListLock.ExitReadLock();
                }
            }
        }
        public void                 setValueFromPLC(byte[] aBytes)
        {
            if (mNeedWrite)
            {
                return;
            }

            bool   lValueChanged = false;
            object lNewValue     = aBytes;

            if (mLength == 1)
            {
                if (mDataType == EWordlen.S7_Bit)
                {
                    lNewValue = (aBytes[0] > 0);
                }
                else if (mDataType == EWordlen.S7_Byte)
                {
                    if (mSigned)
                    {
                        lNewValue = unchecked ((sbyte)aBytes[0]);
                    }
                    else
                    {
                        lNewValue = aBytes[0];
                    }
                }
                else if (mDataType == EWordlen.S7_Word)
                {
                    Array.Reverse(aBytes);

                    if (mSigned)
                    {
                        lNewValue = BitConverter.ToInt16(aBytes, 0);
                    }
                    else
                    {
                        lNewValue = BitConverter.ToUInt16(aBytes, 0);
                    }
                }
                else if (mDataType == EWordlen.S7_DoubleWord)
                {
                    Array.Reverse(aBytes);
                    if (mFloatingP)
                    {
                        lNewValue = BitConverter.ToSingle(aBytes, 0);
                    }
                    else
                    {
                        if (mSigned)
                        {
                            lNewValue = BitConverter.ToInt32(aBytes, 0);
                        }
                        else
                        {
                            lNewValue = BitConverter.ToUInt32(aBytes, 0);
                        }
                    }
                }
            }
            else
            {
                if (mDataType == EWordlen.S7_Byte)
                {
                    if (mSigned)
                    {
                        sbyte[] lArray = new sbyte[mLength];
                        for (int i = 0; i < mLength; i++)
                        {
                            lArray[i] = unchecked ((sbyte)aBytes[i]);
                        }
                        lNewValue = lArray;
                    }
                    else
                    {
                        lNewValue = aBytes;
                    }
                }
                else if (mDataType == EWordlen.S7_Word)
                {
                    byte[] lValue = new byte[2];
                    int    lIndex = 0;

                    if (mSigned)
                    {
                        short[] lArray = new short[mLength];

                        for (int i = 0; i < mLength; i++)
                        {
                            Array.Copy(aBytes, lIndex, lValue, 0, 2);
                            Array.Reverse(lValue);
                            lArray[i] = BitConverter.ToInt16(lValue, 0);
                            lIndex    = lIndex + 2;
                        }

                        lNewValue = lArray;
                    }
                    else
                    {
                        ushort[] lArray = new ushort[mLength];

                        for (int i = 0; i < mLength; i++)
                        {
                            Array.Copy(aBytes, lIndex, lValue, 0, 2);
                            Array.Reverse(lValue);
                            lArray[i] = BitConverter.ToUInt16(lValue, 0);
                            lIndex    = lIndex + 2;
                        }

                        lNewValue = lArray;
                    }
                }
                else if (mDataType == EWordlen.S7_DoubleWord)
                {
                    byte[] lValue = new byte[4];
                    int    lIndex = 0;

                    if (mFloatingP)
                    {
                        float[] lArray = new float[mLength];

                        for (int i = 0; i < mLength; i++)
                        {
                            Array.Copy(aBytes, lIndex, lValue, 0, 4);
                            Array.Reverse(lValue);
                            lArray[i] = BitConverter.ToSingle(lValue, 0);
                            lIndex    = lIndex + 4;
                        }

                        lNewValue = lArray;
                    }
                    else
                    {
                        if (mSigned)
                        {
                            int[] lArray = new int[mLength];

                            for (int i = 0; i < mLength; i++)
                            {
                                Array.Copy(aBytes, lIndex, lValue, 0, 4);
                                Array.Reverse(lValue);
                                lArray[i] = BitConverter.ToInt32(lValue, 0);
                                lIndex    = lIndex + 4;
                            }

                            lNewValue = lArray;
                        }
                        else
                        {
                            uint[] lArray = new uint[mLength];

                            for (int i = 0; i < mLength; i++)
                            {
                                Array.Copy(aBytes, lIndex, lValue, 0, 4);
                                Array.Reverse(lValue);
                                lArray[i] = BitConverter.ToUInt32(lValue, 0);
                                lIndex    = lIndex + 4;
                            }

                            lNewValue = lArray;
                        }
                    }
                }
            }

            Monitor.Enter(mValueLock);
            //=========================================
            try
            {
                if (mNeedWrite)
                {
                    return;
                }

                if (ValuesCompare.isNotEqual(mValue, lNewValue))
                {
                    mValue        = lNewValue;
                    lValueChanged = true;
                }
            }
            finally
            {
                //=========================================
                Monitor.Exit(mValueLock);
            }

            if (lValueChanged)
            {
                raiseValueChanged();
            }
        }