# frozen_string_literal: true module DjiMqttConnect module Thing::Product class OsdTopicRepository < TopicRepository OSD_TOPIC_REGEX = /\Athing\/product\/(?.+)\/osd\z/ def listen! listen_to_topic("thing/product/+/osd") do |topic, raw_message| logger.debug(raw_message) matched_topic = OSD_TOPIC_REGEX.match(topic) raise Error, "Unknown topic: #{topic}" unless matched_topic device_identifier = matched_topic[:device_identifier] message = OsdMarshal.load(raw_message) logger.info("Received #{message} from #{device_identifier}") if message.instance_of?(OsdMessage) # Broadcast an unsupported message event broadcast(:unsupported_message, topic, raw_message) else # Build event name and broadcast (i.e. ::RemoteOsdMessage => remote_osd_update) event_name = message.class.name.demodulize.sub(/Message\z/, "Update").underscore.to_sym broadcast(event_name, device_identifier, message) end rescue ParseError => error broadcast(:parse_error, error, topic, raw_message) end end end end end