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} "); } }
public static string ComputeHash(byte[] bytes, CrcAlgorithm crcAlgorithm) { Parameters parameters = CrcStdParams.Get(crcAlgorithm); Crc crc = new Crc(parameters); IEnumerable <byte> result = crc.ComputeHash(bytes).Take(parameters.HashSize / 8).Reverse(); return(HexUtil.ConvertHexStringArrayToHexString(HexUtil.ConvertByteArrayToHexStringArray(result))); }
public override Location Parse(MessageInput input) { PackageIdentifier packageIdentifier = (PackageIdentifier)input.DataMessage.Bytes[2]; Dictionary <PackageIdentifier, Func <Location> > dictionary = new Dictionary <PackageIdentifier, Func <Location> > { { PackageIdentifier.Login, () => { HandleLoginPackage(input); return(null); } }, { PackageIdentifier.Heartbeat, () => { SendAcknowledge(input); return(null); } }, { PackageIdentifier.Location, () => HandleLocationForV20(input) }, { PackageIdentifier.Warning, () => HandleLocationForV20(input) }, { PackageIdentifier.Report, () => HandleLocationForV20(input) }, { PackageIdentifier.Message, () => { Location location = GetLocationV20(input); string number = HexUtil.ConvertHexStringArrayToHexString( HexUtil.ConvertByteArrayToHexStringArray(input.DataMessage.ByteReader.Get(21))); SendAcknowledge(input, number); return(location); } }, { PackageIdentifier.ParamSet, () => { SendAcknowledge(input, "00"); return(null); } }, { PackageIdentifier.GPSOld, () => HandleMessageForV18(input, false) }, { PackageIdentifier.AlarmOld, () => HandleMessageForV18(input) }, { PackageIdentifier.TerminalStateOld, () => HandleMessageForV18(input) } }; return(dictionary.ContainsKey(packageIdentifier) ? dictionary[packageIdentifier].Invoke() : null); }