コード例 #1
0
        public override void addDataHandler(IModuleBoardBridge bridge)
        {
            if (source.eventConfig[2] != DataTypeBase.NO_ID)
            {
                bridge.addDataIdHeader(Tuple.Create(source.eventConfig[0], source.eventConfig[1]));
            }

            if (dataResponseHandler == null)
            {
                Func <Type, object> accounterExtra(uint value)
                {
                    return(type => {
                        if (type == typeof(uint))
                        {
                            return value;
                        }
                        return null;
                    });
                }

                if (source.attributes.copies > 1)
                {
                    byte dataUnitLength = source.attributes.unitLength();
                    dataResponseHandler = response => {
                        DateTime    now         = DateTime.Now;
                        var         accounter   = findParent(bridge.GetModule <IDataProcessor>() as DataProcessor, source, DataProcessor.TYPE_ACCOUNTER);
                        AccountType accountType = accounter != null ? (accounter.Item2.configObj as AccounterConfig).type : AccountType.Time;

                        for (int i = 0, j = source.eventConfig[2] == DataTypeBase.NO_ID ? 2 : 3; i < source.attributes.copies && j < response.Length; i++, j += dataUnitLength)
                        {
                            var    account = fillTimestamp(bridge, accounter, response, j);
                            byte[] dataRaw = new byte[dataUnitLength - (account.Item2 - j)];
                            Array.Copy(response, account.Item2, dataRaw, 0, dataRaw.Length);

                            var data = source.createData(false, bridge, dataRaw, accounter == null || accountType == AccountType.Count ? now : account.Item1);
                            if (accountType == AccountType.Count)
                            {
                                data.extraFn = accounterExtra(account.Item3);
                            }
                            call(data);
                        }
                    };
                }
                else
                {
                    dataResponseHandler = response => {
                        byte[] dataRaw;
                        if (source.eventConfig[2] == DataTypeBase.NO_ID)
                        {
                            dataRaw = new byte[response.Length - 2];
                            Array.Copy(response, 2, dataRaw, 0, dataRaw.Length);
                        }
                        else
                        {
                            dataRaw = new byte[response.Length - 3];
                            Array.Copy(response, 3, dataRaw, 0, dataRaw.Length);
                        }

                        AccountType accountType = AccountType.Time;
                        Tuple <DateTime, int, uint> extra;
                        if (source.eventConfig[0] == (byte)DATA_PROCESSOR && source.eventConfig[1] == DataProcessor.NOTIFY)
                        {
                            var dataprocessor = bridge.GetModule <IDataProcessor>() as DataProcessor;
                            var processor     = dataprocessor.lookupProcessor(source.eventConfig[2]);
                            extra = fillTimestamp(bridge, processor, dataRaw, 0);

                            if (extra.Item2 > 0)
                            {
                                byte[] copy = new byte[dataRaw.Length - extra.Item2];
                                Array.Copy(dataRaw, extra.Item2, copy, 0, copy.Length);
                                dataRaw     = copy;
                                accountType = (processor.Item2.configObj as AccounterConfig).type;
                            }
                        }
                        else
                        {
                            extra = Tuple.Create(DateTime.Now, 0, (uint)0);
                        }

                        var packer = findParent(bridge.GetModule <IDataProcessor>() as DataProcessor, source, DataProcessor.TYPE_PACKER);
                        if (packer != null)
                        {
                            byte   dataUnitLength = packer.Item2.source.attributes.unitLength();
                            byte[] unpacked       = new byte[dataUnitLength];
                            for (int i = 0, j = 3 + extra.Item2; i < packer.Item2.source.attributes.copies && j < response.Length; i++, j += dataUnitLength)
                            {
                                Array.Copy(response, j, unpacked, 0, unpacked.Length);

                                var data = source.createData(false, bridge, unpacked, extra.Item1);
                                if (accountType == AccountType.Count)
                                {
                                    data.extraFn = accounterExtra(extra.Item3);
                                }
                                call(data);
                            }
                        }
                        else
                        {
                            var data = source.createData(false, bridge, dataRaw, extra.Item1);
                            if (accountType == AccountType.Count)
                            {
                                data.extraFn = accounterExtra(extra.Item3);
                            }
                            call(data);
                        }
                    };
                }
            }

            bridge.addDataHandler(source.eventConfigAsTuple(), dataResponseHandler);
        }
コード例 #2
0
        public override void addDataHandler(IModuleBoardBridge bridge)
        {
            if (source.eventConfig[2] != DataTypeBase.NO_ID)
            {
                bridge.addDataIdHeader(Tuple.Create(source.eventConfig[0], source.eventConfig[1]));
            }

            if (dataResponseHandler == null)
            {
                if (source.attributes.copies > 1)
                {
                    byte dataUnitLength = source.attributes.unitLength();
                    dataResponseHandler = response => {
                        DateTime now = DateTime.Now;
                        Tuple <DataTypeBase, EditorImplBase> accounter = findParent(bridge.GetModule <IDataProcessor>() as DataProcessor, source, DataProcessor.TYPE_ACCOUNTER);
                        for (int i = 0, j = source.eventConfig[2] == DataTypeBase.NO_ID ? 2 : 3; i < source.attributes.copies && j < response.Length; i++, j += dataUnitLength)
                        {
                            Tuple <DateTime, int> account = fillTimestamp(bridge, accounter, response, j);
                            byte[] dataRaw = new byte[dataUnitLength - (account.Item2 - j)];
                            Array.Copy(response, account.Item2, dataRaw, 0, dataRaw.Length);
                            call(source.createData(false, bridge, dataRaw, accounter == null ? now : account.Item1));
                        }
                    };
                }
                else
                {
                    dataResponseHandler = response => {
                        byte[] dataRaw;

                        if (source.eventConfig[2] == DataTypeBase.NO_ID)
                        {
                            dataRaw = new byte[response.Length - 2];
                            Array.Copy(response, 2, dataRaw, 0, dataRaw.Length);
                        }
                        else
                        {
                            dataRaw = new byte[response.Length - 3];
                            Array.Copy(response, 3, dataRaw, 0, dataRaw.Length);
                        }

                        Tuple <DateTime, int> account = fillTimestamp(bridge, source, dataRaw);
                        if (account.Item2 > 0)
                        {
                            byte[] copy = new byte[dataRaw.Length - account.Item2];
                            Array.Copy(dataRaw, account.Item2, copy, 0, copy.Length);
                            dataRaw = copy;
                        }

                        Tuple <DataTypeBase, EditorImplBase> packer = findParent(bridge.GetModule <IDataProcessor>() as DataProcessor, source, DataProcessor.TYPE_PACKER);
                        if (packer != null)
                        {
                            byte   dataUnitLength = packer.Item2.source.attributes.unitLength();
                            byte[] unpacked       = new byte[dataUnitLength];
                            for (int i = 0, j = 3 + account.Item2; i < packer.Item2.source.attributes.copies && j < response.Length; i++, j += dataUnitLength)
                            {
                                Array.Copy(response, j, unpacked, 0, unpacked.Length);
                                call(source.createData(false, bridge, unpacked, account.Item1));
                            }
                        }
                        else
                        {
                            call(source.createData(false, bridge, dataRaw, account.Item1));
                        }
                    };
                }
            }

            bridge.addDataHandler(source.eventConfigAsTuple(), dataResponseHandler);
        }