コード例 #1
0
        public static KNXmessage fromByteArray(byte[] frame, DateTime timestamp)
        {
            KNXmessage k = new KNXmessage
            {
                Timestamp = timestamp,
            };

            k.ProcessKNXMessage(frame);
            return(k);
        }
コード例 #2
0
ファイル: DeviceLearner.cs プロジェクト: roel80/FreeAtHome
        private static void KNXUart_OnKNXMessage(KNXNetworkLayerTemplate caller, KNXBaseTypes.KNXmessage Message, KNXNetworkLayerTemplate.KnxPacketEvents uartEvent)
        {
            if (Message.ControlField.RepeatFrame)
            {
                //Repeatframe
                return;
            }
            if (Message.SourceAddress == SysApEmulator.KnxAddress)
            {
                //Self
                return;
            }

            switch (devLearnState)
            {
            case DeviceLearningState.deviceDiscovery:
                if (Message.Payload.Apdu.apduType == KNXAdpu.ApduType.DeviceDescriptorResponse)
                {
                    Message.Payload.ReadablePayloadPacket = new FAHDeviceDescriptorResponse(Message.Payload);
                    bool more;
                    ((FAHDeviceDescriptorResponse)Message.Payload.ReadablePayloadPacket).SaveToDevice(ref deviceToLearn, out more);
                    devLearnState = DeviceLearningState.deviceDiscoveryResponse;
                    return;
                }
                break;

            case DeviceLearningState.deviceReadSettings:
                if (Message.SourceAddress == deviceToLearn.KnxAddress)
                {
                    if (Message.ControlField.RepeatFrame)
                    {
                        //For now ignore
                        return;
                    }
                    knxMsgtoProcess = Message;
                }
                break;

            default:
                Console.Write(string.Format("{0} ", Message.HeaderAsString));
                Message.Payload.ReadablePayloadPacket.PrintUnaccountedBytes();
                break;
            }
        }
コード例 #3
0
ファイル: FaHGroupMonitor.cs プロジェクト: roel80/FreeAtHome
        private void KNXUart_OnKNXMessage(KNXNetworkLayerTemplate caller, KNXBaseTypes.KNXmessage Message, KNXNetworkLayerTemplate.KnxPacketEvents uartEvent)
        {
            if (Message.ControlField.RepeatFrame)
            {
                return;
            }

            if (Message.TargetAddress == knxGroupToMonitor)
            {
                FahPayloadInterpeter.TryToInterpret(ref Message);

                switch (Message.Payload.Apdu.apduType)
                {
                case KNXAdpu.ApduType.GroupValueWrite:
                    FAHGroupValueWrite fAHGroupValueWrite = new FAHGroupValueWrite(Message.Payload);
                    Console.Write("GroupMonitor: {0}; {1} ", Message.Timestamp.ToString(KNXHelpers.DateTimeFormat), Message.HeaderAsString);
                    data = fAHGroupValueWrite.MessageData;
                    OnGroupValueChange?.Invoke(this, data);
                    Message.Payload.ReadablePayloadPacket.PrintUnaccountedBytes(false);
                    break;

                case KNXAdpu.ApduType.GroupValueRead:
                    FAHGroupValueRead fAHGroupValueRead = new FAHGroupValueRead(Message.Payload);
                    Console.Write("GroupMonitor: {0}; {1} ", Message.Timestamp.ToString(KNXHelpers.DateTimeFormat), Message.HeaderAsString);
                    Message.Payload.ReadablePayloadPacket.PrintUnaccountedBytes(false);
                    break;

                case KNXAdpu.ApduType.GroupValueResponse:
                    FAHGroupValueResponse fAHGroupValueReponse = new FAHGroupValueResponse(Message.Payload);
                    Console.Write("GroupMonitor: {0}; {1} ", Message.Timestamp.ToString(KNXHelpers.DateTimeFormat), Message.HeaderAsString);
                    data = fAHGroupValueReponse.MessageData;
                    OnGroupValueChange?.Invoke(this, data);
                    Message.Payload.ReadablePayloadPacket.PrintUnaccountedBytes(false);
                    break;

                default:
                    Console.WriteLine("???" + Message.Payload.Apdu.apduType);
                    break;
                }
            }
        }
