Sha256: d2bb2405256717ba9b3b2109e18c6560448af9306080ad1ca2045b1416bae3ea

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 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}/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

4 entries across 4 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.2 lib/dji_mqtt_connect/client.rb
dji_mqtt_connect-0.1.1.4 lib/dji_mqtt_connect/client.rb
dji_mqtt_connect-0.1.1.3 lib/dji_mqtt_connect/client.rb
dji_mqtt_connect-0.1.1.2 lib/dji_mqtt_connect/client.rb