def __init__(self): self._log = LOGGER_FACTORY.get_logger('ipa.adc') self._spi = spidev.SpiDev() self._spi.open(0, 0) if len(SpiAdc._CHANNELS) != len(SpiAdc._VREFS): raise config.InvalidConfigException( 'config has %d channels, but %d vrefs' % (len(SpiAdc._CHANNELS), len(SpiAdc._VREFS))) self._log.info('initialized (channels: %s; vrefs: %s)' % (SpiAdc._CHANNELS, SpiAdc._VREFS))
def __init__(self): # We need WindStats.compute_kmh(). Timestamp isn't needed, so just use 0. self._stats = wind_stats.WindStats(C.WIND_HSF(), C.WIND_LSF(), C.WIND_MAX_ROTATION(), 0) self._v_max = 0 self._v_window = {} self._queue = Queue.Queue() self._worker = LoggerWorker(self._queue, LOGGER_FACTORY.get_logger('wind_calibrate')) self._worker.start()
def __init__(self): self._log = LOGGER_FACTORY.get_logger('weather.rain') self._ticks = rain_ticks.Ticks() # self._startup_time = common.timestamp() # TODO: Consider removing start timestamp and only use sample start/end timestamps. self._register_callback(self._ticks.add_edge) self._log.info('initialized') self._log.info( 'pin=%d debounce=%dms Wide=%g Long=%g Drops=%d' % (C.RAIN_INPUT_PIN(), C.RAIN_DEBOUNCE_MILLIS(), C.RAIN_SIZE_WIDE_MM(), C.RAIN_SIZE_LONG_MM(), C.RAIN_DROPS()))
def __init__(self): self._log = LOGGER_FACTORY.get_logger('weather.count') # Only one blinking thread at a time. self._blink_semaphore = threading.Semaphore() # Synchronize access in _read_callback() and get_sample(). self._lock = threading.Lock() self._count = 0 self._pilots = [] self._last_reset_yday = time.localtime(time.time()).tm_yday self._log.info(('initialized (plus: pin=%d trigger=%d debounce=%d; ' + 'minus: pin=%d trigger=%d debounce=%d)') % (PilotCount._PLUS_PIN, PilotCount._PLUS_TRIGGER_STATE, C.PILOTS_PLUS_DEBOUNCE_MILLIS(), PilotCount._MINUS_PIN, PilotCount._MINUS_TRIGGER_STATE, C.PILOTS_MINUS_DEBOUNCE_MILLIS())) # Read previous count if it's fresh. try: mtime = os.stat(_STATE_FILE).st_mtime if (time.time() - mtime) / 60.0 < _STATE_LIFETIME_MINUTES: with open(_STATE_FILE) as f: self._count = int(f.read()) with self._lock: # not really necessary at this point self._append_count_locked() self._log.info('read pilot count from file: %d @ %s' % (self._count, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(mtime)))) except Exception: self._log.warning('could not read state from file %s' % _STATE_FILE) GPIO.setup(PilotCount._PLUS_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN if PilotCount._PLUS_TRIGGER_STATE else GPIO.PUD_UP) GPIO.setup(PilotCount._MINUS_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN if PilotCount._MINUS_TRIGGER_STATE else GPIO.PUD_UP) GPIO.setup(PilotCount._LED_PIN, GPIO.OUT) GPIO.add_event_detect(PilotCount._PLUS_PIN, GPIO.BOTH, callback=self._read_callback, bouncetime=C.PILOTS_PLUS_DEBOUNCE_MILLIS()) GPIO.add_event_detect(PilotCount._MINUS_PIN, GPIO.BOTH, callback=self._read_callback, bouncetime=C.PILOTS_MINUS_DEBOUNCE_MILLIS())
def __init__(self): self._log = LOGGER_FACTORY.get_logger('ipa.main') self._log.info(K.CLIENT_GREETING) auth_info = ((' (authenticated as user "%s")' % ARGS.server_username()) if ARGS.server_username() else '') self._log.info('URL: %s%s' % (ARGS.server_url(), auth_info)) self._log.info('client version: %s' % C.CLIENT_VERSION()) if C.DEMO_MODE_ENABLED(): self._log.warn('DEMO MODE ENABLED') # Create main command queue. self._main_cq = Queue.Queue() # Create thread termination Event for Uploader. self._uploader_termination_event = threading.Event()
def __init__(self, command_queue, termination_event): """Commands received from the server will be put into the command_queue.""" threading.Thread.__init__(self) self._log = LOGGER_FACTORY.get_logger('ipa.upload') self._main_cq = command_queue self._termination_event = termination_event self._queue = {} # To judge the quality of the uplink, the client transmits the number of failed upload attempts # in the metadata. Only counts connection errors, not server errors. self._failed_uploads = 0 self._sources = [] # holds (data_source, buffering) # The uploader is itself a data source (number of failed attempts, and maybe more, some day). self.add_data_source(self, False)
def __init__(self): self._log = LOGGER_FACTORY.get_logger('weather.door') self._lock = threading.Lock() self._events = [] self._previous_door_open = None GPIO.setup(Door._PIN, GPIO.IN, pull_up_down=C.DOOR_PUD()) GPIO.add_event_detect(Door._PIN, GPIO.BOTH, callback=self._read_door_callback, bouncetime=C.DOOR_DEBOUNCE_MILLIS()) door_open = self._read_door_callback(0) self._log.info('initialized (pin=%d open=%d debounce=%d)' % (Door._PIN, door_open, C.DOOR_DEBOUNCE_MILLIS()))
def __init__(self): self.temperature = 0 self.pressure = 0 # Setup Raspberry PINS, as numbered on BOARD self.SCK = bmp183._SCK # GPIO for SCK, other name SCLK. ORIGINAL=14 self.SDO = bmp183._SDO # GPIO for SDO, other name MISO. ORIGINAL=15 self.SDI = bmp183._SDI # GPIO for SDI, other name MOSI. ORIGINAL=18 self.CS = bmp183._CS # GPIO for CS, other name CE. ORIGINAL=23 self._log = LOGGER_FACTORY.get_logger('weather.bmp183') # SCK frequency 1 MHz self.delay = 1 / 1000000.0 self.set_up_gpio() # Check comunication / read ID ret = self.read_byte(self.BMP183_REG['ID']) if ret != self.BMP183_CMD['ID_VALUE']: self._log.critical( 'BMP183 returned %d instead of 0x55. Communication failed, expect problems.' % (ret)) self.read_calibration_data() # Proceed with initial pressure/temperature measurement self.measure_pressure()
def __init__(self): self._log = LOGGER_FACTORY.get_logger('ipa.count') # Only one blinking thread at a time. self._blink_semaphore = threading.Semaphore() # Synchronize access in _read_callback() and get_sample(). self._lock = threading.Lock() self._count = 0 self._pilots = [] self._last_reset_yday = time.localtime(time.time()).tm_yday self._log.info(('initialized (plus: pin=%d trigger=%d debounce=%d; ' + 'minus: pin=%d trigger=%d debounce=%d)') % (PilotCount._PLUS_PIN, PilotCount._PLUS_TRIGGER_STATE, C.PILOTS_PLUS_DEBOUNCE_MILLIS(), PilotCount._MINUS_PIN, PilotCount._MINUS_TRIGGER_STATE, C.PILOTS_MINUS_DEBOUNCE_MILLIS())) # Read previous count if it's fresh. try: mtime = os.stat(_STATE_FILE).st_mtime if (time.time() - mtime) / 60.0 < _STATE_LIFETIME_MINUTES: with open(_STATE_FILE) as f: self._count = int(f.read()) with self._lock: # not really necessary at this point self._append_count_locked() self._log.info('read pilot count from file: %d @ %s' % (self._count, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(mtime)))) except Exception: self._log.warning('could not read state from file %s' % _STATE_FILE) GPIO.setup(PilotCount._PLUS_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN if PilotCount._PLUS_TRIGGER_STATE else GPIO.PUD_UP) GPIO.setup(PilotCount._MINUS_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN if PilotCount._MINUS_TRIGGER_STATE else GPIO.PUD_UP) GPIO.setup(PilotCount._LED_PIN, GPIO.OUT) GPIO.add_event_detect(PilotCount._PLUS_PIN, GPIO.BOTH, callback=self._read_callback, bouncetime=C.PILOTS_PLUS_DEBOUNCE_MILLIS()) GPIO.add_event_detect(PilotCount._MINUS_PIN, GPIO.BOTH, callback=self._read_callback, bouncetime=C.PILOTS_MINUS_DEBOUNCE_MILLIS())
def __init__(self, calibration_mode=False): self._log = LOGGER_FACTORY.get_logger('weather.wind') self._revolutions = wind_revolutions.Revolutions( C.WIND_EDGES_PER_REV()) self._startup_time = common.timestamp() # TODO: Consider removing start timestamp and only use sample start/end timestamps. self._calibration_mode = calibration_mode if calibration_mode: self._calibration_logger = calibration_logger.CalibrationLogger() self._revolutions.calibration_init(self._calibration_logger) self._register_callback( self._revolutions.calibration_add_edge_and_log) else: self._register_callback(self._revolutions.add_edge) self._stats = wind_stats.WindStats(C.WIND_HSF(), C.WIND_LSF(), C.WIND_MAX_ROTATION(), self._startup_time) self._log.info('initialized - CALIBRATION MODE' if calibration_mode else 'initialized') self._log.info('pin=%d edges=%d debounce=%dms LSF=%g HSF=%g max=%dms' % (C.WIND_INPUT_PIN(), C.WIND_EDGES_PER_REV(), C.WIND_DEBOUNCE_MILLIS(), C.WIND_LSF(), C.WIND_HSF(), C.WIND_MAX_ROTATION()))
# TODO: Make this a class. import common from config import C from logger import LOGGER_FACTORY MAX_TEMP = C.TEMPERATURE_SHUTDOWN_AT() LOG = LOGGER_FACTORY.get_logger('ipa.intcpt') def process(queue): """Process the queue, possibly modifying it. May return a command.""" command = None last_temp = _get_last_sample(queue, 'temp') if last_temp and last_temp[1] >= MAX_TEMP: LOG.info('shutting down since temperature of %s exceeds maximum of %s' % (last_temp[1], MAX_TEMP)) queue['status'] = (common.timestamp(), 'shutdown: CPU temp = %s (max: %s)' % (last_temp[1], MAX_TEMP)) command = ('exit', 100) # 100 = shutdown else: queue['status'] = (common.timestamp(), 'ok') return command def _get_last_sample(queue, type_key): samples = queue.get(type_key, []) return samples[-1] if samples else None
def __init__(self): self._log = LOGGER_FACTORY.get_logger('weather.pressure') self._log.info( 'CS=%d SCK=%d SDI=%d SDO=%d' % (Pressure._CS, Pressure._SCK, Pressure._SDI, Pressure._SDO))
# TODO: Make this a class. import common from config import C from logger import LOGGER_FACTORY MAX_TEMP = C.TEMPERATURE_SHUTDOWN_AT() LOG = LOGGER_FACTORY.get_logger('weather.intcpt') def process(queue): """Process the queue, possibly modifying it. May return a command.""" command = None last_temp = _get_last_sample(queue, 'temp') if last_temp and last_temp[1] >= MAX_TEMP: LOG.info( 'shutting down since temperature of %s exceeds maximum of %s' % (last_temp[1], MAX_TEMP)) queue['status'] = (common.timestamp(), 'shutdown: CPU temp = %s (max: %s)' % (last_temp[1], MAX_TEMP)) command = ('exit', 100) # 100 = shutdown else: queue['status'] = (common.timestamp(), 'ok') return command def _get_last_sample(queue, type_key): samples = queue.get(type_key, []) return samples[-1] if samples else None
def __init__(self): self._log = LOGGER_FACTORY.get_logger('ipa.link') self._log.info('initialized')
#!/usr/bin/env python import RPi.GPIO as GPIO #@UnresolvedImport import subprocess import traceback import common from config import C from logger import LOGGER_FACTORY from wind import Wind if __name__ == "__main__": log = LOGGER_FACTORY.get_logger('wind_calibrate') try: log.info('--- wind_calibrate started, waiting for ntpd ---') if subprocess.call('../await_clock_sync.sh'): log.critical('--- failed to sync clock ---') else: log.info('--- clock in sync ---') GPIO.setmode(GPIO.BCM) wind = Wind(calibration_mode=True) raw_input('Press ENTER to quit...\n' ) # just in case there's a kb and a screen GPIO.cleanup() wind.terminate_calibration() threads_left = common.join_all_threads(C.TIMEOUT_SHUTDOWN_SECONDS()) print '--- exiting - threads left: %d ---' % threads_left except Exception: log.critical(traceback.format_exc())
def setUp(self): LOGGER_FACTORY.initialize('DEBUG')
# TODO: Make this a class. import common from config import C from logger import LOGGER_FACTORY MAX_TEMP = C.TEMPERATURE_SHUTDOWN_AT() LOG = LOGGER_FACTORY.get_logger('ipa.intcpt') def process(queue): """Process the queue, possibly modifying it. May return a command.""" command = None last_temp = _get_last_sample(queue, 'temp') if last_temp and last_temp[1] >= MAX_TEMP: LOG.info( 'shutting down since temperature of %s exceeds maximum of %s' % (last_temp[1], MAX_TEMP)) queue['status'] = (common.timestamp(), 'shutdown: CPU temp = %s (max: %s)' % (last_temp[1], MAX_TEMP)) command = ('exit', 100) # 100 = shutdown else: queue['status'] = (common.timestamp(), 'ok') return command def _get_last_sample(queue, type_key): samples = queue.get(type_key, []) return samples[-1] if samples else None
def __init__(self): self._log = LOGGER_FACTORY.get_logger('weather.vane') self._log.info('Vane Active')
def __init__(self): self._log = LOGGER_FACTORY.get_logger('ipa.dht') self._log.info('sensor=%d pin=%d retries=%d' % (Dht._SENSOR, Dht._PIN, Dht._RETRIES))
#!/usr/bin/env python import RPi.GPIO as GPIO #@UnresolvedImport import subprocess import traceback import common from config import C from logger import LOGGER_FACTORY from wind import Wind if __name__ == "__main__": log = LOGGER_FACTORY.get_logger('wind_calibrate') try: log.info('--- wind_calibrate started, waiting for ntpd ---') if subprocess.call('../await_clock_sync.sh'): log.critical('--- failed to sync clock ---') else: log.info('--- clock in sync ---') GPIO.setmode(GPIO.BCM) wind = Wind(calibration_mode=True) raw_input('Press ENTER to quit...\n') # just in case there's a kb and a screen GPIO.cleanup() wind.terminate_calibration() threads_left = common.join_all_threads(C.TIMEOUT_SHUTDOWN_SECONDS()) print '--- exiting - threads left: %d ---' % threads_left except Exception: log.critical(traceback.format_exc())
def __init__(self): self._log = LOGGER_FACTORY.get_logger('weather.camera') self._log.info('Resolution: Horizontal=%d Vertical=%d' % (Picture._RESOLUTION_HOR, Picture._RESOLUTION_VER))