Beispiel #1
0
        public static void AllInOneSample()
        {
            const string MsbWebsocketInterfaceUrl = "ws://ws.msb.ipa.cell.vfk.fraunhofer.de";

            const string MyMsbSmartObjectUuid        = "5607fd6f-50ae-44e7-bd6a-9416fbb25d43";
            const string MyMsbSmartObjectName        = "C# Sample SmartObject";
            const string MyMsbSmartObjectDescription = "Description of my C# sample SmartObject";
            const string MyMsbSmartObjectToken       = "30e47482-c140-49a9-a79f-6f2396d8e0ab";

            const string MyMsbApplicationUuid        = "f69067bc-6e8c-4479-a358-8a56f7b7a54d";
            const string MyMsbApplicationName        = "C# Sample Application";
            const string MyMsbApplicationDescription = "Description of my C# sample Application";
            const string MyMsbApplicationToken       = "5b6b273b-18ff-420b-bbff-5f40288c18f9";

            // Create a new MsbClient which allows SmartObjects and Applications to communicate with the MSB
            MsbClient myMsbClient = new MsbClient(MsbWebsocketInterfaceUrl);

            // Create the self description of a sample SmartObject and a sample Application
            SmartObject myMsbSmartObject = new SmartObject(MyMsbSmartObjectUuid, MyMsbSmartObjectName, MyMsbApplicationDescription, MyMsbSmartObjectToken);
            Application myMsbApplication = new Application(MyMsbApplicationUuid, MyMsbApplicationName, MyMsbSmartObjectDescription, MyMsbApplicationToken);

            // Add configuration parameters
            myMsbSmartObject.AddConfigurationParameter("sampleParameter1", new ConfigurationParameterValue(1337));
            myMsbSmartObject.AddConfigurationParameter("sampleParameter2", new ConfigurationParameterValue("SampleValue"));

            // Add events
            Event simpleEvent  = new Event("SimpleEventId", "Name of simple event", "Event with simple data format", typeof(string));
            Event flatEvent    = new Event("FlatEventId", "Name of flat event", "Event with flat data format", typeof(Events.SimpleEvent));
            Event complexEvent = new Event("ComplexEventId", "Name of complex event", "Event with nested data format", typeof(Events.ComplexEvent));
            Event sendInts     = new Event("SendIntsId", "Send Ints", "sends two integer values", typeof(IntegerValues));

            myMsbSmartObject.AddEvent(simpleEvent);
            myMsbSmartObject.AddEvent(flatEvent);
            myMsbSmartObject.AddEvent(complexEvent);
            myMsbSmartObject.AddEvent(sendInts);
            myMsbApplication.AddEvent(simpleEvent);
            myMsbApplication.AddEvent(flatEvent);
            myMsbApplication.AddEvent(complexEvent);
            myMsbApplication.AddEvent(sendInts);


            // Add functions
            SampleFunctionHandler simpleFunctions = new SampleFunctionHandler();

            myMsbSmartObject.AddFunctionHandler(simpleFunctions);

            // Connect to the MSB and register the sample SmartObject and sample Application via the MsbClient
            myMsbClient.ConnectAsync().Wait();

            myMsbClient.RegisterAsync(myMsbSmartObject).Wait();
            myMsbClient.RegisterAsync(myMsbApplication).Wait();

            // Publish events
            while (true)
            {
            }
        }
                public void NoResponseEventShouldBeSendForFunctionCall()
                {
                    var mockWebsocketInterface = new MockWebsocketInterface();

                    mockWebsocketInterface.Start();
                    var testMsbClient   = new MsbClient(mockWebsocketInterface.URL);
                    var testSmartObject = new SmartObject(Guid.NewGuid().ToString(), "Name", "Description", Guid.NewGuid().ToString());
                    var responseEventWhichShouldNotBeSend = new Event("ResponseEventWhichShouldNotBeSend", string.Empty, string.Empty, new DataFormat());

                    testSmartObject.AddEvent(responseEventWhichShouldNotBeSend);
                    var testFunction = new Function(this.GetType().GetRuntimeMethod("NoResponseEventShouldBeSendForFunctionCallMsbFunction", new Type[] { typeof(FunctionCallInfo) }), this);

                    testSmartObject.AddFunction(testFunction);

                    Assert.True(testMsbClient.ConnectAsync().Result);
                    Assert.True(testMsbClient.RegisterAsync(testSmartObject).Result);
                    string functionCallJson = $@"{MessageType.FUNCTION_CALLBACK} {{
                        ""uuid"" : ""{testSmartObject.Uuid}"",
                        ""correlationId"" : ""{Guid.NewGuid().ToString()}"",
                        ""functionId"" : ""NoResponseEventShouldBeSendForFunctionCallMsbFunction"",
                        ""functionParameters"" : {{ }}
                    }}";

                    try
                    {
                        var raisedEvent = Assert.RaisesAnyAsync <EventArgs>(
                            h => testMsbClient.EventPublished += h,
                            h => testMsbClient.EventPublished -= h,
                            () => Task.Run(() =>
                        {
                            mockWebsocketInterface.SendMessageOfType(functionCallJson);
                            Thread.Sleep(100);
                        })).Result;
                    }
                    catch (AggregateException e)
                    {
                        Assert.Contains("No event was raised", e.InnerException.Message);
                    }
                }
                public void PrimtiveFunctionParameterConversion_Integers()
                {
                    var mockWebsocketInterface = new MockWebsocketInterface();

                    mockWebsocketInterface.Start();
                    var testMsbClient   = new MsbClient(mockWebsocketInterface.URL);
                    var testSmartObject = new SmartObject(Guid.NewGuid().ToString(), "Name", "Description", Guid.NewGuid().ToString());
                    var testFunction    = new Function(this.GetType().GetRuntimeMethod("TestFunction", new Type[] { typeof(int), typeof(FunctionCallInfo) }), this);

                    testSmartObject.AddFunction(testFunction);

                    Assert.True(testMsbClient.ConnectAsync().Result);
                    Assert.True(testMsbClient.RegisterAsync(testSmartObject).Result);
                    string functionCallJson = $@"{MessageType.FUNCTION_CALLBACK} {{
                        ""uuid"" : ""{testSmartObject.Uuid}"",
                        ""correlationId"" : ""{Guid.NewGuid().ToString()}"",
                        ""functionId"" : ""TestFunction"",
                        ""functionParameters"" : {{
                            ""testParameter"" : 1234
                        }}
                    }}";

                    mockWebsocketInterface.SendMessageOfType(functionCallJson);
                    var maxPollTries = 10;

                    for (int i = 0; i < maxPollTries; i++)
                    {
                        if (this.testFunctionCallReceived)
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(50);
                        }
                    }

                    Assert.True(this.testFunctionCallReceived);
                }
        public static void AllInOneSample()
        {
            const string MsbWebsocketInterfaceUrl = "ws://localhost:8085";

            const string MyMsbSmartObjectUuid        = "1a17b5e3-3a6a-4e62-97b0-82cfdd1cc818";
            const string MyMsbSmartObjectName        = "C# Sample SmartObject";
            const string MyMsbSmartObjectDescription = "Description of my C# sample SmartObject";
            const string MyMsbSmartObjectToken       = "30e47482-c140-49a9-a79f-6f2396d8e0ab";

            const string MyMsbApplicationUuid        = "46441dc8-c3ab-4c93-9632-d1f356afb8ca";
            const string MyMsbApplicationName        = "C# Sample Application";
            const string MyMsbApplicationDescription = "Description of my C# sample Application";
            const string MyMsbApplicationToken       = "5b6b273b-18ff-420b-bbff-5f40288c18f9";

            // Create a new MsbClient which allows SmartObjects and Applications to communicate with the MSB
            MsbClient myMsbClient = new MsbClient(MsbWebsocketInterfaceUrl);

            // Create the self description of a sample SmartObject and a sample Application
            SmartObject myMsbSmartObject = new SmartObject(MyMsbSmartObjectUuid, MyMsbSmartObjectName, MyMsbApplicationDescription, MyMsbSmartObjectToken);
            Application myMsbApplication = new Application(MyMsbApplicationUuid, MyMsbApplicationName, MyMsbSmartObjectDescription, MyMsbApplicationToken);

            // Add configuration parameters
            myMsbSmartObject.AddConfigurationParameter("sampleParameter1", new ConfigurationParameterValue(1337));
            myMsbSmartObject.AddConfigurationParameter("sampleParameter2", new ConfigurationParameterValue("SampleValue"));

            // Add events
            Event simpleEvent  = new Event("SimpleEventId", "Name of simple event", "Event with simple data format", typeof(string));
            Event flatEvent    = new Event("FlatEventId", "Name of flat event", "Event with flat data format", typeof(Events.SimpleEvent));
            Event complexEvent = new Event("ComplexEventId", "Name of complex event", "Event with nested data format", typeof(Events.ComplexEvent));

            myMsbSmartObject.AddEvent(simpleEvent);
            myMsbSmartObject.AddEvent(flatEvent);
            myMsbSmartObject.AddEvent(complexEvent);
            myMsbApplication.AddEvent(simpleEvent);
            myMsbApplication.AddEvent(flatEvent);
            myMsbApplication.AddEvent(complexEvent);

            // Add functions
            SampleFunctionHandler simpleFunctions = new SampleFunctionHandler();

            myMsbSmartObject.AddFunctionHandler(simpleFunctions);

            // Connect to the MSB and register the sample SmartObject and sample Application via the MsbClient
            myMsbClient.ConnectAsync().Wait();

            myMsbClient.RegisterAsync(myMsbSmartObject).Wait();
            myMsbClient.RegisterAsync(myMsbApplication).Wait();

            // Publish events
            while (true)
            {
                EventData eventData_SimpleEvent = new EventDataBuilder(simpleEvent).SetValue("TestString").Build();
                myMsbClient.PublishAsync(myMsbSmartObject, eventData_SimpleEvent).Wait();

                EventData eventData_FlatEvent = new EventDataBuilder(flatEvent).SetValue(new Events.SimpleEvent()).Build();
                myMsbClient.PublishAsync(myMsbSmartObject, eventData_FlatEvent).Wait();

                EventData eventData_ComplexEvent = new EventDataBuilder(complexEvent).SetValue(new Events.ComplexEvent()).Build();
                myMsbClient.PublishAsync(myMsbSmartObject, eventData_ComplexEvent).Wait();

                Thread.Sleep(3000);
            }
        }