Commit ce2015e1 authored by Nelso Jost's avatar Nelso Jost

FIX: better genaral usage from single Makefile

parent 995c2c8b
.venv/ .venv/
ino/src .ino
ino/lib
*.pyc *.pyc
*.swp *.swp
*.csv *.csv
......
PY := 2 PYVER := 3
VENV := .venv VENV := .venv
all: help all: help
...@@ -6,31 +6,45 @@ all: help ...@@ -6,31 +6,45 @@ all: help
help: help:
@ echo "USAGE" @ echo "USAGE"
@ echo "" @ echo ""
@ echo " make apt-install -- Uses apt to install system tools" @ echo " make apt-install -- Uses Debian's apt to install required system tools"
@ echo "" @ echo ""
@ echo " make setup -- Install Python requirements" @ echo " make setup -- Install Python requirements"
@ echo "" @ echo ""
@ echo " make build -- Compile the sketch" @ echo " make build -- Compile the sketch"
@ echo "" @ echo ""
@ echo " make upload -- Send the compiled sketch to the board" @ echo " make upload -- Send the compiled sketch to the board"
@ echo "" @ echo ""
@ echo " make bus -- build & upload & start serial picocom" @ echo " make serial -- Enter IPython session with serial opened"
@ echo "" @ echo ""
@ echo " make serial -- Starts a picocom serial communication" @ echo " make bu -- build & upload"
@ echo " make bus -- build & upload & serial"
@ echo "" @ echo ""
@ echo " make list-models -- List possible Arduino models for ino.ini" @ echo " make sync-rtc -- Synchronizes the board's clock with the system's"
@ echo ""
@ echo " make run-logger -- Starts the logger. Results apper on the screen."
@ echo ""
@ echo " make deploy-logger -- Activate logger daemon. Results apper on logs/execution.log"
@ echo ""
@ echo " make undeploy-logger -- Deactivate logger daemon."
@ echo ""
@ echo " make tail-logger -- Exibits and follow daemon execution log."
apt-install: apt-install:
sudo apt-get install python-pip picocom sudo apt-get install python python3 python3-pip supervisor
sudo pip${PYVER} install virtualenv
install-ino:
sudo apt-get install python-pip
sudo pip2 install ino
setup: clean-venv setup: clean-venv
@ echo "-------------------------------------------------------" @ echo "-------------------------------------------------------"
virtualenv -v --python='python${PYVER}' ${VENV} virtualenv -v --python='python${PYVER}' ${VENV}
@ echo "Virtualenv with 'python${PYVER}' interpreter was created at ${VENV}" @ echo "Virtualenv with 'python${PYVER}' interpreter was created at ${VENV}"
@ echo "-------------------------------------------------------" @ echo "-------------------------------------------------------"
${VENV}/bin/pip${PYVER} install --upgrade pip ${VENV}/bin/pip install --upgrade pip
@ echo "-------------------------------------------------------" @ echo "-------------------------------------------------------"
${VENV}/bin/pip${PYVER} install -r requirements.pip ${VENV}/bin/pip install -r logger/app/requirements.pip
@ echo "-------------------------------------------------------" @ echo "-------------------------------------------------------"
@ echo "Virtualenv is ready at ${VENV}!" @ echo "Virtualenv is ready at ${VENV}!"
@ echo " " @ echo " "
...@@ -44,18 +58,34 @@ clean: ...@@ -44,18 +58,34 @@ clean:
rm -rf .build src/* rm -rf .build src/*
build: build:
mkdir -p src/ lib/ mkdir -p .ino
cp -rf ../meteorolog/. src/ mkdir -p .ino/src/ .ino/lib/
${VENV}/bin/ino build cp -rf meteorolog/. .ino/src/
cp logger/ino.ini .ino/
cd .ino/ && ino build
upload: upload:
${VENV}/bin/ino upload cd .ino/ && ino upload
serial:
${VENV}/bin/ipython3 -i logger/init_serial.py
bu: build upload
bus: build upload serial bus: build upload serial
serial: sync-rtc:
${VENV}/bin/ino serial -- -c -r -l ${VENV}/bin/python${PYVER} run.py --syncrtc
run-logger:
${VENV}/bin/python logger/run.py
deploy-logger:
mkdir -p logger/logs
sudo ${VENV}/bin/python${PYVER} logger/deploy.py
list-models: undeploy-logger:
${VENV}/bin/ino list-models sudo ${VENV}/bin/python${PYVER} logger/deploy.py -u
tail-logger:
tail -F logger/logs/execution.log
PY := 3
VENV := .venv
all: help
help:
@ echo "USAGE:"
@ echo ""
@ echo " make apt-install -- Uses apt to install required system tools"
@ echo " (for Debian-based systems only)"
@ echo ""
@ echo " make setup -- Create a local Python virtual environment"
@ echo " that will hold requirements.pip modules"
@ echo ""
@ echo " make run -- Executes the logger"
@ echo ""
@ echo " make deploy -- Register meteorologger proccess on the"
@ echo " supervisor daemon tool (requires root)"
@ echo ""
@ echo " make undeploy -- Unregister meteorologger proccess on the"
@ echo " supervisor daemon tool (requires root)"
@ echo ""
@ echo " make tail-exec -- Follow the logger's execution.log"
@ echo ""
@ echo " make sync-rtc -- Send the current system time to the board"
apt-install:
sudo apt-get install python python3-pip supervisor
sudo pip${PYVER} install virtualenv
setup: clean-venv
@ echo "-------------------------------------------------------"
virtualenv -v --python='python${PYVER}' ${VENV}
@ echo "Virtualenv with 'python${PYVER}' interpreter was created at ${VENV}"
@ echo "-------------------------------------------------------"
${VENV}/bin/pip${PYVER} install --upgrade pip
@ echo "-------------------------------------------------------"
${VENV}/bin/pip${PYVER} install -r requirements.pip
@ echo "-------------------------------------------------------"
@ echo "Virtualenv is ready at ${VENV}!"
@ echo " "
@ echo "TOTAL SIZE: "
@ du -sh ${VENV}
clean-venv:
rm -rf ${VENV}
run:
${VENV}/bin/python${PYVER} run.py
deploy:
sudo ${VENV}/bin/python${PYVER} app/deploy.py
undeploy:
sudo ${VENV}/bin/python${PYVER} app/deploy.py -u
tail-exec:
tail -F logs/execution.log
sync-rtc:
${VENV}/bin/python${PYVER} run.py --syncrtc
...@@ -47,10 +47,10 @@ class Meteorologger: ...@@ -47,10 +47,10 @@ class Meteorologger:
Call the run() method to start the logging process. Call the run() method to start the logging process.
''' '''
SETTINGS_SCHEMA_FILENAME = 'app/settings_schema.yaml' SETTINGS_SCHEMA_FILENAME = 'logger/app/settings_schema.yaml'
SETTINGS_FILENAME = 'settings.yaml' SETTINGS_FILENAME = 'settings.yaml'
EXECUTION_LOG_FILENAME = 'logs/execution.log' EXECUTION_LOG_FILENAME = 'logger/logs/execution.log'
OUTGOING_BASENAME = 'outgoing.json' OUTGOING_BASENAME = 'outgoing.json'
FILE_TIMESTAMP_FORMAT = '%Y-%m-%d-%H-%M-%S' FILE_TIMESTAMP_FORMAT = '%Y-%m-%d-%H-%M-%S'
...@@ -121,10 +121,6 @@ class Meteorologger: ...@@ -121,10 +121,6 @@ class Meteorologger:
if using_rtc: if using_rtc:
d['datetime']['source'] = rtc d['datetime']['source'] = rtc
rtc_datetime_fmt = self.CFG['LOGGER']['RTC_DATETIME_FORMAT'] rtc_datetime_fmt = self.CFG['LOGGER']['RTC_DATETIME_FORMAT']
else:
d['datetime']['source'] = 'logger'
d['datetime']['value'] = datetime.now().strftime(
d['datetime']['format'])
for i, v in enumerate(raw_line.split(self.SERIAL_CSV_SEP)): for i, v in enumerate(raw_line.split(self.SERIAL_CSV_SEP)):
...@@ -132,7 +128,7 @@ class Meteorologger: ...@@ -132,7 +128,7 @@ class Meteorologger:
type_name = self.CFG['SENSORS_AVAILABLE'][nickname]['data_format'] type_name = self.CFG['SENSORS_AVAILABLE'][nickname]['data_format']
if type_name == 'datetime': if type_name == 'datetime':
if using_rtc: if using_rtc and not v.strip().startswith('<'):
d['datetime']['value'] = datetime.strptime( d['datetime']['value'] = datetime.strptime(
v, rtc_datetime_fmt).strftime(d['datetime']['format']) v, rtc_datetime_fmt).strftime(d['datetime']['format'])
continue continue
...@@ -146,6 +142,11 @@ class Meteorologger: ...@@ -146,6 +142,11 @@ class Meteorologger:
d['sensors'][nickname] = v d['sensors'][nickname] = v
if not 'value' in d['datetime']:
d['datetime']['source'] = 'logger'
d['datetime']['value'] = datetime.now().strftime(
d['datetime']['format'])
logging.info("Validated JSON: {}".format(d)) logging.info("Validated JSON: {}".format(d))
return d return d
......
import jinja2
import os import os
import subprocess import subprocess
import sys import sys
...@@ -12,22 +11,27 @@ SUPERVISOR_CONFIG_FILENAME = '/etc/supervisor/conf.d/{}.conf'\ ...@@ -12,22 +11,27 @@ SUPERVISOR_CONFIG_FILENAME = '/etc/supervisor/conf.d/{}.conf'\
PID_FILENAME = 'logs/pid_{}'.format(PROCESS_NAME) PID_FILENAME = 'logs/pid_{}'.format(PROCESS_NAME)
def deploy_supervisor(): TEMPLATE_SUPERVISOR_CONF =\
"""\
with open('app/supervisor.conf') as f_temp: [program:{PROCESS_NAME}]
template = jinja2.Template(f_temp.read()) command={BASE_DIR}/.venv/bin/python3 {BASE_DIR}/logger/run.py -s
config_file_str = template.render(base_dir=BASE_DIR, directory={BASE_DIR}
process_name=PROCESS_NAME) user=root
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile={BASE_DIR}/logger/logs/stdout.log
logfile={BASE_DIR}/logger/logs/supervisor-{PROCESS_NAME}.log
""".format(**globals())
print('\nRegistering supervisor config at \n {}' def deploy_supervisor():
.format(SUPERVISOR_CONFIG_FILENAME))
print('='*60) print('\nRegistering supervisor config at \n {}\n'
print(config_file_str) .format(SUPERVISOR_CONFIG_FILENAME) + '='*60 + '\n'
print('='*60) + TEMPLATE_SUPERVISOR_CONF + '='*60)
with open(SUPERVISOR_CONFIG_FILENAME, 'w') as f: with open(SUPERVISOR_CONFIG_FILENAME, 'w') as f:
f.write(config_file_str + '\n') f.write(TEMPLATE_SUPERVISOR_CONF + '\n')
print('\nRestarting supervisor..') print('\nRestarting supervisor..')
proc = subprocess.Popen('supervisorctl update', shell=True) proc = subprocess.Popen('supervisorctl update', shell=True)
......
import serial
import time
ser = serial.Serial(port='/dev/ttyACM0',
baudrate=9600)
def send(command_str):
ser.write(bytes(command_str, encoding='utf-8'))
time.sleep(1.5)
try:
raw = ser.readline()
return raw.decode('ascii').strip()
except:
print("Unable do decode raw line read:\n{}".format(raw))
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