示例#1
0
        public async Task GetComponentsItems(DeltaComponent component)
        {
            var componentItemsPtr = await Task.Run(() => _native.TmcDntOpenItem(_cid,
                                                                                (uint)component.TraceChain.Length,
                                                                                component.TraceChain))
                                    .ConfigureAwait(false);

            if (componentItemsPtr == IntPtr.Zero)
            {
                return;
            }

            component.ClearItems();

            while (true)
            {
                var itemPtr = await Task.Run(() => _native.TmcDntGetNextItem(componentItemsPtr))
                              .ConfigureAwait(false);

                if (itemPtr == IntPtr.Zero)
                {
                    break;
                }

                var deltaCommonStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaCommon>(itemPtr);

                switch ((TmNativeDefs.DeltaItemTypes)deltaCommonStruct.Type)
                {
                case TmNativeDefs.DeltaItemTypes.Description:
                    var descriptionStruct     = Marshal.PtrToStructure <TmNativeDefs.DeltaDescription>(itemPtr);
                    var descriptionStructSize = Marshal.SizeOf(descriptionStruct);

                    var descriptionString =
                        EncodingUtil.Win1251ToUtf8(Marshal.PtrToStringAnsi(IntPtr.Add(itemPtr, descriptionStructSize - 1)));

                    if (descriptionStruct.Text[0] == '*')
                    {
                        component.Items.Add(DeltaItem.CreateDescriptionDeltaItem(descriptionString));
                    }
                    else
                    {
                        component.Description += descriptionString + Environment.NewLine;
                    }

                    break;

                case TmNativeDefs.DeltaItemTypes.Status:
                    var statusStruct     = Marshal.PtrToStructure <TmNativeDefs.DeltaStatus>(itemPtr);
                    var statusStructSize = Marshal.SizeOf(statusStruct);

                    var numStatus = GetDeltaItemNum(itemPtr,
                                                    statusStructSize,
                                                    statusStruct.Length,
                                                    statusStruct.Number);

                    var addStringStatus = GetDeltaItemAdditionalText(itemPtr,
                                                                     statusStructSize,
                                                                     statusStruct.Length);

                    var tmAddrStatus = statusStruct.TmsRtu == 0 || statusStruct.TmsRtu == 0
                                 ? null
                                 : new TmAddr(TmType.Status,
                                              statusStruct.TmsChn,
                                              statusStruct.TmsRtu,
                                              statusStruct.TmsPoint);

                    var statusObjectName = await GetObjectName(tmAddrStatus).ConfigureAwait(false);

                    component.Items.Add(DeltaItem.CreateStatusDeltaItem(numStatus,
                                                                        statusStruct.LastUpdate,
                                                                        (TmNativeDefs.DeltaItemsFlags)statusStruct.DeltaFlags,
                                                                        statusStruct.Value,
                                                                        addStringStatus,
                                                                        tmAddrStatus,
                                                                        statusObjectName));

                    break;

                case TmNativeDefs.DeltaItemTypes.Analog:
                    var analogStruct     = Marshal.PtrToStructure <TmNativeDefs.DeltaAnalog>(itemPtr);
                    var analogStructSize = Marshal.SizeOf(analogStruct);

                    var numAnalog = GetDeltaItemNum(itemPtr,
                                                    analogStructSize,
                                                    analogStruct.Length,
                                                    analogStruct.Number);

                    var addStringAnalog = GetDeltaItemAdditionalText(itemPtr,
                                                                     analogStructSize,
                                                                     analogStruct.Length);
                    var tmAddrAnalog = analogStruct.TmsRtu == 0 || analogStruct.TmsRtu == 0
                                 ? null
                                 : new TmAddr(TmType.Analog,
                                              analogStruct.TmsChn,
                                              analogStruct.TmsRtu,
                                              analogStruct.TmsPoint);

                    var analogObjectName = await GetObjectName(tmAddrAnalog).ConfigureAwait(false);

                    component.Items.Add(DeltaItem.CreateAnalogDeltaItem(numAnalog,
                                                                        analogStruct.LastUpdate,
                                                                        (TmNativeDefs.DeltaItemsFlags)analogStruct.DeltaFlags,
                                                                        analogStruct.Value,
                                                                        addStringAnalog,
                                                                        tmAddrAnalog,
                                                                        analogObjectName));

                    break;

                case TmNativeDefs.DeltaItemTypes.Accum:
                    var accumStruct     = Marshal.PtrToStructure <TmNativeDefs.DeltaAccum>(itemPtr);
                    var accumStructSize = Marshal.SizeOf(accumStruct);

                    var numAccum = GetDeltaItemNum(itemPtr,
                                                   accumStructSize,
                                                   accumStruct.Length,
                                                   accumStruct.Number);

                    var addStringAccum = GetDeltaItemAdditionalText(itemPtr,
                                                                    accumStructSize,
                                                                    accumStruct.Length);

                    var tmAddrAccum = accumStruct.TmsRtu == 0 || accumStruct.TmsRtu == 0
                                ? null
                                : new TmAddr(TmType.Accum,
                                             accumStruct.TmsChn,
                                             accumStruct.TmsRtu,
                                             accumStruct.TmsPoint);

                    var accumObjectName = await GetObjectName(tmAddrAccum).ConfigureAwait(false);

                    component.Items.Add(DeltaItem.CreateAccumDeltaItem(numAccum,
                                                                       accumStruct.LastUpdate,
                                                                       (TmNativeDefs.DeltaItemsFlags)accumStruct.DeltaFlags,
                                                                       accumStruct.Value,
                                                                       addStringAccum,
                                                                       tmAddrAccum,
                                                                       accumObjectName));

                    break;

                case TmNativeDefs.DeltaItemTypes.Control:
                    var controlStruct     = Marshal.PtrToStructure <TmNativeDefs.DeltaControl>(itemPtr);
                    var controlStructSize = Marshal.SizeOf(controlStruct);

                    var numControl = GetDeltaItemNum(itemPtr,
                                                     controlStructSize,
                                                     controlStruct.Length,
                                                     controlStruct.Number);

                    var addStringControl = GetDeltaItemAdditionalText(itemPtr,
                                                                      controlStructSize,
                                                                      controlStruct.Length);

                    var tmAddrControl = controlStruct.TmsRtu == 0 || controlStruct.TmsRtu == 0
                                  ? null
                                  : new TmAddr(TmType.Status,
                                               controlStruct.TmsChn,
                                               controlStruct.TmsRtu,
                                               controlStruct.TmsPoint);

                    var controlObjectName = await GetObjectName(tmAddrControl).ConfigureAwait(false);

                    component.Items.Add(DeltaItem.CreateControlDeltaItem(numControl,
                                                                         controlStruct.LastUpdate,
                                                                         (TmNativeDefs.DeltaItemsFlags)controlStruct
                                                                         .DeltaFlags,
                                                                         controlStruct.CtrlBlock,
                                                                         controlStruct.CtrlGroup,
                                                                         controlStruct.CtrlPoint,
                                                                         addStringControl,
                                                                         tmAddrControl,
                                                                         controlObjectName));

                    break;

                case TmNativeDefs.DeltaItemTypes.AnalogF:
                    var analogFStruct     = Marshal.PtrToStructure <TmNativeDefs.DeltaAnalogF>(itemPtr);
                    var analogFStructSize = Marshal.SizeOf(analogFStruct);

                    var numAnalogF = GetDeltaItemNum(itemPtr,
                                                     analogFStructSize,
                                                     analogFStruct.Length,
                                                     analogFStruct.Number);

                    var addStringAnalogF = GetDeltaItemAdditionalText(itemPtr,
                                                                      analogFStructSize,
                                                                      analogFStruct.Length);
                    var tmAddrAnalogF = analogFStruct.TmsRtu == 0 || analogFStruct.TmsRtu == 0
                                  ? null
                                  : new TmAddr(TmType.Analog,
                                               analogFStruct.TmsChn,
                                               analogFStruct.TmsRtu,
                                               analogFStruct.TmsPoint);

                    var analogFObjectName = await GetObjectName(tmAddrAnalogF).ConfigureAwait(false);

                    component.Items.Add(DeltaItem.CreateAnalogFloatDeltaItem(numAnalogF,
                                                                             analogFStruct.LastUpdate,
                                                                             (TmNativeDefs.DeltaItemsFlags)analogFStruct
                                                                             .DeltaFlags,
                                                                             analogFStruct.Value,
                                                                             addStringAnalogF,
                                                                             tmAddrAnalogF,
                                                                             analogFObjectName));
                    break;

                case TmNativeDefs.DeltaItemTypes.AccumF:
                    var accumFStruct     = Marshal.PtrToStructure <TmNativeDefs.DeltaAccumF>(itemPtr);
                    var accumFStructSize = Marshal.SizeOf(accumFStruct);

                    var numAccumF = GetDeltaItemNum(itemPtr,
                                                    accumFStructSize,
                                                    accumFStruct.Length,
                                                    accumFStruct.Number);

                    var addStringAccumF = GetDeltaItemAdditionalText(itemPtr,
                                                                     accumFStructSize,
                                                                     accumFStruct.Length);
                    var tmAddrAccumF = accumFStruct.TmsRtu == 0 || accumFStruct.TmsRtu == 0
                                 ? null
                                 : new TmAddr(TmType.Accum,
                                              accumFStruct.TmsChn,
                                              accumFStruct.TmsRtu,
                                              accumFStruct.TmsPoint);

                    var accumFObjectName = await GetObjectName(tmAddrAccumF).ConfigureAwait(false);

                    component.Items.Add(DeltaItem.CreateAccumFloatDeltaItem(numAccumF,
                                                                            accumFStruct.LastUpdate,
                                                                            (TmNativeDefs.DeltaItemsFlags)accumFStruct
                                                                            .DeltaFlags,
                                                                            accumFStruct.Value,
                                                                            addStringAccumF,
                                                                            tmAddrAccumF,
                                                                            accumFObjectName));
                    break;

                case TmNativeDefs.DeltaItemTypes.StrVal:
                    var strValStruct     = Marshal.PtrToStructure <TmNativeDefs.DeltaStrval>(itemPtr);
                    var strValStructSize = Marshal.SizeOf(strValStruct);

                    var strValValueString =
                        EncodingUtil.Win1251ToUtf8(Marshal.PtrToStringAnsi(IntPtr.Add(itemPtr, strValStructSize - 1)));

                    var structSizeWithValueString = strValStructSize + strValValueString.Length;

                    var strValDescriptionString = strValStruct.Length > structSizeWithValueString
                                            ? EncodingUtil.Win1251ToUtf8(Marshal.PtrToStringAnsi(IntPtr.Add(itemPtr,
                                                                                                            structSizeWithValueString)))
                                            : "";

                    var tmAddrStrVal = strValStruct.TmsRtu == 0 || strValStruct.TmsRtu == 0
                                 ? null
                                 : new TmAddr(TmType.Unknown,
                                              strValStruct.TmsChn,
                                              strValStruct.TmsRtu,
                                              strValStruct.TmsPoint);

                    component.Items.Add(DeltaItem.CreateStrValDeltaItem(strValStruct.Number,
                                                                        strValStruct.LastUpdate,
                                                                        (TmNativeDefs.DeltaItemsFlags)strValStruct.DeltaFlags,
                                                                        strValValueString,
                                                                        strValDescriptionString,
                                                                        tmAddrStrVal));
                    break;

                default:
                    continue;
                }
            }

            await Task.Run(() => _native.TmcDntCloseItem(componentItemsPtr))
            .ConfigureAwait(false);
        }