Commit f495fa68 authored by Olivier Kaufmann's avatar Olivier Kaufmann
Browse files

Updates logging mechanisms

Showing with 61 additions and 47 deletions
+61 -47
import logging
from paho.mqtt.client import MQTTv31
# OhmPi configuration
......@@ -21,8 +23,8 @@ CONTROL_CONFIG = {
}
# Execution logging configuration
EXEC_LOGGING_CONFIG = {
'debug_mode': True,
'logging_to_console': False,
'logging_level': logging.DEBUG,
'logging_to_console': True,
'file_name': 'ohmpi_log',
'max_bytes': 262144,
'backup_count': 30,
......@@ -32,6 +34,8 @@ EXEC_LOGGING_CONFIG = {
# Data logging configuration
DATA_LOGGING_CONFIG = {
'logging_level': logging.INFO,
'logging_to_console': True,
'file_name': 'data_log',
'logging_to_console': False,
'max_bytes': 16777216,
......
doc/source/Ohmpi_V2_00/step_n_3/a/MUX_07.jpg

4.34 MB | W: | H:

doc/source/Ohmpi_V2_00/step_n_3/a/MUX_07.jpg

3.56 MB | W: | H:

doc/source/Ohmpi_V2_00/step_n_3/a/MUX_07.jpg
doc/source/Ohmpi_V2_00/step_n_3/a/MUX_07.jpg
doc/source/Ohmpi_V2_00/step_n_3/a/MUX_07.jpg
doc/source/Ohmpi_V2_00/step_n_3/a/MUX_07.jpg
  • 2-up
  • Swipe
  • Onion skin
......@@ -107,7 +107,13 @@
<div class="col-sm-10">
<input type="file" class="form-control" id="sequence">
</div>
</div>
</div>
<div class="form-group row">
<label for="elecSpacing" class="col-sm-2 col-form-label">Electrode spacing [m]</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="elecSpacing", value="1">
</div>
</div>
</form>
</div>
<div class="modal-footer">
......
......@@ -5,6 +5,7 @@ from time import gmtime
import logging
from mqtt_logger import MQTTHandler
from compressed_sized_timed_rotating_logger import CompressedSizedTimedRotatingFileHandler
import sys
def setup_loggers(mqtt=True):
......@@ -26,13 +27,6 @@ def setup_loggers(mqtt=True):
data_log_filename = path.join(data_path, 'data_log')
data_logger = logging.getLogger('data_logger')
# Debug and logging
debug = EXEC_LOGGING_CONFIG['debug_mode']
if debug:
logging_level = logging.DEBUG
else:
logging_level = logging.INFO
# Set message logging format and level
log_format = '%(asctime)-15s | %(process)d | %(levelname)s: %(message)s'
logging_to_console = EXEC_LOGGING_CONFIG['logging_to_console']
......@@ -46,10 +40,14 @@ def setup_loggers(mqtt=True):
exec_formatter.datefmt = '%Y/%m/%d %H:%M:%S UTC'
exec_handler.setFormatter(exec_formatter)
exec_logger.addHandler(exec_handler)
exec_logger.setLevel(logging_level)
exec_logger.setLevel(MQTT_LOGGING_CONFIG['logging_level'])
if logging_to_console:
exec_logger.addHandler(logging.StreamHandler())
console_exec_handler = logging.StreamHandler(sys.stdout)
console_exec_handler.setLevel(MQTT_LOGGING_CONFIG['logging_level'])
console_exec_handler.setFormatter(exec_formatter)
exec_logger.addHandler(console_exec_handler)
if mqtt:
mqtt_settings = MQTT_LOGGING_CONFIG.copy()
[mqtt_settings.pop(i) for i in ['client_id', 'exec_topic', 'data_topic', 'soh_topic']]
......
......@@ -31,6 +31,11 @@ cmd_id = uuid.uuid4().hex
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'set_sequence', 'args': sequence})
print(f'Set sequence message: {payload} to {publisher_config["topic"]} with config {publisher_config}')
publish.single(payload=payload, **publisher_config)
cmd_id = uuid.uuid4().hex
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'rs_check'})
print(f'Run rs_check: {payload} to {publisher_config["topic"]} with config {publisher_config}')
publish.single(payload=payload, **publisher_config)
for i in range(4):
cmd_id = uuid.uuid4().hex
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'start'})
......
......@@ -66,8 +66,6 @@ class OhmPi(object):
self.run = False # flag is True when measuring
self.thread = None # contains the handle for the thread taking the measurement
# self.path = 'data/' # where to save the .csv
#self.cmd_thread = threading.Thread(
# target=self.process_commands) # thread handling commands received on tcp port
# set loggers
config_exec_logger, _, config_data_logger, _, _ = setup_loggers(mqtt=mqtt) # TODO: add SOH
......@@ -572,19 +570,17 @@ class OhmPi(object):
# measure current and voltage
current = AnalogIn(self.ads_current, ads.P0).voltage / (50 * self.r_shunt)
voltage = -AnalogIn(self.ads_voltage, ads.P0, ads.P1).voltage * 2.5
resistance = voltage / current
# compute resistance measured (= contact resistance)
resist = abs(resistance / 1000)
msg = 'Contact resistance {:s}: {:.3f} kOhm'.format(
str(quad), resist)
resistance = np.abs(voltage / current)
msg = f'Contact resistance {str(quad):s}: I: {current * 1000.:>10.3f} mA, V: {voltage * 1000.:>10.3f} mV, ' \
f'R: {resistance /1000.:>10.3f} kOhm'
print(msg)
self.exec_logger.debug(msg)
# if contact resistance = 0 -> we have a short circuit!!
if resist < 1e-5:
msg = '!!!SHORT CIRCUIT!!! {:s}: {:.3f} kOhm'.format(
str(quad), resist)
if resistance < 1e-2:
msg = f'!!!SHORT CIRCUIT!!! {str(quad):s}: {resistance / 1000.:.3f} kOhm'
self.exec_logger.warning(msg)
print(msg)
......@@ -592,7 +588,7 @@ class OhmPi(object):
self.append_and_save(export_path_rs, {
'A': quad[0],
'B': quad[1],
'RS [kOhm]': resist,
'RS [kOhm]': resistance / 1000.,
})
# close mux path and put pin back to GND
......
#!bin/bash
source ./ohmpy/bin/activate
python webserver.py
echo "# start OhmPi web interface" >> $HOME/.bashrc
echo "(cd $HOME/OhmPi; ./run.sh)" >> $HOME/.bashrc
# to automatically start the webserver on start, you need to place the
# following line just before the line with 'exit 0' in the /etc/rc.local.
(cd /home/jkl/OhmPi; bash run.sh > startup.log) &
from http.server import SimpleHTTPRequestHandler, HTTPServer
# import time
import time
import os
import json
from ohmpi import OhmPi
# import threading
import threading
import pandas as pd
import shutil
hostName = "raspberrypi.local" # works for AP-STA
# hostName = "192.168.50.1" # fixed IP in AP-STA mode
# hostName = "0.0.0.0" # for AP mode (not AP-STA)
#hostName = "raspberrypi.local" # works for AP-STA
#hostName = "192.168.50.1" # fixed IP in AP-STA mode
hostName = "0.0.0.0" # for AP mode (not AP-STA)
serverPort = 8080
# https://gist.github.com/MichaelCurrie/19394abc19abd0de4473b595c0e37a3a
with open('ohmpi_param.json') as json_file:
settings = json.load(json_file)
pardict = json.load(json_file)
ohmpi = OhmPi(settings, sequence='breadboard.txt')
# ohmpi = OhmPi(settings, sequence='dd16s0no8.txt')
ohmpi = OhmPi(pardict, sequence='dd.txt')
#ohmpi = OhmPi(pardict, sequence='dd16s0no8.txt')
class MyServer(SimpleHTTPRequestHandler):
# because we use SimpleHTTPRequestHandler, we do not need to implement
# the do_GET() method (if we use the BaseHTTPRequestHandler, we would need to)
# def do_GET(self):
# # normal get for wepages (not so secure!)
# print(self.command)
......@@ -36,7 +35,7 @@ class MyServer(SimpleHTTPRequestHandler):
# self.end_headers()
# with open(os.path.join('.', self.path[1:]), 'r') as f:
# self.wfile.write(bytes(f.read(), "utf-8"))
def do_POST(self):
global ohmpiThread, status, run
dic = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
......@@ -50,9 +49,9 @@ class MyServer(SimpleHTTPRequestHandler):
fnames = [fname for fname in os.listdir('data/') if fname[-4:] == '.csv']
ddic = {}
for fname in fnames:
if (fname.replace('.csv', '') not in dic['surveyNames']
and fname != 'readme.txt'
and '_rs' not in fname):
if (fname.replace('.csv', '') not in dic['surveyNames']
and fname != 'readme.txt'
and '_rs' not in fname):
df = pd.read_csv('data/' + fname)
ddic[fname.replace('.csv', '')] = {
'a': df['A'].tolist(),
......@@ -68,17 +67,17 @@ class MyServer(SimpleHTTPRequestHandler):
elif dic['command'] == 'setConfig':
ohmpi.stop()
cdic = dic['config']
ohmpi.settings['nb_electrodes'] = int(cdic['nbElectrodes'])
ohmpi.settings['injection_duration'] = float(cdic['injectionDuration'])
ohmpi.settings['nbr_meas'] = int(cdic['nbMeasurements'])
ohmpi.settings['nb_stack'] = int(cdic['nbStack'])
ohmpi.settings['sequence_delay'] = int(cdic['sequenceDelay'])
ohmpi.pardict['nb_electrodes'] = int(cdic['nbElectrodes'])
ohmpi.pardict['injection_duration'] = float(cdic['injectionDuration'])
ohmpi.pardict['nbr_meas'] = int(cdic['nbMeasurements'])
ohmpi.pardict['nb_stack'] = int(cdic['nbStack'])
ohmpi.pardict['sequence_delay'] = int(cdic['sequenceDelay'])
if cdic['sequence'] != '':
with open('sequence.txt', 'w') as f:
f.write(cdic['sequence'])
ohmpi.read_quad('sequence.txt')
print('new sequence set.')
print('setConfig', ohmpi.settings)
print('setConfig', ohmpi.pardict)
elif dic['command'] == 'invert':
pass
elif dic['command'] == 'getResults':
......@@ -103,7 +102,7 @@ class MyServer(SimpleHTTPRequestHandler):
else:
# command not found
rdic['response'] = 'command not found'
rdic['status'] = ohmpi.status
self.send_response(200)
self.send_header('Content-Type', 'text/json')
......@@ -111,7 +110,7 @@ class MyServer(SimpleHTTPRequestHandler):
self.wfile.write(bytes(json.dumps(rdic), 'utf8'))
if __name__ == "__main__":
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
......
Supports Markdown
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