most of it implemented
This commit is contained in:
Vendored
+9
-1
@@ -1,3 +1,11 @@
|
|||||||
{
|
{
|
||||||
"cSpell.enabled": false
|
"cSpell.enabled": false,
|
||||||
|
"sqltools.connections": [
|
||||||
|
{
|
||||||
|
"previewLimit": 50,
|
||||||
|
"driver": "SQLite",
|
||||||
|
"name": "database",
|
||||||
|
"database": "${workspaceFolder:RaspberryPie}/RaspberryPie/database.db"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#general imports
|
||||||
|
import datetime
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
# imports from Project
|
||||||
|
from RaspberryPie.logger import captScribe
|
||||||
|
from RaspberryPie.config import config
|
||||||
|
|
||||||
|
|
||||||
|
class BGenSecretary:
|
||||||
|
def __init__(self, database_path, image_path):
|
||||||
|
self.database = database_path
|
||||||
|
self.image_path = image_path
|
||||||
|
|
||||||
|
def _record(self, data: dict):
|
||||||
|
connection = None
|
||||||
|
try:
|
||||||
|
connection = sqlite3.connect(self.database)
|
||||||
|
cursor = connection.cursor()
|
||||||
|
for typ in data:
|
||||||
|
time = datetime.datetime.now()
|
||||||
|
data[typ]["time_string"] = str(time)
|
||||||
|
data[typ]["unix_time"] = int(time.timestamp())
|
||||||
|
command = "INSERT INTO {} ({}) VALUES ({})".format(typ, str([i for i in data[typ]])[1:-1], str([data[typ][i] for i in data[typ]])[1:-1])
|
||||||
|
cursor.execute(command)
|
||||||
|
captScribe.info("Inserted data into table '{}' of database".format(typ), "BGenSecretary._record")
|
||||||
|
connection.commit()
|
||||||
|
except sqlite3.Error as e:
|
||||||
|
captScribe.error(str(e), "BGenSecretary._record")
|
||||||
|
finally:
|
||||||
|
if connection is sqlite3.Connection:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def recordPicture(self, picture):
|
||||||
|
with open("{}/{}.jpg".format(self.image_path, str(datetime.datetime.now())), "w") as f:
|
||||||
|
f.write(picture)
|
||||||
|
|
||||||
|
self._record({"picture": {"picture": picture}})
|
||||||
|
|
||||||
|
def recordAir(self, data):
|
||||||
|
self._record({"air": {"temperature": data[0], "humidity": data[1]}})
|
||||||
|
|
||||||
|
bGenSecretary = BGenSecretary(config["File Locations"]["database_file"], config["File Locations"],["image_path"])
|
||||||
Binary file not shown.
@@ -10,8 +10,8 @@ import RPi.GPIO as GPIO
|
|||||||
# imports from project
|
# imports from project
|
||||||
from RaspberryPie.config import config
|
from RaspberryPie.config import config
|
||||||
from RaspberryPie.logger import captScribe
|
from RaspberryPie.logger import captScribe
|
||||||
|
from RaspberryPie.dataHandling import bGenSecretary
|
||||||
|
|
||||||
lib = {}
|
|
||||||
|
|
||||||
class MajGenGPIOController:
|
class MajGenGPIOController:
|
||||||
def __init__(self, pin_open, pin_close, delta_switchOff):
|
def __init__(self, pin_open, pin_close, delta_switchOff):
|
||||||
@@ -60,16 +60,19 @@ class MajGenObserver:
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.captured)
|
return str(self.captured)
|
||||||
|
|
||||||
def get_picture(self):
|
def __init__(self, schedule_timing):
|
||||||
camera = PiCamera()
|
self.schedule_timing = schedule_timing
|
||||||
majGensDictatingMachine = self.DictatingMachine()
|
|
||||||
camera.start_preview()
|
|
||||||
time.sleep(5) # letting camera adjust to environment (exposure and wb)
|
|
||||||
camera.capture(majGensDictatingMachine)
|
|
||||||
camera.stop_preview()
|
|
||||||
return str(majGensDictatingMachine)
|
|
||||||
|
|
||||||
def get_sensorData(self):
|
def get_schedule(self):
|
||||||
|
return self.schedule_timing
|
||||||
|
|
||||||
|
class MajGenAirChecker(MajGenObserver):
|
||||||
|
def __init__(self, schedule_timing=None):
|
||||||
|
if not schedule_timing:
|
||||||
|
schedule_timing = config["Timing"]["environmentdelta"]
|
||||||
|
super().__init__(schedule_timing)
|
||||||
|
|
||||||
|
def execute():
|
||||||
sensor = Adafruit_DHT.DHT22
|
sensor = Adafruit_DHT.DHT22
|
||||||
pin = int(config["Hardware"]["dhtpin"])
|
pin = int(config["Hardware"]["dhtpin"])
|
||||||
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
|
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
|
||||||
@@ -78,32 +81,43 @@ class MajGenObserver:
|
|||||||
humidity, temperature = (-1, -274) # if you need any numbers, why don't take impossible ones, to show error
|
humidity, temperature = (-1, -274) # if you need any numbers, why don't take impossible ones, to show error
|
||||||
return humidity, temperature
|
return humidity, temperature
|
||||||
|
|
||||||
class LtGenDataCollector:
|
class MajGenVisualObserver(MajGenObserver):
|
||||||
def __init__(self, functions: dict, library, schedule_timing=None):
|
def __init__(self, schedule_timing=None):
|
||||||
self.functions = functions
|
|
||||||
if not schedule_timing:
|
if not schedule_timing:
|
||||||
self.schedule_timing = {"air": config["Timing"]["environmentdelta"], "camera": config["Timing"]["picturedelta"]}
|
schedule_timing = config["Timing"]["picturedelta"]
|
||||||
else:
|
super().__init__(schedule_timing)
|
||||||
self.schedule_timing = schedule_timing
|
|
||||||
self.schedule = {"air": datetime.datetime.now(), "camera": datetime.datetime.now()}
|
|
||||||
self.library = library
|
|
||||||
|
|
||||||
def get_schedule(self):
|
def execute():
|
||||||
arr = []
|
camera = PiCamera()
|
||||||
for event in self.schedule_timing:
|
majGensDictatingMachine = super.DictatingMachine()
|
||||||
arr.append(self.schedule[event])
|
camera.start_preview()
|
||||||
return arr
|
time.sleep(5) # letting camera adjust to environment (exposure and wb)
|
||||||
|
camera.capture(majGensDictatingMachine, format="jpeg")
|
||||||
|
camera.stop_preview()
|
||||||
|
return str(majGensDictatingMachine)
|
||||||
|
|
||||||
def _archive(self, place, data):
|
class MajGenAirRecoder(MajGenAirChecker):
|
||||||
pass
|
def __init__(self, dataCollector, schedule_timing=None):
|
||||||
|
self.dataCollector = dataCollector
|
||||||
|
super().__init__(schedule_timing=schedule_timing)
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
data = super.execute()
|
||||||
|
self.dataCollector.recordeAir({"air": (str(datetime.datetime.now()), data)})
|
||||||
|
|
||||||
|
class MajGenVisualRecoder(MajGenVisualObserver):
|
||||||
|
def __init__(self, dataCollector, schedule_timing=None):
|
||||||
|
self.dataCollector = dataCollector
|
||||||
|
super().__init__(schedule_timing=schedule_timing)
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
data = super.execute()
|
||||||
|
self.dataCollector.recordePicture({"picture": (str(datetime.datetime.now()), data)})
|
||||||
|
|
||||||
def collect(self, function_names: list):
|
|
||||||
time = datetime.datetime.now()
|
|
||||||
for function in function_names:
|
|
||||||
data = self.functions[function]()
|
|
||||||
self._archive(function, {time: data})
|
|
||||||
|
|
||||||
|
|
||||||
majGenObserver = MajGenObserver()
|
|
||||||
majGenGPIOController = MajGenGPIOController(config["Hardware"]["pinopen"], config["Hardware"]["pinclose"], config["Timing"]["gpioswitchoff"])
|
majGenGPIOController = MajGenGPIOController(config["Hardware"]["pinopen"], config["Hardware"]["pinclose"], config["Timing"]["gpioswitchoff"])
|
||||||
ltGenDataCollector = LtGenDataCollector({"air": majGenObserver.get_sensorData, "picture": majGenObserver.get_picture}, lib)
|
majGenVisualObserver = MajGenVisualObserver()
|
||||||
|
majGenAirChecker = MajGenAirChecker()
|
||||||
|
majGenAirRecoder = MajGenAirRecoder(bGenSecretary)
|
||||||
|
majGenVisualRecoder = MajGenVisualRecoder(bGenSecretary)
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ class MajGenApiCom:
|
|||||||
MajGenApiCom.delete_mail(id)
|
MajGenApiCom.delete_mail(id)
|
||||||
|
|
||||||
def __init__(self, email, dropbox):
|
def __init__(self, email, dropbox):
|
||||||
# using functions so in future, the storing can "easily" made more secure (not always stored in memory hopefully)
|
|
||||||
self.email = email
|
self.email = email
|
||||||
self.dropbox = dropbox
|
self.dropbox = dropbox
|
||||||
self.mail_config = config["Mail"]
|
self.mail_config = config["Mail"]
|
||||||
|
|||||||
@@ -2,13 +2,14 @@
|
|||||||
import logging
|
import logging
|
||||||
from RaspberryPie.config import config
|
from RaspberryPie.config import config
|
||||||
|
|
||||||
|
|
||||||
class CaptScribe:
|
class CaptScribe:
|
||||||
def __init__(self, log_info, log_error):
|
def __init__(self, log_info, log_error):
|
||||||
self.logfile_info = log_info
|
self.logfile_info = log_info
|
||||||
self.logfile_error = log_error
|
self.logfile_error = log_error
|
||||||
self.loggers = [logging.getLogger("log_info"), logging.getLogger("log_error")]
|
self.loggers = [logging.getLogger("log_info"), logging.getLogger("log_error")]
|
||||||
self.formatters = [logging.Formatter("%(asctime)s : %(level), %(message)s"), logging.Formatter("%(asctime)s : %(message)s: %(sinfo)")]
|
self.formatters = [logging.Formatter("%(asctime)s : %(level), %(message)s"), logging.Formatter("%(asctime)s : %(message)s: %(sinfo)")]
|
||||||
self.handlers = [logging.FileHandler(self.logfile_info, mode='w'), logging.FileHandler(self.logfile_error, mode='w')]
|
self.handlers = [logging.TimedRotatingFileHandler(self.logfile_info, when='midnight', interval=1), logging.TimedRotatingFileHandler(self.logfile_error, when='midnight', interval=1)]
|
||||||
for i, handler in enumerate(self.handlers):
|
for i, handler in enumerate(self.handlers):
|
||||||
handler.setFormatter(self.formatters[i])
|
handler.setFormatter(self.formatters[i])
|
||||||
|
|
||||||
@@ -38,4 +39,5 @@ class CaptScribe:
|
|||||||
for logger in self.loggers:
|
for logger in self.loggers:
|
||||||
logger.debug(func.upper() + ": " + msg)
|
logger.debug(func.upper() + ": " + msg)
|
||||||
|
|
||||||
|
|
||||||
captScribe = CaptScribe(config["File Locations"]["logfile_info"], config["File Locations"]["logfile_error"])
|
captScribe = CaptScribe(config["File Locations"]["logfile_info"], config["File Locations"]["logfile_error"])
|
||||||
@@ -10,11 +10,12 @@ Capt << BGen << MajGen << LtGen << Gen
|
|||||||
"""
|
"""
|
||||||
# imports from project
|
# imports from project
|
||||||
from RaspberryPie.tasks import Task
|
from RaspberryPie.tasks import Task
|
||||||
from RaspberryPie.hardwareControl import majGenObserver, majGenGPIOController, ltGenDataCollector
|
from RaspberryPie.hardwareControl import majGenGPIOController, majGenVisualRecorder, majGenAirRecoder
|
||||||
from RaspberryPie.config import config
|
from RaspberryPie.config import config
|
||||||
from RaspberryPie.logger import captScribe
|
from RaspberryPie.logger import captScribe
|
||||||
from RaspberryPie.internetHandling import majGenApiCom, MajGenApiCom
|
from RaspberryPie.internetHandling import majGenApiCom, MajGenApiCom
|
||||||
|
|
||||||
|
|
||||||
ltGenInterpreter, genScheduler = None
|
ltGenInterpreter, genScheduler = None
|
||||||
|
|
||||||
tasks = [
|
tasks = [
|
||||||
@@ -22,11 +23,18 @@ tasks = [
|
|||||||
Task(majGenGPIOController.close, lambda msg: 1 if ("zu" == msg.subject.lower() or "close" == msg.subject.lower()) else 0)
|
Task(majGenGPIOController.close, lambda msg: 1 if ("zu" == msg.subject.lower() or "close" == msg.subject.lower()) else 0)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class LtGenInterpreter:
|
class LtGenInterpreter:
|
||||||
def __init__(self, tasks: list, communicator: MajGenApiCom):
|
def __init__(self, tasks: list, communicator: MajGenApiCom, schedule_timing=None):
|
||||||
|
if not schedule_timing:
|
||||||
|
schedule_timing = config["Timing"]["maildelta"]
|
||||||
|
self.schedule_timing
|
||||||
self.tasks = tasks
|
self.tasks = tasks
|
||||||
self.communicator = communicator
|
self.communicator = communicator
|
||||||
|
|
||||||
|
def get_schedule(self):
|
||||||
|
return self.schedule_timing
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
messages = self.communicator.get_emails()
|
messages = self.communicator.get_emails()
|
||||||
for message in messages:
|
for message in messages:
|
||||||
@@ -67,7 +75,23 @@ class GenScheduler:
|
|||||||
def init():
|
def init():
|
||||||
global ltGenInterpreter, genScheduler
|
global ltGenInterpreter, genScheduler
|
||||||
ltGenInterpreter = LtGenInterpreter(tasks, majGenApiCom)
|
ltGenInterpreter = LtGenInterpreter(tasks, majGenApiCom)
|
||||||
genScheduler = GenScheduler((majGenApiCom, ltGenDataCollector))
|
genScheduler = GenScheduler((ltGenInterpreter, majGenVisualRecorder, majGenAirRecoder))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
suspend_shutdown = False
|
||||||
|
if genScheduler is GenScheduler:
|
||||||
|
try:
|
||||||
|
genScheduler.execute()
|
||||||
|
except Exception as e:
|
||||||
|
captScribe.critical(str(e), "main()")
|
||||||
|
if e is KeyboardInterrupt:
|
||||||
|
suspend_shutdown = True
|
||||||
|
finally:
|
||||||
|
if not suspend_shutdown:
|
||||||
|
genScheduler.initiate_shutdown()
|
||||||
|
print("The programm has finished. For more information see the log files.")
|
||||||
|
|
||||||
|
|
||||||
init()
|
init()
|
||||||
|
main()
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from RaspberryPie.internetHandling import MajGenApiCom
|
||||||
|
|
||||||
class Task:
|
class Task:
|
||||||
def __init__(self, function, check):
|
def __init__(self, function, check):
|
||||||
self.function = function
|
self.function = function
|
||||||
|
|||||||
Reference in New Issue
Block a user