From da735ed34ae3da36790fbcd643f2a7d6d87d384c Mon Sep 17 00:00:00 2001
From: KwanghoKim <kh.kim@sdt.inc>
Date: Thu, 21 Dec 2023 13:52:04 +0900
Subject: [PATCH] =?UTF-8?q?1.=20MFC=5FRead=5FFlow:=20=EC=93=B0=EB=A0=88?=
 =?UTF-8?q?=EA=B8=B0=EA=B0=92=20=EB=8C=80=EC=B2=98=20=EB=A1=9C=EC=A7=81=20?=
 =?UTF-8?q?=EC=B6=94=EA=B0=80=202.=20Temp=5FHum=5FPress=5FRead:=20?=
 =?UTF-8?q?=EC=9E=A5=EB=B9=84=20=EC=9C=84=EC=B9=98=EC=97=90=20=EB=94=B0?=
 =?UTF-8?q?=EB=A5=B8=20=EC=9E=A5=EB=B9=84=20ID=EA=B0=92=20=EB=B3=80?=
 =?UTF-8?q?=EA=B2=BD=20=EB=B0=8F=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EB=B3=80?=
 =?UTF-8?q?=EA=B2=BD,=20=EC=93=B0=EB=A0=88=EA=B8=B0=EA=B0=92=20=EB=8C=80?=
 =?UTF-8?q?=EC=B2=98=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80=203.=20time?=
 =?UTF-8?q?stamp=20=EA=B0=92=20milisecond=20=EA=B0=92=EC=9C=BC=EB=A1=9C=20?=
 =?UTF-8?q?=ED=91=9C=ED=98=84=EB=90=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?=
 =?UTF-8?q?=EA=B2=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 cnt_collector_232.py | 106 +++++++++++++++++++++++++------------------
 1 file changed, 63 insertions(+), 43 deletions(-)

diff --git a/cnt_collector_232.py b/cnt_collector_232.py
index 9c3dec9..da3122c 100644
--- a/cnt_collector_232.py
+++ b/cnt_collector_232.py
@@ -46,13 +46,20 @@ def MFC_Read_Flow(client):
     ch1 = client.readline()
     # print(ch1)
     ch1 = str(ch1, 'utf-8')
-    # print(ch1)
+    # print(f'ch1: {ch1}')
+    ch1 = ch1.replace('\r', '')
+
     if ch1 != '':        
-        ch1 = float(ch1[:-1])
+        if len(ch1) < 8:
+            ch1 = float(ch1)
+        else:
+            ch1 = None
     else:
         ch1 = None
     time.sleep(0.02)
 
+    client.reset_input_buffer()
+
     command_hex = '235246320D' # '#RF2'
     command_bytes = bytes.fromhex(command_hex)
 
@@ -61,17 +68,23 @@ def MFC_Read_Flow(client):
     ch2 = client.readline()
     # print(ch2)
     ch2 = str(ch2, 'utf-8')
-    # print(ch2)
+    # print(f'ch2: {ch2}')
+    ch2 = ch2.replace('\r', '')
     if ch2 != '':
-        ch2 = float(ch2[:-1])
+        if len(ch2) < 8:
+            ch2 = float(ch2)
+        else:
+            ch2 = None
     else:
         ch2 = None
     
     time.sleep(0.02)
 
+    client.reset_input_buffer()
+
     data = {
         "assetCode": info['mqtt']['assetCode'],
-        "timestamp": int(time.time()),
+        "timestamp": int(time.time() * 1000),
         "dataType": "DATA",
         "data": {
         }
@@ -123,55 +136,62 @@ def MFC_Write(client, cmd, ch, subcmd=None, value=None):
 
 def Temp_Hum_Press_Read(client):
     ch1_temp, ch1_humi, ch1_pres, ch2_temp, ch2_humi = None, None, None, None, None
-    command_hex = '410030305A53300D'
-    command_bytes = bytes.fromhex(command_hex)
     
-    client.write(command_bytes)
-    time.sleep(0.3)
-    ch1 = client.readline()
-    #time.sleep(0.02)  
-    # print(f'Receive: 00 {ch1}')
-    parts = ch1.split()
-    if len(parts) > 7:
-        if b'\xf8C' in parts[2]:
-            ch1_temp = float(parts[2][:-2])
-        else:
-            ch1_temp = None
-        
-        if b'RH%' in parts[3]:
-            ch1_humi = float(parts[3][:-3])
-        else:
-            ch1_humi = None
-        
-        if b'hPa' in parts[7]:
-            ch1_pres = float(parts[7][:-3])
-    
-    #time.sleep(5)
-
     command_hex = '410030315A53300D'
     command_bytes = bytes.fromhex(command_hex)
 
     client.write(command_bytes)
-    time.sleep(0.3)
+    time.sleep(0.4)
+    ch1 = client.readline()
+    time.sleep(0.02)
+    # print(f'Receive: 01 {ch1}')
+    parts = ch1.split()
+
+    for i in parts:
+        if b'\xf8C' in i:
+            i = i.replace(b'\xf8C', b'')
+            if i != b'':
+                ch1_temp = float(i)
+            
+
+        if b'RH%' in i:
+            i = i.replace(b'RH%', b'')
+            if i != b'':
+                ch1_humi = float(i)
+
+        if b'hPa' in i:
+            i = i.replace(b'hPa', b'')
+            if i != b'':
+                ch1_pres = float(i)
+
+    client.reset_input_buffer()
+
+    command_hex = '410030305A53300D'
+    command_bytes = bytes.fromhex(command_hex)
+    
+    client.write(command_bytes)
+    time.sleep(0.4)
     ch2 = client.readline()
-    #time.sleep(0.02)
-    # print(f'Receive: 01 {ch2}')
+    time.sleep(0.02)
+    # print(f'Receive: 00 {ch2}')
     parts = ch2.split()
 
-    if len(parts) > 7:
-        if b'\xf8C' in parts[2]:
-            ch2_temp = float(parts[2][:-2])
-        else:
-            ch2_temp = None
+    for i in parts:
+        if b'\xf8C' in i:
+            i = i.replace(b'\xf8C', b'')
+            if i != b'':
+                ch2_temp = float(i)
         
-        if b'RH%' in parts[3]:
-            ch2_humi = float(parts[3][:-3])
-        else:
-            ch2_humi = None
+        if b'RH%' in i:
+            i = i.replace(b'RH%', b'')
+            if i != b'':
+                ch2_humi = float(i)
+
+    client.reset_input_buffer()
 
     data = {
         "assetCode":info['mqtt']['assetCode'],
-        "timestamp": int(time.time()),
+        "timestamp": int(time.time() * 1000),
         "dataType": "DATA",
         "data": {
         }
@@ -180,7 +200,7 @@ def Temp_Hum_Press_Read(client):
         data['data']['tempOn'] = ch1_temp
     
     if ch1_humi != None:
-        data['data']['humiOn'] = ch1_temp
+        data['data']['humiOn'] = ch1_humi
     
     if ch1_pres != None:
         data['data']['presOn'] = ch1_pres