Sha256: 9dc73d673d7c846977144059688900918a8ae38f8cfb5900222baadb10e60bda

Contents?: true

Size: 945 Bytes

Versions: 4

Compression:

Stored size: 945 Bytes

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}")
          broadcast(message._method.to_sym, gateway_sn, message)
        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

4 entries across 4 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.1.4 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.1.3 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.1.2 lib/dji_mqtt_connect/topics/thing/product/requests.rb
dji_mqtt_connect-0.1.1.1 lib/dji_mqtt_connect/topics/thing/product/requests.rb