Class: DjiMqttConnect::Thing::Product::RequestsTopicRepository

Inherits:
DjiMqttConnect::TopicRepository show all
Defined in:
lib/dji_mqtt_connect/topics/thing/product/requests.rb

Constant Summary collapse

REQUESTS_TOPIC_REGEX =
/\Athing\/product\/(?<gateway_sn>.+)\/requests\z/

Instance Method Summary collapse

Methods inherited from DjiMqttConnect::TopicRepository

#initialize

Constructor Details

This class inherits a constructor from DjiMqttConnect::TopicRepository

Instance Method Details

#listen!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dji_mqtt_connect/topics/thing/product/requests.rb', line 8

def listen!
  listen_to_topic("thing/product/+/requests") do |topic, raw_message|
    logger.debug(raw_message)

    matched_topic = REQUESTS_TOPIC_REGEX.match(topic)
    raise Error, "Unknown topic: #{topic}" unless matched_topic

    gateway_sn = matched_topic[:gateway_sn]
    message = requests_marshal.load(raw_message)

    logger.info("Received #{message} from #{gateway_sn}")

    # Broadcast a generic received requests message event
    broadcast(:received_requests_message, gateway_sn, message)

    if message.instance_of?(RequestsMessage)
      # Broadcast an unsupported message event
      broadcast(:unsupported_message, topic, raw_message)
    else
      # Build event name and broadcast (i.e. ::ConfigRequestsMessage => config)
      broadcast(message._method.to_sym, gateway_sn, message)
    end
  rescue ParseError => error
    broadcast(:parse_error, error, topic, raw_message)
  end
end