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} "); } }
protected Location Parse(MessageInput input, params Func <MessageInput, Location>[] parsers) { foreach (Func <MessageInput, Location> parse in parsers) { try { input.DataMessage.Reader.Reset(); Location location = parse(input); if (location != null) { return(location); } } catch (Exception) { // ignored } } return(null); }
protected IEnumerable <Location> ParseRange(MessageInput input, params Func <MessageInput, IEnumerable <Location> >[] parsers) { foreach (Func <MessageInput, IEnumerable <Location> > parse in parsers) { try { input.DataMessage.Reader.Reset(); input.DataMessage.ByteReader.Reset(); IEnumerable <Location> location = parse(input); if (location != null) { return(location); } } catch (Exception) { // ignored } } return(null); }
public virtual Location Parse(MessageInput input) { return(null); }
public virtual IEnumerable <Location> ParseRange(MessageInput input) { Location location = Parse(input); return(location != null ? new[] { location } : null); }