Sha256: c6350948bd995bb173f73c02a5b02416a9054f13980476e126ad36e4318f160f

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module DjiMqttConnect
  module Thing::Product
    class ServicesReplyTopicRepository < TopicRepository
      SERVICES_REPLY_TOPIC_REGEX = /\Athing\/product\/(?<gateway_sn>.+)\/services_reply\z/

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

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

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

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

          if message.instance_of?(ServicesReplyMessage)
            # Broadcast an unsupported message event
            broadcast(:unsupported_message, topic, raw_message)
          else
            # Build event name and broadcast (i.e. ::FlighttaskPrepareServicesReplyMessage => flighttask_prepare_reply)
            event_name = "#{message._method.to_sym}_reply"
            broadcast(event_name, gateway_sn, message)
          end

          # Broadcast a generic received services reply message event
          broadcast(:received_services_reply_message, gateway_sn, message)
        rescue ParseError => error
          broadcast(:parse_error, error, topic, raw_message)
        end
      end

      private

      def services_reply_marshal
        @services_reply_marshal ||= ServicesReplyMarshal.new
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.23.1 lib/dji_mqtt_connect/topics/thing/product/services_reply.rb
dji_mqtt_connect-0.1.23 lib/dji_mqtt_connect/topics/thing/product/services_reply.rb