Interacting with devices

We now have what we need to start interacting with our devices. In the previous chapter, we demonstrated how to create a sensor server and an actuator server. The controller, however, will be a sensor client and actuator client. We prepare the controller with variables for this purpose. The classes are available in the same NuGets and namespaces as the server counterparts:

private SensorClient sensorClient = null; 
private ControlClient controlClient = null; 

When we instantiate them, we provide them with a reference to our XMPP client:

this.sensorClient = new SensorClient(this.xmppClient); 
this.controlClient = new ControlClient(this.xmppClient); 

We will also maintain the current state, from the controller point of view, of the current values of the sensor and actuator, as well as the timestamps of when the values were last assigned:

private double? light = null; 
private bool? motion = null; 
private bool? output = null; 
private DateTime lastEventFields = DateTime.Now; 
private DateTime lastEventErrors = DateTime.Now; 
private DateTime lastOutput = DateTime.Now;  

As a sensor client, we have two options to retrieve data. Either we poll it, using the Request/Response pattern, or we use the event subscription pattern to get informed of changes as they occur. As hinted at in previous sections, the latter is the best option for our controller. We will maintain our current subscription in a separate variable:

private SensorDataSubscriptionRequest subscription = null; 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset