Exemplo n.º 1
0
def return_output_usage(dict_outputs, table_misc, outputs,
                        table_output_channels,
                        custom_options_values_output_channels):
    """Return output usage and cost."""
    date_now = datetime.date.today()
    time_now = datetime.datetime.now()
    past_month_seconds = 0

    if table_misc.output_usage_dayofmonth == datetime.datetime.today().day:
        past_month_seconds = (time_now - time_now.replace(
            hour=0, minute=0, second=0, microsecond=0)).total_seconds()
    elif table_misc.output_usage_dayofmonth > datetime.datetime.today().day:
        first_day = date_now.replace(day=1)
        last_month = first_day - datetime.timedelta(days=1)
        past_month = last_month.replace(day=table_misc.output_usage_dayofmonth)
        past_month_seconds = (date_now - past_month).total_seconds()
    elif table_misc.output_usage_dayofmonth < datetime.datetime.today().day:
        past_month = date_now.replace(day=table_misc.output_usage_dayofmonth)
        past_month_seconds = (date_now - past_month).total_seconds()

    output_stats = OrderedDict()

    # Calculate output on duration for different time periods
    # Use OrderedDict to ensure proper order when saved to csv file
    output_stats['total_duration'] = dict.fromkeys(
        ['1d', '1w', '1m', '1m_date', '1y'], 0)
    output_stats['total_kwh'] = dict.fromkeys(
        ['1d', '1w', '1m', '1m_date', '1y'], 0)
    output_stats['total_cost'] = dict.fromkeys(
        ['1d', '1w', '1m', '1m_date', '1y'], 0)

    for each_output in outputs:
        output_channels = table_output_channels.query.filter(
            table_output_channels.output_id == each_output.unique_id).all()
        for each_channel in output_channels:
            channel_options = custom_options_values_output_channels[
                each_output.unique_id][each_channel.channel]
            if ('types' in dict_outputs[each_output.output_type]
                ['channels_dict'][each_channel.channel]
                    and 'on_off' in dict_outputs[each_output.output_type]
                ['channels_dict'][each_channel.channel]['types']
                    and 'amps' in channel_options):
                past_1d_hours = output_sec_on(
                    each_output.unique_id,
                    86400,
                    output_channel=each_channel.channel) / 3600
                past_1w_hours = output_sec_on(
                    each_output.unique_id,
                    604800,
                    output_channel=each_channel.channel) / 3600
                past_1m_hours = output_sec_on(
                    each_output.unique_id,
                    2629743,
                    output_channel=each_channel.channel) / 3600
                past_1m_date_hours = output_sec_on(
                    each_output.unique_id,
                    int(past_month_seconds),
                    output_channel=each_channel.channel) / 3600
                past_1y_hours = output_sec_on(
                    each_output.unique_id,
                    31556926,
                    output_channel=each_channel.channel) / 3600

                past_1d_kwh = table_misc.output_usage_volts * channel_options[
                    'amps'] * past_1d_hours / 1000
                past_1w_kwh = table_misc.output_usage_volts * channel_options[
                    'amps'] * past_1w_hours / 1000
                past_1m_kwh = table_misc.output_usage_volts * channel_options[
                    'amps'] * past_1m_hours / 1000
                past_1m_date_kwh = table_misc.output_usage_volts * channel_options[
                    'amps'] * past_1m_date_hours / 1000
                past_1y_kwh = table_misc.output_usage_volts * channel_options[
                    'amps'] * past_1y_hours / 1000

                if each_output.unique_id not in output_stats:
                    output_stats[each_output.unique_id] = {}
                output_stats[each_output.unique_id][each_channel.unique_id] = {
                    '1d': {
                        'hours_on': past_1d_hours,
                        'kwh': past_1d_kwh,
                        'cost': table_misc.output_usage_cost * past_1d_kwh
                    },
                    '1w': {
                        'hours_on': past_1w_hours,
                        'kwh': past_1w_kwh,
                        'cost': table_misc.output_usage_cost * past_1w_kwh
                    },
                    '1m': {
                        'hours_on': past_1m_hours,
                        'kwh': past_1m_kwh,
                        'cost': table_misc.output_usage_cost * past_1m_kwh
                    },
                    '1m_date': {
                        'hours_on': past_1m_date_hours,
                        'kwh': past_1m_date_kwh,
                        'cost': table_misc.output_usage_cost * past_1m_date_kwh
                    },
                    '1y': {
                        'hours_on': past_1y_hours,
                        'kwh': past_1y_kwh,
                        'cost': table_misc.output_usage_cost * past_1y_kwh
                    }
                }

                output_stats['total_duration']['1d'] += past_1d_hours
                output_stats['total_duration']['1w'] += past_1w_hours
                output_stats['total_duration']['1m'] += past_1m_hours
                output_stats['total_duration']['1m_date'] += past_1m_date_hours
                output_stats['total_duration']['1y'] += past_1y_hours

                output_stats['total_kwh']['1d'] += past_1d_kwh
                output_stats['total_kwh']['1w'] += past_1w_kwh
                output_stats['total_kwh']['1m'] += past_1m_kwh
                output_stats['total_kwh']['1m_date'] += past_1m_date_kwh
                output_stats['total_kwh']['1y'] += past_1y_kwh

                output_stats['total_cost'][
                    '1d'] += table_misc.output_usage_cost * past_1d_kwh
                output_stats['total_cost'][
                    '1w'] += table_misc.output_usage_cost * past_1w_kwh
                output_stats['total_cost'][
                    '1m'] += table_misc.output_usage_cost * past_1m_kwh
                output_stats['total_cost'][
                    '1m_date'] += table_misc.output_usage_cost * past_1m_date_kwh
                output_stats['total_cost'][
                    '1y'] += table_misc.output_usage_cost * past_1y_kwh

    return output_stats
