-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (98 loc) · 4.4 KB
/
main.py
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
"""
This module contains the main loop to log data once every minute.
After setting up the logger and the connection with the database,
the code enters a permanent while loop where each time the seconds are 0,
data gets logged to the database.
"""
import sys
from pathlib import Path
from time import sleep
from argparse import ArgumentParser
from pydantic.v1.utils import deep_update
from modules.sensors import Parsivel, Thies
from modules.util_functions import yaml2dict, get_general_config_dict, create_logger, create_sensor
from modules.telegram import ParsivelTelegram, ThiesTelegram, create_telegram
from modules.now_time import NowTime
from modules.sqldb import create_db, connect_db
######################## BOILER PLATE ##################
def main(config_site):
"""
Main function to log data once every minute
:param config_site: the config file for the site
"""
### Config files ###
wd = Path(__file__).parent
config_dict_site = yaml2dict(path=wd / config_site)
### Log ###
logger = create_logger(log_dir=Path(config_dict_site['log_dir']),
script_name=config_dict_site['script_name'],
sensor_name=config_dict_site['global_attrs']['sensor_name'])
logger.info(msg=f"Starting {__file__} for {config_dict_site['global_attrs']['sensor_name']}")
print(f"{__file__} running\nLogs written to {config_dict_site['log_dir']}")
sensor_type = config_dict_site['global_attrs']['sensor_type']
config_dict_general = get_general_config_dict(wd, sensor_type, logger)
if config_dict_general is None:
sys.exit(1)
config_dict = deep_update(config_dict_general, config_dict_site)
### Serial connection ###
sensor_id = config_dict['global_attrs']['sensor_name'][-2:]
sensor = create_sensor(sensor_type=sensor_type, logger=logger, sensor_id=sensor_id)
sensor.init_serial_connection(port=config_dict['port'], baud=config_dict['baud'], logger=logger)
sensor.sensor_start_sequence(config_dict=config_dict, logger=logger, include_in_log=True)
sleep(2)
### DB ###
db_path = Path(config_dict['data_dir']) / config_dict['db_filename']
create_db(dbpath=str(db_path))
#########################################################
while True:
now_utc = NowTime()
# if the seconds are not 0, sleep for 1 second and then continue
if int(now_utc.time_list[2]) != 0:
sleep(1)
continue
# only log data if the seconds are 0, resulting in data getting logged once a minute
con, cur = connect_db(dbpath=str(db_path))
logger.debug(msg=f'writing Telegram to DB on: {now_utc.time_list}, {now_utc.utc}')
# Read telegram from the sensor
telegram_lines = sensor.read(logger=logger)
# throw error if telegram_lines is empty
try:
telegram_lines[0]
except IndexError:
logger.error(msg="sensor_lines is EMPTY")
telegram = create_telegram(config_dict=config_dict,
telegram_lines=telegram_lines,
db_row_id=None,
timestamp=now_utc.utc,
db_cursor=cur,
telegram_data={},
logger=logger)
if telegram is None:
logger.error(msg=f"telegram is None on: {now_utc.time_list}, {now_utc.utc}")
else:
telegram.capture_prefixes_and_data()
telegram.prep_telegram_data4db()
telegram.insert2db()
con.commit()
cur.close()
con.close()
sensor.sensor_start_sequence(config_dict=config_dict, logger=logger, include_in_log=False)
# sleep for 2 seconds to guarantee you don't log the same data twice
# this causes issues with a computation time of 58 seconds
sleep(2)
def get_config_file():
"""
Function that gets th config file from the command line
:return: the config file's name
"""
parser = ArgumentParser(
description="Ruisdael: OTT Disdrometer data logger. Run: python main.py -c config_*.yml")
parser.add_argument(
'-c',
'--config',
required=True,
help='Path to site config file. ie. -c configs_netcdf/config_PAR_008_GV.yml')
args = parser.parse_args()
return args.config
if __name__ == '__main__':
main(get_config_file())