예제 #1
0
 protected virtual void OnKnxNetIpMessageReceived(KnxNetIpMessage message)
 {
     if (message != null)
     {
         Debug.WriteLine("{0} RECV <= {1} (HANDLED)", DateTime.Now.ToLongTimeString(), message);
     }
 }
예제 #2
0
        protected override void OnKnxNetIpMessageReceived(KnxNetIpMessage message)
        {
            base.OnKnxNetIpMessageReceived(message);

            if (!(message.Body is RoutingIndication routingIndication))
            {
                return;
            }

            InvokeKnxMessageReceived(routingIndication.Cemi);
        }
예제 #3
0
        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="knxMessage">The KNX message.</param>
        public override async Task SendMessage(IKnxMessage knxMessage)
        {
            // create net ip message
            var netIpMessage = KnxNetIpMessage.Create(KnxNetIpServiceType.RoutingIndication);

            // set knx message
            ((RoutingIndication)netIpMessage.Body).Cemi = knxMessage;

            // send
            Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} SEND => {netIpMessage}");

            var bytes = netIpMessage.ToByteArray();
            await UdpClient.SendAsync(bytes, bytes.Length);
        }
예제 #4
0
        internal static KnxNetIpMessage GetDisconnectRequest(IPEndPoint localEndPoint, byte communicationChannel)
        {
            if (localEndPoint == null)
            {
                throw new ArgumentNullException(nameof(localEndPoint), "LocalEndpoint cannot be null");
            }

            var msg = KnxNetIpMessage.Create(KnxNetIpServiceType.DisconnectRequest);

            if (msg.Body is DisconnectRequest body)
            {
                InitializeHostProtocolAddressInformation(body.HostProtocolAddressInfo, localEndPoint);
            }

            return(msg);
        }
예제 #5
0
        public static KnxNetIpMessage GetSearchRequest(IPEndPoint localEndPoint)
        {
            if (localEndPoint == null)
            {
                throw new ArgumentNullException(nameof(localEndPoint), "LocalEndpoint cannot be null");
            }

            var msg = KnxNetIpMessage.Create(KnxNetIpServiceType.SearchRequest);

            if (msg.Body is SearchRequest body)
            {
                InitializeHostProtocolAddressInformation(body.Endpoint, localEndPoint);
            }

            return(msg);
        }
예제 #6
0
        /// <summary>
        /// Receives the data from the UDP client.
        /// </summary>
        private async void ReceiveData()
        {
            KnxNetIpMessage lastMessage = null;

            var receivedBuffer = new List <byte>();

            try
            {
                while (true)
                {
                    var receivedResult = await UdpClient.ReceiveAsync();

                    var receivedData = receivedResult.Buffer.ToArray();
                    receivedBuffer.AddRange(receivedData);
                    if (!receivedBuffer.Any())
                    {
                        continue;
                    }

                    var msg = KnxNetIpMessage.Parse(receivedBuffer.ToArray());
                    if (msg == null)
                    {
                        continue;
                    }

                    receivedBuffer.Clear();

                    // verify that the message differ from last one.
                    if ((lastMessage != null) && (lastMessage.ServiceType == msg.ServiceType))
                    {
                        if (lastMessage.ToByteArray().SequenceEqual(msg.ToByteArray()))
                        {
                            continue;
                        }
                    }

                    OnKnxNetIpMessageReceived(msg);
                    lastMessage = msg;
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine("Listener exception: " + exception.Message);
            }
        }
예제 #7
0
        internal static KnxNetIpMessage GetConnectRequest(IPEndPoint localEndPoint)
        {
            if (localEndPoint == null)
            {
                throw new ArgumentNullException(nameof(localEndPoint));
            }

            var msg = KnxNetIpMessage.Create(KnxNetIpServiceType.ConnectionRequest);

            if (msg.Body is ConnectionRequest body)
            {
                InitializeHostProtocolAddressInformation(body.ControlEndpoint, localEndPoint);
                InitializeHostProtocolAddressInformation(body.DataEndpoint, localEndPoint);
                body.Data.ConnectionType = ConnectionType.TunnelingConnection;
            }

            return(msg);
        }
예제 #8
0
        /// <summary>
        /// Tries the parse.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes">The bytes.</param>
        /// <param name="message">The message.</param>
        /// <returns><c>true</c>, if the parse was possible; otherwise <c>false</c></returns>
        public static bool TryParse <T>(byte[] bytes, out KnxNetIpMessage <T> message) where T : TunnelingMessageBody, new()
        {
            message = null;

            // prevent throwing a Parse exception
            if (bytes.Length < HeaderLength)
            {
                return(false);
            }

            try
            {
                message = Parse(bytes) as KnxNetIpMessage <T>;
                return(message != null);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #9
0
        public static KnxNetIpMessage CreateTunnelingRequestMessage()
        {
            var message = KnxNetIpMessage.Create(KnxNetIpServiceType.TunnelingRequest);

            return(null);
        }
예제 #10
0
 public static KnxNetIpMessage GetTunnelingRequest()
 {
     return(KnxNetIpMessage.Create(KnxNetIpServiceType.TunnelingRequest));
 }