.Net Development

Comments

5 comments

  • Damian Kuźma

    Hi Eric,

    Please take a look at Paho libraries - https://www.eclipse.org/paho/clients/dotnet/ 

    When connecting to the stream address, make sure that you do it with SSL enabled. 

  • Robin Leblond

    Hello, We have the same interest here, but your link is no longer working, any other reference or way of doing it (sample, links, etc.) ? Thanks,

  • Eric T

    Hi Robin,

    you can use the M2Mqqt by Paolo Patierno Library from the Nuget

    and here's the sample code:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using uPLibrary.Networking.M2Mqtt;
    using uPLibrary.Networking.M2Mqtt.Messages;
    using Newtonsoft.Json;
    using System.IO;

     


    namespace KontaktM2MQTT
    {
    class Program
    {
    private static List<PresenceData> pd;
    private static string beaconId = string.Empty;
    private static string ReaderId = string.Empty;
    private static int testCase;
    private static int rssiThreshold = -80;
    private static bool setRssiThreshold = true;
    static void Main(string[] args)
    {

    beaconId = Properties.Settings.Default.BeaconID;
    ReaderId = Properties.Settings.Default.ReaderID;
    testCase = Properties.Settings.Default.TestCase;
    rssiThreshold = Properties.Settings.Default.RssiThreshold;
    setRssiThreshold = Properties.Settings.Default.SetRssiThreshold;


    Console.WriteLine("GATEWAY ID : {0}", ReaderId);
    Console.WriteLine("BEACON ID : {0}", beaconId);
    Console.WriteLine("RSSI THRESHOLD : {0}", rssiThreshold);
    // create client instance
    MqttClient client = new MqttClient("ovs.kontakt.io", 8083, true, MqttSslProtocols.TLSv1_2, null, null);


    // register to message received
    client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
    client.MqttMsgSubscribed += Client_MqttMsgSubscribed;
    client.MqttMsgPublished += Client_MqttMsgPublished;


    string clientId = Guid.NewGuid().ToString();
    client.Connect(clientId, "hello", "jivoTkJdRowoGEBNgvVdcpJfMILrwPqy", true, 10000);

     


    // subscribe to the topic "/home/temperature" with QoS 2
    client.Subscribe(new string[] { "/presence/stream/" + ReaderId}, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
    //client.Subscribe(new string[] { "/stream/:WBM Test/presence" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });

    //client.Subscribe(new string[] { "/stream/:JsChS5/button" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });


    //client.Publish("/presence/stream/:JskULW", null);

    //client.Subscribe(new string[] { "/stream/Js0FoW/sensor" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
    }

    private static void Client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs pd)


    {
    throw new NotImplementedException();
    }

    private static void Client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
    {
    Console.WriteLine(e.ToString());
    }

    private static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs p)
    {
    string json = Encoding.ASCII.GetString(p.Message); ;

    //Console.WriteLine(json);

    pd = JsonConvert.DeserializeObject<List<PresenceData>>(json);

    foreach(PresenceData presenceData in pd)
    {

    switch(testCase)
    {
    case 0: // Beacon and reader
    if (presenceData.trackingId == beaconId && presenceData.sourceId == ReaderId)
    {
    DisplayData(presenceData);
    }
    break;

    case 1: // Beacon only
    if (presenceData.trackingId == beaconId)
    {
    DisplayData(presenceData);
    }
    break;

    case 2: // Reader only
    if (presenceData.sourceId == ReaderId)
    {
    DisplayData(presenceData);
    }
    break;


    }

     


    }
    pd = null;

    }

    public static void DisplayData(PresenceData p)
    {
    DateTime d = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    d = d.AddSeconds(p.timestamp);

    if(setRssiThreshold && p.rssi <= rssiThreshold)
    Console.WriteLine("VIOLATION: timestamp: {0}, sourceId: {1}, trackingId: {2}, rssi: {3}, proximity: {4}, scanType: {5}, deviceAddress: {6}",d.ToLocalTime(), p.sourceId, p.trackingId, p.rssi, p.proximity, p.scanType, p.deviceAddress);
    else
    Console.WriteLine("timestamp: {0}, sourceId: {1}, trackingId: {2}, rssi: {3}, proximity: {4}, scanType: {5}, deviceAddress: {6}", d.ToLocalTime(), p.sourceId, p.trackingId, p.rssi, p.proximity, p.scanType, p.deviceAddress);

    }
    }
    }

     

     

  • Robin Leblond

    Thanks Eric! 

  • Eric T

    here is it

     

    public class PresenceData
    {
    public int timestamp { get; set; }
    public string sourceId { get; set; }
    public string trackingId { get; set; }
    public int rssi { get; set; }
    public string proximity { get; set; }
    public string scanType { get; set; }
    public string deviceAddress { get; set; }
    }

Please sign in to leave a comment.