Sha256: 500d4283d45d05af15c5007602c9a2d010c864dfcbd8cbda9603c2f9a6872f50

Contents?: true

Size: 949 Bytes

Versions: 4

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

module DjiMqttConnect
  module Thing::Product
    class StateTopicRepository < TopicRepository
      STATE_TOPIC_REGEX = /\Athing\/product\/(?<gateway_sn>.+)\/state\z/

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

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

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

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

          # Broadcast the state update
          broadcast(:state_update, gateway_sn, message)
        rescue ParseError => error
          broadcast(:parse_error, error, topic, raw_message)
        end
      end

      private

      def state_marshal
        @state_marshal ||= StateMarshal.new
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.22.2 lib/dji_mqtt_connect/topics/thing/product/state.rb
dji_mqtt_connect-0.1.22.1 lib/dji_mqtt_connect/topics/thing/product/state.rb
dji_mqtt_connect-0.1.22 lib/dji_mqtt_connect/topics/thing/product/state.rb
dji_mqtt_connect-0.1.21 lib/dji_mqtt_connect/topics/thing/product/state.rb