Ejemplo n.º 1
0
        public override void OnReceived(TcpSession tcpSession, byte[] buffer, long offset, long size)
        {
            var receivedDataByteArray = new byte[size];

            Array.Copy(buffer, receivedDataByteArray, size);
            var receivedData = ParserInfo.ParserDataType == ParserDataType.Hex
                ? SplitData.GetHexValueFromByteArray(receivedDataByteArray)
                : Encoding.UTF8.GetString(receivedDataByteArray);

            Console.WriteLine($"{receivedData}");
            if (ParserInfo.IsAknowledge)
            {
                SendAcknowledgeMent(tcpSession, receivedData);
            }
            RavenDB.RavenDbConnector.Add(new RavenDB.GpsData {
                Data = receivedData
            });
            Rabbitmq.RabbitBusProvider.AddToQueue(new Rabbitmq.GpsData {
                DeviceName = ParserInfo.ParserName, CreatedDateTime = DateTime.Now, Message = receivedData
            }, ParserInfo.ParserName);
            //Rabbitmq.QueueHandler.AddToSubscriber(ParserInfo.ParserName, ReceivedData);
            if (ParserInfo.IsForwardData)
            {
                if (!string.IsNullOrEmpty(ParserInfo.RedirectIp) && ParserInfo.RedirectPort > 0)
                {
                    if (TcpForwarder == null)
                    {
                        Console.WriteLine($"Forwarding data:{receivedData}");

                        TcpForwarder = new TcpForwarder(IPAddress.Parse(ParserInfo.RedirectIp), ParserInfo.RedirectPort);
                        TcpForwarder.ForwardData(receivedDataByteArray);
                    }
                    else
                    {
                        Console.WriteLine($"Forwarding data:{receivedData}");
                        TcpForwarder.ForwardData(receivedDataByteArray);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static string BuildAcknowledgeMessage(string message, out string IMEI)
        {
            IMEI = string.Empty;
            string acknowledge = string.Empty;

            try
            {
                string packetType = message.Substring(6, 2);

                byte[] receivedMessageArray = SplitData.StringToByteArray(message);

                int messageLength = receivedMessageArray[2];
                var serialNumber  = receivedMessageArray.Skip(2 + 1 + messageLength - 4).Take(2).ToArray();

                switch (packetType)
                {
                case "01":
                    serialNumber.CopyTo(loginResponse, 4);

                    var sendCRC = CrcGenerator.crc_bytes(loginResponse.Skip(2).Take(loginResponse.Length - 6).ToArray());

                    loginResponse[loginResponse.Length - 4] = (byte)((sendCRC >> 8) & 0xFF);
                    loginResponse[loginResponse.Length - 3] = (byte)((sendCRC) & 0xFF);

                    IMEI = Encoding.ASCII.GetString(receivedMessageArray.Skip(4).Take(messageLength - 5).ToArray());

                    acknowledge = SplitData.GetHexValueFromByteArray(loginResponse);

                    Console.WriteLine("Imei : '{0}'", IMEI);

                    Console.WriteLine("Acknowledge Location Data : '{0}'", acknowledge);

                    break;

                case "13":
                    serialNumber.CopyTo(heartbeatResponse, 4);
                    sendCRC = CrcGenerator.crc_bytes(heartbeatResponse.Skip(2).Take(heartbeatResponse.Length - 6).ToArray());


                    heartbeatResponse[heartbeatResponse.Length - 4] = (byte)((sendCRC >> 8) & 0xFF);
                    heartbeatResponse[heartbeatResponse.Length - 3] = (byte)((sendCRC) & 0xFF);



                    acknowledge = SplitData.GetHexValueFromByteArray(heartbeatResponse);

                    Console.WriteLine("Send Message : '{0}'", acknowledge);

                    break;

                case "16":
                case "26":
                    int alarmPacketLen = alarmResponse.Length - 5;
                    alarmResponse[2] = (byte)(alarmPacketLen & 0xFF);

                    serialNumber.CopyTo(alarmResponse, alarmPacketLen - 1);

                    sendCRC = CrcGenerator.crc_bytes(alarmResponse.Skip(2).Take(alarmPacketLen - 1).ToArray());

                    alarmResponse[alarmPacketLen + 1] = (byte)((sendCRC >> 8) & 0xFF);
                    alarmResponse[alarmPacketLen + 2] = (byte)((sendCRC) & 0xFF);

                    acknowledge = SplitData.GetHexValueFromByteArray(alarmResponse);

                    Console.WriteLine("Acknowledge Location Data : '{0}'", acknowledge);
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(acknowledge);
        }