コード例 #1
0
    def test_update_iocs_status(self):
        running = self.wrapper.update_iocs_status()
        self.assertEqual(len(running), 0)

        # Start some IOCs up
        ps = MockProcServWrapper()
        ps.start_ioc(self.prefix, "SIMPLE1")
        ps.start_ioc(self.prefix, "SIMPLE2")
        self.wrapper = IOCData(TEST_DB, ps, self.prefix)

        running = self.wrapper.update_iocs_status()
        self.assertEqual(len(running), 2)
コード例 #2
0
    def __init__(self, ca_server, dbid, options_folder, test_mode=False):
        """Constructor.

        Args:
            ca_server (CAServer): The CA server used for generating PVs on the fly
            dbid (string): The id of the database that holds IOC information.
            options_folder (string): The location of the folder containing the config.xml file that holds IOC options
        """
        if test_mode:
            ps = MockProcServWrapper()
        else:
            super(DatabaseServer, self).__init__()
            ps = ProcServWrapper()
        self._ca_server = ca_server
        self._options_holder = OptionsHolder(options_folder, OptionsLoader())

        # Initialise database connection
        try:
            self._db = IOCData(dbid, ps, MACROS["$(MYPVPREFIX)"])
            print_and_log("Connected to database", "INFO", "DBSVR")
        except Exception as err:
            self._db = None
            print_and_log("Problem initialising DB connection: %s" % err, "MAJOR", "DBSVR")

        # Initialise experimental database connection
        try:
            self._ed = ExpData(MACROS["$(MYPVPREFIX)"])
            print_and_log("Connected to experimental details database", "INFO", "DBSVR")
        except Exception as err:
            self._ed = None
            print_and_log("Problem connecting to experimental details database: %s" % err, "MAJOR", "DBSVR")

        if self._db is not None and not test_mode:
            # Start a background thread for keeping track of running IOCs
            self.monitor_lock = RLock()
            monitor_thread = Thread(target=self.update_ioc_monitors, args=())
            monitor_thread.daemon = True  # Daemonise thread
            monitor_thread.start()
コード例 #3
0
class DatabaseServer(Driver):
    """The class for handling all the static PV access and monitors etc.
    """
    def __init__(self, ca_server, dbid, options_folder, test_mode=False):
        """Constructor.

        Args:
            ca_server (CAServer): The CA server used for generating PVs on the fly
            dbid (string): The id of the database that holds IOC information.
            options_folder (string): The location of the folder containing the config.xml file that holds IOC options
        """
        if test_mode:
            ps = MockProcServWrapper()
        else:
            super(DatabaseServer, self).__init__()
            ps = ProcServWrapper()
        self._ca_server = ca_server
        self._options_holder = OptionsHolder(options_folder, OptionsLoader())

        # Initialise database connection
        try:
            self._db = IOCData(dbid, ps, MACROS["$(MYPVPREFIX)"])
            print_and_log("Connected to database", "INFO", "DBSVR")
        except Exception as err:
            self._db = None
            print_and_log("Problem initialising DB connection: %s" % err, "MAJOR", "DBSVR")

        # Initialise experimental database connection
        try:
            self._ed = ExpData(MACROS["$(MYPVPREFIX)"])
            print_and_log("Connected to experimental details database", "INFO", "DBSVR")
        except Exception as err:
            self._ed = None
            print_and_log("Problem connecting to experimental details database: %s" % err, "MAJOR", "DBSVR")

        if self._db is not None and not test_mode:
            # Start a background thread for keeping track of running IOCs
            self.monitor_lock = RLock()
            monitor_thread = Thread(target=self.update_ioc_monitors, args=())
            monitor_thread.daemon = True  # Daemonise thread
            monitor_thread.start()

    def read(self, reason):
        """A method called by SimpleServer when a PV is read from the DatabaseServer over Channel Access.

        Args:
            reason (string): The PV that is being requested (without the PV prefix)

        Returns:
            string : A compressed and hexed JSON formatted string that gives the desired information based on reason.
        """
        if reason == 'SAMPLE_PARS':
            value = self.encode4return(self.get_sample_par_names())
        elif reason == 'BEAMLINE_PARS':
            value = self.encode4return(self.get_beamline_par_names())
        elif reason == 'USER_PARS':
            value = self.encode4return(self.get_user_par_names())
        elif reason == "IOCS_NOT_TO_STOP":
            value = self.encode4return(IOCS_NOT_TO_STOP)
        else:
            value = self.getParam(reason)
        return value

    def write(self, reason, value):
        """A method called by SimpleServer when a PV is written to the DatabaseServer over Channel Access.

        Args:
            reason (string): The PV that is being requested (without the PV prefix)
            value (string): The data being written to the 'reason' PV

        Returns:
            bool : True
        """
        status = True
        try:
            if reason == 'ED:RBNUMBER:SP':
                #print_and_log("Updating to use experiment ID: " + value, "INFO", "DBSVR")
                self._ed.updateExperimentID(value)
            elif reason == 'ED:USERNAME:SP':
                self._ed.updateUsername(dehex_and_decompress(value))
        except Exception as err:
            value = compress_and_hex(convert_to_json("Error: " + str(err)))
            print_and_log(str(err), "MAJOR")
        # store the values
        if status:
            self.setParam(reason, value)
        return status

    def update_ioc_monitors(self):
        """Updates all the PVs that hold information on the IOCS and their associated PVs
        """
        while True:
            if self._db is not None:
                self._db.update_iocs_status()
                self.setParam("IOCS", self.encode4return(self._get_iocs_info()))
                self.setParam("PVS:ALL", self.encode4return(self._get_interesting_pvs("")))
                self.setParam("PVS:ACTIVE", self.encode4return(self._get_active_pvs()))
                self.setParam("PVS:INTEREST:HIGH", self.encode4return(self._get_interesting_pvs("HIGH")))
                self.setParam("PVS:INTEREST:MEDIUM", self.encode4return(self._get_interesting_pvs("MEDIUM")))
                self.setParam("PVS:INTEREST:FACILITY", self.encode4return(self._get_interesting_pvs("FACILITY")))
                # Update them
                with self.monitor_lock:
                    self.updatePVs()
            sleep(1)

    def encode4return(self, data):
        """Converts data to JSON, compresses it and converts it to hex.

        Args:
            data (string): The data to encode

        Returns:
            string : The encoded data
        """
        return compress_and_hex(json.dumps(data).encode('ascii', 'replace'))

    def _get_iocs_info(self):
        iocs = self._db.get_iocs()
        options = self._options_holder.get_config_options()
        for iocname in iocs.keys():
            if iocname in options:
                iocs[iocname].update(options[iocname])
        return iocs

    def _get_interesting_pvs(self, level, ioc=None):
        if self._db is not None:
            return self._db.get_interesting_pvs(level, ioc)
        else:
            return list()

    def _get_active_pvs(self):
        if self._db is not None:
            return self._db.get_active_pvs()
        else:
            return list()

    def get_sample_par_names(self):
        """Returns the sample parameters from the database, replacing the MYPVPREFIX macro

        Returns:
            list : A list of sample parameter names, an empty list if the database does not exist
        """
        if self._db is not None:
            return [p.replace(MACROS["$(MYPVPREFIX)"], "") for p in self._db.get_sample_pars()]
        else:
            return list()

    def get_beamline_par_names(self):
        """Returns the beamline parameters from the database, replacing the MYPVPREFIX macro

        Returns:
            list : A list of beamline parameter names, an empty list if the database does not exist
        """
        if self._db is not None:
            return [p.replace(MACROS["$(MYPVPREFIX)"], "") for p in self._db.get_beamline_pars()]
        else:
            return list()

    def get_user_par_names(self):
        """Returns the user parameters from the database, replacing the MYPVPREFIX macro

        Returns:
            list : A list of user parameter names, an empty list if the database does not exist
        """
        if self._db is not None:
            return [p.replace(MACROS["$(MYPVPREFIX)"], "") for p in self._db.get_user_pars()]
        else:
            return list()
