<commit_before>#!/usr/bin/env python

# Based on previous work by
# <NAME> (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and <NAME> (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

# daemon21.py measures the TMP36 temperature.
# uses moving averages

import syslog, traceback
import os, sys, time, math
import Adafruit_BBIO.ADC  as ADC
import Adafruit_BBIO.GPIO as GPIO
import MySQLdb as mdb

# own libraries:
from libdaemon import Daemon

DEBUG = False
IS_SYSTEMD = os.path.isfile('/bin/journalctl')
sensor_pin = 'AIN6'
# SENSOR CALIBRATION PROCEDURE
# Given the existing gain and offset.
# 1 Determine a linear least-squares fit between the output of this program and
#   data obtained from the selected reference
# 2 The least-squares fit will yield the gain(calc) and offset(calc)
# 3 Determine gain(new) and offset(new) as shown here:
#     gain(new)   = gain(old)   * gain(calc)
#     offset(new) = offset(old) * gain(calc) + offset(calc)
# 4 Replace the existing values for gain(old) and offset(old) with the values
#   found for gain(new) and offset(new)

# gain(old)
TMP36_gain = 0.1
# offset(old)
TMP36_offset = -50.0

class MyDaemon(Daemon):
  def run(self):
    # Initialisation
    try:
      # Initialise hardware
      ADC.setup()
      GPIO.setup("USR0", GPIO.OUT)
    except Exception as e:
      if DEBUG:
        print("Unexpected error:")
        print e.message
      syslog.syslog(syslog.LOG_ALERT,e.__doc__)
      syslog_trace(traceback.format_exc())
      raise
    try:
      # Initialise MySQLdb
      consql = mdb.connect(host='sql.lan', db='domotica', read_default_file='~/.my.cnf')
      # activate a cursor
      cursql = consql.cursor()
      # test the connection
      cursql.execute("SELECT VERSION()")
      versql = cursql.fetchone()
      logtext = "{0} : {1}".format("Attached to MySQL server", versql)
      syslog.syslog(syslog.LOG_INFO, logtext)
    except mdb.Error, e:
      if DEBUG:
        print("Unexpected MySQL error")
        print "Error %d: %s" % (e.args[0],e.args[1])
      syslog.syslog(syslog.LOG_ALERT,e.__doc__)
      syslog_trace(traceback.format_exc())
      # attempt to close connection to MySQLdb
      if consql:consql.close()
      raise

    # Initialise parameters
    reportTime = 60                                 # time [s] between reports
    cycles = 3                                      # number of cycles to aggregate
    samplesperCycle = 5                             # total number of samples in each cycle
    samples = samplesperCycle * cycles              # total number of samples averaged
    sampleTime = reportTime/samplesperCycle         # time [s] between samples
    cycleTime = samples * sampleTime                # time [s] per cycle

    data = []                                       # array for holding sampledata
<commit_msg>moving MySQL connection to start of the code<commit_after>#!/usr/bin/env python

# Based on previous work by
# <NAME> (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and <NAME> (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

# daemon21.py measures the TMP36 temperature.
# uses moving averages

import syslog, traceback
import os, sys, time, math
import Adafruit_BBIO.ADC  as ADC
import Adafruit_BBIO.GPIO as GPIO
import MySQLdb as mdb

# own libraries:
from libdaemon import Daemon

DEBUG = False
IS_SYSTEMD = os.path.isfile('/bin/journalctl')

sensor_pin = 'AIN6'
# SENSOR CALIBRATION PROCEDURE
# Given the existing gain and offset.
# 1 Determine a linear least-squares fit between the output of this program and
#   data obtained from the selected reference
# 2 The least-squares fit will yield the gain(calc) and offset(calc)
# 3 Determine gain(new) and offset(new) as shown here:
#     gain(new)   = gain(old)   * gain(calc)
#     offset(new) = offset(old) * gain(calc) + offset(calc)
# 4 Replace the existing values for gain(old) and offset(old) with the values
#   found for gain(new) and offset(new)

# gain(old)
TMP36_gain = 0.1
# offset(old)
TMP36_offset = -50.0

try:
  # Initialise MySQLdb
  consql = mdb.connect(host='sql.lan', db='domotica', read_default_file='~/.my.cnf')
  # activate a cursor
  cursql = consql.cursor()
  # test the connection
  cursql.execute("SELECT VERSION()")
  versql = cursql.fetchone()
  cursql.close()
  logtext = "{0} : {1}".format("Attached to MySQL server", versql)
  syslog.syslog(syslog.LOG_INFO, logtext)
except mdb.Error, e:
  if DEBUG:
    print("Unexpected MySQL error")
    print "Error %d: %s" % (e.args[0],e.args[1])
  syslog.syslog(syslog.LOG_ALERT,e.__doc__)
  syslog_trace(traceback.format_exc())
  # attempt to close connection to MySQLdb
  if consql:consql.close()
  raise

class MyDaemon(Daemon):
  def run(self):
    # Initialisation
    try:
      # Initialise hardware
      ADC.setup()
      GPIO.setup("USR0", GPIO.OUT)
    except Exception as e:
      if DEBUG:
        print("Unexpected error:")
        print e.message
      syslog.syslog(syslog.LOG_ALERT,e.__doc__)
      syslog_trace(traceback.format_exc())
      raise

    # Initialise parameters
    reportTime = 60                                 # time [s] between reports
    cycles = 3                                      # number of cycles to aggregate
    samplesperCycle = 5                             # total number of samples in each cycle
    samples = samplesperCycle * cycles              # total number of samples averaged
    sampleTime = reportTime/samplesperCycle         # time [s] between samples
    cycleTime = samples * sampleTime                # time [s] per cycle

    data = []                                       # array for holding sampledata

{'fraction_non_alphanumeric': 0.10105657805044307, 'fraction_numerical': 0.015337423312883436, 'mean_word_length': 2.8011658031088085, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1005875', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1818, 'n_words': 528}