From fc1e9e2218cd353d6899453f78b83a4bca2ce37d Mon Sep 17 00:00:00 2001
From: Guillaume <sagitta1618@gmail.com>
Date: Tue, 10 May 2022 18:14:18 +0200
Subject: [PATCH] add threading for TCP listening

---
 config.py       |  5 ++--
 data/readme.txt |  1 -
 ohmpi.py        |  7 +++--
 run.sh          |  0
 webserver.py    | 76 ++++++++++++++++++++++++++++++++++++++++++-------
 5 files changed, 72 insertions(+), 17 deletions(-)
 delete mode 100644 data/readme.txt
 mode change 100644 => 100755 run.sh

diff --git a/config.py b/config.py
index 00589d3e..df197825 100644
--- a/config.py
+++ b/config.py
@@ -24,7 +24,7 @@ CONTROL_CONFIG = {
 # Execution logging configuration
 EXEC_LOGGING_CONFIG = {
     'logging_level': logging.DEBUG,
-    'logging_to_console': False,
+    'logging_to_console': True,
     'file_name': 'ohmpi_log',
     'max_bytes': 262144,
     'backup_count': 30,
@@ -35,9 +35,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,
+    'file_name': 'data_log',
     'max_bytes': 16777216,
     'backup_count': 1024,
     'when': 'd',
diff --git a/data/readme.txt b/data/readme.txt
deleted file mode 100644
index f311a0ee..00000000
--- a/data/readme.txt
+++ /dev/null
@@ -1 +0,0 @@
-In this folder will be logged all measurements done with the OhmPi as .csv.
diff --git a/ohmpi.py b/ohmpi.py
index dd652fb1..b8eeaee6 100644
--- a/ohmpi.py
+++ b/ohmpi.py
@@ -116,8 +116,9 @@ 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_thread.start()
-        self.process_commands()
+        self.cmd_thread = threading.Thread(target=self.process_commands)
+        self.cmd_thread.start()
+        #self.process_commands()
 
     def _update_acquisition_settings(self, config):
         """Update acquisition settings from a json file or dictionary.
@@ -681,6 +682,7 @@ class OhmPi(object):
                             self.rs_check()
                             status = True
                         except Exception as e:
+                            print('error====', e)
                             self.exec_logger.warning(f'Unable to run rs-check: {e}')
                     else:
                         self.exec_logger.warning(f'Unkown command {cmd} - cmd_id: {cmd_id}')
@@ -770,6 +772,7 @@ class OhmPi(object):
         if self.thread is not None:
             self.thread.join()
         self.exec_logger.debug(f'Status: {self.status}')
+    
 
 
 VERSION = '2.1.0'
diff --git a/run.sh b/run.sh
old mode 100644
new mode 100755
diff --git a/webserver.py b/webserver.py
index dabf2130..d925f579 100644
--- a/webserver.py
+++ b/webserver.py
@@ -2,10 +2,14 @@ from http.server import SimpleHTTPRequestHandler, HTTPServer
 import time
 import os
 import json
-from ohmpi import OhmPi
+import uuid
+#from ohmpi import OhmPi
+from config import CONTROL_CONFIG
+from termcolor import colored
 import threading
 import pandas as pd
 import shutil
+import zmq # to write on TCP
 
 #hostName = "raspberrypi.local" # works for AP-STA
 #hostName = "192.168.50.1"  # fixed IP in AP-STA mode
@@ -17,11 +21,16 @@ serverPort = 8080
 with open('ohmpi_param.json') as json_file:
     pardict = json.load(json_file)
 
-ohmpi = OhmPi(pardict, sequence='dd.txt')
+#ohmpi = OhmPi(pardict, sequence='dd.txt')
 #ohmpi = OhmPi(pardict, sequence='dd16s0no8.txt')
 
+tcp_port = CONTROL_CONFIG['tcp_port']
+context = zmq.Context()
+socket = context.socket(zmq.REQ)
+socket.connect(f'tcp://localhost:{CONTROL_CONFIG["tcp_port"]}')
+print(colored(f'Sending commands and listenning on tcp port {tcp_port}.'))
 
-class MyServer(SimpleHTTPRequestHandler):
+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)
    
@@ -37,13 +46,24 @@ class MyServer(SimpleHTTPRequestHandler):
     #         self.wfile.write(bytes(f.read(), "utf-8"))
         
     def do_POST(self):
-        global ohmpiThread, status, run
+        cmd_id = uuid.uuid4().hex
+        global socket
+
+        #global ohmpiThread, status, run
         dic = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
-        rdic = {}
+        rdic = {} # response dictionnary
         if dic['command'] == 'start':
-            ohmpi.measure()
+            #ohmpi.measure()
+            socket.send_string(json.dumps({
+                'cmd_id': cmd_id,
+                'command': 'start'
+            }))
         elif dic['command'] == 'stop':
-            ohmpi.stop()
+            #ohmpi.stop()
+            socket.send_string(json.dumps({
+                'cmd_id': cmd_id,
+                'command': 'stop'
+            }))
         elif dic['command'] == 'getData':
             # get all .csv file in data folder
             fnames = [fname for fname in os.listdir('data/') if fname[-4:] == '.csv']
@@ -64,9 +84,16 @@ class MyServer(SimpleHTTPRequestHandler):
         elif dic['command'] == 'removeData':
             shutil.rmtree('data')
             os.mkdir('data')
-        elif dic['command'] == 'setConfig':
-            ohmpi.stop()
+        elif dic['command'] == 'update_settings':
+            #ohmpi.stop()
+            socket.send_string(json.dumps({
+                'cmd_id': cmd_id,
+                'cmd': 'update_settings',
+                'args': dic['config']
+            }))
             cdic = dic['config']
+            
+            """
             ohmpi.pardict['nb_electrodes'] = int(cdic['nbElectrodes'])
             ohmpi.pardict['injection_duration'] = float(cdic['injectionDuration'])
             ohmpi.pardict['nbr_meas'] = int(cdic['nbMeasurements'])
@@ -78,12 +105,17 @@ class MyServer(SimpleHTTPRequestHandler):
                 ohmpi.read_quad('sequence.txt')
                 print('new sequence set.')
             print('setConfig', ohmpi.pardict)
+            """
         elif dic['command'] == 'invert':
             pass
         elif dic['command'] == 'getResults':
             pass
         elif dic['command'] == 'rsCheck':
-            ohmpi.rs_check()
+            #ohmpi.rs_check()
+            socket.send_string(json.dumps({
+                'cmd_id': cmd_id,
+                'cmd': 'rs_check'
+            }))
             fnames = sorted([fname for fname in os.listdir('data/') if fname[-7:] == '_rs.csv'])
             df = pd.read_csv('data/' + fnames[-1])
             ddic = {
@@ -103,7 +135,29 @@ class MyServer(SimpleHTTPRequestHandler):
             # command not found
             rdic['response'] = 'command not found'
         
-        rdic['status'] = ohmpi.status
+        #rdic['status'] = ohmpi.status
+        rdic['status'] = 'unknown' # socket_out.
+        # wait for reply
+        
+        message = socket.recv()
+        print('+++////', message)
+        rdic['data'] = message
+        """
+        while False:
+            message = socket.recv()
+            print(f'Received command: {message}')
+            e = None
+            try:
+                decoded_message = json.loads(message.decode('utf-8'))
+                cmd = decoded_message.pop('cmd', None)
+                args = decoded_message.pop('args', None)
+                status = False
+                e = None
+                if cmd is not None and cmd_id is decoded_message.pop('cmd_id', None):
+                    print('reply=', decoded_message)
+            except Exception as e:
+                print(f'Unable to decode command {message}: {e}')
+            """
         self.send_response(200)
         self.send_header('Content-Type', 'text/json')
         self.end_headers()
-- 
GitLab