Ejemplo n.º 1
0
        public void Register <TIn, TOut>(string path, DataDelegate <TIn, TOut> callback) where TIn : new()
        {
            _dataCallbacks[path] = async(devId, strm) =>
            {
                TIn data;
                try
                {
                    data = PayloadHelper.ReadData <TIn>(strm);
                }
                catch (Exception e)
                {
                    _logger.Warn(e, $"Error to deserialize input data '{path}'. Payload type {typeof(TIn).Name}");
                    await SendError(devId, path, ErrorType.ArgsError, e.Message, CancellationToken.None);

                    return;
                }

                try
                {
                    var result = await callback(devId, data);
                    await SendResult(devId, path, result, CancellationToken.None);
                }
                catch (Exception e)
                {
                    Status.Error($"Execute {path}:{e.Message}");
                    _logger.Warn(e, $"Error to execute '{path}':{e.Message}");
                    await SendError(devId, path, ErrorType.InternalError, e.Message, CancellationToken.None);
                }
            };
        }
Ejemplo n.º 2
0
        private void OnData(V2ExtensionPacket v2ExtensionPacket)
        {
            try
            {
                using (var ms = new MemoryStream(v2ExtensionPacket.Payload.Payload))
                {
                    var header = PayloadHelper.ReadData <PayloadPacketHeader>(ms);
                    if (FilterDoublePackets(header) == false)
                    {
                        Interlocked.Increment(ref _doublePacketsCount);
                        return;
                    }


                    if (!_dataCallbacks.TryGetValue(header.Path, out var callback))
                    {
                        //_logger.Warn($"Receive unsupported data '{packetInfo.Path}'");
                        return;
                    }
                    callback(new DeviceIdentity {
                        ComponentId = v2ExtensionPacket.ComponenId, SystemId = v2ExtensionPacket.SystemId
                    }, ms);
                }
            }
            catch (Exception e)
            {
                _logger.Warn($"Error execute data:{e.Message}");
            }
        }
Ejemplo n.º 3
0
        public IObservable <Result <TOut> > Register <TOut>(string path) where TOut : new()
        {
            return(_onData.Where(_ => _.Header.Path == path).Select(_ =>
            {
                using (var strm = new MemoryStream(_.Data))
                {
                    try
                    {
                        if (!path.Equals(_.Header.Path))
                        {
                            return default(Result <TOut>);
                        }
                        if (_.MessageType == PayloadHelper.DefaultErrorMessageType)
                        {
                            var err = PayloadHelper.ReadData <ErrorCode>(strm);
                            return new Result <TOut>
                            {
                                Error = err,
                                IsError = true,
                            };
                        }

                        if (_.MessageType == PayloadHelper.DefaultSuccessMessageType)
                        {
                            var data = PayloadHelper.ReadData <TOut>(strm);
                            return new Result <TOut>
                            {
                                IsError = false,
                                Value = data,
                            };
                        }

                        return default(Result <TOut>);
                    }
                    catch (Exception e)
                    {
                        _logger.Warn(e, $"Error to deserialize input data '{path}'. Payload type {typeof(TOut).Name}");
                        return default(Result <TOut>);
                    }
                }
            }).Where(_ => !object.Equals(_, default(TOut))));
        }
Ejemplo n.º 4
0
        private void OnData(V2ExtensionPacket v2ExtensionPacket)
        {
            try
            {
                Interlocked.Increment(ref _txPacketsCounter);

                PayloadPacketHeader packetInfo = null;

                using (var ms = new MemoryStream(v2ExtensionPacket.Payload.Payload))
                {
                    packetInfo = PayloadHelper.ReadData <PayloadPacketHeader>(ms);

                    if (FilterDoublePackets(packetInfo) == false)
                    {
                        Interlocked.Increment(ref _doublePacketsCounter);
                        return;
                    }


                    var data = new byte[ms.Length - ms.Position];
                    ms.Read(data, 0, data.Length);
                    var pkt = new V2Packet
                    {
                        Device = new DeviceIdentity {
                            ComponentId = v2ExtensionPacket.ComponenId, SystemId = v2ExtensionPacket.SystemId
                        },
                        Header      = packetInfo,
                        Data        = data,
                        MessageType = v2ExtensionPacket.Payload.MessageType,
                    };
                    _onData.OnNext(pkt);
                }
            }
            catch (Exception e)
            {
                _logger.Warn($"Error execute data:{e.Message}");
            }
        }