Sha256: dae29e1f24b05fd4d6777367ee742980b44c613bc528c8eb18762caa84afc447

Contents?: true

Size: 956 Bytes

Versions: 4

Compression:

Stored size: 956 Bytes

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

4 entries across 4 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.1.4 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.1.3 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.1.2 lib/dji_mqtt_connect/topics/sys/product/status.rb
dji_mqtt_connect-0.1.1.1 lib/dji_mqtt_connect/topics/sys/product/status.rb