<commit_before>import json
import yaml
import sys
import time
import datetime
import os
import glob
import threading
from enum import Enum
from decimal import *

import gspread
from oauth2client.service_account import ServiceAccountCredentials

from nameko.rpc import rpc
from nameko.events import EventDispatcher, event_handler
from nameko.timer import timer

from AutoReef.integrations import LogLevel, Relay, RelayMode, State, DeviceType, TempSensor, DS18B20
from AutoReef.common import load_config

import RPi.GPIO as GPIO
import Adafruit_CharLCD as LCD

"""
    Logging Service 
"""

#Global spreasheet handle and its resource lock
spreadsheet = None

def lcd_init():
    lcd = LCD.Adafruit_CharLCDPlate()
    lcd.clear()
    lcd.message("Autoreef init...")
    return lcd

lcd_init()

lock = threading.RLock()

def login_open_sheet(oauth_key_file, spreadsheet):
    """Connect to Google Docs spreadsheet and return the first worksheet."""

    try:
        scope =  ['https://spreadsheets.google.com/feeds']
        credentials = ServiceAccountCredentials.from_json_keyfile_name(oauth_key_file, scope)
        gc = gspread.authorize(credentials)
        spreadsheet = gc.open(spreadsheet)
        return spreadsheet
    except Exception as ex:
        print('Unable to login and get spreadsheet.  Check OAuth credentials, spreadsheet name, and make sure spreadsheet is shared to the client_email address in the OAuth .json file!')
        print('Google sheet login failed with error:', ex)
        sys.exit(1)


class LoggingService:

    name = "LoggingService"

    print("Init " + name)

    # Google docs details
    GDOCS_OAUTH_JSON = os.path.join(os.path.dirname(__file__), "../config/gdocs-config.json")
    GDOCS_SPREADSHEET_NAME = 'Aquarium Params'
    
    #Start Temp Logging
    @event_handler("TempProbeService", "high_temp_critical")
    def high_temp_critical_handler(self, payload):
        self.log_temp(payload, "high_temp_critical", LogLevel.ERROR)

    @event_handler("TempProbeService", "high_temp_warning")
    def high_temp_warning_handler(self, payload):
        self.log_temp(payload, "high_temp_warning", LogLevel.CRIT)

    @event_handler("TempProbeService", "high_temp")
    def high_temp_handler(self, payload):
        self.log_temp(payload, "high_temp", LogLevel.WARN)        

    @event_handler("TempProbeService", "low_temp")
    def low_temp_handler(self, payload):
        self.log_temp(payload, "low_temp", LogLevel.WARN)

    @event_handler("TempProbeService", "low_temp_warning")
    def low_temp_warning_handler(self, payload):
        self.log_temp(payload, "low_temp_warning", LogLevel.CRIT)

    @event_handler("TempProbeService", "low_temp_critical")
    def low_temp_critical_handler(self, payload):
        self.log_temp(payload, "low_temp_critical", LogLevel.ERROR)

    @event_handler("TempProbeService", "norm_temp")
    def norm_temp_handler(self, payload):
        self.log_temp(payload, "norm_temp", LogLevel.INFO)

    #Start Event Logging

    @event_handler("Relay", "event_log")
    def event_log_temp_monitor(self, payload):
        time = str(payload["time"])
        log_level = str(payload["log_level"])
        device_type = str(payload["device_type"])
        name = str(payload["name"])
        message = str(payload["message"])

        global spreadsheet
        
        with lock:
            if spreadsheet == None:
                spreadsheet = login_open_sheet(self.GDOCS_OAUTH_JSON, self.GDOCS_SPREADSHEET_NAME)
        
            try:
                spreadsheet.worksheet('EventLog').append_row((time, name, device_type, message, log_level))
                print("Wrote event log [" + name + "] " + message)
            except Exception as e:
                print("Append error, logging on again")
                print(e)
                spreadsheet = None
                self.event_log_temp_monitor(payload)
    
    def log_temp_lcd(self, state, temp, log_level):

        lcd = lcd_init()
        lcd.clear()
        lcd.message("Temp: " + str(temp))

        if log_level == LogLevel.ERROR:
            lcd.set_color(1.0, 0.0, 0.0)
        elif log_level == LogLevel.CRIT:
            lcd.set_color(1.0, 0.5, 0.0)
        elif log_level == LogLevel.WARN:
            lcd.set_color(1.0, 1.0, 0.0)
        elif log_level == LogLevel.INFO:
            lcd.set_color(0.0, 1.0, 0.0)
        elif log_level == LogLevel.DEBUG:
            lcd.set_color(0.0, 0.0, 1.0)
        else:
            lcd.set_color(1.0, 0.0, 1.0)
<commit_msg>Modified the colours, since apparently it isn't possible to give fractional parameters to set_color() in the Adafruit LCD Plate library<commit_after>import json
import yaml
import sys
import time
import datetime
import os
import glob
import threading
from enum import Enum
from decimal import *

