Sha256: baf6d6e9bac2c1edf2130bc0859578957060cd0634897418023ff0554b8ef111

Contents?: true

Size: 1.22 KB

Versions: 43

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module DjiMqttConnect
  module Sys::Product
    class StatusTopicRepository < TopicRepository
      STATUS_TOPIC_REGEX = /\Asys\/product\/(?<device_identifier>.+)\/status\z/

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

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

          device_identifier = matched_topic[:device_identifier]
          message = status_marshal.load(raw_message)

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

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

      private

      def status_marshal
        @status_marshal ||= StatusMarshal.new
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.25.1 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.25 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.24.1 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.24 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.23.3 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.23.2 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.23.1 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.23 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.22.2 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.22.1 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.22 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.21 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.20.1 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.20 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.19.2 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.19.1 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.19 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.18 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.17.1 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.17 lib/dji_mqtt_connect/topics/sys/product/status.rb