Exemplo n.º 1
0
 def stop(self):
     logging.getLogger("HWR").info("TangoDCMotor.stop")
     stopcmd = self.get_command_object("Stop")()
     if not stopcmd:
         stopcmd = TangoCommand("stopcmd", "Stop", self.tangoname)
     stopcmd()
    def add_command(self, arg1, arg2=None, addNow=True):
        if not addNow:
            self.__commandsToAdd.append((arg1, arg2))
            return
        newCommand = None

        if isinstance(arg1, dict):
            attributesDict = arg1
            cmd = arg2

            cmdName = attributesDict["name"]
            cmdType = attributesDict["type"]
            del attributesDict["name"]
            del attributesDict["type"]
        else:
            attributesDict = {}
            attributesDict.update(arg1.getProperties())

            try:
                cmdName = attributesDict["name"]
                cmdType = attributesDict["type"]
                cmd = attributesDict["toexecute"]
            except KeyError as err:
                logging.getLogger().error(
                    '%s: cannot add command: missing "%s" property',
                    self.name(),
                    err.args[0],
                )
                return
            else:
                del attributesDict["name"]
                del attributesDict["type"]
                del attributesDict["toexecute"]

        if cmdType.lower() == "spec":
            if "version" not in attributesDict:
                try:
                    attributesDict["version"] = self.specversion
                except AttributeError:
                    pass

            try:
                from HardwareRepository.Command.Spec import SpecCommand

                newCommand = SpecCommand(cmdName, cmd, **attributesDict)
            except Exception:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(),
                    cmdName,
                )
        elif cmdType.lower() == "taco":
            if "taconame" not in attributesDict:
                try:
                    attributesDict["taconame"] = self.taconame
                except AttributeError:
                    pass

            try:
                from HardwareRepository.Command.Taco import TacoCommand

                newCommand = TacoCommand(cmdName, cmd, **attributesDict)
            except Exception:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(),
                    cmdName,
                )
        elif cmdType.lower() == "tango":
            if "tangoname" not in attributesDict:
                try:
                    attributesDict["tangoname"] = self.tangoname
                except AttributeError:
                    pass
            try:
                from HardwareRepository.Command.Tango import TangoCommand

                newCommand = TangoCommand(cmdName, cmd, **attributesDict)
            except ConnectionError:
                logging.getLogger().error(
                    "%s: could not connect to device server %s (hint: is it running ?)",
                    self.name(),
                    attributesDict["tangoname"],
                )
                raise ConnectionError
            except Exception:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(),
                    cmdName,
                )

        elif cmdType.lower() == "exporter":
            if "exporter_address" not in attributesDict:
                try:
                    attributesDict["exporter_address"] = self.exporter_address
                except AttributeError:
                    pass
            host, port = attributesDict["exporter_address"].split(":")

            try:
                attributesDict["address"] = host
                attributesDict["port"] = int(port)
                del attributesDict["exporter_address"]

                from HardwareRepository.Command.Exporter import ExporterCommand

                newCommand = ExporterCommand(cmdName, cmd, **attributesDict)
            except Exception:
                logging.getLogger().exception(
                    "%s: cannot add command %s (hint: check attributes)",
                    self.name(),
                    cmdName,
                )
        elif cmdType.lower() == "epics":
            try:
                from HardwareRepository.Command.Epics import EpicsCommand

                newCommand = EpicsCommand(cmdName, cmd, **attributesDict)
            except Exception:
                logging.getLogger().exception(
                    "%s: cannot add EPICS channel %s (hint: check PV name)",
                    self.name(),
                    cmdName,
                )

        elif cmdType.lower() == "sardana":

            doorname = None
            taurusname = None
            cmdtype = None
            door_first = False
            tango_first = False

            if "doorname" not in attributesDict:
                try:
                    attributesDict["doorname"] = self.doorname
                    doorname = self.doorname
                except AttributeError:
                    pass
            else:
                door_first = True
                doorname = attributesDict["doorname"]

            if "taurusname" not in attributesDict:
                try:
                    attributesDict["taurusname"] = self.taurusname
                    taurusname = self.taurusname
                except AttributeError:
                    pass
            else:
                tango_first = True
                taurusname = attributesDict["taurusname"]

            if "cmdtype" in attributesDict:
                cmdtype = attributesDict["cmdtype"]

            # guess what kind of command to create
            if cmdtype is None:
                if taurusname is not None and doorname is None:
                    cmdtype = "command"
                elif doorname is not None and taurusname is None:
                    cmdtype = "macro"
                elif doorname is not None and taurusname is not None:
                    if door_first:
                        cmdtype = "macro"
                    elif tango_first:
                        cmdtype = "command"
                    else:
                        cmdtype = "macro"
                else:
                    logging.getLogger().error(
                        "%s: incomplete sardana command declaration. ignored",
                        self.name(),
                    )

            from HardwareRepository.Command.Sardana import SardanaCommand, SardanaMacro

            if cmdtype == "macro" and doorname is not None:
                try:
                    newCommand = SardanaMacro(cmdName, cmd, **attributesDict)
                except ConnectionError:
                    logging.getLogger().error(
                        "%s: could not connect to sardana door %s (hint: is it running ?)",
                        self.name(),
                        attributesDict["doorname"],
                    )
                    raise ConnectionError
                except Exception:
                    logging.getLogger().exception(
                        '%s: could not add command "%s" (hint: check command attributes)',
                        self.name(),
                        cmdName,
                    )
            elif cmdtype == "command" and taurusname is not None:
                try:
                    newCommand = SardanaCommand(cmdName, cmd, **attributesDict)
                except ConnectionError:
                    logging.getLogger().error(
                        "%s: could not connect to sardana device %s (hint: is it running ?)",
                        self.name(),
                        taurusname,
                    )
                    raise ConnectionError
                except Exception:
                    logging.getLogger().exception(
                        '%s: could not add command "%s" (hint: check command attributes)',
                        self.name(),
                        cmdName,
                    )
            else:
                logging.getLogger().error(
                    "%s: incomplete sardana command declaration. ignored",
                    self.name())

        elif cmdType.lower() == "pool":
            if "tangoname" not in attributesDict:
                try:
                    attributesDict["tangoname"] = self.tangoname
                except AttributeError:
                    pass
            try:
                from HardwareRepository.Command.Pool import PoolCommand

                newCommand = PoolCommand(cmdName, cmd, **attributesDict)
            except ConnectionError:
                logging.getLogger().error(
                    "%s: could not connect to device server %s (hint: is it running ?)",
                    self.name(),
                    attributesDict["tangoname"],
                )
                raise ConnectionError
            except Exception:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(),
                    cmdName,
                )
        elif cmdType.lower() == "tine":
            if "tinename" not in attributesDict:
                try:
                    attributesDict["tinename"] = self.tinename
                except AttributeError:
                    pass

            try:
                from HardwareRepository.Command.Tine import TineCommand

                newCommand = TineCommand(cmdName, cmd, **attributesDict)
            except Exception:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(),
                    cmdName,
                )

        elif cmdType.lower() == "mockup":
            try:
                from HardwareRepository.Command.Mockup import MockupCommand

                newCommand = MockupCommand(cmdName, cmd, **attributesDict)
            except Exception:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(),
                    cmdName,
                )

        if newCommand is not None:
            self.__commands[cmdName] = newCommand

            if not isinstance(arg1, dict):
                i = 1
                for arg in arg1.getObjects("argument"):
                    onchange = arg.getProperty("onchange")
                    if onchange is not None:
                        onchange = (onchange, weakref.ref(self))
                    valuefrom = arg.getProperty("valuefrom")
                    if valuefrom is not None:
                        valuefrom = (valuefrom, weakref.ref(self))

                    try:
                        comboitems = arg["type"]["item"]
                    except IndexError:
                        try:
                            newCommand.addArgument(
                                arg.getProperty("name"),
                                arg.type,
                                onchange=onchange,
                                valuefrom=valuefrom,
                            )
                        except AttributeError:
                            logging.getLogger().error(
                                '%s, command "%s": could not add argument %d, missing type or name',
                                self.name(),
                                cmdName,
                                i,
                            )
                            continue
                    else:
                        if isinstance(comboitems, list):
                            combo_items = []
                            for item in comboitems:
                                name = item.getProperty("name")
                                value = item.getProperty("value")
                                if name is None or value is None:
                                    logging.getLogger().error(
                                        "%s, command '%s': could not add argument %d, missing combo item name or value",
                                        self.name(),
                                        cmdName,
                                        i,
                                    )
                                    continue
                                else:
                                    combo_items.append((name, value))
                        else:
                            name = comboitems.getProperty("name")
                            value = comboitems.getProperty("value")
                            if name is None or value is None:
                                combo_items = ((name, value), )
                            else:
                                logging.getLogger().error(
                                    "%s, command '%s': could not add argument %d, missing combo item name or value",
                                    self.name(),
                                    cmdName,
                                    i,
                                )
                                continue

                        newCommand.addArgument(
                            arg.getProperty("name"),
                            "combo",
                            combo_items,
                            onchange,
                            valuefrom,
                        )

                    i += 1

            return newCommand