# 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:, client_name: DjiMqttConnect.client_name, &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: MQTT::Client.generate_client_id(client_name), 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}/events_reply def thing_product_events_reply_topic Thing::Product::EventsReplyTopicRepository.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