Example #1
0
        public void AddMessage(FrameVdS frame)
        {
            if (frame == null)
            {
                throw new ArgumentNullException("frame");
            }

            if (frame is FrameVdS_02)
            {
                this.NeedsAck = true;
            }

            this.IsAcked = false;
            this.transmitQueue.Enqueue(frame);
        }
Example #2
0
        public void SendRequest(InformationId informationId)
        {
            switch (informationId)
            {
            case InformationId.SyncReq:
                var syncReq = new FrameTcp(
                    this.MySendCounter,
                    this.OtherSendCounter,
                    this.KeyNumber,
                    informationId,
                    FrameVdS.CreateSyncRequestResponse(InformationId.SyncReq));
                Log.Info("{0} >> {1}", this.Type, syncReq);

                var syncReqbuff = syncReq.Serialize();
                this.stream.Write(syncReqbuff, 0, syncReqbuff.Length);
                this.IncrementMySendCounter();
                break;

            case InformationId.SyncRes:
                break;

            case InformationId.PollReqRes:
                var pollReq = new FrameTcp(
                    this.MySendCounter,
                    this.OtherSendCounter,
                    this.KeyNumber,
                    informationId,
                    FrameVdS.CreateEmpty(InformationId.PollReqRes));
                Log.Info("{0} >> {1}", this.Type, pollReq);

                var polReqBuff = pollReq.Serialize();
                this.stream.Write(polReqBuff, 0, polReqBuff.Length);
                this.IncrementMySendCounter();
                break;

            case InformationId.Payload:
                break;

            case InformationId.ErrorInformationIdUnknown:
                break;

            case InformationId.ErrorProtocolIdUnknown:
                break;

            default:
                throw new ArgumentOutOfRangeException("intInformationId");
            }
        }
Example #3
0
        private void HandleReceived(FrameTcp tcpFrame)
        {
            Log.Info("{0} << {1}", this.Type, tcpFrame);
            this.OtherSendCounter = tcpFrame.SendCounter;
            this.IncrementOtherSendCounter();

            switch (tcpFrame.InformationId)
            {
            case InformationId.ErrorInformationIdUnknown:
                Log.Warn("Unknown information Id");
                break;

            case InformationId.ErrorProtocolIdUnknown:
                Log.Warn("Unknown protocol Id");
                break;

            case InformationId.SyncReq:
                Log.Warn("Sync request received");
                this.SendResponse(FrameVdS.CreateSyncRequestResponse(InformationId.SyncRes));
                break;

            case InformationId.SyncRes:
                Log.Warn("Sync response received");
                this.KeyNumber = tcpFrame.KeyNumber;
                break;

            case InformationId.PollReqRes:
                Log.Warn("Polling request/response received");
                this.LastPollReqReceived = DateTime.Now;
                if (isServer)
                {
                    break;
                }

                // client checks whether there is some data to transmit
                var outFrames = new List <FrameVdS>();

                if (!this.transmitQueue.Any())
                {
                    this.SendResponse(FrameVdS.CreateEmpty(InformationId.PollReqRes));
                }

                FrameVdS outFrame;
                if (!this.transmitQueue.TryDequeue(out outFrame))
                {
                    throw new NullReferenceException("Frame missing");
                }

                outFrames.Add(outFrame);
                var alertFrame = outFrame as FrameVdS_02;
                if (alertFrame != null && alertFrame.MessageType == 0x73)
                {
                    // check for measurement value changed
                    // another message is expected in the queue to indicate which measurement value has changed
                    if (!this.transmitQueue.TryDequeue(out outFrame))
                    {
                        throw new NullReferenceException("Text frame for measurement value change event is missing");
                    }

                    var asciiFrame = outFrame as FrameVdS_54;
                    if (asciiFrame == null)
                    {
                        throw new NullReferenceException();
                    }

                    // create a herstelleridmessage with text to report value change (customer idea!?!)
                    FrameVdS.CreateHerstellerIdMessage(asciiFrame.Text);
                    outFrames.Add(outFrame);
                }
                else
                {
                    outFrames.Add(FrameVdS.CreateHerstellerIdMessage());
                }

                //< always add device id as last messages when data is transmitted
                outFrames.Add(FrameVdS.CreateIdentificationNumberMessage());

                this.SendResponse(outFrames.ToArray());

                break;

            case InformationId.Payload:
                Log.Warn("Payload received");

                foreach (var frame in tcpFrame.Payload)
                {
                    // Handle received frames
                    if (frame.VdsType == VdSType.Quittungsruecksendung)
                    {
                        var ackFrame = new FrameVdS_03(frame.Serialize(), 0);
                        if (ackFrame.MessageType != 0x00)
                        {
                            //TODO: check for ack
                        }

                        Log.Info("ACK received");
                        this.IsAcked  = true;
                        this.NeedsAck = false;
                    }
                    else if (frame.VdsType == VdSType.Verbindung_wird_nicht_mehr_benoetigt)
                    {
                        this.IsAcked = true;
                    }
                    else
                    {
                        throw new NotImplementedException("Only Quittungsruecksendung supported");
                    }
                }

                break;

            default:
                Log.Warn("Invalid Information ID");
                break;
            }
        }
Example #4
0
        public static FrameTcp CreateSyncRequest(uint sendCounter, uint receiveCounter)
        {
            var frame = new FrameTcp(sendCounter, receiveCounter, 0, InformationId.SyncReq, FrameVdS.CreateSyncRequestResponse(InformationId.SyncReq))
            {
                InformationId  = InformationId.SyncReq,
                SendCounter    = sendCounter,
                ReceiveCounter = receiveCounter
            };

            return(frame);
        }