import gspread
from oauth2client.service_account import ServiceAccountCredentials

from nameko.rpc import rpc
from nameko.events import EventDispatcher, event_handler
from nameko.timer import timer

from AutoReef.integrations import LogLevel, Relay, RelayMode, State, DeviceType, TempSensor, DS18B20
from AutoReef.common import load_config

import RPi.GPIO as GPIO
import Adafruit_CharLCD as LCD

"""
    Logging Service 
"""

#Global spreasheet handle and its resource lock
spreadsheet = None

def lcd_init():
    lcd = LCD.Adafruit_CharLCDPlate()
    lcd.clear()
    lcd.message("Autoreef init...")
    return lcd

lcd_init()

lock = threading.RLock()

def login_open_sheet(oauth_key_file, spreadsheet):
    """Connect to Google Docs spreadsheet and return the first worksheet."""

    try:
        scope =  ['https://spreadsheets.google.com/feeds']
        credentials = ServiceAccountCredentials.from_json_keyfile_name(oauth_key_file, scope)
        gc = gspread.authorize(credentials)
        spreadsheet = gc.open(spreadsheet)
        return spreadsheet
    except Exception as ex:
        print('Unable to login and get spreadsheet.  Check OAuth credentials, spreadsheet name, and make sure spreadsheet is shared to the client_email address in the OAuth .json file!')
        print('Google sheet login failed with error:', ex)
        sys.exit(1)


class LoggingService:

    name = "LoggingService"

    print("Init " + name)

    # Google docs details
    GDOCS_OAUTH_JSON = os.path.join(os.path.dirname(__file__), "../config/gdocs-config.json")
    GDOCS_SPREADSHEET_NAME = 'Aquarium Params'
    
    #Start Temp Logging
    @event_handler("TempProbeService", "high_temp_critical")
    def high_temp_critical_handler(self, payload):
        self.log_temp(payload, "high_temp_critical", LogLevel.ERROR)

    @event_handler("TempProbeService", "high_temp_warning")
    def high_temp_warning_handler(self, payload):
        self.log_temp(payload, "high_temp_warning", LogLevel.CRIT)

    @event_handler("TempProbeService", "high_temp")
    def high_temp_handler(self, payload):
        self.log_temp(payload, "high_temp", LogLevel.WARN)        

    @event_handler("TempProbeService", "low_temp")
    def low_temp_handler(self, payload):
        self.log_temp(payload, "low_temp", LogLevel.WARN)

    @event_handler("TempProbeService", "low_temp_warning")
    def low_temp_warning_handler(self, payload):
        self.log_temp(payload, "low_temp_warning", LogLevel.CRIT)

    @event_handler("TempProbeService", "low_temp_critical")
    def low_temp_critical_handler(self, payload):
        self.log_temp(payload, "low_temp_critical", LogLevel.ERROR)

    @event_handler("TempProbeService", "norm_temp")
    def norm_temp_handler(self, payload):
        self.log_temp(payload, "norm_temp", LogLevel.INFO)

    #Start Event Logging

    @event_handler("Relay", "event_log")
    def event_log_temp_monitor(self, payload):
        time = str(payload["time"])
        log_level = str(payload["log_level"])
        device_type = str(payload["device_type"])
        name = str(payload["name"])
        message = str(payload["message"])

        global spreadsheet
        
        with lock:
            if spreadsheet == None:
                spreadsheet = login_open_sheet(self.GDOCS_OAUTH_JSON, self.GDOCS_SPREADSHEET_NAME)
        
            try:
                spreadsheet.worksheet('EventLog').append_row((time, name, device_type, message, log_level))
                print("Wrote event log [" + name + "] " + message)
            except Exception as e:
                print("Append error, logging on again")
                print(e)
                spreadsheet = None
                self.event_log_temp_monitor(payload)
    
    def log_temp_lcd(self, state, temp, log_level):

        lcd = lcd_init()
        lcd.clear()
        lcd.message("Temp: " + str(temp))

        if log_level == LogLevel.ERROR:
            lcd.set_color(1.0, 0.0, 0.0)
        elif log_level == LogLevel.CRIT:
            lcd.set_color(1.0, 1.0, 0.0)
        elif log_level == LogLevel.WARN:
            lcd.set_color(0.0, 1.0, 1.0)
        elif log_level == LogLevel.INFO:
            lcd.set_color(0.0, 1.0, 0.0)
        elif log_level == LogLevel.DEBUG:
            lcd.set_color(0.0, 0.0, 1.0)
        else:
            lcd.set_color(1.0, 0.0, 1.0)

{'fraction_non_alphanumeric': 0.10157273918741809, 'fraction_numerical': 0.009174311926605505, 'mean_word_length': 3.102598566308244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2443413', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2829, 'n_words': 659}