Beispiel #1
0
def enerpi_daemon_logger(with_pitemps=False):
    """
    Punto de entrada directa a ENERPI Logger con la configuración de DATA_PATH/config_enerpi.ini.

    Se utiliza para iniciar ENERPI como daemon mediante 'enerpi-daemon start|stop|restart'
    (en conjunción con enerpiweb, con user www-data: 'sudo -u www-data %(path_env_bin)/enerpi-daemon start')

    :param with_pitemps: :bool: Logs RPI temperature every 3 seconds

    """
    sleep(2)
    set_logging_conf(FILE_LOGGING, LOGGING_LEVEL, with_initial_log=False)
    timer_temps = show_pi_temperature(with_pitemps, 3)
    enerpi_logger(path_st=HDF_STORE_PATH, is_demo=False, timeout=None, verbose=False,
                  delta_sampling=SENSORS.delta_sec_data, roll_time=SENSORS.rms_roll_window_sec,
                  sampling_ms=SENSORS.ts_data_ms)
    if timer_temps is not None:
        log('Stopping RPI TEMPS sensing desde enerpi_main_logger...', 'debug', False, True)
        timer_temps.cancel()
Beispiel #2
0
def enerpi_daemon(test_mode=False):
    """
    Main logic para el demonio de ejecución de enerpi_daemon_logger

    Ej de uso:
    sudo -u www-data /home/pi/PYTHON/py35/bin/enerpi-daemon start|stop|status|restart
    enerpi-daemon start|stop|status|restart
    Opciones:
        - start
        - stop
        - status
        - restart
    """
    set_logging_conf(FILE_LOGGING, LOGGING_LEVEL, with_initial_log=False)
    daemon = EnerPiDaemon(DAEMON_PIDFILE, test_mode=test_mode, stdout=DAEMON_STDOUT, stderr=DAEMON_STDERR)
    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            try:
                daemon.start()
                log('ENERPI Logger daemon started', 'ok')
            except PermissionError as e:
                sys.stderr.write("PERMISSIONERROR: pidfile can't be registered ({}). Need sudo powers?".format(e))
        elif 'stop' == sys.argv[1]:
            stopped = daemon.stop()
            log('ENERPI Logger daemon stopped:{}'
                .format(stopped), 'warn', True, True)
        elif 'status' == sys.argv[1]:
            log('ENERPI Logger daemon status?', 'debug', True, True)
            daemon.status()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
            log('ENERPI Logger daemon restarting', 'info', True, True)
        else:
            log("Unknown command", 'warn', True, True)
            if not test_mode:
                sys.exit(2)
        if not test_mode:
            sys.exit(0)
    else:
        print("usage: %s start|stop|restart|status".format(sys.argv[0]))
        if not test_mode:
            sys.exit(2)
Beispiel #3
0
def enerpi_daemon_logger(with_pitemps=False):
    """
    Punto de entrada directa a ENERPI Logger con la configuración de DATA_PATH/config_enerpi.ini.

    Se utiliza para iniciar ENERPI como daemon mediante 'enerpi-daemon start|stop|restart'
    (en conjunción con enerpiweb, con user www-data: 'sudo -u www-data %(path_env_bin)/enerpi-daemon start')

    :param with_pitemps: :bool: Logs RPI temperature every 3 seconds

    """
    sleep(2)
    set_logging_conf(FILE_LOGGING, LOGGING_LEVEL, with_initial_log=False)
    timer_temps = show_pi_temperature(with_pitemps, 3)
    enerpi_logger(path_st=HDF_STORE_PATH,
                  is_demo=False,
                  timeout=None,
                  verbose=False,
                  delta_sampling=SENSORS.delta_sec_data,
                  roll_time=SENSORS.rms_roll_window_sec,
                  sampling_ms=SENSORS.ts_data_ms)
    if timer_temps is not None:
        log('Stopping RPI TEMPS sensing desde enerpi_main_logger...', 'debug',
            False, True)
        timer_temps.cancel()