コード例 #4
0
ファイル: TCPknxServer.cs プロジェクト: roel80/FreeAtHome
        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);
            }
        }
コード例 #5
0
        public void StartClient(Object sObject)
        {
            bool       first      = true;
            IPEndPoint iPEndPoint = sObject as IPEndPoint;

            while (first || autoReconnect)
            {
                try
                {
                    first     = false;
                    tcpClient = new TcpClient();
                    this.OnParentKNXEvent(this, KNXNetworkLayerTemplate.KnxPacketEvents.DeviceConnecting);
                    Console.WriteLine("Connecting to: {0}", iPEndPoint.Address);
                    if (!tcpClient.ConnectAsync(iPEndPoint.Address, iPEndPoint.Port).Wait(5000))
                    {
                        Console.WriteLine("Failed to connect, retry loop");
                        continue;
                    }
                    this.OnParentKNXEvent(this, KNXNetworkLayerTemplate.KnxPacketEvents.DeviceOnline);
                    try
                    {
                        while (tcpClient.Connected)
                        {
                            while (tcpClient.Available > 0)
                            {
                                byte[] data = new byte[tcpClient.Available];

                                using (StreamReader reader = new StreamReader(tcpClient.GetStream(), Encoding.UTF8))
                                {
                                    string line;
                                    while ((line = reader.ReadLine()) != null)
                                    {
                                        try
                                        {
                                            List <byte> knxmsg   = new List <byte>(Convert.FromBase64String(line));
                                            byte        checksum = knxmsg.Last();
                                            knxmsg.RemoveAt(knxmsg.Count - 1);

                                            if (CheckChecksum(knxmsg, checksum))
                                            {
                                                //Console.Write("OK: ");
                                                KNXBaseTypes.KNXmessage kNXmessage = KNXBaseTypes.KNXmessage.fromByteArray(knxmsg.ToArray(), DateTime.Now);
                                                base.OnParentKNXMessage(this, kNXmessage, KnxPacketEvents.GotKNXPacket);
                                            }
                                            else
                                            {
                                                Console.Write("CheckSumMismatch: " + line);
                                            }
                                        }
                                        catch
                                        {
                                            Console.Write("Cannot process Base64: " + line);
                                        }
                                    }
                                }
                            }
                        }
                        Thread.Sleep(25);
                    }
                    catch (Exception e)
                    {
                        this.OnParentKNXEvent(this, KNXNetworkLayerTemplate.KnxPacketEvents.DeviceOffline);
                        Console.WriteLine("Connection Failed: " + e);
                        Thread.Sleep(5000);
                    }
                }
                catch (Exception e)
                {
                    this.OnParentKNXEvent(this, KNXNetworkLayerTemplate.KnxPacketEvents.DeviceOffline);
                    Console.WriteLine("Cannot Connect: " + e);
                    Thread.Sleep(5000);
                }
            }
        }
