Sha256: 8c6ffe9382e8417029b192d65fe00476d00508cc9029a10c228cce528ca1b646

Contents?: true

Size: 1.2 KB

Versions: 35

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module DjiMqttConnect
  module Thing::Product
    class RequestsTopicRepository < TopicRepository
      REQUESTS_TOPIC_REGEX = /\Athing\/product\/(?<gateway_sn>.+)\/requests\z/

      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}")

          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

      private

      def requests_marshal
        @requests_marshal ||= RequestsMarshal.new
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.22.2 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.22.1 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.22 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.21 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.20.1 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.20 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.19.2 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.19.1 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.19 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.18 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.17.1 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.17 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.16.2 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.16.1 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.16 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.15.2 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.15.1 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.15 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.14.1 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.14 lib/dji_mqtt_connect/topics/thing/product/requests.rb