コード例 #4
0
 def setUp(self):
     self.prefix = ""
     self.wrapper = IOCData(TEST_DB, MockProcServWrapper(), self.prefix)
コード例 #5
0
class TestMySQLWrapperSequence(unittest.TestCase):
    def setUp(self):
        self.prefix = ""
        self.wrapper = IOCData(TEST_DB, MockProcServWrapper(), self.prefix)

    def test_get_iocs(self):
        iocs = self.wrapper.get_iocs()
        self.assertEqual(iocs["TESTIOC"]["running"], False)
        self.assertEqual(iocs["SIMPLE1"]["running"], False)
        self.assertEqual(iocs["SIMPLE2"]["running"], False)

    def test_update_iocs_status(self):
        running = self.wrapper.update_iocs_status()
        self.assertEqual(len(running), 0)

        # Start some IOCs up
        ps = MockProcServWrapper()
        ps.start_ioc(self.prefix, "SIMPLE1")
        ps.start_ioc(self.prefix, "SIMPLE2")
        self.wrapper = IOCData(TEST_DB, ps, self.prefix)

        running = self.wrapper.update_iocs_status()
        self.assertEqual(len(running), 2)

    def test_connecting_to_a_bad_db_fails(self):
        with self.assertRaises(Exception):
            IOCData("not_a_db", MockProcServWrapper(), self.prefix)

    def test_get_interesting_pvs_all(self):
        # Get all PVs
        pvs = self.wrapper.get_interesting_pvs()
        for pv in pvs:
            self.assertTrue(pv[0] in MEDIUM_PV_NAMES or pv[0] in HIGH_PV_NAMES or pv[0] in FACILITY_PV_NAMES)

    def test_get_interesting_pvs_high(self):
        # Get all PVs
        pvs = self.wrapper.get_interesting_pvs("HIGH")
        for pv in pvs:
            self.assertTrue(pv[0] in HIGH_PV_NAMES)

    def test_get_interesting_pvs_medium(self):
        # Get all PVs
        pvs = self.wrapper.get_interesting_pvs("MEDIUM")
        for pv in pvs:
            self.assertTrue(pv[0] in MEDIUM_PV_NAMES)

    def test_get_interesting_pvs_facility(self):
        # Get all PVs
        pvs = self.wrapper.get_interesting_pvs("FACILITY")
        for pv in pvs:
            self.assertTrue(pv[0] in FACILITY_PV_NAMES)

    def test_get_active_pvs_high(self):
        # Get all Active PVs
        pvs = self.wrapper.get_active_pvs()
        for pv in pvs:
            self.assertTrue("STOPDIOC" not in pv[0])

    def test_get_beamline_pars(self):
        pars = self.wrapper.get_beamline_pars()
        self.assertEqual(len(pars), len(BL_PVS))
        for n in BL_PVS:
            self.assertTrue(n in pars)

    def test_get_sample_pars(self):
        pars = self.wrapper.get_sample_pars()
        self.assertEqual(len(pars), len(SAMPLE_PVS))
        for n in SAMPLE_PVS:
            self.assertTrue(n in pars)