Class: RSMP::Matcher
- Defined in:
- lib/rsmp/collect/matcher.rb
Overview
Base class for waiting for specific status or command responses, specified by a list of queries. Queries are defined as an array of hashes, e.g [
{"cCI"=>"M0104", "cO"=>"setDate", "n"=>"securityCode", "v"=>"1111"},
{"cCI"=>"M0104", "cO"=>"setDate", "n"=>"year", "v"=>"2020"},
{"cCI"=>"M0104", "cO"=>"setDate", "n"=>"month", "v"=>/\d+/}
]
Note that queries can contain regex patterns for values, like /d+/ in the example above.
When an input messages is received it typically contains several items, eg: [
{"cCI"=>"M0104", "n"=>"month", "v"=>"9", "age"=>"recent"},
{"cCI"=>"M0104", "n"=>"day", "v"=>"29", "age"=>"recent"},
{"cCI"=>"M0104", "n"=>"hour", "v"=>"17", "age"=>"recent"}
]
Each input item is matched against each of the queries. If a match is found, it's stored in the @results hash, with the query as the key, and a mesage and status as the key. In the example above, this query:
“cO”=>“setDate”, “n”=>“month”, “v”=>/d+/
matches this input:
“n”=>“month”, “v”=>“9”, “age”=>“recent”
And the result is stored as:
{"cCI"=>"M0104", "cO"=>"setDate", "n"=>"month", "v"=>/\d+/ =>
{ <StatusResponse message>, "cO"=>"setDate", "n"=>"month", "v"=>"9" }
}
Direct Known Subclasses
Instance Attribute Summary collapse
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
Attributes inherited from Collector
Instance Method Summary collapse
-
#build_query(want) ⇒ Object
Build a query object.
-
#done? ⇒ Boolean
Are there queries left to type_match?.
-
#initialize(proxy, want, options = {}) ⇒ Matcher
constructor
Initialize with a list of wanted statuses.
-
#messages ⇒ Object
Get messages from results.
-
#perform_match(message) ⇒ Object
Check if a messages matches our criteria.
-
#progress ⇒ Object
Return progress as completes queries vs.
-
#query_result(want) ⇒ Object
Get a results.
-
#query_status ⇒ Object
Get a simplified hash of queries, with values set to either true or false, indicating which queries have been matched.
-
#reached ⇒ Object
Get an array of the last item received for each query.
-
#summary ⇒ Object
Get a simply array of bools, showing which queries have been matched.
Methods inherited from Collector
#cancel, #collect, #collect!, #complete, #describe_progress, #do_stop, #ingoing?, #inspect, #keep, #notify, #notify_disconnect, #notify_error, #notify_schema_error, #outgoing?, #reject_not_ack, #reset, #start, #type_match?, #wait, #wait!
Methods inherited from Listener
#change_notifier, #listen, #notify, #notify_error
Methods included from Inspect
Constructor Details
#initialize(proxy, want, options = {}) ⇒ Matcher
Initialize with a list of wanted statuses
39 40 41 42 43 |
# File 'lib/rsmp/collect/matcher.rb', line 39 def initialize proxy, want, ={} raise ArgumentError.new("num option cannot be used") if [:num] super proxy, .merge( ingoing: true, outgoing: false) @queries = want.map { |item| build_query item } end |
Instance Attribute Details
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
36 37 38 |
# File 'lib/rsmp/collect/matcher.rb', line 36 def queries @queries end |
Instance Method Details
#build_query(want) ⇒ Object
Build a query object. Sub-classes should override to use their own query classes.
47 48 49 |
# File 'lib/rsmp/collect/matcher.rb', line 47 def build_query want Query.new want end |
#done? ⇒ Boolean
Are there queries left to type_match?
76 77 78 |
# File 'lib/rsmp/collect/matcher.rb', line 76 def done? @queries.all? { |query| query.done? } end |
#messages ⇒ Object
Get messages from results
64 65 66 |
# File 'lib/rsmp/collect/matcher.rb', line 64 def @queries.map { |query| query. }.uniq end |
#perform_match(message) ⇒ Object
Check if a messages matches our criteria. Match each query against each item in the message
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rsmp/collect/matcher.rb', line 93 def perform_match return unless type_match?() @queries.each do |query| # look through queries get_items().each do |item| # look through items in message matched = query.perform_match(item,) if matched == true matched = @block.call(,item) if @block end if matched != nil type = {true=>'match',false=>'mismatch'}[matched] @notifier.log "#{@title.capitalize} #{.m_id_short} collect #{type} #{query.want}, item #{item}", level: :debug break end end end complete if done? @notifier.log "#{@title.capitalize} collect reached #{summary}", level: :debug end |
#progress ⇒ Object
Return progress as completes queries vs. total number of queries
69 70 71 72 73 |
# File 'lib/rsmp/collect/matcher.rb', line 69 def progress need = @queries.size reached = @queries.count { |query| query.done? } { need: need, reached: reached } end |
#query_result(want) ⇒ Object
Get a results
52 53 54 55 56 |
# File 'lib/rsmp/collect/matcher.rb', line 52 def query_result want query = @queries.find { |q| q.want == want} raise unless query query.got end |
#query_status ⇒ Object
Get a simplified hash of queries, with values set to either true or false, indicating which queries have been matched.
82 83 84 |
# File 'lib/rsmp/collect/matcher.rb', line 82 def query_status @queries.map { |query| [query.want, query.done?] }.to_h end |
#reached ⇒ Object
Get an array of the last item received for each query
59 60 61 |
# File 'lib/rsmp/collect/matcher.rb', line 59 def reached @queries.map { |query| query.got }.compact end |
#summary ⇒ Object
Get a simply array of bools, showing which queries have been matched.
87 88 89 |
# File 'lib/rsmp/collect/matcher.rb', line 87 def summary @queries.map { |query| query.done? } end |