Sha256: ba3acbbf4f2bd393486b8ff5c8d0b9557c1a5bed255247e32317b2cde2abb727

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

require "mqtt"

module DjiMqttConnect
  # Client for communicating with DJI Cloud API MQTT Messages
  # see: https://developer.dji.com/doc/cloud-api-tutorial/en/server-api-reference/mqtt/topic-definition.html
  class Client
    def self.build(mqtt_address, username:, password:, &block)
      mqtt_url = URI.parse(mqtt_address)
      mqtt_host = mqtt_url.host
      mqtt_port = mqtt_url.port.to_i
      mqtt_scheme = mqtt_url.scheme

      DjiMqttConnect.logger.tagged(name) do
        DjiMqttConnect.logger.info("Connecting to #{mqtt_scheme}://#{mqtt_host}:#{mqtt_port}...")

        mqtt_client = MQTT::Client.connect(
          host: mqtt_host,
          port: mqtt_port,
          client_id: DjiMqttConnect.client_id,
          username: username,
          password: password,
          ssl: !%w[tcp mqtt ws].include?(mqtt_scheme)
        )
        new(mqtt_client)
      end
    end

    def initialize(mqtt_client)
      @mqtt_client = mqtt_client
    end

    # Handles topic sys/product/#{pid}/status
    def sys_product_status_topic
      Sys::Product::StatusTopicRepository.new(self)
    end

    # Handles topic sys/product/#{pid}/status_reply
    def sys_product_status_reply_topic
      Sys::Product::StatusReplyTopicRepository.new(self)
    end

    # Handles topic thing/product/#{pid}/events
    def thing_product_events_topic
      Thing::Product::EventsTopicRepository.new(self)
    end

    # Handles topic thing/product/#{pid}/osd
    def thing_product_osd_topic
      Thing::Product::OsdTopicRepository.new(self)
    end

    # Handles topic thing/product/#{pid}/requests
    def thing_product_requests_topic
      Thing::Product::RequestsTopicRepository.new(self)
    end

    # Handles topic thing/product/#{pid}/requests_reply
    def thing_product_requests_reply_topic
      Thing::Product::RequestsReplyTopicRepository.new(self)
    end

    attr_reader :mqtt_client
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.7 lib/dji_mqtt_connect/client.rb
dji_mqtt_connect-0.1.6 lib/dji_mqtt_connect/client.rb
dji_mqtt_connect-0.1.5 lib/dji_mqtt_connect/client.rb
dji_mqtt_connect-0.1.4.1 lib/dji_mqtt_connect/client.rb
dji_mqtt_connect-0.1.4 lib/dji_mqtt_connect/client.rb
dji_mqtt_connect-0.1.3 lib/dji_mqtt_connect/client.rb