<commit_before>#!/usr/bin/python

import yaml
import os.path
import time
import subprocess

CONFIG_FILE_NAME = "/opt/pi/CheeseCave/config.yaml"
STATS_FILE_NAME  = "/var/lib/CheeseCave/sensor1-stats.yaml"
RELAY_EXECUTABLE = "/opt/pi/CheeseCave/relay"

config_mod_time = 0

def load_config():
	global config_mod_time
	with open(CONFIG_FILE_NAME, 'r') as f:
		data = yaml.load(f)
		print yaml.dump(data)
		config_mod_time = os.path.getmtime(CONFIG_FILE_NAME)
	return data['targets']

def config_has_changed():
	return os.path.getmtime(CONFIG_FILE_NAME) != config_mod_time

def get_relay_state():
	#return ["on"]
	return [subprocess.Popen(["sudo", RELAY_EXECUTABLE, "state"], stdout=subprocess.PIPE).communicate()[0]]

def set_relay_state(state):
	#pass
	subprocess.Popen(["sudo", RELAY_EXECUTABLE, state])

def get_current_stats():
	stats = open(STATS_FILE_NAME, 'r')
	data = yaml.load(stats)
	print yaml.dump(data)		
	return data

if __name__ == "__main__":
	first_time = True
	while True:
		# get configuration if we need to
		if (config_has_changed() or first_time):
			first_time = False
			config = load_config()
			print config

		# get relay state(s)
		relays = get_relay_state()

		# get current stats (temperature, humidity)
		stats = get_current_stats()

		# modify relay based on desired conditions,
		# current stats, and relay state.
		# For the moment, we only do the temperature 
		# relay
		if ((stats['temperature'] > config['temperature'] + 
			(0.5 * config['temperature_range'])) and 
			relays[0] == 'off'):
			set_relay_state("on")
			print(time.strftime("%b %d %Y %1:%M%p: turned relay on"))
		elif ((stats['temperature'] < config['temperature'] -
			(0.5 * config['temperature_range'])) and 
			relays[0] == 'on'):
			set_relay_state("off")
			print(time.strftime("%b %d %Y %1:%M%p %Z: turned relay off"))
		else:
			pass
			#print "no changes needed"	
		time.sleep(5)<commit_msg>fixups for logging of ctrl script<commit_after>#!/usr/bin/python

import yaml
import os.path
import time
import subprocess
import sys

CONFIG_FILE_NAME = "/opt/pi/CheeseCave/config.yaml"
STATS_FILE_NAME  = "/var/lib/CheeseCave/sensor1-stats.yaml"
RELAY_EXECUTABLE = "/opt/pi/CheeseCave/relay"

config_mod_time = 0

def load_config():
	global config_mod_time
	with open(CONFIG_FILE_NAME, 'r') as f:
		data = yaml.load(f)
		#print yaml.dump(data)
		config_mod_time = os.path.getmtime(CONFIG_FILE_NAME)
	return data['targets']

def config_has_changed():
	return os.path.getmtime(CONFIG_FILE_NAME) != config_mod_time

def get_relay_state():
	#return ["on"]
	return [subprocess.Popen(["sudo", RELAY_EXECUTABLE, "state"], stdout=subprocess.PIPE).communicate()[0]]

def set_relay_state(state):
	#pass
	subprocess.Popen(["sudo", RELAY_EXECUTABLE, state])

def get_current_stats():
	stats = open(STATS_FILE_NAME, 'r')
	data = yaml.load(stats)
	#print yaml.dump(data)		
	return data

if __name__ == "__main__":
	first_time = True
	while True:
		# get configuration if we need to
		if (config_has_changed() or first_time):
			first_time = False
			config = load_config()
			#print config

		# get relay state(s)
		relays = get_relay_state()

		# get current stats (temperature, humidity)
		stats = get_current_stats()

		# modify relay based on desired conditions,
		# current stats, and relay state.
		# For the moment, we only do the temperature 
		# relay
		if ((stats['temperature'] > config['temperature'] + 
			(0.5 * config['temperature_range'])) and 
			relays[0] == 'off'):
			set_relay_state("on")
			print time.strftime("%b %d %Y %I:%M%p %Z: turned relay ON")
			sys.stdout.flush()	
		elif ((stats['temperature'] < config['temperature'] -
			(0.5 * config['temperature_range'])) and 
			relays[0] == 'on'):
			set_relay_state("off")
			print time.strftime("%b %d %Y %I:%M%p %Z: turned relay OFF")
			sys.stdout.flush()	
		else:
			pass
			#print "no changes needed"	
		time.sleep(5)
{'fraction_non_alphanumeric': 0.1400978624774659, 'fraction_numerical': 0.0056657223796034, 'mean_word_length': 4.604617604617605, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6372538', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1399, 'n_words': 331}