Beispiel #1
0
    def __init__(self, motor_index, item_id, name, protocol):
        self._motor_index = motor_index
        self._protocol = protocol

        ParlayStandardItem.__init__(self, item_id, name)

        # manually add command options
        dropdown_options = [("spin", "spin")]
        dropdown_sub_fields = [[
            self.create_field("speed",
                              INPUT_TYPES.NUMBER,
                              label="speed to spin (-9 to 9)",
                              required=True)
        ]]

        dropdown_options.append(("stop", "stop"))
        dropdown_sub_fields.append([])

        self.add_field("COMMAND",
                       INPUT_TYPES.DROPDOWN,
                       dropdown_options=dropdown_options,
                       dropdown_sub_fields=dropdown_sub_fields)
    def _get_item_discovery_info(self, subsystem):
        """
        The discovery protocol for the embedded core:

        GET SUBSYSTEMS IDS
        GET ITEM IDS
        GET ERROR CODE INFO
        GET ITEM INFORMATION

        Where ITEM INFORMATION is:
            GET ITEM NAME
            GET ITEM TYPE
            GET COMMAND IDS
            GET PROPERTY IDS

            For each command:
                GET COMMAND NAME
                GET COMMAND INPUT PARAM FORMAT
                GET COMMAND INPUT PARAM NAMES
                GET COMMAND OUTPUT PARAM DESCRIPTION

            For each property:
                GET PROPERTY NAME
                GET PROPERTY TYPE

        :param subsystem: The subsystem we will be getting the item IDs from
        :return:
        """

        # Subsystem ID is the high byte of the item ID
        subsystem_id = subsystem

        # If the item was already discovered attach nothing to
        # the Deferred object's return value.
        # Otherwise add the item ID to the set of already discovered
        # IDs because we are about to discover it!

        # if item_id in self._already_discovered:
        #     defer.returnValue({})
        # else:
        #     # add the item ID to the already_discovered set.
        #     self._already_discovered.add(item_id)

        discovery = {"Subsystem ID": subsystem_id}
        print "Running discovery on subsystem: ", subsystem_id

        # Convert subsystem IDs to ints so that we can send them
        # back down the serial line to retrieve their attached item
        # For each subsystem ID, fetch the items attached to it

        print "Fetching items from subsystem ID: ", subsystem_id

        REACTOR = subsystem_id << self.SUBSYSTEM_SHIFT
        response = yield self.send_command(REACTOR, "DIRECT")
        self._item_ids = [int(item_id) for item_id in response.data]  # TODO: Change to extend() to get all item IDs


        # Fetch error codes
        self._initialize_reactor_command_map(REACTOR)
        response = yield self.send_command(to=REACTOR, tx_type="DIRECT", command_id=GET_ERROR_CODES)
        self._error_codes = [int(error_code) for error_code in response.data]

        for error_code in self._error_codes:
            response = yield self.send_command(to=REACTOR, tx_type="DIRECT", command_id=GET_ERROR_STRING, params=["code"], data=[error_code])
            error_code_string = response.data[0]
            error_code_map[error_code] = error_code_string

        print "---> ITEM IDS FOUND: ", self._item_ids

        for item_id in self._item_ids:
            self.adapter.subscribe(self.add_message_to_queue, TO=item_id)
            PCOMSerial.initialize_command_maps(item_id)

        for item_id in self._item_ids:
            response = yield self.send_command(item_id, command_id=GET_ITEM_NAME, tx_type="DIRECT")
            item_name = str(response.data[0])

            parlay_item = ParlayStandardItem(item_id=item_id, name=item_name)

            response = yield self.send_command(item_id, command_id=GET_ITEM_TYPE, tx_type="DIRECT")

            item_type = int(response.data[0])

            response = yield self.send_command(item_id, command_id=GET_COMMAND_IDS, tx_type="DIRECT")

            command_ids = response.data

            command_dropdowns = []
            command_subfields = []

            parlay_item.add_field('COMMAND', INPUT_TYPES.DROPDOWN,
                                  dropdown_options=command_dropdowns,
                                  dropdown_sub_fields=command_subfields)

            discovered_command = defer.DeferredList([])

            for command_id in command_ids:
                # Loop through the command IDs and build the Parlay Item object
                # for each one

                command_name = self.get_command_name(item_id, command_id)
                command_input_format = self.get_command_input_param_format(item_id, command_id)
                command_input_param_names = self.get_command_input_param_names(item_id, command_id)
                command_output_desc = self.get_command_output_parameter_desc(item_id, command_id)

                discovered_command = defer.gatherResults([command_name, command_input_format, command_input_param_names, command_output_desc])
                discovered_command.addCallback(PCOMSerial.command_cb, item_id=item_id, command_id=command_id,
                                               command_subfields=command_subfields, command_dropdowns=command_dropdowns,
                                               parlay_item=parlay_item, hidden=(command_id in DISCOVERY_MESSAGES))

            yield discovered_command

            response = yield self.send_command(item_id, command_id=GET_PROPERTY_IDS, tx_type="DIRECT")
            property_ids = response.data

            discovered_property = defer.DeferredList([])

            for property_id in property_ids:
                property_name = self.get_property_name(item_id, property_id)
                property_type = self.get_property_type(item_id, property_id)

                discovered_property = defer.gatherResults([property_name, property_type])
                discovered_property.addCallback(PCOMSerial.property_cb, item_id=item_id, property_id=property_id,
                                                parlay_item=parlay_item)

            yield discovered_property

            if item_type != ITEM_TYPE_HIDDEN:
                self.items.append(parlay_item)

            print "Finished ITEM:", item_name

        print "Finished subsystem:", subsystem
        defer.returnValue(discovery)