Exemplo n.º 2
0
def return_output_usage(table_misc, table_outputs):
    """ Return output usage and cost """
    dict_outputs = parse_output_information()
    date_now = datetime.date.today()
    time_now = datetime.datetime.now()
    past_month_seconds = 0

    if table_misc.output_usage_dayofmonth == datetime.datetime.today().day:
        past_month_seconds = (time_now - time_now.replace(
            hour=0, minute=0, second=0, microsecond=0)).total_seconds()
    elif table_misc.output_usage_dayofmonth > datetime.datetime.today().day:
        first_day = date_now.replace(day=1)
        last_month = first_day - datetime.timedelta(days=1)
        past_month = last_month.replace(day=table_misc.output_usage_dayofmonth)
        past_month_seconds = (date_now - past_month).total_seconds()
    elif table_misc.output_usage_dayofmonth < datetime.datetime.today().day:
        past_month = date_now.replace(day=table_misc.output_usage_dayofmonth)
        past_month_seconds = (date_now - past_month).total_seconds()

    output_stats = OrderedDict()

    # Calculate output on duration for different time periods
    # Use OrderedDict to ensure proper order when saved to csv file
    output_stats['total_duration'] = dict.fromkeys(
        ['1d', '1w', '1m', '1m_date', '1y'], 0)
    output_stats['total_kwh'] = dict.fromkeys(
        ['1d', '1w', '1m', '1m_date', '1y'], 0)
    output_stats['total_cost'] = dict.fromkeys(
        ['1d', '1w', '1m', '1m_date', '1y'], 0)

    for each_output in table_outputs:
        if ('output_types' in dict_outputs[each_output.output_type]
                and 'on_off'
                in dict_outputs[each_output.output_type]['output_types']
                and each_output.amps):
            past_1d_hours = output_sec_on(each_output.unique_id, 86400) / 3600
            past_1w_hours = output_sec_on(each_output.unique_id, 604800) / 3600
            past_1m_hours = output_sec_on(each_output.unique_id,
                                          2629743) / 3600
            past_1m_date_hours = output_sec_on(each_output.unique_id,
                                               int(past_month_seconds)) / 3600
            past_1y_hours = output_sec_on(each_output.unique_id,
                                          31556926) / 3600

            past_1d_kwh = table_misc.output_usage_volts * each_output.amps * past_1d_hours / 1000
            past_1w_kwh = table_misc.output_usage_volts * each_output.amps * past_1w_hours / 1000
            past_1m_kwh = table_misc.output_usage_volts * each_output.amps * past_1m_hours / 1000
            past_1m_date_kwh = table_misc.output_usage_volts * each_output.amps * past_1m_date_hours / 1000
            past_1y_kwh = table_misc.output_usage_volts * each_output.amps * past_1y_hours / 1000

            output_stats[each_output.unique_id] = {
                '1d': {
                    'hours_on': past_1d_hours,
                    'kwh': past_1d_kwh,
                    'cost': table_misc.output_usage_cost * past_1d_kwh
                },
                '1w': {
                    'hours_on': past_1w_hours,
                    'kwh': past_1w_kwh,
                    'cost': table_misc.output_usage_cost * past_1w_kwh
                },
                '1m': {
                    'hours_on': past_1m_hours,
                    'kwh': past_1m_kwh,
                    'cost': table_misc.output_usage_cost * past_1m_kwh
                },
                '1m_date': {
                    'hours_on': past_1m_date_hours,
                    'kwh': past_1m_date_kwh,
                    'cost': table_misc.output_usage_cost * past_1m_date_kwh
                },
                '1y': {
                    'hours_on': past_1y_hours,
                    'kwh': past_1y_kwh,
                    'cost': table_misc.output_usage_cost * past_1y_kwh
                }
            }

            output_stats['total_duration']['1d'] += past_1d_hours
            output_stats['total_duration']['1w'] += past_1w_hours
            output_stats['total_duration']['1m'] += past_1m_hours
            output_stats['total_duration']['1m_date'] += past_1m_date_hours
            output_stats['total_duration']['1y'] += past_1y_hours

            output_stats['total_kwh']['1d'] += past_1d_kwh
            output_stats['total_kwh']['1w'] += past_1w_kwh
            output_stats['total_kwh']['1m'] += past_1m_kwh
            output_stats['total_kwh']['1m_date'] += past_1m_date_kwh
            output_stats['total_kwh']['1y'] += past_1y_kwh

            output_stats['total_cost'][
                '1d'] += table_misc.output_usage_cost * past_1d_kwh
            output_stats['total_cost'][
                '1w'] += table_misc.output_usage_cost * past_1w_kwh
            output_stats['total_cost'][
                '1m'] += table_misc.output_usage_cost * past_1m_kwh
            output_stats['total_cost'][
                '1m_date'] += table_misc.output_usage_cost * past_1m_date_kwh
            output_stats['total_cost'][
                '1y'] += table_misc.output_usage_cost * past_1y_kwh

    return output_stats
