<commit_before>#!/usr/bin/env python
# Author: <NAME> for Explo (http://www.explo.org)
# Some bits are from Adafruit (see README)

import logging, time, subprocess, os
import toml

import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855


# Define a function to convert celsius to fahrenheit.
def c_to_f(c):
  return c * 9.0 / 5.0 + 32.0


# Grab config information from the log file (TOML format)
with open("/home/pi/raspberry-pi-temperature-monitor/config.toml") as conffile:
  conf = toml.loads(conffile.read())

# Set device name
if conf.get('device_name'):
  device_name = conf['device_name']
else:
  device_name = "Unknown Device"

# Set up logging if applicable
LOGFORMAT = "%(asctime)s\t%(message)s"
if conf.get('log_file'):
  if conf.get('log_level') == 'DEBUG':
    logging.basicConfig(filename=conf['log_file'], format=LOGFORMAT, level=logging.DEBUG)
  else:
    # Default to INFO-level logging
    logging.basicConfig(filename=conf['log_file'], format=LOGFORMAT, level=logging.INFO)
  print("Logging to {0}".format(conf['log_file']))
else:
  # Just log to STDOUT
  logging.basicConfig(level=logging.INFO, format=LOGFORMAT)


# Set up the temperature poll rate
if conf.get('poll_rate'):
  poll_rate = conf.get('poll_rate')
else:
  # Default to 10 seconds if none is set
  poll_rate = 10


# Set our temperature sensor based on the desired connection method
if conf.get('pi').get('connect_method') == "HARDWARE":
  sensor = MAX31855.MAX31855(spi=SPI.SpiDev(conf['pi']['SPI_PORT'], conf['pi']['SPI_DEVICE']))
else:
  # Default to software SPI
  sensor = MAX31855.MAX31855(conf['pi']['CLK'], conf['pi']['CS'], conf['pi']['DO'])


# Grab our (wireless) IP address so that we can send a link to the webcam if
# the temp is too high/low
ip_command = "ifconfig | awk 'BEGIN{ORS=\"\"}/wlan0/{getline;gsub(/addr:/,\"\",$2);print $2}'"
my_ip = str(subprocess.check_output(ip_command, shell=True))


# We sometimes get false freezing measurements on startup. So we double-check
# for freezing temps
might_be_freezing = None

# Loop measurements every second.
while True:
  # Rotate log file
  if conf.get('log_file'):
    log_size = os.stat(conf.get('log_file')).st_size 

    if conf.get('log_file_rotate_size') == None:
      log_file_rotate_size = 10000000 #10MB
    else:
      log_file_rotate_size = conf['log_file_rotate_size']

    if log_size > conf.get('log_file_rotate_size'):
      # Store log in hipchat
      if conf.get('hipchat').get('token') and conf.get('hipchat').get('room'):
        subprocess.call(['/usr/local/bin/hipfile', "--token", conf['hipchat']['token'], "--room", conf['hipchat']['room'], "--path", conf.get('log_file'), "--message", "{0} Temperature Log".format(device_name)])
      # Empty log file
      with open(conf.get('log_file'), 'w'):
        pass


  # now do the temperature reading
  temp = sensor.readTempC()
  #internal = sensor.readInternalC()
  logging.info("{0:0.3F}*C\t{1:0.3F}*F".format(temp, c_to_f(temp)))

  if temp > conf['temp']['max']:
    warning_text = "{0} WARNING: {1] is hot, potential fire risk: {2:0.3F}*F. Webcam: http://{3}:8081".format(conf.get('hipchat').get('notify_names'), device_name, c_to_f(temp), my_ip)
    logging.warning(warning_text)
    if conf.get('hipchat').get('room'):<commit_msg>typo fix<commit_after>#!/usr/bin/env python
# Author: <NAME> for Explo (http://www.explo.org)
# Some bits are from Adafruit (see README)

import logging, time, subprocess, os
import toml

import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855


# Define a function to convert celsius to fahrenheit.
def c_to_f(c):
  return c * 9.0 / 5.0 + 32.0


# Grab config information from the log file (TOML format)
with open("/home/pi/raspberry-pi-temperature-monitor/config.toml") as conffile:
  conf = toml.loads(conffile.read())

# Set device name
if conf.get('device_name'):
  device_name = conf['device_name']
else:
  device_name = "Unknown Device"

# Set up logging if applicable
LOGFORMAT = "%(asctime)s\t%(message)s"
if conf.get('log_file'):
  if conf.get('log_level') == 'DEBUG':
    logging.basicConfig(filename=conf['log_file'], format=LOGFORMAT, level=logging.DEBUG)
  else:
    # Default to INFO-level logging
    logging.basicConfig(filename=conf['log_file'], format=LOGFORMAT, level=logging.INFO)
  print("Logging to {0}".format(conf['log_file']))
else:
  # Just log to STDOUT
  logging.basicConfig(level=logging.INFO, format=LOGFORMAT)


# Set up the temperature poll rate
if conf.get('poll_rate'):
  poll_rate = conf.get('poll_rate')
else:
  # Default to 10 seconds if none is set
  poll_rate = 10


# Set our temperature sensor based on the desired connection method
if conf.get('pi').get('connect_method') == "HARDWARE":
  sensor = MAX31855.MAX31855(spi=SPI.SpiDev(conf['pi']['SPI_PORT'], conf['pi']['SPI_DEVICE']))
else:
  # Default to software SPI
  sensor = MAX31855.MAX31855(conf['pi']['CLK'], conf['pi']['CS'], conf['pi']['DO'])


# Grab our (wireless) IP address so that we can send a link to the webcam if
# the temp is too high/low
ip_command = "ifconfig | awk 'BEGIN{ORS=\"\"}/wlan0/{getline;gsub(/addr:/,\"\",$2);print $2}'"
my_ip = str(subprocess.check_output(ip_command, shell=True))


# We sometimes get false freezing measurements on startup. So we double-check
# for freezing temps
might_be_freezing = None

# Loop measurements every second.
while True:
  # Rotate log file
  if conf.get('log_file'):
    log_size = os.stat(conf.get('log_file')).st_size 

    if conf.get('log_file_rotate_size') == None:
      log_file_rotate_size = 10000000 #10MB
    else:
      log_file_rotate_size = conf['log_file_rotate_size']

    if log_size > conf.get('log_file_rotate_size'):
      # Store log in hipchat
      if conf.get('hipchat').get('token') and conf.get('hipchat').get('room'):
        subprocess.call(['/usr/local/bin/hipfile', "--token", conf['hipchat']['token'], "--room", conf['hipchat']['room'], "--path", conf.get('log_file'), "--message", "{0} Temperature Log".format(device_name)])
      # Empty log file
      with open(conf.get('log_file'), 'w'):
        pass


  # now do the temperature reading
  temp = sensor.readTempC()
  #internal = sensor.readInternalC()
  logging.info("{0:0.3F}*C\t{1:0.3F}*F".format(temp, c_to_f(temp)))

  if temp > conf['temp']['max']:
    warning_text = "{0} WARNING: {1} is hot, potential fire risk: {2:0.3F}*F. Webcam: http://{3}:8081".format(conf.get('hipchat').get('notify_names'), device_name, c_to_f(temp), my_ip)
    logging.warning(warning_text)
    if conf.get('hipchat').get('room'):
{'fraction_non_alphanumeric': 0.16186556927297668, 'fraction_numerical': 0.023472031702484377, 'mean_word_length': 4.63745704467354, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '454629', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2256, 'n_words': 554}