Skip to content

Instantly share code, notes, and snippets.

@tetherit
Created January 20, 2013 21:04
Show Gist options
  • Save tetherit/4581789 to your computer and use it in GitHub Desktop.
Save tetherit/4581789 to your computer and use it in GitHub Desktop.
def parse_command(command_id, data)
command = find_command(command_id)
parsed = {name: command[:name]}.with_indifferent_access
return parsed unless command[:bytes] # Return if no parsable data
# Detect Expected Size
expected_length = 0
command[:bytes].each do |byte_range, value_id|
value = data[byte_range]
# Should have expected size
return unless value.length == [*byte_range].size
expected_length+=value.length
# Detect values that are integers
value = value.to_i if value.match(/^\d+$/)
# See if the value is a key (e.g. error_id has error_ids)
key = value_id.pluralize
parsed[value_id] = value
if command.has_key? key
value_key = key.split('_')[0] # Key is error if error_code
value_result = command[key][value]
parsed[value_key] = value_result
elsif value_id == "partition_id" and not [*1..8].include? value
return # Partition is out of range
elsif value_id == "zone_id" and not [*1..64].include? value
return # Zone is out of range
elsif value_id == "command_id" # Get command name
@dsc_app.each do |app_cmd_name, app_cmd|
parsed[:command] = app_cmd_name if app_cmd[:command] == value
end
elsif value_id == "dsc_date" # Parse DSC date to time object
parsed[:time] = Time.strptime("%010d" % value, '%H%M%m%d%y')
end
end
if expected_length == data.length
parsed[:description] = command[:description] % parsed
parsed
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment