Exemple #1
0
        /// <summary>
        /// Sets the variable value.
        /// </summary>
        private void SetVariable(VarItem varItem, CnlData[] cnlData, int dataIndex, DateTime timestamp)
        {
            try
            {
                DeviceTag deviceTag  = varItem.DeviceTag;
                int       dataLength = deviceTag.DataLength;
                object    value      = 0.0;

                switch (deviceTag.DataType)
                {
                case TagDataType.Double:
                    value = deviceTag.IsArray ?
                            (object)CnlDataConverter.GetDoubleArray(cnlData, dataIndex, dataLength) :
                            CnlDataConverter.GetDouble(cnlData, dataIndex);
                    break;

                case TagDataType.Int64:
                    value = deviceTag.IsArray ?
                            (object)CnlDataConverter.GetInt64Array(cnlData, dataIndex, dataLength) :
                            CnlDataConverter.GetInt64(cnlData, dataIndex);
                    break;

                case TagDataType.ASCII:
                    value = CnlDataConverter.GetAscii(cnlData, dataIndex, dataLength);
                    break;

                case TagDataType.Unicode:
                    value = CnlDataConverter.GetUnicode(cnlData, dataIndex, dataLength);
                    break;
                }

                BaseDataVariableState variable = varItem.Variable;
                variable.Value      = value;
                variable.StatusCode = CnlDataConverter.GetStatus(cnlData, dataIndex, dataLength) > CnlStatusID.Undefined ?
                                      StatusCodes.Good : StatusCodes.Bad;
                variable.Timestamp = timestamp;
                variable.ClearChangeMasks(SystemContext, false);
            }
            catch (Exception ex)
            {
                log.WriteException(ex, Locale.IsRussian ?
                                   "Ошибка при установке значения переменной {0}" :
                                   "Error setting the variable {0}", varItem.Variable.NodeId);
            }
        }
Exemple #2
0
        /// <summary>
        /// Builds a payload based on the device tag data to publish.
        /// </summary>
        public string BuildPayload(DeviceTag deviceTag, CnlData[] cnlData, int dataIndex)
        {
            int  dataLength    = deviceTag.DataLength;
            int  stat          = CnlDataConverter.GetStatus(cnlData, dataIndex, dataLength);
            bool formatIsEmpty = string.IsNullOrEmpty(dsOptions.PublishOptions.PublishFormat);

            if (formatIsEmpty && stat <= CnlStatusID.Undefined)
            {
                return(dsOptions.PublishOptions.UndefinedValue);
            }

            string valStr = "";

            switch (deviceTag.DataType)
            {
            case TagDataType.Double:
                valStr = deviceTag.IsNumericArray
                        ? ArrayToString(CnlDataConverter.GetDoubleArray(cnlData, dataIndex, dataLength))
                        : CnlDataConverter.GetDouble(cnlData, dataIndex).ToString(NumberFormatInfo.InvariantInfo);
                break;

            case TagDataType.Int64:
                valStr = deviceTag.IsNumericArray
                        ? ArrayToString(CnlDataConverter.GetInt64Array(cnlData, dataIndex, dataLength))
                        : CnlDataConverter.GetInt64(cnlData, dataIndex).ToString(NumberFormatInfo.InvariantInfo);
                break;

            case TagDataType.ASCII:
                valStr = CnlDataConverter.GetAscii(cnlData, dataIndex, dataLength);
                break;

            case TagDataType.Unicode:
                valStr = CnlDataConverter.GetUnicode(cnlData, dataIndex, dataLength);
                break;
            }

            return(formatIsEmpty
                ? valStr
                : MqttUtils.FormatPayload(dsOptions.PublishOptions.PublishFormat, valStr, stat));
        }