Ejemplo n.º 1
0
        public async Task HandleMessage(Client client, INetworkStreamWrapper networkStream, byte[] bytes)
        {
            ICustomMessageHandler customMessageHandler = GetMessageHandler(client.Protocol);

            MessageInput messageInput = new MessageInput
            {
                Client        = client,
                NetworkStream = networkStream,
                DataMessage   = new DataMessage(bytes, client.Protocol.SplitMessageBy)
            };

            logger.LogTrace(
                $"{client.Protocol}: received {HexUtil.ConvertHexStringArrayToHexString(messageInput.DataMessage.Hex)}");

            int connectionMessageId = await connectionService.AddMessage(client.DeviceConnection.Id, messageInput.DataMessage.HexString);

            try
            {
                List <Location> locations = customMessageHandler.ParseRange(messageInput)?.ToList();

                if (locations != null && locations.Any())
                {
                    // TODO refactor this
                    await connectionService.SetDeviceId(client);

                    await locationService.AddRange(locations, connectionMessageId);
                }
            }
            catch (Exception e)
            {
                logger.LogCritical(e,
                                   $"{customMessageHandler.GetType()}: Error parsing {messageInput.DataMessage.Hex} ");
            }
        }
Ejemplo n.º 2
0
        public async Task HandleStream(ProtocolInput protocolInput)
        {
            using BinaryReader binaryReader = new BinaryReader(protocolInput.NetworkStream);
            await using (BinaryWriter binaryWriter = new BinaryWriter(protocolInput.NetworkStream))
            {
                byte[] data = new byte[2048];
                bool   firstDataReceived = true;

                string imei = string.Empty;

                while (binaryReader.Read() != -1 || !protocolInput.StoppingToken.IsCancellationRequested)
                {
                    binaryReader.Read(data, 0, data.Length);

                    if (firstDataReceived)
                    {
                        for (int i = 1; i <= 15; i++)
                        {
                            imei += (char)data[i];
                        }

                        binaryWriter.Write(01);
                        firstDataReceived = false;
                    }
                    else
                    {
                        List <LocationHolder> locations = teltonikaLocationParser.Convert(data, imei).ToList();

                        if (locations.Any())
                        {
                            await locationService.AddRange(locations.Select(x => x.Location).ToList());

                            string reply = "000000" + Utility.ConvertToHex(locations.Count);

                            binaryWriter.Write(Utility.HexStringToByteArray(reply));
                        }
                    }
                }

                binaryWriter.Close();
            }

            binaryReader.Close();
        }