public void sendData( KPIN tagTrigered )
 {
     lock (sendKpinBuff)
     {
         if (sendKpinBuff.Count < KPIN_QUE_LIMIT)
         {
             sendKpinBuff.Enqueue(tagTrigered);
         }
         else
         {
             sendKpinBuff.Clear();
             sendKpinBuff.Enqueue(tagTrigered);
         }
     }
 }
        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;
                }
            }
        }
        public void Process()
        {
            while (this.ThreadOn )
            {
                Thread.Sleep(1);
                //Console.WriteLine("thread process");
                lock (RxPacketBuff)
                {
                    if (RxPacketBuff.Count()>0)
                    {
                        value = RxPacketBuff.Dequeue();
                        /*
                        var sb = new StringBuilder("new byte[] { ");
                        foreach (var b in value)
                        {
                            sb.Append(b + ", ");
                        }
                        sb.Append("}");
                        Console.WriteLine(sb.ToString());
                         */
                    }
                }
                //update value with one packet
                //packet decription
                if(value[0] == 0x5A)
                {
                    //packet is initialized and correct packet
                    for (int i = 1; i <= 12; i++ )
                    {
                        try
                        {
                            tmp[2 * i - 2] = (byte)((value[i] >> 4) + 48);
                            tmp[2 * i-1] = (byte)((value[i] & (byte)15) + 48);
                        }
                        catch (Exception)
                        {

                        }

                    }
                    //String tmp1 = tmp.ToString();
                    this.UnitId = System.Text.Encoding.ASCII.GetString(tmp);
                    //pass event as byte in new KPIN class
                    //this.Event = (char)(value[13]+48);
                    this.Event = (byte)value[13];
                    KPIN kpin = new KPIN(this.UnitId, this.Event);
                    S_Iot.sendData( kpin );

                    /*
                    Console.Write(" Unit id - ");
                    Console.Write(this.UnitId);

                    Console.Write("     ");
                    Console.Write("event id");
                    Console.Write(" - ");
                    Console.WriteLine(this.Event);
                     */

                    Array.Clear(value,0,14);

                }
            }
        }
 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.
 }