<commit_before>
"""
<NAME> <<EMAIL>
bike_power_center.py
Provides services for monitoring and managing bicycle electrical systems
"""

from __future__ import print_function
import sys
sys.path.append('/home/pi/lib/python')

import signal
import ina219
import power_meter as pm
import rgb
from threading import Event, Timer
from datetime import datetime

class PowerCenter():
    """
    Methods for monitoring power generating, consumption, and modifying on bicycle electronics to control
    power consumption.
    """

    CHARGING    = 0
    ON_BATTERY  = 1
    LOW_BATTERY = 2
    LOW_BATTERY_VOLTS = 3.2

    LOG_FILE = "/var/log/power_center.log"
    POWER_MONITOR_INTERVAL = 0.08
    RUNNING = 0
    SHUTDOWN = 1

    # timer intervals
    CHECK_BATTERY_INTERVAL = 1.0
    LOG_DATA_INTERVAL = 1.0


    def __init__(self):
        """
        Instantiates power center which for now just monitors battery and external power ina219 devices.
        """
        self._trigger = Event().clear()
        self._close_event = Event().clear()
        self._state = None
        self.battery_power = pm.PowerMeter(pm.INA219_Monitor(0.1, 0x40, 1, ina219.INA219_CALIB_32V_1A, 128),
                                           self._trigger)
        self.dyno_power = pm.PowerMeter(pm.INA219_Monitor(0.1, 0x41, 1, ina219.INA219_CALIB_32V_2A, 128),
                                        self._trigger)
        self.log_file = PowerCenter.LOG_FILE
        self.log_fh = None
        self.battery_status = None
        self.battery_status_led = rgb.RGB_led(21, 20, 16)
        self._battery_timer = Timer(PowerCenter.CHECK_BATTERY_INTERVAL, self._check_battery)
        self._log_timer = Timer(PowerCenter.LOG_DATA_INTERVAL, self._output_log)
        self._power_monitor_timer = Timer(PowerCenter.POWER_MONITOR_INTERVAL, self._power_monitor_sync)

        signal.signal(signal.SIGTERM, self._sig_handler)
        signal.signal(signal.SIGINT, self._sig_handler)
        signal.signal(signal.SIGUSR1, self._sig_handler)


    def run(self):
        """
        Starts things up and launches timers
        """
        self._state = PowerCenter.RUNNING
        self.battery_power.open()
        self.dyno_power.open()
        self._power_monitor_timer.start()
        self._battery_timer.start()
        if self.log_fh:
            self._log_timer.start()

    def wait(self):
        """
        Provides a an event calling process can wait on until termination of PowerCenter
        """
        self._close_event.wait()

    def close(self):
        """
        Shuts things down and cancels timers
        """
        self._status = PowerCenter.SHUTDOWN
        self._power_monitor_timer.cancel()
        self._log_timer.cancel()
        self._battery_timer.cancel()
        self.battery_power.close()
        self.dyno_power.close()
        self.battery_status.close()
        if self.log_fh:
            self._log_message('Shutting down Power Center')
            self.log_fh.flush()
            self.log_fh.close()

    def _power_monitor_sync(self):
        """
        Sets event controlling synchronization of power monitors
        """
        self._trigger.set()
        if self._state == PowerCenter.RUNNING:
            self._power_monitor_timer.start()

    def _check_battery(self):
        """
        Runs battery check and then relaunches timer
        """
        self._update_battery_status()
        if self._state == PowerCenter.RUNNING:
            self._battery_timer.start()

    def _update_battery_status(self):
        """
        Checks battery voltage and amperage updating LED indication if there is a change
        CHARGING := negative battery amperage
        ON_BATTERY := positive battery amperage
        LOW_BATTERY := battery voltage <= LOW_BATTERY_VOLTS
        """
        amperage = self.battery_power.amp
        voltage = self.battery_power.volt

        if voltage <= PowerCenter.LOW_BATTERY_VOLTS:
            if self.battery_status != PowerCenter.LOW_BATTERY:
                self.battery_status = PowerCenter.LOW_BATTERY
                self.battery_status_led.red()
        elif amperage > 0:
            if self.battery_status != PowerCenter.CHARGING:
                self.battery_status = PowerCenter.CHARGING
                self.battery_status_led.green()<commit_msg>add full charge battery status<commit_after>
"""
<NAME> <<EMAIL>
bike_power_center.py
Provides services for monitoring and managing bicycle electrical systems
"""

from __future__ import print_function
import sys
sys.path.append('/home/pi/lib/python')

