private void sendMQT(KPIN kpin) { //this is same as the code taken from the pdf //MqttClient clientPub = new MqttClient(IPAddress.Parse(ipAddress)); string timeStamp = (DateTime.UtcNow).ToString("o"); string topic = "v1/items/pin/" + kpin.getUnitId() + "/" + kpin.getEvent(); MqttClient clientPub = new MqttClient(ipAddress); string clientIdPub = Guid.NewGuid().ToString(); clientPub.Connect(clientIdPub); //{"datetime": "2015-07-15t13:18:46z","data" :{“actuatorID”:”1234”,”productID”:”123”}} string strValue = Convert.ToString("{\"datetime\": \"" + timeStamp + "\",\"data\" :{\"actuatorID\": \"" + kpin.getUnitId() + "\",\"productID\":\"\"}}"); //string strValue = Convert.ToString("{‘data’ : {‘id’ : ‘" + kpin.getUnitId() + "’},'timestamp' : '"+ timeStamp +"'}"); clientPub.Publish(topic, Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); //It is important to have the last param set false. It is the retain parameter, if set to true //this will retain the message in broker and will send it for new subscribers which will //trigger unsolicited events. }
public void sending() { KPIN tmpKpin = new KPIN("Not a kping", (byte)0x02); bool sendStat = false; while (ThreadOn) { Thread.Sleep(1); lock (sendKpinBuff) { if(sendKpinBuff.Count>0) { tmpKpin = sendKpinBuff.Dequeue(); sendStat = true; } } if (sendStat) { try { // this is the format of the topic //string topic = "v1/items/hanger/1003430/event/active"; sendMQT(tmpKpin); } catch (Exception) { } //do the sending part with releasing the buffer to others to use Console.Write("Sending - "); Console.Write(tmpKpin.getUnitId()); Console.Write(" - "); Console.WriteLine(tmpKpin.getEvent()); sendStat = false; } } }