# frozen_string_literal: true module DjiMqttConnect module Sys::Product class StatusTopicRepository < TopicRepository STATUS_TOPIC_REGEX = /\Asys\/product\/(?.+)\/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}") if message.instance_of?(StatusMessage) # Broadcast an unsupported message event broadcast(:unsupported_message, topic, raw_message) else # Build event name and broadcast (i.e. ::UpdateTopoStatusMessage => update_topo) broadcast(message._method.to_sym, device_identifier, message) end rescue ParseError => error broadcast(:parse_error, error, topic, raw_message) end end private def status_marshal @status_marshal ||= StatusMarshal.new end end end end