예제 #1
0
        /// <inheritdoc/>
        public override Task <Empty> Write(grpc.WriteMessage request, ServerCallContext context)
        {
            var connectorId = request.ConnectorId.To <ConnectorId>();

            if (!_pullConnectors.Has(connectorId))
            {
                return(Task.FromResult(new Empty()));
            }
            var connector = _pullConnectors.GetById(connectorId);

            _tagDataPointCoordinator.Handle(connector.Name, request.Data);

            return(Task.FromResult(new Empty()));
        }
        /// <inheritdoc/>
        public override async Task <Empty> Open(IAsyncStreamReader <grpc.PushTagDataPoints> requestStream, ServerCallContext context)
        {
            var pushConnectorIdAsString = context.RequestHeaders.SingleOrDefault(_ => string.Equals(_.Key, "pushconnectorid", StringComparison.InvariantCultureIgnoreCase))?.Value;

            if (string.IsNullOrEmpty(pushConnectorIdAsString))
            {
                throw new MissingConnectorIdentifierOnRequestHeader();
            }
            var pushConnectorName = context.RequestHeaders.SingleOrDefault(_ => string.Equals(_.Key, "pushconnectorname", StringComparison.InvariantCultureIgnoreCase))?.Value;

            if (string.IsNullOrEmpty(pushConnectorName))
            {
                throw new MissingConnectorNameOnRequestHeader();
            }
            var id = (ConnectorId)Guid.Parse(pushConnectorIdAsString);

            PushConnector pushConnector = null;

            try
            {
                _logger.Information($"Register connector : '{pushConnectorName}' with Id: '{id}'");
                pushConnector = new PushConnector(id, pushConnectorName);
                _pushConnectors.Register(pushConnector);

                while (await requestStream.MoveNext().ConfigureAwait(false))
                {
                    _tagDataPointCoordinator.Handle(pushConnector.Name, requestStream.Current.DataPoints);
                }
            }
            finally
            {
                if (pushConnector != null)
                {
                    _pushConnectors.Unregister(pushConnector);
                }
            }

            return(new Empty());
        }