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) { } }
private void RegisterGateway() { const string MyMsbGatewayUuid = "baf642fa-d3c8-40bf-9c63-780ccd50bf24"; const string MyMsbGatewayName = "C# Sample Gateway"; const string MyMsbGatewayDescription = "Description of my C# sample Gateway"; const string MyMsbGatewayToken = "41da7f7c-a119-4279-b45b-8caad5157016"; const string MyMsbGatewaySubserviceUuid = "b8132e26-1ac9-11eb-adc1-0242ac120002"; const string MyMsbGatewaySubserviceName = "C# Sample Gateway Subservice"; const string MyMsbGatewaySubserviceDescription = "Description of my C# sample Gateway Subservice"; const string MyMsbGatewaySubserviceToken = "bb516bc0-1ac9-11eb-adc1-0242ac120002"; Gateway myMsbGateway = new Gateway(MyMsbGatewayUuid, MyMsbGatewayName, MyMsbGatewayDescription, MyMsbGatewayToken); SmartObject myMsbGatewaySubservice = new SmartObject(MyMsbGatewaySubserviceUuid, MyMsbGatewaySubserviceName, MyMsbGatewaySubserviceDescription, MyMsbGatewaySubserviceToken); myMsbGateway.AddService(myMsbGatewaySubservice); var simpleEvent = new Event("SimpleStringEvent", "Simple String Event", "Description of Simple String Event", typeof(string)); myMsbGatewaySubservice.AddEvent(simpleEvent); MethodInfo methodInfo = this.GetType().GetRuntimeMethod("SampleMsbFunction", new Type[] { typeof(string), typeof(int), typeof(FunctionCallInfo) }); Function sampleMsbFunction = new Function(methodInfo, this); myMsbGatewaySubservice.AddFunction(sampleMsbFunction); this.myMsbClient.RegisterAsync(myMsbGatewaySubservice).Wait(); EventData newEventData = new EventData(simpleEvent); newEventData.Value = "TestString"; this.myMsbClient.PublishAsync(myMsbGatewaySubservice, newEventData).Wait(); }
public void EventPublished() { Event testEvent = new Event("Id", "Name", "Description", typeof(string)); EventData testEventData = new EventDataBuilder(testEvent).Build(); SmartObject testSmartobject = new SmartObject("Id", "Name", "Description", "Token"); testSmartobject.AddEvent(testEvent); Assert.True(this.testMsbClient.ConnectAsync().Result); Assert.True(this.testMsbClient.RegisterAsync(testSmartobject).Result); var raisedEvent = Assert.RaisesAnyAsync <EventArgs>( h => this.testMsbClient.EventPublished += h, h => this.testMsbClient.EventPublished -= h, () => Task.Run(() => { Assert.True(this.testMsbClient.PublishAsync(testSmartobject, testEventData).Result); })).Result; Assert.IsType <EventArgs>(raisedEvent.Arguments); }
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 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); } }
public static void Main(string[] args) { // Setup logging Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console(outputTemplate: "[{Timestamp:yyyy-MM-dd - HH:mm:ss}] [{SourceContext:s}] [{Level:u3}] {Message:lj}{NewLine}{Exception}") .CreateLogger(); Log.Information("Sample application started"); /* Seite A */ const string MyMsbSmartObjectUuid = "1a17b5e3-3a6a-4e62-97b0-82cfdd1cc818"; const string MyMsbSmartObjectName = "C# Sample SmartObject"; const string MyMsbSmartObjectDescription = "{" + "\"Separate\":[{" + "\"Type\":\"TCP\"," + "\"Patterns\":{" + "\"Events\":{" + "\"EventId\":\"String\"," + "\"Ip\":\"String\"," + "\"Port\":\"Int\"" + "}," + "\"Functions\":{" + "\"EventId\":\"String\"," + "\"Ip\":\"String\"," + "\"Port\":\"Int\"" + "}" + "}" + "}]" + "}"; const string MyMsbSmartObjectToken = "30e47482-c140-49a9-a79f-6f2396d8e0ab"; SmartObject myMsbSmartObject = new SmartObject(MyMsbSmartObjectUuid, MyMsbSmartObjectName, MyMsbSmartObjectDescription, MyMsbSmartObjectToken); var tcp_pub1 = new TCPConfiguration.TCPPublicationInstruction() { EventId = "SimpleEventId", Ip = "127.0.0.1", Port = 1884 }; var tcp_pub2 = new TCPConfiguration.TCPPublicationInstruction() { EventId = "FlatEventId", Ip = "127.0.0.1", Port = 1884 }; var tcp_pub3 = new TCPConfiguration.TCPPublicationInstruction() { EventId = "ComplexEventId", Ip = "127.0.0.1", Port = 1884 }; myMsbSmartObject.AddConfigurationParameter("msb.separate.direct.tcp.publications", new ConfigurationParameterValue(new List <Object>() { tcp_pub1, tcp_pub2, tcp_pub3 })); 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(SimpleEvent)); Event complexEvent = new Event("ComplexEventId", "Name of complex event", "Event with nested data format", typeof(ComplexEvent)); myMsbSmartObject.AddEvent(simpleEvent); myMsbSmartObject.AddEvent(flatEvent); myMsbSmartObject.AddEvent(complexEvent); var config_myMsbSmartObject = new TCPConfiguration(); config_myMsbSmartObject.publications = new System.Collections.Generic.Dictionary <string, TCPConfiguration.TCPPublicationInstruction>(); var list_pub = (List <TCPConfiguration.TCPPublicationInstruction>)myMsbSmartObject.Configuration.Parameters["msb.separate.direct.tcp.publications"].Value; foreach (var cp in list_pub) { config_myMsbSmartObject.publications.Add(cp.EventId, cp); } var tcp_myMsbSmartObject = new TCPInterface(config_myMsbSmartObject); tcp_myMsbSmartObject.Start(); /* Seite B */ const string MyMsbApplicationUuid = "46441dc8-c3ab-4c93-9632-d1f356afb8ca"; const string MyMsbApplicationName = "C# Sample Application"; const string MyMsbApplicationDescription = "{" + "\"Separate\":[{" + "\"Type\":\"TCP\"," + "\"Patterns\":{" + "\"Events\":{" + "\"EventId\":\"String\"," + "\"Ip\":\"String\"," + "\"Port\":\"Int\"" + "}," + "\"Functions\":{" + "\"EventId\":\"String\"," + "\"Ip\":\"String\"," + "\"Port\":\"Int\"" + "}" + "}" + "}]" + "}"; const string MyMsbApplicationToken = "5b6b273b-18ff-420b-bbff-5f40288c18f9"; Application myMsbApplication = new Application(MyMsbApplicationUuid, MyMsbApplicationName, MyMsbApplicationDescription, MyMsbApplicationToken); SampleFunctionHandler simpleFunctions = new SampleFunctionHandler(); myMsbSmartObject.AddFunctionHandler(simpleFunctions); var tcp_sub1 = new TCPConfiguration.TCPSubscriptionInstruction() { EventId = "SimpleEventId", Ip = "127.0.0.1", Port = 1884, FunctionPointer = null, IntegrationFlow = new System.Collections.Generic.Dictionary <string, string>() { { "SampleFunctionWithParameters", "TestString" } } }; myMsbApplication.AddConfigurationParameter("msb.separate.direct.tcp.subscriptions", new ConfigurationParameterValue(tcp_sub1)); var config_myMsbApplication = new TCPConfiguration(); config_myMsbApplication.subscriptions = new System.Collections.Generic.Dictionary <string, TCPConfiguration.TCPSubscriptionInstruction>(); var fPtr = msb.separate.Interfaces.BaseInterfaceUtils.CreateFunctionPointer(typeof(SampleFunctionHandler).GetMethod("EmptySampleFunction"), null); var list_sub = (List <TCPConfiguration.TCPSubscriptionInstruction>)myMsbSmartObject.Configuration.Parameters["msb.separate.direct.tcp.subscriptions"].Value; foreach (var cp in list_sub) { cp.FunctionPointer = fPtr; config_myMsbApplication.subscriptions.Add(cp.EventId, cp); } var tcp_myMsbApplication = new TCPInterface(config_myMsbApplication); tcp_myMsbApplication.Start(); //starten const string MsbWebsocketInterfaceUrl = "ws://localhost:8085"; var myMsbClient = new Fraunhofer.IPA.MSB.Client.Websocket.MsbClient(MsbWebsocketInterfaceUrl); 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(); tcp_myMsbSmartObject.PublishEvent(new msb.separate.EventData() { Id = "SimpleEventId", Data = new System.Collections.Generic.Dictionary <string, object> { { "TestString", "123" } } }); EventData eventData_FlatEvent = new EventDataBuilder(flatEvent).SetValue(new SimpleEvent()).Build(); myMsbClient.PublishAsync(myMsbSmartObject, eventData_FlatEvent).Wait(); EventData eventData_ComplexEvent = new EventDataBuilder(complexEvent).SetValue(new ComplexEvent()).Build(); myMsbClient.PublishAsync(myMsbSmartObject, eventData_ComplexEvent).Wait(); Thread.Sleep(3000); } }