From 6a7d1d756139ca2195e0e5fade4168156efff5ae Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Tue, 10 May 2022 18:54:24 +0200
Subject: [PATCH] Adds quit method to stop listening to tcp port and exit
 OhmPi.py

---
 config.py           |  3 ++-
 mqtt_controller.py  |  2 +-
 ohmpi.py            | 16 +++++++++++++---
 ohmpi_settings.json |  2 +-
 4 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/config.py b/config.py
index c0ee048c..f92ef7e6 100644
--- a/config.py
+++ b/config.py
@@ -15,7 +15,8 @@ OHMPI_CONFIG = {
     'version': 2,
     'max_elec': 64,
     'board_address': {'A': 0x70, 'B': 0x71, 'M': 0x72, 'N': 0x73},  # def. {'A': 0x76, 'B': 0x71, 'M': 0x74, 'N': 0x70}
-}
+    'settings': 'ohmpi_settings.json'
+}  # TODO: add a dictionary with INA models and associated gain values
 
 CONTROL_CONFIG = {
     'tcp_port': 5555,
diff --git a/mqtt_controller.py b/mqtt_controller.py
index df30e2a0..93abf662 100644
--- a/mqtt_controller.py
+++ b/mqtt_controller.py
@@ -15,7 +15,7 @@ publisher_config['topic'] = MQTT_CONTROL_CONFIG['ctrl_topic']
 publisher_config.pop('ctrl_topic')
 settings = {
             'injection_duration': 0.2,
-            'nbr_meas': 100,
+            'nbr_meas': 1,
             'sequence_delay': 1,
             'nb_stack': 1,
             'export_path': 'data/measurement.csv'
diff --git a/ohmpi.py b/ohmpi.py
index 64a8f465..a07fc22c 100644
--- a/ohmpi.py
+++ b/ohmpi.py
@@ -20,7 +20,7 @@ from datetime import datetime
 from termcolor import colored
 import threading
 from logging_setup import setup_loggers
-from config import CONTROL_CONFIG
+from config import CONTROL_CONFIG, OHMPI_CONFIG
 from subprocess import Popen
 
 # finish import (done only when class is instantiated as some libs are only available on arm64 platform)
@@ -118,6 +118,7 @@ class OhmPi(object):
             self.ads_voltage = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=0x49)
 
         # Starts the command processing thread
+        self.cmd_listen = True
         self.cmd_thread = threading.Thread(target=self.process_commands)
         self.cmd_thread.start()
 
@@ -650,7 +651,7 @@ class OhmPi(object):
         print(colored(f'Listening to commands on tcp port {tcp_port}.'
                       f' Make sure your client interface is running and bound to this port...', 'blue'))
         self.exec_logger.debug(f'Start listening for commands on port {tcp_port}')
-        while True:
+        while self.cmd_listen:
             message = socket.recv()
             self.exec_logger.debug(f'Received command: {message}')
             e = None
@@ -780,6 +781,15 @@ class OhmPi(object):
         if self.thread is not None:
             self.thread.join()
         self.exec_logger.debug(f'Status: {self.status}')
+
+    def quit(self):
+        """Quit OhmPi.
+        """
+        self.cmd_listen = False
+        if self.cmd_thread is not None:
+            self.cmd_thread.join()
+        self.exec_logger.debug(f'Stopped listening to tcp port.')
+        exit()
     
 
 
@@ -812,6 +822,6 @@ if __name__ == "__main__":
     # start interface
     Popen(['python', CONTROL_CONFIG['interface']])
 
-    ohmpi = OhmPi(settings='ohmpi_settings.json')
+    ohmpi = OhmPi(settings=OHMPI_CONFIG['settings'])
     
         
diff --git a/ohmpi_settings.json b/ohmpi_settings.json
index eea47405..273f2290 100644
--- a/ohmpi_settings.json
+++ b/ohmpi_settings.json
@@ -1,7 +1,7 @@
 {
     "nb_electrodes": 64,
     "injection_duration":0.2,
-    "nbr_meas": 100,
+    "nbr_meas": 1,
     "sequence_delay": 1,
     "nb_stack": 1,
     "export_path": "data/measurement.csv"
-- 
GitLab