Skip to content

Commit

Permalink
Merge pull request #3 from J-shw/V1.1.0
Browse files Browse the repository at this point in the history
V1.1.0
  • Loading branch information
J-shw authored Feb 22, 2024
2 parents e971d50 + 9dc5df8 commit dc57c39
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 62 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

static/.DS_Store
.DS_Store
.DS_Store
static/.DS_Store
*.pyc
Binary file modified __pycache__/pond.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/webhook.cpython-310.pyc
Binary file not shown.
33 changes: 33 additions & 0 deletions modules/sysLogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import logging
from logging.handlers import RotatingFileHandler

# Configure a rotating file handler
handler = RotatingFileHandler(
filename='static/data/crashLogs/log.txt',
mode='a',
maxBytes=31457280, # Set the maximum size of each log file in bytes
backupCount=5 # Number of backup log files to keep
)

# Configure the logging format and level for the handler
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s() - Line %(lineno)d - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)

# Create a logger and add the handler
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.ERROR)

# Logger level options:
# DEBUG
# INFO
# WARNING
# ERRRO
# CRITICAL

# Example uses:
# logger.debug("This is just a harmless debug message")
# logger.info("This is just an information for you")
# logger.warning("OOPS!!!Its a Warning")
# logger.error("Have you tried to divide a number by zero")
# logger.critical("The Internet is not working....")
101 changes: 39 additions & 62 deletions pond.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime as dt
from w1thermsensor import W1ThermSensor
import RPi.GPIO as io
from modules.sysLogger import logger
# import webhook - Maybe use requests instead

class pc:
Expand All @@ -14,8 +15,10 @@ class pc:

# Variables setup
allData = []
configData = {}
deviceData = []
data = []
lastErrorTime = 0
levelCheckValue = 'Ok' # 'Ok', 'Low, 'High'
waterState = 'Off' # 'Off', 'Filling', 'Draining'
nexusPump = True
Expand Down Expand Up @@ -411,16 +414,6 @@ def log(data : list, logFilePath : str, logFilesRow : list, current_time : float
writer = csv.writer(file)
writer.writerow(dataToSave)

def systemState() -> list: # return layout - [Status, Running, Crashed, Error]

if pc.crash[0] != True:
try:
return ([200, True, False])
except Exception as e:
return([500, None, True, str(e)])
else:
return ([200, False, pc.crash[0], pc.crash[1]])

def pondStatus() -> list: # Use to keep track of pond alerts
return [pc.pondStateArray, state.levelSensors]

Expand Down Expand Up @@ -767,37 +760,24 @@ def updateJson(data : list) -> list:

return [200, "None"]

def logCrash(configData, crashData : list, crashAlerted : bool, crashFilePath : str, lastCrashTime : float): #Changed this to save actual time not time in long format

crash_time = crashData[2]
if crash_time != lastCrashTime:
lastCrashTime = crash_time

time = datetime.datetime.now()

formatted_date = time.strftime("%Y-%m-%d")

time_obj = datetime.datetime.fromtimestamp(crash_time)
crash_time = time_obj.strftime("%H:%M:%S")

filename = f"{crashFilePath}{formatted_date}.txt"
row = f"{str(crashData[1])}, {str(crash_time)}\n"

if not os.path.exists(filename):
with open(filename, 'w', newline='') as file:
file.write(row)
else:
with open(filename, 'a', newline='') as file:
file.write(row)
def reportCrash():
configData = pc.configData
tries = 0
response = None

if time.time+60 > pc.lastErrorTime:
while response != 200 and tries < 3:
server = configData['webhook']['server']
key = configData['webhook']['keys']['alert']
response = webhook.send(server, key)
tries+=1
time.sleep(0.2)
pc.lastErrorTime = time.time()
if response != 200:
logger.warning("Failed to send crash webhook")

if crashAlerted == False:
server = configData['webhook']['server']
key = configData['webhook']['keys']['crash']
response = webhook.send(server, key)
if response == 200:
crashAlerted = True


def start():

runTime = 0
Expand All @@ -806,54 +786,51 @@ def start():

try:
with open(pc.configPath) as config_file:
configData = json.load(config_file)

runTime = time.time() + configData['updateFreq']['time']
pc.configData = json.load(config_file)
runTime = time.time() + pc.configData['updateFreq']['time']
except Exception as e:
pc.crash = [True, "config | " +str(e), time.time()]

if pc.crash[0]:
logCrash(configData, pc.crash, pc.crashAlerted, pc.crashFilePath, pc.lastCrashTime)
else:
pc.crashAlerted = False

try:
if time.time() >= pc.crash[2] + 30: pc.crash = [False, None, None]
except: pass
logger.critical(e)
reportCrash()

current_time = datetime.datetime.now() # Used for logging date/time

# Collect data
try:
pc.deviceData = getDeviceData()
except Exception as e:
pc.crash = [True, "getDeviceData() | " +str(e), time.time()]
logger.critical(e)
reportCrash()
try:
pc.data = getData(configData, pc.levelCheckValue)
pc.data = getData(pc.configData, pc.levelCheckValue)
except Exception as e:
pc.crash = [True, "getData() | " + str(e), time.time()]
logger.critical(e)
reportCrash()

pc.allData = [pc.data, pc.deviceData, state.levelSensors]
# - - - - - - -

try:
log(pc.allData, pc.logFilePath, pc.logFilesRow, current_time)
except Exception as e:
pc.crash = [True, "log() | " +str(e), time.time()]
logger.critical(e)
reportCrash()

try:
pondState(configData, pc.allData)
pondState(pc.configData, pc.allData)
except Exception as e:
pc.crash = [True, "pondState() | " + str(e), time.time()]
logger.critical(e)
reportCrash()

try:
pumpControl(configData, pc.allData)
pumpControl(pc.configData, pc.allData)
except Exception as e:
pc.crash = [True, "pumpControl() | " + str(e), time.time()]
logger.critical(e)
reportCrash()

try:
cleanMode(configData, pc.allData)
cleanMode(pc.configData, pc.allData)
except Exception as e:
pc.crash = [True, "cleanMode() | " + str(e), time.time()]
logger.critical(e)
reportCrash()

time.sleep(0.2)
Binary file modified static/.DS_Store
Binary file not shown.

0 comments on commit dc57c39

Please sign in to comment.