# frozen_string_literal: true module DjiMqttConnect module Thing::Product class StateTopicRepository < TopicRepository STATE_TOPIC_REGEX = /\Athing\/product\/(?.+)\/state\z/ def listen! listen_to_topic("thing/product/+/state") do |topic, raw_message| logger.debug(raw_message) matched_topic = STATE_TOPIC_REGEX.match(topic) raise Error, "Unknown topic: #{topic}" unless matched_topic gateway_sn = matched_topic[:gateway_sn] message = state_marshal.load(raw_message) logger.info("Received #{message} from #{gateway_sn}") # Broadcast the state update broadcast(:state_update, gateway_sn, message) rescue ParseError => error broadcast(:parse_error, error, topic, raw_message) end end private def state_marshal @state_marshal ||= StateMarshal.new end end end end