Beispiel #4
0
# -*- coding: utf-8 -*-
"""
ENERPIWEB __main__
    - Install configuration & CRON task for web resources generation
    - Uninstall CRON task
    - Run Flask server

"""
import os
# noinspection PyUnresolvedReferences
from enerpiweb import app as application
from enerpiweb import SERVER_FILE_LOGGING, LOGGING_LEVEL_SERVER
from enerpiweb.command_enerpiweb import main, check_resource_files
from enerpi.base import set_logging_conf


# logging conf
check_resource_files(os.path.dirname(SERVER_FILE_LOGGING), verbose=True)
set_logging_conf(SERVER_FILE_LOGGING, level=LOGGING_LEVEL_SERVER,
                 verbose=True, with_initial_log=True)


if __name__ == '__main__':
    # Para ejecutar 'a mano'
    main()
Beispiel #5
0
# -*- coding: utf-8 -*-
"""
ENERPIWEB __main__
    - Install configuration & CRON task for web resources generation
    - Uninstall CRON task
    - Run Flask server

"""
import os
# noinspection PyUnresolvedReferences
from enerpiweb import app as application
from enerpiweb import SERVER_FILE_LOGGING, LOGGING_LEVEL_SERVER
from enerpiweb.command_enerpiweb import main, check_resource_files
from enerpi.base import set_logging_conf

# logging conf
check_resource_files(os.path.dirname(SERVER_FILE_LOGGING), verbose=True)
set_logging_conf(SERVER_FILE_LOGGING,
                 level=LOGGING_LEVEL_SERVER,
                 verbose=True,
                 with_initial_log=True)

if __name__ == '__main__':
    # Para ejecutar 'a mano'
    main()
Beispiel #6
0
# noinspection PyPep8
from enerpi.base import (check_resource_files, log, set_logging_conf,
                         STATIC_PATH, SERVER_FILE_LOGGING_RSCGEN,
                         LOGGING_LEVEL, IMG_TILES_BASEPATH)
# noinspection PyPep8
from enerpiplot.enerplot import gen_svg_tiles
# noinspection PyPep8
from enerpi.database import init_catalog

VERBOSE = False
TILES_GENERATION_INTERVAL_LOOP = 180

# Establecemos logging
LOGGING_LEVEL_SERVER = LOGGING_LEVEL
set_logging_conf(SERVER_FILE_LOGGING_RSCGEN,
                 LOGGING_LEVEL_SERVER,
                 with_initial_log=True,
                 verbose=VERBOSE)

basedir_enerweb = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                               '..', 'enerpiweb')
check_resource_files(STATIC_PATH,
                     os.path.join(basedir_enerweb, 'static'),
                     verbose=VERBOSE)
check_resource_files(os.path.join(IMG_TILES_BASEPATH, 'any_image'),
                     verbose=VERBOSE)


