Exemplo n.º 1
0
def main(args):
	# Parse the command line and read in the configuration file
	cmdConfig = parseOptions(args)
	
	# Setup logging
	logger = logging.getLogger(__name__)
	logFormat = logging.Formatter('%(asctime)s [%(levelname)-8s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
	logFormat.converter = time.gmtime
	logHandler = WatchedFileHandler(cmdConfig['logfile'])
	logHandler.setFormatter(logFormat)
	logger.addHandler(logHandler)
	if cmdConfig['debug']:
		logger.setLevel(logging.DEBUG)
	else:
		logger.setLevel(logging.INFO)
		
	# PID file
	if cmdConfig['pidFile'] is not None:
		fh = open(cmdConfig['pidFile'], 'w')
		fh.write("%i\n" % os.getpid())
		fh.close()
		
	# CherryPy configuration
	cherrypy.config.update({'server.socket_host': '0.0.0.0', 'environment': 'production'})
	cpConfig = {'/css': {'tools.staticdir.on': True,
						 'tools.staticdir.dir': CSS_PATH},
          		'/js':  {'tools.staticdir.on': True,
          				 'tools.staticdir.dir': JS_PATH}
          		}
				
	# Report on who we are
	logger.info('Starting wxPi.py with PID %i', os.getpid())
	logger.info('All dates and times are in UTC except where noted')
	
	# Load in the configuration
	config = loadConfig(cmdConfig['configFile'])
	
	# Get the latest from the database
	db = Archive()
	db.start()
	
	tData, sensorData = db.getData()
	if time.time() - tData > 2*config.getfloat('Station', 'duration'):
		sensorData = {}
		
	# Initialize the LEDs
	leds = initLEDs(config)
	
	# Make sure the database has provided something useful.  If not, we need to
	# run several iterations to build up the current picture of what is going on.
	if len(sensorData.keys()) == 0:
		buildState = True
		loopsForState = 3
	else:
		buildState = False
		loopsForState = 1
		
	# Start the sensor polling
	bg = PollingProcessor(config, db, leds, buildState=buildState, loopsForState=loopsForState, sensorData=sensorData)
	bg.start()
	
	# Initialize the web interface
	ws = Interface(config, db, leds)
	#cherrypy.quickstart(ws, config=cpConfig)
	cherrypy.tree.mount(ws, "/", config=cpConfig)
	cherrypy.engine.start()
	cherrypy.engine.block()
	
	# Shutdown process
	logger.info('Shutting down wxPi, please wait...')
	
	# Stop the polling thread
	bg.cancel()
	
	# Make sure the LEDs are off
	for color in leds.keys():
		leds[color].off()
		
	# Shutdown the archive
	db.cancel()
Exemplo n.º 2
0
def main(args):
    # Parse the command line and read in the configuration file
    cmdConfig = parseOptions(args)
    
    # Setup logging
    logger = logging.getLogger(__name__)
    logFormat = logging.Formatter('%(asctime)s [%(levelname)-8s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    logFormat.converter = time.gmtime
    logHandler = WatchedFileHandler(cmdConfig['logfile'])
    logHandler.setFormatter(logFormat)
    logger.addHandler(logHandler)
    if cmdConfig['debug']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)
        
    # PID file
    if cmdConfig['pidFile'] is not None:
        fh = open(cmdConfig['pidFile'], 'w')
        fh.write("%i\n" % os.getpid())
        fh.close()
        
    # CherryPy configuration
    cherrypy.config.update({'server.socket_host': '0.0.0.0', 'server.socket_port': 80, 'environment': 'production'})
    cpConfig = {'/css': {'tools.staticdir.on': True,
                         'tools.staticdir.dir': CSS_PATH},
                  '/js':  {'tools.staticdir.on': True,
                           'tools.staticdir.dir': JS_PATH}
                  }
                
    # Report on who we are
    logger.info('Starting Pi2O.py with PID %i', os.getpid())
    logger.info('All dates and times are in UTC except where noted')
     
    # Load in the configuration
    config = loadConfig(cmdConfig['configFile'])
    
    # Initialize the archive
    history = Archive(config)
    history.start()
    
    # Initialize the hardware
    hardwareZones = initZones(config)
    for previousRun in history.getData(scheduledOnly=True):
        logger.info('Previous run of zone %i was on %s LT', previousRun['zone'], datetime.fromtimestamp(previousRun['dateTimeStart']))
        
        if hardwareZones[previousRun['zone']-1].lastStart == 0:
            hardwareZones[previousRun['zone']-1].lastStart = previousRun['dateTimeStart']
            hardwareZones[previousRun['zone']-1].lastStop = previousRun['dateTimeStop']
            
    # Initialize the scheduler
    bg = ScheduleProcessor(config, hardwareZones, history)
    bg.start()
    
    # Initialize the web interface
    ws = Interface(config, hardwareZones, history)
    #cherrypy.quickstart(ws, config=cpConfig)
    cherrypy.tree.mount(ws, "/", config=cpConfig)
    cherrypy.engine.start()
    cherrypy.engine.block()
    
    # Shutdown process
    logger.info('Shutting down Pi2O, please wait...')
    
    # Stop the scheduler thread
    bg.cancel()
    
    # Make sure the sprinkler zones are off
    for i,zone in enumerate(hardwareZones):
        if zone.isActive():
            zone.off()
            history.writeData(time.time(), i, 'off')
            
    # Shutdown the archive
    history.cancel()
    
    # Save the final configuration
    saveConfig(cmdConfig['configFile'], config)
Exemplo n.º 3
0
def main(args):
    # Parse the command line and read in the configuration file
    cmdConfig = parseOptions(args)

    # Setup logging
    logger = logging.getLogger(__name__)
    logFormat = logging.Formatter('%(asctime)s [%(levelname)-8s] %(message)s',
                                  datefmt='%Y-%m-%d %H:%M:%S')
    logFormat.converter = time.gmtime
    logHandler = WatchedFileHandler(cmdConfig['logfile'])
    logHandler.setFormatter(logFormat)
    logger.addHandler(logHandler)
    if cmdConfig['debug']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    # PID file
    if cmdConfig['pidFile'] is not None:
        fh = open(cmdConfig['pidFile'], 'w')
        fh.write("%i\n" % os.getpid())
        fh.close()

    # CherryPy configuration
    cherrypy.config.update({
        'server.socket_host': '0.0.0.0',
        'environment': 'production'
    })
    cpConfig = {
        '/css': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': CSS_PATH
        },
        '/js': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': JS_PATH
        }
    }

    # Report on who we are
    logger.info('Starting wxPi.py with PID %i', os.getpid())
    logger.info('All dates and times are in UTC except where noted')

    # Load in the configuration
    config = loadConfig(cmdConfig['configFile'])

    # Get the latest from the database
    db = Archive()
    db.start()

    tData, sensorData = db.getData()
    if time.time() - tData > 2 * config.getfloat('Station', 'duration'):
        sensorData = {}

    # Initialize the LEDs
    leds = initLEDs(config)

    # Make sure the database has provided something useful.  If not, we need to
    # run several iterations to build up the current picture of what is going on.
    if len(sensorData.keys()) == 0:
        buildState = True
        loopsForState = 3
    else:
        buildState = False
        loopsForState = 1

    # Start the sensor polling
    bg = PollingProcessor(config,
                          db,
                          leds,
                          buildState=buildState,
                          loopsForState=loopsForState,
                          sensorData=sensorData)
    bg.start()

    # Initialize the web interface
    ws = Interface(config, db, leds)
    #cherrypy.quickstart(ws, config=cpConfig)
    cherrypy.tree.mount(ws, "/", config=cpConfig)
    cherrypy.engine.start()
    cherrypy.engine.block()

    # Shutdown process
    logger.info('Shutting down wxPi, please wait...')

    # Stop the polling thread
    bg.cancel()

    # Make sure the LEDs are off
    for color in leds.keys():
        leds[color].off()

    # Shutdown the archive
    db.cancel()