Exemple #1
0
        private void OnNPDUReceived(IPEndPoint from, Address fromAddress, OctetString linkService, NPDU npdu, ByteStream raw)
        {
            try
            {
                APDU.APDU apdu = APDU.APDU.Parse(raw);

                incomingApdu(apdu, fromAddress, linkService);

                // TODO return APDU.createAPDU(servicesSupported, queue);
            }
            catch (System.Exception e)
            {
                // If it's already a BACnetException, don't bother wrapping it.
                //throw e;
                Debug.Print(e.Message);
                Debug.Print(e.StackTrace);
            }

            /*if (inBuffer.Length > 20)
             *  {
             *      string result = printRouterInfo(inBuffer);
             *      Debug.Print("Sending> " + result);
             *
             *      using (
             *          Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
             *              ProtocolType.Udp))
             *      {
             *          //EndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.0.14"), 55056);
             *          byte[] messageBytes = Encoding.UTF8.GetBytes(result);
             *          clientSocket.SendTo(messageBytes, remoteEndPoint);
             *      }
             *
             *  }
             *  else
             *  {
             *      string message = new string(Encoding.UTF8.GetChars(inBuffer));
             *      Debug.Print("Received> " + message);
             *  }*/
        }
Exemple #2
0
        private void incomingApdu(APDU.APDU apdu, Address address, OctetString linkService)
        {
            // if (apdu.expectsReply() != npci.isExpectingReply())
            // throw new MessageValidationAssertionException("Inconsistent message: APDU expectsReply="+
            // apdu.expectsReply() +" while NPCI isExpectingReply="+ npci.isExpectingReply());

            if (apdu is ConfirmedRequest)
            {
                ConfirmedRequest confAPDU = (ConfirmedRequest)apdu;
                byte             invokeId = confAPDU.InvokeId;

                if (confAPDU.IsSegmentedMessage && confAPDU.SequenceNumber > 0)
                {
                    // This is a subsequent part of a segmented message. Notify the waiting room.
                    // TODO waitingRoom.notifyMember(address, linkService, invokeId, false, confAPDU);
                }
                else
                {
                    if (confAPDU.IsSegmentedMessage)
                    {
                        // This is the initial part of a segmented message. Go and receive the subsequent parts.

                        /*WaitingRoomKey key = waitingRoom.enterServer(address, linkService, invokeId);
                         * try
                         * {
                         * receiveSegmented(key, confAPDU);
                         * }
                         * finally
                         * {
                         * waitingRoom.leave(key);
                         * }*/
                    }

                    // Handle the request.
                    try
                    {
                        confAPDU.ParseServiceData();

                        /*if (localDevice.getDCCEnableDisable().equals(EnableDisable.disable))
                         * {
                         * // zpracovavame jenom reset a DCC
                         * // povoleni probiha v handleru DCC
                         * if (!(confAPDU.getServiceRequest() is DeviceCommunicationControlRequest ||
                         * confAPDU.getServiceRequest() is ReinitializeDeviceRequest))
                         * {
                         *  //throw new BACnetException("Communication blocked by DCC.");
                         *  return;
                         * }
                         * else
                         * {
                         *  Debug.Print("DCC/Reinitialize received");
                         * }
                         * }*/


                        ConfirmedRequestReceived?.Invoke(address, linkService, invokeId, confAPDU); // EVENT


                        // TODO Move Handlers to LocalDevice
                    }
                    catch (BACnetErrorException e)
                    {
                        network.sendAPDU(address, linkService, new Error(invokeId, e.Error), false);
                    }
                    catch (BACnetRejectException e)
                    {
                        network.sendAPDU(address, linkService, new Reject(invokeId, e.RejectReason), false);
                    }
                    catch (BACnetException e)
                    {
                        Error error = new Error(confAPDU.InvokeId,
                                                new BaseError((byte)127,
                                                              new BACnetError(ErrorClass.Services, ErrorCode.InconsistentParameters)));
                        network.sendAPDU(address, linkService, error, false);
                        // TODO ExceptionDispatch.fireReceivedException(e);
                    }
                }
            }
            else if (apdu is UnconfirmedRequest)
            {
                //DCC - reakce na prichozi zpravy - blokujeme vsechny Unconfirmed

                /* TODO if (localDevice.getDCCEnableDisable().equals(EnableDisable.disable))
                 * {
                 *  return;
                 * }*/
                UnconfirmedRequestReceived?.Invoke(address, linkService, (UnconfirmedRequest)apdu);
            }
            else
            {
                // An acknowledgement.
                AckAPDU ack = (AckAPDU)apdu;

                // Used for testing only. This is required to test the parsing of service data in an ack.
                // ((ComplexACK) ack).parseServiceData();

                //waitingRoom.notifyMember(address, linkService, ack.getOriginalInvokeId(), ack.isServer(), ack);
            }
        }