Choosing relevant values

Before we publish the data, we need to consider what data is relevant to publish. We want to avoid spamming the broker, or any subscribers. We define relevant to mean values that show a significant change (more than one percentage point), or values after significant silence (at least 15 seconds):

private void PublishLight(double Light) 
{ 
   DateTime Now = DateTime.Now; 
 
   this.lastLight = Light; 
 
   if ((!this.lastPublishedLight.HasValue || 
         Math.Abs(this.lastPublishedLight.Value-Light) >= 1.0 || 
         (Now-this.lastLightPublishTime).TotalSeconds >= 15.0) && 
         this.mqttClient != null && 
         this.mqttClient.State == MqttState.Connected) 
   { 
         this.lastPublishedLight = Light; 
         this.lastLightPublishTime = Now; 
What relevant means should be configurable. In following chapters, when discussing the event subscription pattern, a method will be demonstrated where the subscriber defines what relevant means.

We then proceed by creating the string we want to publish. Our goal is to publish this string on the Waher/MIOT/[DEVICE_ID]/Light topic, where [DEVICE_ID] is replaced by the real device ID:

         string ValueStr = ToString(Light, 2) + " %"; 
..................Content has been hidden....................

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