-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·132 lines (100 loc) · 4.01 KB
/
main.py
File metadata and controls
executable file
·132 lines (100 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Here we start
## we check if we have an argument
import sys
import time
if len(sys.argv) > 1:
## we start with argument, we check if it exists and execute the corresponding function
if sys.argv[1] == "clean":
import os
if os.path.exists("logs"):
os.system("rm -rf logs")
if os.path.exists("data"):
os.system("rm -rf data")
exit(0)
## We setup some constants
DEFAULT_CONFIG_PATH = "./config/default.json"
EXTENTIONS_PATH = "./extensions/"
## Before doing anything, we load the configurations
import shutil
from src import config, storage, gpiomanager
from os import path
from shutil import move
from datetime import datetime
import os
import pytz
CONFIG_PATH = config.DATA_DIRECTORY + "current.json"
## We create the default directorys if they don't exist
os.makedirs(config.LOG_DIRECTORY, exist_ok=True)
os.makedirs(config.DATA_DIRECTORY, exist_ok=True)
## Set our timezone
TIMEZONE = pytz.timezone("europe/bucharest")
# We suppose that the default configuration file exist no matter what. Otherwise, python will print a nice error ;-)
with open(DEFAULT_CONFIG_PATH, "r") as f:
defaultConfigurations = f.read()
myConfiguration = config.Configuration(default_config=defaultConfigurations)
# If the custom configuration file doesn't exist, we create it with the defaults values, else we load it
if not path.isfile(CONFIG_PATH):
with open(CONFIG_PATH, "w") as f:
f.write(defaultConfigurations)
else:
with open(CONFIG_PATH, "r") as f:
myConfiguration.load(f.read())
## Now we setup the logging system
previousLogFilePath = myConfiguration.getLoggingPath()
# If there is a previous log file, we need to copy it into the archive directory to not overwrite it with the new logs
if path.isfile(previousLogFilePath):
with open(previousLogFilePath, "r") as f:
# Iterate through the file's lines until we get 10 lines
header = "".join([next(f) for i in range(10)])
timestamp = myConfiguration.getTimestampFromLogs(header)
now = datetime.now(TIMEZONE)
#If we didn't find the timestamp, we use the default naming
if timestamp < 0:
filename = myConfiguration.getArchiveNoTimestampFormat()
filename = now.strftime(filename)
else:
filename = myConfiguration.getArchiveFormat()
now = datetime.fromtimestamp(timestamp)
filename = now.strftime(filename)
filepath = path.join(myConfiguration.getArchiveLoggingPath(), filename)
# We create the archive directory if it doesn't exist
os.makedirs(path.dirname(filepath), exist_ok=True)
move(previousLogFilePath, filepath)
# We create the logging directory if it doesn't exist
os.makedirs(path.dirname(previousLogFilePath), exist_ok=True)
now = datetime.now(TIMEZONE)
# We create or overwrite the default logfile and add the default header in it
header = myConfiguration.getArchiveFormattedHeader(int(now.timestamp()))
with open(previousLogFilePath, "w") as f:
f.write(header)
f.write("\r\n")
# Now we can setup the logging !
import logging
logging.basicConfig(
filename=previousLogFilePath,
datefmt=myConfiguration.getLoggingInFileDateFormat(),
format=myConfiguration.getLoggingInFileFormat(),
level=myConfiguration.getLoggingLevel(),
style='{'
)
logging.info("Starting Solar System...")
# Now we can initialize the rest
myStorage = storage.Storage(myConfiguration.getDatabaseFilePath())
myGPIOManager = gpiomanager.GPIOManager(myStorage)
## We dynamically load the modules
for file in os.listdir(EXTENTIONS_PATH):
if file.endswith(".py") and file != "__init__.py":
module = file[:-3]
logging.info("Loading extension: " + module)
# we expect that the class name is the same as the module name
exec("from " + (EXTENTIONS_PATH[2:] + module).replace("/", ".") + " import " + module.capitalize())
exec("myGPIOManager.addSensor(" + module.capitalize() + "())")
logging.info("Extension loaded: " + module.capitalize())
else:
logging.info("Skipping file: " + file)
## We start our gpio loop
myGPIOManager.run_loop()
## Here we manage the API
# TODO: implement the API
while True:
time.sleep(5)