public EventingServiceController(string serviceTransportAddress) { this.serviceTransportAddress = serviceTransportAddress; // Set this device property if you want to ignore this service notifications this.IgnoreRequestFromThisIP = false; // Assigning either a static or random endpoint address will // init the transport address of this client which we will use soon this.EndpointAddress = "urn:uuid:" + Guid.NewGuid(); // Adding a event handler this.ServiceOperations.Add(new WsServiceOperation(c_namespaceUri, // namespace "SimpleEvent")); // event (method) name // Subscribe to the event DpwsServiceType subscriptionType = new DpwsServiceType("SimpleEvent", c_namespaceUri); DpwsSubscribeRequest request = new DpwsSubscribeRequest(subscriptionType, // subscription type serviceTransportAddress, // event source address this.TransportAddress, // notify to address null, // expires null // event identifier ); this.EventingClient.Subscribe(request); }
/// <summary> /// Method runs a loop waiting for Hello events. When on is received method probes, resolves /// and Get's device service information and calls 1way, 2way methods and subscribes to SimpleEvent /// and IntegerEvent and waits for them to fire. When they do Unsubscribe is called on the events /// and the receive hello's flag is reset. /// </summary> public void Run() { // Create Event handlers m_simpleServiceClient.ByeEvent += new ByeEventHandler(m_simpleControl_ByeEvent); m_simpleServiceClient.HelloEvent += new HelloEventHandler(m_simpleControl_HelloEvent); m_simpleServiceClient.SubscriptionEndEvent += new SubscriptionEndEventHandler(m_simpleControl_SubscriptionEndEvent); bool firstPass = true; bool twoWayAttach = false; DpwsServiceTypes typeProbes = new DpwsServiceTypes(); typeProbes.Add(new DpwsServiceType("SimpleDeviceType", "http://schemas.example.org/SimpleService")); // Continuous run loop while (true) { if (firstPass && !m_inDiscovery) { DpwsServiceDescriptions descs = m_simpleServiceClient.DiscoveryClient.Probe(typeProbes, 3, 1000); for (int i = 0; i < descs.Count; i++) { DpwsServiceDescription desc = descs[i]; if (desc.XAddrs != null && desc.XAddrs.Length > 0) { CheckConnection(desc.ServiceTypes, desc.Endpoint.Address.AbsoluteUri); } } } // If hello was received and a SimpleService device was found. SeletedService will not be null. // Process until Bye is received. if (m_deviceSelected.WaitOne(5000, false)) { // If this is the first time through the loop for this device subscribe to events if (firstPass) { // Test service host call System.Ext.Console.Write("Testing Host service..."); DpwsSubscribeRequest subReq; subReq = new DpwsSubscribeRequest(m_eventingServiceClient.EventSources["SimpleEvent"], m_eventingServiceClient.EndpointAddress, m_eventingServiceClient.TransportAddress, "PT1H", null); m_simpleEventSubscription = m_eventingServiceClient.EventingClient.Subscribe(subReq); subReq = new DpwsSubscribeRequest(m_eventingServiceClient.EventSources["IntegerEvent"], m_eventingServiceClient.EndpointAddress, m_eventingServiceClient.TransportAddress, "PT1H", null); m_integerEventSubscription = m_eventingServiceClient.EventingClient.Subscribe(subReq); firstPass = false; } // Make 1Way and 2Way service calls if (m_deviceSelected.WaitOne(0, false)) { PrintMetadataInfo(); System.Ext.Console.Write(""); System.Ext.Console.Write(">>>>>>>>>>>>> Sending 1way(10) request to: " + m_selectedService.ThisDevice.FriendlyName); try { m_simpleServiceClient.OneWay(new OneWayRequest()); } catch (Exception e) { System.Ext.Console.Write(""); System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! OneWay method failed. " + e.Message); } } if (m_deviceSelected.WaitOne(0, false)) { int x = System.Math.Abs(m_random.Next()) % 100; int y = System.Math.Abs(m_random.Next()) % 100; System.Ext.Console.Write(""); System.Ext.Console.Write(">>>>>>>>>>>>> Sending 2way(" + x.ToString() + ", " + y.ToString() + ") request to: " + m_selectedService.ThisDevice.FriendlyName); try { TwoWayRequest req = new TwoWayRequest(); req.X = x; req.Y = y; TwoWayResponse resp = m_simpleServiceClient.TwoWay(req); if (resp.Sum == 0) { System.Ext.Console.Write(""); System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2way method did not receive a valid response."); } else { System.Ext.Console.Write(""); System.Ext.Console.Write("<<<<<<<<<<<<< 2way response returned " + resp.Sum); } } catch (Exception e) { System.Ext.Console.Write(""); System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TwoWay method failed. " + e.Message); } } // Make 1wayattach or a 2wayattach service calls if (m_deviceSelected.WaitOne(0, false)) { if (!twoWayAttach) { // create an instance of the help icon test object HelpIcon helpIcon = new HelpIcon(); System.Ext.Console.Write(""); System.Ext.Console.Write(">>>>>>>>>>>>> Sending 1wayattach request to: " + m_selectedService.ThisDevice.FriendlyName); try { OneWayAttachmentRequest req = new OneWayAttachmentRequest(); req.Param = helpIcon.Data.ToArray(); m_attachmentServiceClient.OneWayAttachment(req); } catch (Exception e) { System.Ext.Console.Write(""); System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 1wayattach method failed: " + e.Message); } } else { // create an instance of the help icon test object HelpIcon helpIcon = new HelpIcon(); System.Ext.Console.Write(""); System.Ext.Console.Write(">>>>>>>>>>>>> Sending 2wayattach request to: " + m_selectedService.ThisDevice.FriendlyName); try { TwoWayAttachmentRequest req = new TwoWayAttachmentRequest(); req.Param = helpIcon.Data.ToArray(); TwoWayAttachmentResponse resp = m_attachmentServiceClient.TwoWayAttachment(req); System.Ext.Console.Write(""); System.Ext.Console.Write("<<<<<<<<<<<<< Sending 2wayattach request succeeded"); } catch (Exception e) { System.Ext.Console.Write(""); System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TwoWay method failed. " + e.Message); } } twoWayAttach = !twoWayAttach; } } else { firstPass = true; } } }