Ejemplo n.º 1
0
        public void HandleTCPClient(Object obj)
        {
            TcpClient client   = (TcpClient)obj;
            IPAddress clientIP = ((IPEndPoint)(client.Client.RemoteEndPoint)).Address;

            try
            {
                while (client.Connected)
                {
                    while (client.Available > 0)
                    {
                        using (StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8))
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                try
                                {
                                    List <byte> knxmsg = new List <byte>(Convert.FromBase64String(line));
                                    if (knxmsg.Count < 3)
                                    {
                                        continue;
                                    }

                                    //Get Checksum and remove from array.
                                    byte checksum = knxmsg.Last();
                                    knxmsg.RemoveAt(knxmsg.Count - 1);

                                    if (KNXUartModule.KNXUartConnection.CheckChecksum(knxmsg, checksum))
                                    {
                                        if (knxmsg[0] == KNXNetworkLayer.KNXNetworkLayerTemplate.ADDR_NETWORK_MSG)
                                        {
                                            Console.Write("knxLayerCommand ");
                                            if (knxmsg[1] == KNXNetworkLayer.KNXNetworkLayerTemplate.ADDR_ADDKNXADDRTOACK)
                                            {
                                                if (knxmsg.Count == 4)
                                                {
                                                    KNXAddress k = new KNXAddress(knxmsg.ToArray(), 2);
                                                    Console.WriteLine("KnxAddToAck: {0}", k.knxAddress.ToString());
                                                    OnKNXAddressAdd(this, k);
                                                }
                                                else
                                                {
                                                    Console.Write("KnxAddToAck lenghtfailed");
                                                }
                                            }
                                            else if (knxmsg[1] == KNXNetworkLayer.KNXNetworkLayerTemplate.ADDR_REMOVEKNXADDRTOACK)
                                            {
                                                if (knxmsg.Count == 4)
                                                {
                                                    KNXAddress k = new KNXAddress(knxmsg.ToArray(), 2);
                                                    Console.WriteLine("KnxRemoveFromAck: {0}", k.knxAddress.ToString());
                                                    OnKNXAddressRemove(this, k);
                                                }
                                                else
                                                {
                                                    Console.Write("KnxRemoveFromAck lenghtfailed");
                                                }
                                            }
                                            else if (knxmsg[1] == KNXNetworkLayer.KNXNetworkLayerTemplate.ADDR_NETWORK_KEEPALIVE)
                                            {
                                                //Child still alive (timer???)
                                            }
                                            else
                                            {
                                                Console.Write("Unkown");
                                            }
                                            continue;
                                        }
                                        else
                                        {
                                            KNXBaseTypes.KNXmessage kNXmessage = KNXBaseTypes.KNXmessage.fromByteArray(knxmsg.ToArray(), DateTime.Now);
                                            OnKNXMessage?.Invoke(this, kNXmessage);
                                            Console.Write("OK: ");
                                            Console.WriteLine(kNXmessage.ToHexString());
                                        }
                                    }
                                    else
                                    {
                                        Console.Write("CheckSumMismatch: " + line);
                                    }
                                }
                                catch
                                {
                                    Console.WriteLine("Cannot process Base64: " + line);
                                }
                            }
                        }
                    }
                    Thread.Sleep(1);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Client Closed: {0}", e.ToString());
            }
            Console.WriteLine("Client Closed");
            onClientDisconnect?.Invoke(this, clientIP);
            if (client != null)
            {
                if (client.Connected)
                {
                    client.Close();
                }
            }
            lock (tLockClientList)
            {
                tcpClients.Remove(client);
            }
        }
Ejemplo n.º 2
0
        //protected internal void OnU


        protected internal void OnParentKNXMessage(KNXNetworkLayerTemplate caller, KNXmessage Message, KnxPacketEvents uartEvent)
        {
            OnKNXMessage?.Invoke(caller, Message, uartEvent);
        }