Sha256: 035a68618e5b4978ddc19acfeb6049bdea73f4829817d68f625e3896d775b73d

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module DjiMqttConnect
  module Thing::Product
    class EventsTopicRepository < TopicRepository
      EVENTS_TOPIC_REGEX = /\Athing\/product\/(?<device_sn>.+)\/events\z/

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

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

          device_sn = matched_topic[:device_sn]
          message = EventsMarshal.load(raw_message)

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

          # Broadcast a generic received events message event
          broadcast(:received_events_message, device_sn, message)

          if message.instance_of?(EventsMessage)
            # Broadcast an unsupported message event
            broadcast(:unsupported_message, topic, raw_message)
          else
            # Build event name and broadcast (i.e. ::HmsEventsMessage => hms_event)
            event_name = message.class.name.demodulize.sub(/sMessage\z/, "").underscore.to_sym
            broadcast(event_name, device_sn, message)
          end
        rescue ParseError => error
          broadcast(:parse_error, error, topic, raw_message)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.25.1 lib/dji_mqtt_connect/topics/thing/product/events.rb
dji_mqtt_connect-0.1.25 lib/dji_mqtt_connect/topics/thing/product/events.rb
dji_mqtt_connect-0.1.24.1 lib/dji_mqtt_connect/topics/thing/product/events.rb
dji_mqtt_connect-0.1.24 lib/dji_mqtt_connect/topics/thing/product/events.rb
dji_mqtt_connect-0.1.23.3 lib/dji_mqtt_connect/topics/thing/product/events.rb
dji_mqtt_connect-0.1.23.2 lib/dji_mqtt_connect/topics/thing/product/events.rb