Exemplo n.º 3
0
def return_output_usage(table_misc, table_outputs):
    """ Return output usage and cost """
    date_now = datetime.date.today()
    time_now = datetime.datetime.now()
    past_month_seconds = 0

    if table_misc.output_usage_dayofmonth == datetime.datetime.today().day:
        past_month_seconds = (time_now - time_now.replace(
            hour=0, minute=0, second=0, microsecond=0)).total_seconds()
    elif table_misc.output_usage_dayofmonth > datetime.datetime.today().day:
        first_day = date_now.replace(day=1)
        last_month = first_day - datetime.timedelta(days=1)
        past_month = last_month.replace(day=table_misc.output_usage_dayofmonth)
        past_month_seconds = (date_now - past_month).total_seconds()
    elif table_misc.output_usage_dayofmonth < datetime.datetime.today().day:
        past_month = date_now.replace(day=table_misc.output_usage_dayofmonth)
        past_month_seconds = (date_now - past_month).total_seconds()

    output_stats = OrderedDict()

    for each_output in table_outputs:
        if each_output.output_type != 'pwm':
            output_stats[each_output.unique_id] = None

    # Calculate output on duration for different time periods
    # Use OrderedDict to ensure proper order when saved to csv file
    output_stats['total_duration'] = dict.fromkeys(['1d', '1w', '1m', '1m_date', '1y'], 0)
    output_stats['total_kwh'] = dict.fromkeys(['1d', '1w', '1m', '1m_date', '1y'], 0)
    output_stats['total_cost'] = dict.fromkeys(['1d', '1w', '1m', '1m_date', '1y'], 0)

    for each_output in table_outputs:
        if each_output.output_type != 'pwm':
            past_1d_hours = output_sec_on(each_output.unique_id, 86400) / 3600
            past_1w_hours = output_sec_on(each_output.unique_id, 604800) / 3600
            past_1m_hours = output_sec_on(each_output.unique_id, 2629743) / 3600
            past_1m_date_hours = output_sec_on(each_output.unique_id, int(past_month_seconds)) / 3600
            past_1y_hours = output_sec_on(each_output.unique_id, 31556926) / 3600

            past_1d_kwh = table_misc.output_usage_volts * each_output.amps * past_1d_hours / 1000
            past_1w_kwh = table_misc.output_usage_volts * each_output.amps * past_1w_hours / 1000
            past_1m_kwh = table_misc.output_usage_volts * each_output.amps * past_1m_hours / 1000
            past_1m_date_kwh = table_misc.output_usage_volts * each_output.amps * past_1m_date_hours / 1000
            past_1y_kwh = table_misc.output_usage_volts * each_output.amps * past_1y_hours / 1000

            output_stats[each_output.unique_id] = {
                '1d': {
                    'hours_on': past_1d_hours,
                    'kwh': past_1d_kwh,
                    'cost': table_misc.output_usage_cost * past_1d_kwh
                },
                '1w': {
                    'hours_on': past_1w_hours,
                    'kwh': past_1w_kwh,
                    'cost': table_misc.output_usage_cost * past_1w_kwh
                },
                '1m': {
                    'hours_on': past_1m_hours,
                    'kwh': past_1m_kwh,
                    'cost': table_misc.output_usage_cost * past_1m_kwh
                },
                '1m_date': {
                    'hours_on': past_1m_date_hours,
                    'kwh': past_1m_date_kwh,
                    'cost': table_misc.output_usage_cost * past_1m_date_kwh
                },
                '1y': {
                    'hours_on': past_1y_hours,
                    'kwh': past_1y_kwh,
                    'cost': table_misc.output_usage_cost * past_1y_kwh
                }
            }

            output_stats['total_duration']['1d'] += past_1d_hours
            output_stats['total_duration']['1w'] += past_1w_hours
            output_stats['total_duration']['1m'] += past_1m_hours
            output_stats['total_duration']['1m_date'] += past_1m_date_hours
            output_stats['total_duration']['1y'] += past_1y_hours

            output_stats['total_kwh']['1d'] += past_1d_kwh
            output_stats['total_kwh']['1w'] += past_1w_kwh
            output_stats['total_kwh']['1m'] += past_1m_kwh
            output_stats['total_kwh']['1m_date'] += past_1m_date_kwh
            output_stats['total_kwh']['1y'] += past_1y_kwh

            output_stats['total_cost']['1d'] += table_misc.output_usage_cost * past_1d_kwh
            output_stats['total_cost']['1w'] += table_misc.output_usage_cost * past_1w_kwh
            output_stats['total_cost']['1m'] += table_misc.output_usage_cost * past_1m_kwh
            output_stats['total_cost']['1m_date'] += table_misc.output_usage_cost * past_1m_date_kwh
            output_stats['total_cost']['1y'] += table_misc.output_usage_cost * past_1y_kwh

    return output_stats