import signal
import ina219
import power_meter as pm
import rgb
from threading import Event, Timer
from datetime import datetime

class PowerCenter():
    """
    Methods for monitoring power generating, consumption, and modifying on bicycle electronics to control
    power consumption.
    """

    CHARGING    = 0
    FULL_CHARGE = 1
    ON_BATTERY  = 2
    LOW_BATTERY = 3
    LOW_BATTERY_VOLTS = 3.2

    LOG_FILE = "/var/log/power_center.log"
    POWER_MONITOR_INTERVAL = 0.08
    RUNNING = 0
    SHUTDOWN = 1

    # timer intervals
    CHECK_BATTERY_INTERVAL = 1.0
    LOG_DATA_INTERVAL = 1.0


    def __init__(self):
        """
        Instantiates power center which for now just monitors battery and external power ina219 devices.
        """
        self._trigger = Event().clear()
        self._close_event = Event().clear()
        self._state = None
        self.battery_power = pm.PowerMeter(pm.INA219_Monitor(0.1, 0x40, 1, ina219.INA219_CALIB_32V_1A, 128),
                                           self._trigger)
        self.dyno_power = pm.PowerMeter(pm.INA219_Monitor(0.1, 0x41, 1, ina219.INA219_CALIB_32V_2A, 128),
                                        self._trigger)
        self.log_file = PowerCenter.LOG_FILE
        self.log_fh = None
        self.battery_status = None
        self.battery_status_led = rgb.RGB_led(21, 20, 16)
        self._battery_timer = Timer(PowerCenter.CHECK_BATTERY_INTERVAL, self._check_battery)
        self._log_timer = Timer(PowerCenter.LOG_DATA_INTERVAL, self._output_log)
        self._power_monitor_timer = Timer(PowerCenter.POWER_MONITOR_INTERVAL, self._power_monitor_sync)

        signal.signal(signal.SIGTERM, self._sig_handler)
        signal.signal(signal.SIGINT, self._sig_handler)
        signal.signal(signal.SIGUSR1, self._sig_handler)


    def run(self):
        """
        Starts things up and launches timers
        """
        self._state = PowerCenter.RUNNING
        self.battery_power.open()
        self.dyno_power.open()
        self._power_monitor_timer.start()
        self._battery_timer.start()
        if self.log_fh:
            self._log_timer.start()

    def wait(self):
        """
        Provides an event that a calling process can wait on until termination of PowerCenter
        """
        self._close_event.wait()

    def close(self):
        """
        Shuts things down and cancels timers
        """
        self._status = PowerCenter.SHUTDOWN
        self._power_monitor_timer.cancel()
        self._log_timer.cancel()
        self._battery_timer.cancel()
        self.battery_power.close()
        self.dyno_power.close()
        self.battery_status.close()
        if self.log_fh:
            self._log_message('Shutting down Power Center')
            self.log_fh.flush()
            self.log_fh.close()

    def _power_monitor_sync(self):
        """
        Sets event controlling synchronization of power monitors
        """
        self._trigger.set()
        if self._state == PowerCenter.RUNNING:
            self._power_monitor_timer.start()

    def _check_battery(self):
        """
        Runs battery check and then relaunches timer
        """
        self._update_battery_status()
        if self._state == PowerCenter.RUNNING:
            self._battery_timer.start()

    def _update_battery_status(self):
        """
        Checks battery voltage and amperage updating LED indication if there is a change
        CHARGING := negative battery amperage
        FULL_CHARGE := positive battery amperage with greater external amperage
        ON_BATTERY := positive battery amperage
        LOW_BATTERY := battery voltage <= LOW_BATTERY_VOLTS
        """
        amperage = self.battery_power.amp
        voltage = self.battery_power.volt
        ext_amperage = self.dyno_power.amp

        if voltage <= PowerCenter.LOW_BATTERY_VOLTS:
            if self.battery_status != PowerCenter.LOW_BATTERY:
                self.battery_status = PowerCenter.LOW_BATTERY
                self.battery_status_led.red()
        elif amperage < 0:
            if self.battery_status != PowerCenter.CHARGING:
                self.battery_status = PowerCenter.CHARGING
                self.battery_status_led.green(True)
        elif ext_amperage > amperage:
            if self.battery_status != PowerCenter.FULL_CHARGE:
                self.batter_status = PowerCenter.FULL_CHARGE
                self.battery_status_led.green()
{'fraction_non_alphanumeric': 0.08308341663889815, 'fraction_numerical': 0.015661446184605132, 'mean_word_length': 2.323735695828719, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3583169', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2657, 'n_words': 571}