예제 #1
0
def db_connect():
    try:
        database.connect(reuse_if_open=True)
        logger.info('Database connection successful.')
    except Exception as e:
        logger.error(e)
        log_exception(*sys.exc_info())
예제 #2
0
def main():
    # Setup the channel access address list in order to connect to PVs
    os.environ['EPICS_CA_ADDR_LIST'] = CA.EPICS_CA_ADDR_LIST

    # External PVs
    external_pvs_configs = [MercuryPVs()]
    external_pvs_list = [y for x in external_pvs_configs for y in x.get_full_pv_list()]

    # Initialize and establish the database connection
    initialize_database(name=HEDB.NAME, user=HEDB.USER, password=HEDB.PASS, host=HEDB.HOST)
    db_connect()
    check_db_connection()

    # Get the user configuration and the list of measurement PVs
    config = UserConfig()
    pv_list = config.get_measurement_pvs(no_duplicates=True, full_names=True)
    logger.info(f'He Recovery PLC PVs to monitor: {pv_list}')

    # Add external PVs to monitoring list
    logger.info(f'Non-PLC PVs to monitor: {external_pvs_list}')
    pv_list.extend(external_pvs_list)

    # Set up monitoring and fetching of the PV data
    pv_monitors = PvMonitors(pv_list)

    # Initialize and set-up the PV import in charge of preparing the PV data, handling logging periods & tasks,
    # running content checks for the user config, and looping through each record every few seconds to check for
    # records scheduled to be updated with a new measurement.
    this.pv_import = PvImport(pv_monitors, config, external_pvs_configs)

    # Start the monitors and continuously store the PV data received on every update
    pv_monitors.start_monitors()

    # Start the PV Import main loop
    this.pv_import.start()
예제 #3
0
    def _check_measurement_pvs_connect(self):
        """
        Checks whether the measurement PVs from the user configuration connect.

        Raises:
            ValueError: If one or more PVs were not found.
        """
        logger.info('PVConfig: Checking measurement PVs...')
        config_pvs = self.get_measurement_pvs(no_duplicates=True,
                                              full_names=True)
        connected_pvs = get_connected_pvs(config_pvs)
        not_connected = set(config_pvs) ^ set(connected_pvs)

        if not_connected:
            logger.warning(
                f'PVConfig: Could not connect to one or more PVs: {not_connected}'
            )
        else:
            logger.info('PVConfig: All PVs connected.')
예제 #4
0
def add_measurement(object_id, mea_values: dict):
    """
    Adds a measurement to the database.
    The measurement will be added to the module if the object has one.
    Otherwise, the measurement will be added to the object itself.

    Args:
        object_id (int): Record/Object id of the object the measurement is for.
        mea_values (dict): A dict of the measurement values, max 5, in measurement_number(str)/pv_value pairs.
    """
    obj = GamObject.get(GamObject.ob_id == object_id)
    obj_class_id = obj.ob_objecttype.ot_objectclass.oc_id

    object_module = get_object_module(object_id=obj.ob_id,
                                      object_class=obj_class_id)
    if object_module is not None:
        object_id = object_module.ob_id
    mea_comment = _generate_mea_comment(obj, object_module)
    mea_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    mea_values = _calculate_mea_values(object_id, obj_class_id, mea_values)

    record_id = GamMeasurement.insert(
        mea_object=object_id,
        mea_date=mea_date,
        mea_date2=mea_date,
        mea_comment=mea_comment,
        mea_value1=mea_values['1'],
        mea_value2=mea_values['2'],
        mea_value3=mea_values['3'],
        mea_value4=mea_values['4'],
        mea_value5=mea_values['5'],
        mea_valid=1,
        mea_bookingcode=
        0  # 0 = measurement is not from the balance program (HZB)
    ).execute()

    logger.info(
        f'Added measurement {record_id} for {obj.ob_name} ({object_id}) with values: {dict(mea_values)}'
    )
    # noinspection PyProtectedMember
    db_logger.info(
        f"Added record no. {record_id} to {GamMeasurement._meta.table_name}")
예제 #5
0
 def stop():
     logger.info("Stop request received")
     main_.pv_import.stop()
예제 #6
0
 def main():
     logger.info("Starting service")
     main_.main()