/// <summary>
        /// Event firing method.
        /// </summary>
        /// <param name="frame">The Datagram just received.</param>
        protected virtual void OnRaiseDatagramReceivedEvent(DatagramReceivedEventArgs datagramEvent)
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                // copy the event handler to avoid mid process subscribe/un-subscribe
                EventHandler <DatagramReceivedEventArgs> handler = RaiseDatagramReceivedEvent;

                // Check if there are any Subscribers
                if (handler != null)
                {
                    // Call the Event
                    handler(this, datagramEvent);
                }
            }
                                         );
        }
Beispiel #2
0
        protected void OnDatagramReceivedEventHandler(object sender, DatagramReceivedEventArgs a)
        {
            CANFrame frame = new CANFrame();

            if (a.Datagram.Length < 4)
            {
                ServerMsgLog.WarnFormat("UDP out-of-range, length {0}", a.Datagram.Length);
                return;
            }

            uint msgId = 0;

            try
            {
                // work out what it all meens
                // first assume that the datgram is a CNX CAN message
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(a.Datagram, 0, 4);
                }
                msgId = BitConverter.ToUInt32(a.Datagram, 0);

                switch (msgId)
                {
                case (uint)CNXMsgIds.TripProgress:
                    frame.MailboxId = msgId;
                    frame.DataFromArray(a.Datagram, 4, a.Datagram.Length - 4);
                    TripProgressType msgType;
                    int pathId;
                    int position;
                    int?tripNo;
                    CNXCANMsgHelper.UnpackTripProgress(frame, out msgType, out pathId, out position, out tripNo);
                    object[] args = { msgType, pathId, position, tripNo };
                    ServerMsgLog.WarnFormat("UDP Trip Progress {0}, RouteTag {1}, Progress {2}, tripNo {3}.", args);
                    mCANClient.Send(frame);
                    break;

                case (uint)CNXMsgIds.DeviceCatalogue:
                    ServerMsgLog.WarnFormat("UDP Re-Cataloge message (0x101).");
                    // clear our catalogue
                    mCANServer.ClearDeviceCatalogue();
                    // put our self into the new catalogue
                    AddSelfToCatalogue();
                    // reset catalogues on all equipment.
                    frame.ClearData();
                    frame.MailboxId = (uint)CANLib.CNXMsgIds.DeviceCatalogue;
                    mCANClient.Send(frame);
                    mCANClient.Send(CNXCANMsgHelper.PackIdentifiers(mCommsServer.CommsAddress, mCommsServer.CompanyTag));
                    break;

                case (uint)CNXMsgIds.TripNone:
                case (uint)CNXMsgIds.TripOffRoute:
                    frame.MailboxId = msgId;
                    mCANClient.Send(frame);
                    ServerMsgLog.WarnFormat("UDP {0}.", (CNXMsgIds)msgId);
                    break;

                case (uint)CNXMsgIds.TripOnRoute:
                    frame.MailboxId = msgId;
                    frame.DataFromArray(a.Datagram, 4, a.Datagram.Length - 4);
                    ServerMsgLog.WarnFormat("UDP {0}.", frame.ToString());
                    mCANClient.Send(frame);
                    break;

                case (uint)RTTMesg.RTTInMsgIds.FirmwareBuildNos:
                    ServerMsgLog.WarnFormat("UDP firmware version (0x1001) length {0}\n{1}.", a.Datagram.Length, CommsServer.HexDump(a.Datagram, a.Datagram.Length));
                    if (a.Datagram.Length > 5)
                    {
                        StartFirmwareManagement(a.Datagram, 4);
                    }
                    break;

                case (uint)RTTMesg.RTTInMsgIds.ResourceBuildNos:
                    ServerMsgLog.WarnFormat("UDP configuration message (0x1002) length {0}\n{1}.", a.Datagram.Length, CommsServer.HexDump(a.Datagram, a.Datagram.Length));
                    if (a.Datagram.Length > 5)
                    {
                        byte[] versions = new byte[a.Datagram.Length - 4];
                        Array.Copy(a.Datagram, 4, versions, 0, versions.Length);
                        StartDeviceConfigurationManagement(versions);
                    }
                    break;

                case (uint)RTTMesg.RTTInMsgIds.CompanyTag:
                    mCommsServer.CompanyTag = a.Datagram[4];
                    ServerMsgLog.WarnFormat("UDP CompanyTag {0}", mCommsServer.CompanyTag);
                    mCANClient.Send(CNXCANMsgHelper.PackIdentifiers(mCommsServer.CommsAddress, mCommsServer.CompanyTag));
                    break;

                case (uint)RTTMesg.RTTInMsgIds.DriverLogonOK:
                    ServerMsgLog.WarnFormat("UDP {0}.", (RTTMesg.RTTInMsgIds)msgId);
                    frame.MailboxId = (uint)CNXMsgIds.LogonResult;
                    frame.Data      = new byte[1];
                    frame.Data[0]   = (byte)LogonState.LogonOK;
                    mCANClient.Send(frame);
                    break;

                case (uint)RTTMesg.RTTInMsgIds.DriverLogonFail:
                    ServerMsgLog.WarnFormat("UDP {0}.", (RTTMesg.RTTInMsgIds)msgId);
                    frame.MailboxId = (uint)CNXMsgIds.LogonResult;
                    frame.Data      = new byte[1];
                    frame.Data[0]   = (byte)LogonState.LogonFailed;
                    mCANClient.Send(frame);
                    break;

                case (uint)RTTMesg.RTTInMsgIds.DriverLogoff:
                    ServerMsgLog.WarnFormat("UDP {0}.", (RTTMesg.RTTInMsgIds)msgId);
                    BeginSendTransientBlock(Block.DriverLogon, new byte[] { 0 });
                    break;

                case (uint)RTTMesg.RTTInMsgIds.DriverMesg2:
                    ServerMsgLog.WarnFormat("UDP {0}\n{1}.", (RTTMesg.RTTInMsgIds)msgId, CommsServer.HexDump(a.Datagram, a.Datagram.Length));
                    //TransientBlock block = new TransientBlock(mCANClient, (byte)Block.MessageToDriver);
                    byte[] blockData = new byte[a.Datagram.Length - 4];
                    Array.Copy(a.Datagram, 4, blockData, 0, blockData.Length);
                    //block.Send(blockData, false);
                    BeginSendTransientBlock(Block.MessageToDriver, blockData);
                    break;

                case (uint)RTTMesg.RTTInMsgIds.VehicleInfo:
                    mCommsServer.CompanyTag = a.Datagram[4];
                    mCommsServer.VehicleId  = Encoding.ASCII.GetString(a.Datagram, 5, a.Datagram.Length - 5);
                    ServerMsgLog.WarnFormat("UDP VehicleInfo {0} {1}", mCommsServer.CompanyTag, mCommsServer.VehicleId);
                    mCANClient.Send(CNXCANMsgHelper.PackIdentifiers(mCommsServer.CommsAddress, mCommsServer.CompanyTag));
                    break;

                case (uint)RTTMesg.RTTInMsgIds.FlushData:
                    ServerMsgLog.WarnFormat("UDP Flush APC");
                    frame.MailboxId = (uint)CNXMsgIds.ResetLoading;
                    frame.DataFromArray(a.Datagram, 4, a.Datagram.Length - 4);
                    mApc.FrameReceivedEventHandler(null, new FrameReceivedEventArgs(frame));
                    mCANClient.Send(frame);
                    // force daily restart as requested by Iris
                    //if (mTracker.mIris != null)
                    //	mTracker.mIris.Restart();
                    break;

                default:
                    ServerMsgLog.WarnFormat("UDP Unknown id {0} length {1}\n{2}.", msgId, a.Datagram.Length, CommsServer.HexDump(a.Datagram, a.Datagram.Length));
                    break;
                }
            }
            catch (Exception e)
            {
                ServerMsgLog.ErrorFormat("Datagram {0} Handler Error : {1}", msgId, e.ToString());
            }
        }