Example #1
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);
        }
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 SendResponse(params FrameVdS[] frames)
        {
            var response = new FrameTcp(
                this.MySendCounter,
                this.OtherSendCounter,
                this.KeyNumber,
                frames[0].InformationId,
                frames);

            Log.Info("{0} >> {1}", this.Type, response);

            var buff = response.Serialize();

            this.stream.Write(buff, 0, buff.Length);
            this.IncrementMySendCounter();
        }
Example #4
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 #5
0
        public Task Run()
        {
            this.IsActive = true;
            var task = Task.Run(
                () =>
            {
                var rcvBuffer = new byte[256];
                var bytes     = new List <byte>();
                while (!this.cts.IsCancellationRequested)
                {
                    try
                    {
                        var bytesRead = this.stream.Read(rcvBuffer, 0, rcvBuffer.Length);
                        if (bytesRead == 0)
                        {
                            this.cts.Token.WaitHandle.WaitOne(500);
                            continue;
                        }
                        if (bytesRead < 0)
                        {
                            break;
                        }

                        for (int i = 0; i < bytesRead; i++)
                        {
                            bytes.Add(rcvBuffer[i]);
                        }

                        var tmp = bytes.ToArray();
                        if (tmp.Length > 4)
                        {
                            var key    = BitConverter.ToUInt16(tmp.Take(2).Reverse().ToArray(), 0);
                            var length = BitConverter.ToUInt16(tmp.Skip(2).Take(2).Reverse().ToArray(), 0);
                            if (tmp.Length < length + 4)
                            {
                                Log.Info("Incomplete frame, continue reading...");
                                continue;
                            }

                            Array.Resize(ref tmp, length + 4);
                            Log.Trace("RECEIVED: " + BitConverter.ToString(tmp));
                            var frame = new FrameTcp(key, length, tmp);
                            bytes.RemoveRange(0, tmp.Length);
                            this.HandleReceived(frame);
                        }
                    }
                    catch (IOException e)
                    {
                        Log.ErrorException("IO Exception occured", e);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Log.ErrorException("Exception occured", ex);
                        break;
                    }
                }

                this.Close();
            });

            return(task);
        }