Commit 48feba9b authored by Nelso Jost's avatar Nelso Jost

FIX: improving settings usage

parent ec925487
import configparser
import sys
DEFAULT_SETTINGS =\
"""\
[server]
; ID of the board to which data will be uploaded to
;
BOARD_ID =
; authentication token for the board's user
;
USER_HASH =
; wep app base URL
;
URL = http://dados.cta.if.ufrgs.br/emm
[logger]
; comma-separated list of sensor nicknames that will be read
; the order reflect columns of the local DATALOG csv file
;
SENSORS = DHT22_TEMP, DHT22_AH, BMP085_PRESSURE, LDR
; time between readings attempts (cycles of the logger execution)
; format: hours:minutes:seconds
;
INTERVAL = 0:5:0
; True for try reading RTC_DS1307 or False for use the system time
; if the RTC reading fails, the system time will be used instead
;
USE_RTC_DS1307 = False
; expected time stamp of the RTC (see Python's datetime module documentation)
;
RTC_DATETIME_FORMAT = %Y-%m-%d %H:%M:%S
[datalog]
; CSV delimiter (sugestions: ',' or ';' or '\t')
;
CSV_SEP = '\t'
; format of the datetime column (see Python docs on datetime module)
;
DATETIME_FORMAT = %Y-%m-%d-%H-%M-%S
[arduino]
; USB port that have the board plugged in
; keep blank for automatic search on /dev/ttyACM* and /dev/ttyUSB*
;
SERIAL_PORT =
"""
def make_path_here(filename):
''' Append filename to the current __file__ path. '''
return os.path.join(os.path.abspath(os.path.dirname(__file__)), filename)
class Config(dict):
SETTINGS_FILENAME = make_path_here('../../settings.ini')
URL_POST_RAWSENSORDATA = '{base}api/post/rawsensordata/{bid}'
def __init__(self):
super().__init__()
self.load_settings()
def ask_restore_settings_file(self):
a = input("\nRestore default file now and overwrite all
"current values? [y/N] ")
if 'y' in a.lowercase()
try:
with open(self.SETTINGS_FILENAME, 'w') as f:
f.write(DEFAULT_SETTINGS)
print("Done! Restart the application!")
sys.exit(0)
except:
print("Unable to write on file\n {}\n\nTry to run this "
"program from another location!".format(
self.SETTINGS_FILENAME))
sys.exit(1)
def get(self, section, key, validate, ask_restore=True):
try:
r = self.config[section]
except:
print("Config file\n {}\n\nis missing the section [{}]!"
.format(self.SETTINGS_FILENAME, section))
if ask_restore:
self.ask_restore_settings_file()
try:
r = self.config[section][key]
print("On the config file\n {}\n\nsection [{}] is missing the "
"key '{}'!")
if ask_restore:
self.ask_restore_settings_file()
try:
r = validate(self.config[section][key])
except:
print("On the config file\n {}\n\ninvalid value for '{}' for "
"key the [{}][{}]!".format(""))
if ask_restore:
self.ask_restore_settings_file()
def load_settings(self, filename):
'''
Load the configuration file onto the self.CFG attribute.
Some keys will be tested and filenames will be normalized.
'''
self.config = configparser.ConfigParser()
try:
self.config.read(self.SETTINGS_FILENAME)
except:
print("Unable to open configuration file at\n {}".format(
self.SETTINGS_FILENAME))
self.ask_restore_settings_file()
self.URL_POST_RAWSENSORDATA = self.URL_POST_RAWSENSORDATA.format(
base=self.CFG('server', 'URL',
lambda x: x if x.endswith('/') else x + '/'),
bid=self.CFG('server', 'BOARD_ID', int)
self.LOGGER_INTERVAL_SECONDS =\
self.CFG['LOGGER']['INTERVAL']['seconds']\
+ 60 * self.CFG['LOGGER']['INTERVAL']['minutes']\
+ 3600 * self.CFG['LOGGER']['INTERVAL']['hours']
self.DATALOG_CSV_SEP = bytes(self.CFG['DATALOG']['CSV_SEP'],
'utf8').decode('unicode_escape')
......@@ -5,6 +5,8 @@
#-------------------------------------------------------------------------------
from __future__ import print_function, division
from .config import Config
from datetime import datetime
from pprint import pprint
......@@ -17,13 +19,10 @@ import sys
import time
import json
from pykwalify.core import Core
def make_current_file_path(filename):
def make_path_here(filename):
''' Append filename to the current __file__ path. '''
return os.path.join(os.path.abspath(os.path.dirname(__file__)), filename)
class RTCDateTime:
RTC_DT_FMT = '%Y-%m-%d %H:%M:%S'
__qualname__ = "RTCDateTime fmt='{}'".format(RTC_DT_FMT)
......@@ -34,7 +33,6 @@ class RTCDateTime:
def __str__(self):
return self.dt.strftime('%Y%m%d%H%M%S')
class Meteorologger:
'''
Provides a series of mechanisms to collect sensor data from the board
......@@ -45,9 +43,6 @@ class Meteorologger:
Call the run() method to start the logging process.
'''
SETTINGS_SCHEMA_FILENAME = make_current_file_path('settings_schema.yaml')
SETTINGS_FILENAME = make_current_file_path('../../settings.yaml')
DATALOG_DIR = 'data/'
EXECUTION_LOG_PATH = 'logger/logs/'
OUTGOING_BASENAME = 'outgoing.json'
......@@ -67,7 +62,7 @@ class Meteorologger:
def __init__(self, verbose=False):
self.verbose = verbose
self.load_settings()
self.config = Config()
def _decode_bytes(self, raw_bytes, encoding='ascii'):
result = None
......@@ -78,35 +73,6 @@ class Meteorologger:
print("Invalid bytes!")
return result
def load_settings(self):
'''
Load the configuration file onto the self.CFG attribute.
Some keys will be tested and filenames will be normalized.
'''
c = Core(source_file=self.SETTINGS_FILENAME,
schema_files=[self.SETTINGS_SCHEMA_FILENAME])
try:
self.CFG = c.validate(raise_exception=True)
except Exception as e:
print('-'*60)
print("There is something wrong with the configuration file '{}'"
"\nKeep in mind that it uses YAML syntax which require "
"proper identation.\n".format(self.SETTINGS_FILENAME))
print(e)
print("\nPlease fix it up or regenerate it.")
sys.exit(1)
self.READING_INTERVAL_SECONDS =\
self.CFG['LOGGER']['INTERVAL']['seconds']\
+ 60 * self.CFG['LOGGER']['INTERVAL']['minutes']\
+ 3600 * self.CFG['LOGGER']['INTERVAL']['hours']\
+ 86400 * self.CFG['LOGGER']['INTERVAL']['days']
self.DATALOG_CSV_SEP = bytes(self.CFG['DATALOG']['CSV_SEP'],
'utf8').decode('unicode_escape')
def create_json(self, raw_line):
'''
Given the raw serial line response (CSV string), builds and returns
......@@ -284,7 +250,7 @@ class Meteorologger:
if not isinstance(port_index, int):
port_index = 0
serial_port = self.CFG['ARDUINO']['SERIAL_PORTS'][port_index]
# if present, the board will be reseted
......
......@@ -5,4 +5,4 @@ if __name__ == '__main__':
if '--syncrtc' in sys.argv:
Meteorologger().sync_rtc()
else:
Meteorologger(verbose = False if '-s' in sys.argv else True).run()
Meteorologger(verbose = False if '-s' in sys.argv else True)#.run()
[server]
; ID of the board to which data will be uploaded to
;
BOARD_ID = 2
; authentication token for the board's user
;
USER_HASH = pbkdf2:sha1:1000$jVxGFjnU$c5f4a675969d620185660b10968c8027f05e01e7
; wep app base URL
;
URL = http://localhost:5000/emm
[logger]
; comma-separated list of sensor nicknames that will be read
; the order reflect columns of the local DATALOG csv file
;
SENSORS = DHT22_TEMP, DHT22_AH, BMP085_PRESSURE, LDR
; time between readings attempts (cycles of the logger execution)
; format: hours:minutes:seconds
;
INTERVAL = 0:5:0
; True for try reading RTC_DS1307 or False for use the system time
; if the RTC reading fails, the system time will be used instead
;
USE_RTC_DS1307 = False
; expected time stamp of the RTC (see Python's datetime module documentation)
;
RTC_DATETIME_FORMAT = %Y-%m-%d %H:%M:%S
[datalog]
; CSV delimiter (sugestions: ',' or ';' or '\t')
;
CSV_SEP = '\t'
; format of the datetime column (see Python docs on datetime module)
;
DATETIME_FORMAT = %Y-%m-%d-%H-%M-%S
[arduino]
; USB port that have the board plugged in
; keep blank for automatic search on /dev/ttyACM* and /dev/ttyUSB*
;
SERIAL_PORT =
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment