# 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 attr_reader :mqtt_client end end