###############################
# RESOURCE GENERATOR
###############################
def _rsc_generator(catalog, verbose=False):
def enerpi_main_cli(test_mode=False):
    """
    Uso de ENERPI desde CLI

    enerpi -h para mostrar las diferentes opciones

    """
    # CLI Arguments
    args = _enerpi_arguments()
    verbose = not args.silent

    if args.version:
        return __version__

    # CONTROL LOGIC
    # Shows RPI Temps
    timer_temps = show_pi_temperature(args.temps, 3, args.timeout)

    if args.install or args.uninstall:
        from enerpi.config.crontasks import set_command_on_reboot, clear_cron_commands
        # INSTALL / UNINSTALL CRON TASKS & KEY
        cmd_logger = make_cron_command_task_daemon()
        if args.install:
            # Logging configuration
            set_logging_conf(FILE_LOGGING, LOGGING_LEVEL, True)

            log('** Installing CRON task for start logger at reboot:\n"{}"'.format(cmd_logger), 'ok', True, False)
            set_command_on_reboot(cmd_logger, verbose=verbose)
            try:
                os.chmod(DATA_PATH, 0o777)
                [os.chmod(os.path.join(base, f), 0o777)
                 for base, dirs, files in os.walk(DATA_PATH) for f in files + dirs]
            except PermissionError:
                log("Can't set 777 permissions on {0}/* files...\nDo it manually, please: 'sudo chmod 777 -R {0}'"
                    .format(DATA_PATH), 'warning', True, False)
        else:
            log('** Deleting CRON task for start logger at reboot:\n"{}"'.format(cmd_logger), 'warn', True, False)
            clear_cron_commands([cmd_logger], verbose=verbose)
    elif (args.enerpi or args.info or args.backup or args.reprocess or args.config or args.raw or
            args.last or args.clearlog or args.filter or args.plot or args.plot_tiles):
        # Init CLI
        # import matplotlib
        # matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        import pandas as pd
        pd.set_option('display.width', 200)

        # Logging configuration
        set_logging_conf(FILE_LOGGING, LOGGING_LEVEL, True)

        # Shows INI config & SENSORS
        if args.config:
            import json

            log('ENERPI Configuration (from INI file in "{}"):'
                .format(os.path.join(DATA_PATH, CONFIG_FILENAME)), 'ok', True, False)
            for s in CONFIG.sections():
                log('* Section {}:'.format(s), 'info', True, False)
                for opt in CONFIG.options(s):
                    log('{:27} -->\t{}'.format(opt.upper(), CONFIG.get(s, opt)), 'debug', True, False)
            log('*' * 80 + '\n', 'ok', True, False)
            log('\nENERPI SENSORS Config (from JSON file in "{}"):'
                .format(os.path.join(DATA_PATH, SENSORS_CONFIG_JSON_FILENAME)), 'ok', True, False)
            json_content = json.loads(open(os.path.join(DATA_PATH, SENSORS_CONFIG_JSON_FILENAME), 'r').read())
            log('\n'.join(['{}'.format(s) for s in json_content]), 'magenta', True, False)
            log('--> {}\n\n'.format(SENSORS), 'ok', True, False)
        # Delete LOG File
        if args.clearlog:
            from enerpi.database import delete_log_file

            delete_log_file(FILE_LOGGING, verbose=verbose)
        # Data Store Config
        _existe_st, path_st = _check_store_relpath(args.store)

        # Starts ENERPI Logger
        if args.enerpi:
            from enerpi.enerpimeter import enerpi_logger

            # Demo logger
            if args.demo:
                set_logging_conf(FILE_LOGGING + '_demo.log', LOGGING_LEVEL, True)
                path_st = os.path.join(DATA_PATH, 'debug_buffer_disk.h5')
            enerpi_logger(is_demo=args.demo, verbose=verbose, path_st=path_st, delta_sampling=args.delta,
                          roll_time=args.window, sampling_ms=args.ts, timeout=args.timeout)
        elif args.backup:
            from enerpi.database import init_catalog
            # Export data to CSV:
            catalog = init_catalog(sensors=SENSORS, raw_file=path_st, check_integrity=False,
                                   verbose=verbose, test_mode=test_mode)
            export_ok = catalog.export_chunk(args.backup)
            log('EXPORT OK? {}'.format(export_ok), 'ok' if export_ok else 'error', True, False)
        elif args.reprocess:
            from enerpi.database import init_catalog
            # Re-process all data in catalog
            catalog = init_catalog(sensors=SENSORS, raw_file=path_st, check_integrity=False,
                                   verbose=verbose, test_mode=test_mode)
            repro_ok = catalog.reprocess_all_data()
            log('REPROCESS OK? {}'.format(repro_ok), 'ok' if repro_ok else 'error', verbose, verbose)
        # TODO revisar config X11 + ssh -X para plot en display local
        elif args.raw:
            from enerpi.enerpimeter import enerpi_raw_data

            # Raw mode
            delta_secs = args.raw
            raw_data = enerpi_raw_data(path_st.replace('.h5', '_raw_sample.h5'), delta_secs=delta_secs,
                                       use_dummy_sensors=args.demo,
                                       roll_time=args.window, sampling_ms=args.ts, verbose=verbose)
            t0, tf = raw_data.index[0], raw_data.index[-1]
            log('Showing RAW DATA for {} seconds ({} samples, {:.2f} sps)\n** Real data: from {} to {} --> {:.2f} sps'
                .format(delta_secs, len(raw_data), len(raw_data) / delta_secs,
                        t0, tf, len(raw_data) / (tf-t0).total_seconds()), 'info', verbose, False)
            raw_data.plot(lw=.5, figsize=(16, 10))
            plt.show()
        # Shows database info
        elif args.info or args.filter or args.plot or args.plot_tiles:
            from enerpi.database import init_catalog, show_info_data

            catalog = init_catalog(sensors=SENSORS, raw_file=path_st, check_integrity=False,
                                   verbose=verbose, test_mode=test_mode)
            if args.plot_tiles:
                from enerpiplot.enerplot import gen_svg_tiles

                ok = gen_svg_tiles(IMG_TILES_BASEPATH, catalog, color=COLOR_TILES if not test_mode else (1, 0, 0))
                if ok:
                    log('SVG Tiles generated!', 'ok', verbose, True)
                else:
                    log('No generation of SVG Tiles!', 'error', verbose, True)
            else:
                data, consumption = _extract_time_slice_from_args(args.filter, args.info, catalog)

                if (args.info or args.filter) and data is not None and not data.empty:
                    show_info_data(data, consumption)
                if (args.plot and (data is not None) and not data.empty and (consumption is not None) and
                        not consumption.empty):
                    from enerpiplot.enerplot import plot_power_consumption_hourly

                    rs_data, rm_data, path_saveimg, show = _extract_plot_params_from_args(args.plot, args.plot_options)
                    mean_sensor_data = data[SENSORS.columns_sensors_mean] if SENSORS.columns_sensors_mean else None
                    log('Generate PLOT with RESAMPLE={}, ROLLING={}, show={}, path_save="{}"'
                        .format(rs_data, rm_data, show, path_saveimg), 'info', verbose, False)
                    img_name = plot_power_consumption_hourly(data[SENSORS.columns_sensors_rms],
                                                             consumption.kWh, data_mean_s=mean_sensor_data,
                                                             rs_data=rs_data, rm_data=rm_data,
                                                             show=show, path_saveimg=path_saveimg)
                    if img_name is not None:
                        log('IMAGE saved in "{}"'.format(img_name), 'ok', verbose, False)
        # Shows database info
        else:  # Shows last 10 entries
            from enerpi.database import get_ts_last_save

            last = get_ts_last_save(path_st, get_last_sample=True, verbose=verbose, n=10)
            log('Showing last 10 entries in {}:\n{}'.format(path_st, last), 'info', verbose, False)
    # Shows & extract info from LOG File
    elif args.log:
        from enerpi.database import extract_log_file

        data_log = extract_log_file(FILE_LOGGING, extract_temps=True, verbose=verbose)
        try:
            df_temps = data_log[data_log.temp.notnull()].drop(['tipo', 'msg', 'debug_send'], axis=1).dropna(axis=1)
            if len(set(df_temps['exec'])) == 1:
                df_temps = df_temps.drop(['exec'], axis=1)
            path_csv = os.path.join(DATA_PATH, 'debug_rpitemps.csv')
            if not df_temps.empty:
                df_temps.to_csv(path_csv)
                print('*** Saved temperature data extracted from LOG in {}'.format(path_csv))
        except AttributeError:
            print('No se encuentran datos de Tª de RPI en el LOG')
    # Demo sender
    elif args.demo:
        from enerpi.enerpimeter import enerpi_logger

        set_logging_conf(FILE_LOGGING + '_demo.log', LOGGING_LEVEL, True)
        path_st = os.path.join(DATA_PATH, 'debug_buffer_disk.h5')
        enerpi_logger(is_demo=True, verbose=verbose, path_st=path_st, delta_sampling=args.delta,
                      roll_time=args.window, sampling_ms=args.ts, timeout=args.timeout)
    # Receiver
    else:  # elif args.receive:
        from enerpi.enerpimeter import receiver

        log('{}\n   {}'.format(PRETTY_NAME, DESCRIPTION), 'ok', verbose, False)
        receiver(verbose=verbose, timeout=args.timeout, port=args.port)
    if timer_temps is not None:
        log('Stopping RPI TEMPS sensing...', 'debug', True)
        timer_temps.cancel()
    log('Exiting from ENERPI CLI...', 'debug', True)
    if not test_mode:
        sys.exit(0)