Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataFormat"/> class.
        /// </summary>
        /// <param name="rootNodeName">Name of the root node.</param>
        /// <param name="type">Type to convert to data format.</param>
        public DataFormat(string rootNodeName, Type type)
        {
            var deserializedSchema = OpenApiMapper.GetJsonSchemaOfType(type);

            if (OpenApiMapper.IsPrimitiveDataType(type))
            {
                this.Add(rootNodeName, deserializedSchema.ToObject <Dictionary <string, object> >());
            }
            else
            {
                string mainTypeName = type.Name.Replace("[]", string.Empty);
                this.Add(rootNodeName, new Dictionary <string, string>()
                {
                    { "$ref", $"#/definitions/{mainTypeName}" }
                });

                // Move definitions to root level
                if (deserializedSchema["definitions"] != null)
                {
                    foreach (var defintion in deserializedSchema["definitions"].ToObject <Dictionary <string, JObject> >())
                    {
                        (defintion.Value as JObject).Remove("additionalProperties");
                        this.Add(defintion.Key, defintion.Value as JObject);
                    }
                }

                deserializedSchema.Remove("definitions");
                this.Add(mainTypeName, deserializedSchema.ToObject <Dictionary <string, object> >());
            }
        }
Beispiel #2
0
        private void HandleFunctionCall(FunctionCall functionCall)
        {
            Log.Debug($"Callback for function '{functionCall.FunctionId}' of service '{functionCall.ServiceUuid}' received with parameters: {functionCall.FunctionParameters}");
            if (this.RegisteredServices.ContainsKey(functionCall.ServiceUuid))
            {
                var serviceOfFunctionCall = this.RegisteredServices[functionCall.ServiceUuid];
                if (serviceOfFunctionCall.Functions.Find(function => function.Id == functionCall.FunctionId) is Function functionOfService)
                {
                    var pointer    = functionOfService.FunctionPointer;
                    var parameters = functionOfService.FunctionPointer.Method.GetParameters();

                    var parameterArrayForInvoke = new object[parameters.Length];
                    foreach (var functionCallParameter in functionCall.FunctionParameters)
                    {
                        var currentParameterCallIndex = AbstractFunctionHandler.GetParameterIndexFromName(functionOfService.FunctionPointer.Method, functionCallParameter.Key);
                        var currentParameterCallType  = functionOfService.FunctionPointer.Method.GetParameters()[currentParameterCallIndex].ParameterType;

                        object deserializedParameter = null;
                        if (OpenApiMapper.IsPrimitiveDataType(currentParameterCallType))
                        {
                            deserializedParameter = Convert.ChangeType(functionCallParameter.Value, currentParameterCallType);
                        }
                        else
                        {
                            var valueAt = functionCallParameter.Value.ToString();
                            deserializedParameter = JsonConvert.DeserializeObject(valueAt, currentParameterCallType);
                        }

                        parameterArrayForInvoke[currentParameterCallIndex] = deserializedParameter;
                    }

                    var calledFunction = serviceOfFunctionCall.GetFunctionById(functionCall.FunctionId);
                    Dictionary <string, Event> responseEvents = new Dictionary <string, Event>();
                    foreach (var responseEventId in calledFunction.ResponseEventIds)
                    {
                        responseEvents.Add(responseEventId, serviceOfFunctionCall.GetEventById(responseEventId));
                    }

                    var functionCallInfo = new FunctionCallInfo(this, functionCall.CorrelationId, serviceOfFunctionCall, calledFunction, responseEvents);
                    parameterArrayForInvoke[parameterArrayForInvoke.Length - 1] = functionCallInfo;
                    try
                    {
                        var returnValue = functionOfService.FunctionPointer.DynamicInvoke(parameterArrayForInvoke);
                        if (returnValue != null)
                        {
                            EventData responseEventData = (EventData)returnValue;
                            if (responseEventData.Event.Id.Equals(EventData.NoResponseEvent.Event.Id))
                            {
                                Log.Info("No response event sent because result of function exuction was 'EventData.NoResponseEvent'");
                            }
                            else
                            {
                                if (calledFunction.ResponseEventIds.Contains(responseEventData.Event.Id))
                                {
                                    responseEventData.CorrelationId = functionCall.CorrelationId;
                                    #pragma warning disable CS4014 // Da dieser Aufruf nicht abgewartet wird, wird die Ausführung der aktuellen Methode fortgesetzt, bevor der Aufruf abgeschlossen ist
                                    this.PublishAsync(serviceOfFunctionCall, (EventData)responseEventData);
                                    #pragma warning restore CS4014 // Da dieser Aufruf nicht abgewartet wird, wird die Ausführung der aktuellen Methode fortgesetzt, bevor der Aufruf abgeschlossen ist
                                }
                                else
                                {
                                    Log.Error($"Response event not published, because event '{responseEventData.Event.Id}' wasn't defined as response event of function '{calledFunction.Id}'");
                                }
                            }
                        }
                    }
                    catch (ArgumentException e)
                    {
                        Log.Error($"Failed to invoke function with received parameters: {e}");
                    }
                }
                else
                {
                    Log.Warn($"Function call can't be processed, function '{functionCall.FunctionId}' not found in service '{functionCall.ServiceUuid}'");
                }
            }
            else
            {
                Log.Warn($"Function call can't be processed, service '{functionCall.ServiceUuid}' wasn't registered via this MsbClient");
            }
        }