コード例 #6
0
        private static void KNXUart_OnKNXMessage(KNXNetworkLayerTemplate caller, KNXBaseTypes.KNXmessage Message, KNXNetworkLayerTemplate.KnxPacketEvents uartEvent)
        {
            stdOut.WriteOut(Message);
            try
            {
                FahPayloadInterpeter.TryToInterpret(ref Message);

                if (Message.Payload.Apdu.apduType == KNXAdpu.ApduType.FunctionPropertyStateResponse)
                {
                    Message.Payload.ReadablePayloadPacket = ((FAHFunctionPropertyStateResponse)Message.Payload.ReadablePayloadPacket).ProcessPayload(lastRequestedPropertyControl, ByteDataParm);

                    if (((FAHFunctionPropertyStateResponse)Message.Payload.ReadablePayloadPacket).resultCode != KNXHelpers.knxPropertyReturnValues.CommandNotSupported)
                    {
                        /*
                         * bool more;
                         * if (!((FAHFunctionPropertyStateResponse)Message.Payload.ReadablePayloadPacket).SaveToDevice(ref fahDevToLearn, out more))
                         * {
                         *  Console.BackgroundColor = ConsoleColor.Red;
                         *  Console.Write("Not saved: ");
                         *  Console.BackgroundColor = ConsoleColor.Black;
                         * }*/
                    }
                }
                else if (Message.Payload.Apdu.apduType == KNXAdpu.ApduType.FunctionPropertyCommand)
                {
                    ((FAHFunctionPropertyCommand)Message.Payload.ReadablePayloadPacket).GetPropertyControlForReply(ref ByteDataParm, ref lastRequestedPropertyControl);
                }

                string ret = string.Format("{0}; {1} ", Message.Timestamp.ToString(KNXHelpers.DateTimeFormat), Message.HeaderAsString);
                Console.Write(ret);
                Message.Payload.ReadablePayloadPacket.PrintUnaccountedBytes(false);

                switch (Message.Payload.Apdu.apduType)
                {
                case KNXAdpu.ApduType.IndividualAddressSerialNumberWrite:
                    var fasnw = Message.Payload.ReadablePayloadPacket as FAHIndividualAddressSerialNumberWrite;

                    /*
                     * bool more;
                     * fasnw.SaveToDevice(ref fahDevToReturn, out more);
                     */
                    //Succes
                    return;

                case KNXAdpu.ApduType.DeviceDescriptorRead:
                    //LastCreatedMsg = FAHDeviceDescriptorResponse.CreateResponse(fahDevToReturn, Message.SourceAddress);
                    return;

                case KNXAdpu.ApduType.GroupValueWrite:
                    return;

                case KNXAdpu.ApduType.FunctionPropertyCommand:
                    var fpc = Message.Payload.ReadablePayloadPacket as FAHFunctionPropertyCommand;

                    /*KNXmessage k = fpc.ProcessAndCreateResponse(fahDevToReturn);
                     * if (k != null)
                     * {
                     *  LastCreatedMsg = k;
                     * }*/
                    break;

                case KNXAdpu.ApduType.DeviceDescriptorResponse:
                    FAHDeviceDescriptorResponse fAHDeviceDescriptorResponse = Message.Payload.ReadablePayloadPacket as FAHDeviceDescriptorResponse;

                    /*bool mi;
                     * fAHDeviceDescriptorResponse.SaveToDevice(ref fahDevToLearn, out mi);
                     *
                     * var z = fAHDeviceDescriptorResponse.FahDeviceAddress;
                     * if (fAHDeviceDescriptorResponse.FahDeviceAddress == fahDevToReturn.FaHAddress)
                     * {
                     *  if (LastCreatedMsg.ToHexString() != Message.ToHexString())
                     *  {
                     *      Console.BackgroundColor = ConsoleColor.Blue;
                     *      Console.WriteLine("Gen: {0}", LastCreatedMsg.ToHexString());
                     *      Console.BackgroundColor = ConsoleColor.Red;
                     *      Console.WriteLine("Err: {0}", Message.ToHexString());
                     *  }
                     *  Console.BackgroundColor = ConsoleColor.Black;
                     *  Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------");
                     *  LastCreatedMsg = new KNXmessage(knxControlField.KnxPacketType.KNX_PacketShort);
                     *
                     * }
                     */
                    break;

                default:
                    /*if (LastCreatedMsg.ToHexString() != Message.ToHexString())
                     * {
                     *  Console.BackgroundColor = ConsoleColor.Blue;
                     *  Console.WriteLine("Gen: {0}", LastCreatedMsg.ToHexString());
                     *  Console.BackgroundColor = ConsoleColor.Red;
                     *  Console.WriteLine("Err: {0}", Message.ToHexString());
                     * }*/
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------");
                    LastCreatedMsg = new KNXmessage(knxControlField.KnxPacketType.KNX_PacketShort);
                    break;
                }
            }
            catch (Exception e)
            {
                Console.Write("Error parsing: " + e);
            }
        }